diff --git a/src/Mod/TechDraw/Gui/AppTechDrawGui.cpp b/src/Mod/TechDraw/Gui/AppTechDrawGui.cpp index 5f2978d8bb..4e84646f4e 100644 --- a/src/Mod/TechDraw/Gui/AppTechDrawGui.cpp +++ b/src/Mod/TechDraw/Gui/AppTechDrawGui.cpp @@ -125,6 +125,7 @@ PyMOD_INIT_FUNC(TechDrawGui) TechDrawGui::Workbench::init(); TechDrawGui::MDIViewPage::init(); + TechDrawGui::MDIViewPagePy::init_type(); TechDrawGui::ViewProviderPage::init(); TechDrawGui::ViewProviderDrawingView::init(); diff --git a/src/Mod/TechDraw/Gui/CMakeLists.txt b/src/Mod/TechDraw/Gui/CMakeLists.txt index a8cfc77149..1cffb31d8f 100644 --- a/src/Mod/TechDraw/Gui/CMakeLists.txt +++ b/src/Mod/TechDraw/Gui/CMakeLists.txt @@ -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 diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.cpp b/src/Mod/TechDraw/Gui/MDIViewPage.cpp index d5404a5cb8..5ff113d1ea 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.cpp +++ b/src/Mod/TechDraw/Gui/MDIViewPage.cpp @@ -47,7 +47,6 @@ #include #include "MDIViewPage.h" -#include "MDIViewPagePy.h" #include #include @@ -71,6 +70,7 @@ #include #include +#include #include #include #include @@ -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(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 diff --git a/src/Mod/TechDraw/Gui/MDIViewPage.h b/src/Mod/TechDraw/Gui/MDIViewPage.h index ba5c85b4cd..e2f2655ce8 100644 --- a/src/Mod/TechDraw/Gui/MDIViewPage.h +++ b/src/Mod/TechDraw/Gui/MDIViewPage.h @@ -27,6 +27,7 @@ #include "ViewProviderPage.h" #include +#include #include #include @@ -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 deleteItems; }; +class MDIViewPagePy : public Py::PythonExtension +{ +public: + using BaseType = Py::PythonExtension; + 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 diff --git a/src/Mod/TechDraw/Gui/MDIViewPagePy.xml b/src/Mod/TechDraw/Gui/MDIViewPagePy.xml deleted file mode 100644 index 25292969f8..0000000000 --- a/src/Mod/TechDraw/Gui/MDIViewPagePy.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - - - MDIViewPage object - - - - returns the page being displayed - - - - - diff --git a/src/Mod/TechDraw/Gui/MDIViewPagePyImp.cpp b/src/Mod/TechDraw/Gui/MDIViewPagePyImp.cpp deleted file mode 100644 index e59dc8c326..0000000000 --- a/src/Mod/TechDraw/Gui/MDIViewPagePyImp.cpp +++ /dev/null @@ -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 - -using namespace TechDrawGui; - -// returns a string which represents the object e.g. when printed in python -std::string MDIViewPagePy::representation() const -{ - return std::string(""); -} - - - -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; -} - -