From b7658c04f6614e98efdf72fdc63078ffced6499e Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 23 Mar 2013 16:45:37 +0100 Subject: [PATCH] 0001059: Cannot add object to DocumentObjectGroupPython --- src/App/DocumentObjectGroupPyImp.cpp | 39 +++++++++++++++++++++++++-- src/Gui/Tree.cpp | 38 +++++++------------------- src/Gui/ViewProviderPythonFeature.cpp | 5 +++- src/Gui/ViewProviderPythonFeature.h | 4 +-- 4 files changed, 53 insertions(+), 33 deletions(-) diff --git a/src/App/DocumentObjectGroupPyImp.cpp b/src/App/DocumentObjectGroupPyImp.cpp index 2db9c15888..8ec07f7e8d 100644 --- a/src/App/DocumentObjectGroupPyImp.cpp +++ b/src/App/DocumentObjectGroupPyImp.cpp @@ -25,6 +25,7 @@ #include "DocumentObjectGroup.h" #include "Document.h" +#include // inclusion of the generated files (generated out of DocumentObjectGroupPy.xml) #include "DocumentObjectGroupPy.h" @@ -81,7 +82,24 @@ PyObject* DocumentObjectGroupPy::addObject(PyObject *args) } } - getDocumentObjectGroupPtr()->addObject(docObj->getDocumentObjectPtr()); + DocumentObjectGroup* grp = getDocumentObjectGroupPtr(); + + if (grp->getTypeId().isDerivedFrom(App::DocumentObjectGroupPython::getClassTypeId())) { + DocumentObjectGroupPython* grppy = static_cast(grp); + App::Property* proxy = grppy->getPropertyByName("Proxy"); + if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { + Py::Object vp = static_cast(proxy)->getValue(); + if (vp.hasAttr(std::string("addObject"))) { + Py::Callable method(vp.getAttr(std::string("addObject"))); + Py::Tuple args(1); + args[0] = Py::Object(object); + method.apply(args); + Py_Return; + } + } + } + + grp->addObject(docObj->getDocumentObjectPtr()); Py_Return; } @@ -101,7 +119,24 @@ PyObject* DocumentObjectGroupPy::removeObject(PyObject *args) return NULL; } - getDocumentObjectGroupPtr()->removeObject(docObj->getDocumentObjectPtr()); + DocumentObjectGroup* grp = getDocumentObjectGroupPtr(); + + if (grp->getTypeId().isDerivedFrom(App::DocumentObjectGroupPython::getClassTypeId())) { + DocumentObjectGroupPython* grppy = static_cast(grp); + App::Property* proxy = grppy->getPropertyByName("Proxy"); + if (proxy && proxy->getTypeId() == App::PropertyPythonObject::getClassTypeId()) { + Py::Object vp = static_cast(proxy)->getValue(); + if (vp.hasAttr(std::string("removeObject"))) { + Py::Callable method(vp.getAttr(std::string("removeObject"))); + Py::Tuple args(1); + args[0] = Py::Object(object); + method.apply(args); + Py_Return; + } + } + } + + grp->removeObject(docObj->getDocumentObjectPtr()); Py_Return; } diff --git a/src/Gui/Tree.cpp b/src/Gui/Tree.cpp index 16ec6a607d..67190c4fef 100644 --- a/src/Gui/Tree.cpp +++ b/src/Gui/Tree.cpp @@ -437,7 +437,6 @@ void TreeWidget::dropEvent(QDropEvent *event) // Open command App::Document* doc = grp->getDocument(); Gui::Document* gui = Gui::Application::Instance->getDocument(doc); - Base::Type DOGPython = Base::Type::fromName("App::DocumentObjectGroupPython"); gui->openCommand("Move object"); for (QList::Iterator it = items.begin(); it != items.end(); ++it) { // get document object @@ -448,38 +447,21 @@ void TreeWidget::dropEvent(QDropEvent *event) if (par) { // allow an object to be in one group only QString cmd; - if (par->getTypeId().isDerivedFrom(DOGPython)) { - // if this is a python group, call the method of its Proxy - cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").Proxy.removeObject(" - "App.getDocument(\"%1\").getObject(\"%3\"))") - .arg(QString::fromAscii(doc->getName())) - .arg(QString::fromAscii(par->getNameInDocument())) - .arg(QString::fromAscii(obj->getNameInDocument())); - } else { - cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").removeObject(" - "App.getDocument(\"%1\").getObject(\"%3\"))") - .arg(QString::fromAscii(doc->getName())) - .arg(QString::fromAscii(par->getNameInDocument())) - .arg(QString::fromAscii(obj->getNameInDocument())); - } + cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").removeObject(" + "App.getDocument(\"%1\").getObject(\"%3\"))") + .arg(QString::fromAscii(doc->getName())) + .arg(QString::fromAscii(par->getNameInDocument())) + .arg(QString::fromAscii(obj->getNameInDocument())); Gui::Application::Instance->runPythonCode(cmd.toUtf8()); } // build Python command for execution QString cmd; - if (grp->getTypeId().isDerivedFrom(DOGPython)) { - cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").Proxy.addObject(" - "App.getDocument(\"%1\").getObject(\"%3\"))") - .arg(QString::fromAscii(doc->getName())) - .arg(QString::fromAscii(grp->getNameInDocument())) - .arg(QString::fromAscii(obj->getNameInDocument())); - } else { - cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").addObject(" - "App.getDocument(\"%1\").getObject(\"%3\"))") - .arg(QString::fromAscii(doc->getName())) - .arg(QString::fromAscii(grp->getNameInDocument())) - .arg(QString::fromAscii(obj->getNameInDocument())); - } + cmd = QString::fromAscii("App.getDocument(\"%1\").getObject(\"%2\").addObject(" + "App.getDocument(\"%1\").getObject(\"%3\"))") + .arg(QString::fromAscii(doc->getName())) + .arg(QString::fromAscii(grp->getNameInDocument())) + .arg(QString::fromAscii(obj->getNameInDocument())); Gui::Application::Instance->runPythonCode(cmd.toUtf8()); } gui->commitCommand(); diff --git a/src/Gui/ViewProviderPythonFeature.cpp b/src/Gui/ViewProviderPythonFeature.cpp index b008fd04ac..0591312157 100644 --- a/src/Gui/ViewProviderPythonFeature.cpp +++ b/src/Gui/ViewProviderPythonFeature.cpp @@ -270,7 +270,7 @@ QIcon ViewProviderPythonFeatureImp::getIcon() const return QIcon(); } -std::vector ViewProviderPythonFeatureImp::claimChildren() const +std::vector ViewProviderPythonFeatureImp::claimChildren(const std::vector& base) const { std::vector children; Base::PyGILStateLocker lock; @@ -290,6 +290,9 @@ std::vector ViewProviderPythonFeatureImp::claimChildren() } } } + else { + children = base; + } } } catch (Py::Exception&) { diff --git a/src/Gui/ViewProviderPythonFeature.h b/src/Gui/ViewProviderPythonFeature.h index 220ada2fe2..0af989cc88 100644 --- a/src/Gui/ViewProviderPythonFeature.h +++ b/src/Gui/ViewProviderPythonFeature.h @@ -47,7 +47,7 @@ public: // Returns the icon QIcon getIcon() const; - std::vector claimChildren() const; + std::vector claimChildren(const std::vector&) const; std::string getElement(const SoDetail *det) const; std::vector getSelectionShape(const char* Element) const; bool setEdit(int ModNum); @@ -103,7 +103,7 @@ public: } std::vector claimChildren() const { - return imp->claimChildren(); + return imp->claimChildren(ViewProviderT::claimChildren()); } /** @name Nodes */