TD: make MDIViewPagePy acting as sub-class of MDIViewPy

This commit is contained in:
wmayer
2021-11-21 17:33:19 +01:00
parent cf337394a1
commit d64b698d05
6 changed files with 102 additions and 72 deletions

View File

@@ -125,6 +125,7 @@ PyMOD_INIT_FUNC(TechDrawGui)
TechDrawGui::Workbench::init();
TechDrawGui::MDIViewPage::init();
TechDrawGui::MDIViewPagePy::init_type();
TechDrawGui::ViewProviderPage::init();
TechDrawGui::ViewProviderDrawingView::init();

View File

@@ -36,11 +36,8 @@ else(BUILD_QT5)
)
endif(BUILD_QT5)
generate_from_xml(MDIViewPagePy)
# The XML files
set(TechDrawGui_XML_SRCS
MDIViewPagePy.xml
)
link_directories(${OCC_LIBRARY_DIR})
@@ -206,7 +203,6 @@ SET(TechDrawGui_SRCS
SET(TechDrawGuiView_SRCS
MDIViewPage.cpp
MDIViewPage.h
MDIViewPagePyImp.cpp
QGVPage.cpp
QGVPage.h
QGCustomText.cpp

View File

@@ -47,7 +47,6 @@
#include <math.h>
#include "MDIViewPage.h"
#include "MDIViewPagePy.h"
#include <Base/Stream.h>
#include <Base/Tools.h>
@@ -71,6 +70,7 @@
#include <Mod/TechDraw/App/DrawHatch.h>
#include <Mod/TechDraw/App/DrawPage.h>
#include <Mod/TechDraw/App/DrawPagePy.h>
#include <Mod/TechDraw/App/DrawProjGroup.h>
#include <Mod/TechDraw/App/DrawTemplate.h>
#include <Mod/TechDraw/App/DrawView.h>
@@ -1375,4 +1375,82 @@ MDIViewPage *MDIViewPage::getFromScene(const QGraphicsScene *scene)
return nullptr;
}
// ----------------------------------------------------------------------------
void MDIViewPagePy::init_type()
{
behaviors().name("MDIViewPagePy");
behaviors().doc("Python binding class for the MDI view page class");
// you must have overwritten the virtual functions
behaviors().supportRepr();
behaviors().supportGetattr();
behaviors().supportSetattr();
add_varargs_method("getPage", &MDIViewPagePy::getPage, "getPage() returns the page being displayed");
add_varargs_method("cast_to_base", &MDIViewPagePy::cast_to_base, "cast_to_base() cast to MDIView class");
behaviors().readyType();
}
MDIViewPagePy::MDIViewPagePy(MDIViewPage *mdi)
: base(mdi)
{
}
MDIViewPagePy::~MDIViewPagePy()
{
}
Py::Object MDIViewPagePy::repr()
{
std::ostringstream s_out;
if (!getMDIViewPagePtr())
throw Py::RuntimeError("Cannot print representation of deleted object");
s_out << "MDI view page";
return Py::String(s_out.str());
}
// Since with PyCXX it's not possible to make a sub-class of MDIViewPy
// a trick is to use MDIViewPy as class member and override getattr() to
// join the attributes of both classes. This way all methods of MDIViewPy
// appear for SheetViewPy, too.
Py::Object MDIViewPagePy::getattr(const char * attr)
{
if (!getMDIViewPagePtr())
throw Py::RuntimeError("Cannot print representation of deleted object");
std::string name( attr );
if (name == "__dict__" || name == "__class__") {
Py::Dict dict_self(BaseType::getattr("__dict__"));
Py::Dict dict_base(base.getattr("__dict__"));
for (auto it : dict_base) {
dict_self.setItem(it.first, it.second);
}
return dict_self;
}
try {
return BaseType::getattr(attr);
}
catch (Py::AttributeError& e) {
e.clear();
return base.getattr(attr);
}
}
MDIViewPage* MDIViewPagePy::getMDIViewPagePtr()
{
return qobject_cast<MDIViewPage*>(base.getMDIViewPtr());
}
Py::Object MDIViewPagePy::getPage(const Py::Tuple& args)
{
if (!PyArg_ParseTuple(args.ptr(), ""))
throw Py::Exception();
return Py::asObject(new TechDraw::DrawPagePy(getMDIViewPagePtr()->getPage()));
}
Py::Object MDIViewPagePy::cast_to_base(const Py::Tuple&)
{
return Gui::MDIViewPy::create(base.getMDIViewPtr());
}
#include <Mod/TechDraw/Gui/moc_MDIViewPage.cpp>

View File

@@ -27,6 +27,7 @@
#include "ViewProviderPage.h"
#include <Gui/MDIView.h>
#include <Gui/MDIViewPy.h>
#include <Gui/Selection.h>
#include <QPrinter>
@@ -96,7 +97,7 @@ public:
PyObject* getPyObject();
TechDraw::DrawPage * getPage() { return m_vpPage->getDrawPage(); }
QGVPage* getQGVPage(void) {return m_view;};
QGVPage* getQGVPage(void) {return m_view;}
QGraphicsScene* m_scene;
@@ -173,6 +174,26 @@ private:
QList<QGIView *> deleteItems;
};
class MDIViewPagePy : public Py::PythonExtension<MDIViewPagePy>
{
public:
using BaseType = Py::PythonExtension<MDIViewPagePy>;
static void init_type();
MDIViewPagePy(MDIViewPage *mdi);
~MDIViewPagePy();
Py::Object repr();
Py::Object getattr(const char *);
Py::Object getPage(const Py::Tuple&);
Py::Object cast_to_base(const Py::Tuple&);
MDIViewPage* getMDIViewPagePtr();
protected:
Gui::MDIViewPy base;
};
} // namespace MDIViewPageGui

View File

@@ -1,23 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<GenerateModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="generateMetaModel_Module.xsd">
<PythonExport
Father="PyObjectBase"
Name="MDIViewPagePy"
Twin="MDIViewPage"
TwinPointer="MDIViewPage"
Include="Mod/TechDraw/Gui/MDIViewPage.h"
Namespace="TechDrawGui"
FatherInclude="Base/PyObjectBase.h"
FatherNamespace="Base">
<Documentation>
<Author Licence="LGPL" Name="openBrain" EMail="" />
<UserDocu>MDIViewPage object</UserDocu>
</Documentation>
<Methode Name="getPage">
<Documentation>
<UserDocu>returns the page being displayed</UserDocu>
</Documentation>
</Methode>
<CustomAttributes />
</PythonExport>
</GenerateModel>

View File

@@ -1,43 +0,0 @@
#include "PreCompiled.h"
#include "Mod/TechDraw/Gui/MDIViewPage.h"
// inclusion of the generated files (generated out of MDIViewPagePy.xml)
#include "MDIViewPagePy.h"
#include "MDIViewPagePy.cpp"
#include <Mod/TechDraw/App/DrawPagePy.h>
using namespace TechDrawGui;
// returns a string which represents the object e.g. when printed in python
std::string MDIViewPagePy::representation() const
{
return std::string("<TechDrawView object>");
}
PyObject* MDIViewPagePy::getPage(PyObject * args)
{
if (!PyArg_ParseTuple(args, ""))
return nullptr;
return new TechDraw::DrawPagePy(getMDIViewPagePtr()->getPage());
}
PyObject *MDIViewPagePy::getCustomAttributes(const char* /*attr*/) const
{
return nullptr;
}
int MDIViewPagePy::setCustomAttributes(const char* /*attr*/, PyObject* /*obj*/)
{
return 0;
}