From 2d35da45195cff07e8ee2b270f1f2333ba1afeaa Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 28 Aug 2023 13:34:48 +0200 Subject: [PATCH] Gui: improve accessing active object of an MDI view * if 2nd argument of getActiveObject() is False and the requested object doesn't exist then return (None, None, "") * remove the exact same implementations from View3DInventorPy --- src/Gui/MDIViewPy.cpp | 33 ++++++++++++++++++++----------- src/Gui/View3DPy.cpp | 46 ------------------------------------------- src/Gui/View3DPy.h | 2 -- 3 files changed, 22 insertions(+), 59 deletions(-) diff --git a/src/Gui/MDIViewPy.cpp b/src/Gui/MDIViewPy.cpp index bb89725cdb..92e1c0e67c 100644 --- a/src/Gui/MDIViewPy.cpp +++ b/src/Gui/MDIViewPy.cpp @@ -256,26 +256,37 @@ Py::Object MDIViewPy::setActiveObject(const Py::Tuple& args) Py::Object MDIViewPy::getActiveObject(const Py::Tuple& args) { - const char* name; - PyObject *resolve = Py_True; - if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) + const char* name{}; + PyObject *resolve = Py_True; // NOLINT + if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) { throw Py::Exception(); + } App::DocumentObject *parent = nullptr; std::string subname; App::DocumentObject* obj = nullptr; - if (_view) + if (_view) { obj = _view->getActiveObject(name,&parent,&subname); - if (!obj) + } + + if (Base::asBoolean(resolve)) { + if (obj) { + return Py::asObject(obj->getPyObject()); + } + return Py::None(); + } - if (Base::asBoolean(resolve)) - return Py::asObject(obj->getPyObject()); + // NOLINTBEGIN(cppcoreguidelines-slicing) + if (obj) { + return Py::TupleN( + Py::asObject(obj->getPyObject()), + Py::asObject(parent->getPyObject()), + Py::String(subname.c_str())); + } - return Py::TupleN( - Py::asObject(obj->getPyObject()), - Py::asObject(parent->getPyObject()), - Py::String(subname.c_str())); + return Py::TupleN(Py::None(), Py::None(), Py::String()); + // NOLINTEND(cppcoreguidelines-slicing) } Py::Object MDIViewPy::cast_to_base(const Py::Tuple&) diff --git a/src/Gui/View3DPy.cpp b/src/Gui/View3DPy.cpp index 8f29d71e1f..b143fa1a3f 100644 --- a/src/Gui/View3DPy.cpp +++ b/src/Gui/View3DPy.cpp @@ -202,8 +202,6 @@ void View3DInventorPy::init_type() "Remove the DraggerCalback function from the coin node\n" "Possibles types :\n" "'addFinishCallback','addStartCallback','addMotionCallback','addValueChangedCallback'\n"); - add_varargs_method("setActiveObject", &View3DInventorPy::setActiveObject, "setActiveObject(name,object,subname=None)\nadd or set a new active object"); - add_varargs_method("getActiveObject", &View3DInventorPy::getActiveObject, "getActiveObject(name,resolve=True)\nreturns the active object for the given type"); add_varargs_method("getViewProvidersOfType", &View3DInventorPy::getViewProvidersOfType, "getViewProvidersOfType(name)\nreturns a list of view providers for the given type"); add_noargs_method("redraw", &View3DInventorPy::redraw, "redraw(): renders the scene on screen (useful for animations)"); add_varargs_method("setName",&View3DInventorPy::setName,"setName(str): sets a name to this viewer\nThe name sets the widget's windowTitle and appears on the viewer tab"); @@ -2390,50 +2388,6 @@ Py::Object View3DInventorPy::removeDraggerCallback(const Py::Tuple& args) } } -Py::Object View3DInventorPy::setActiveObject(const Py::Tuple& args) -{ - PyObject* docObject = Py_None; - char* name; - char *subname = nullptr; - if (!PyArg_ParseTuple(args.ptr(), "s|Os", &name, &docObject, &subname)) - throw Py::Exception(); - - try { - Base::PyTypeCheck(&docObject, &App::DocumentObjectPy::Type, - "Expect the second argument to be a document object or None"); - App::DocumentObject* obj = docObject ? - static_cast(docObject)->getDocumentObjectPtr() : nullptr; - getView3DIventorPtr()->setActiveObject(obj, name, subname); - - return Py::None(); - } - catch (const Base::Exception& e) { - throw Py::Exception(e.getPyExceptionType(), e.what()); - } -} - -Py::Object View3DInventorPy::getActiveObject(const Py::Tuple& args) -{ - char* name; - PyObject *resolve = Py_True; - if (!PyArg_ParseTuple(args.ptr(), "s|O!", &name, &PyBool_Type, &resolve)) - throw Py::Exception(); - - App::DocumentObject *parent = nullptr; - std::string subname; - App::DocumentObject* obj = getView3DIventorPtr()->getActiveObject(name,&parent,&subname); - if (!obj) - return Py::None(); - - if (Base::asBoolean(resolve)) - return Py::asObject(obj->getPyObject()); - - return Py::TupleN( - Py::asObject(obj->getPyObject()), - Py::asObject(parent->getPyObject()), - Py::String(subname.c_str())); -} - Py::Object View3DInventorPy::getViewProvidersOfType(const Py::Tuple& args) { char* name; diff --git a/src/Gui/View3DPy.h b/src/Gui/View3DPy.h index 6b95c14a90..2a63dc7d04 100644 --- a/src/Gui/View3DPy.h +++ b/src/Gui/View3DPy.h @@ -113,8 +113,6 @@ public: Py::Object hasAxisCross(); Py::Object addDraggerCallback(const Py::Tuple&); Py::Object removeDraggerCallback(const Py::Tuple&); - Py::Object setActiveObject(const Py::Tuple&); - Py::Object getActiveObject(const Py::Tuple&); Py::Object getViewProvidersOfType(const Py::Tuple&); Py::Object redraw(); Py::Object setName(const Py::Tuple&);