From 876339b885f3b84d586dabc917c6bd670c40bb7f Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 12 Sep 2018 15:38:28 +0200 Subject: [PATCH] add method to activate 3d view when setting active object list --- src/Gui/Application.cpp | 40 +++++++++++++++++++++++++------- src/Gui/Application.h | 3 +++ src/Gui/ApplicationPy.cpp | 25 +++++++++++++++----- src/Gui/Document.cpp | 31 +++++++++++++++++++++++++ src/Gui/Document.h | 1 + src/Mod/PartDesign/Gui/Utils.cpp | 1 + 6 files changed, 86 insertions(+), 15 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index d58dd9148d62..d3871781b711 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -279,15 +279,6 @@ struct PyMethodDef FreeCADGui_methods[] = { {NULL, NULL, 0, NULL} /* sentinel */ }; - -Gui::MDIView* Application::activeView(void) const -{ - if (activeDocument()) - return activeDocument()->getActiveView(); - else - return NULL; -} - } // namespace Gui Application::Application(bool GUIenabled) @@ -812,6 +803,37 @@ bool Application::sendHasMsgToActiveView(const char* pMsg) return pView ? pView->onHasMsg(pMsg) : false; } +Gui::MDIView* Application::activeView(void) const +{ + if (activeDocument()) + return activeDocument()->getActiveView(); + else + return NULL; +} + +/** + * @brief Application::activateView + * Activates a view of the given type of the active document. + * If a view of this type doesn't exist and \a create is true + * a new view of this type will be created. + * @param type + * @param create + */ +void Application::activateView(const Base::Type& type, bool create) +{ + Document* doc = activeDocument(); + if (doc) { + MDIView* mdiView = doc->getActiveView(); + if (mdiView && mdiView->isDerivedFrom(type)) + return; + std::list mdiViews = doc->getMDIViewsOfType(type); + if (!mdiViews.empty()) + doc->setActiveWindow(mdiViews.back()); + else if (create) + doc->createView(type); + } +} + /// Getter for the active view Gui::Document* Application::activeDocument(void) const { diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 8345ad1ac07c..2da8e8a3feda 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -149,6 +149,8 @@ class GuiExport Application Gui::Document* getDocument(const App::Document* pDoc) const; /// Getter for the active view of the active document or null Gui::MDIView* activeView(void) const; + /// Activate a view of the given type of the active document + void activateView(const Base::Type&, bool create=false); /// Shows the associated view provider of the given object void showViewProvider(const App::DocumentObject*); /// Hides the associated view provider of the given object @@ -237,6 +239,7 @@ class GuiExport Application static PyObject* sActiveDocument (PyObject *self,PyObject *args); static PyObject* sSetActiveDocument (PyObject *self,PyObject *args); static PyObject* sActiveView (PyObject *self,PyObject *args); + static PyObject* sActivateView (PyObject *self,PyObject *args); static PyObject* sGetDocument (PyObject *self,PyObject *args); static PyObject* sDoCommand (PyObject *self,PyObject *args); diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 4efeb89cb8d8..4ae44fb0efca 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -161,6 +161,9 @@ PyMethodDef Application::Methods[] = { {"activeView", (PyCFunction)Application::sActiveView, METH_VARARGS, "activeView() -> object or None\n\n" "Return the active view of the active document or None if no one exists"}, + {"activateView", (PyCFunction)Application::sActivateView, METH_VARARGS, + "activateView(type)\n\n" + "Activate a view of the given type of the active document"}, {"getDocument", (PyCFunction) Application::sGetDocument, METH_VARARGS, "getDocument(string) -> object\n\n" "Get a document by its name"}, @@ -208,17 +211,27 @@ PyObject* Gui::Application::sActiveView(PyObject * /*self*/, PyObject *args) if (!PyArg_ParseTuple(args, "")) return NULL; - Document *pcDoc = Instance->activeDocument(); - if (pcDoc) { - Gui::MDIView *pcView = pcDoc->getActiveView(); - if (pcView) - // already incremented in getPyObject(). - return pcView->getPyObject(); + Gui::MDIView* mdiView = Instance->activeView(); + if (mdiView) { + // already incremented in getPyObject(). + return mdiView->getPyObject(); } Py_Return; } +PyObject* Gui::Application::sActivateView(PyObject * /*self*/, PyObject *args) +{ + char* typeStr; + PyObject *create = Py_False; + if (!PyArg_ParseTuple(args, "sO!", &typeStr, &PyBool_Type, &create)) + return NULL; + + Base::Type type = Base::Type::fromName(typeStr); + Instance->activateView(type, PyObject_IsTrue(create) ? true : false); + Py_Return; +} + PyObject* Gui::Application::sSetActiveDocument(PyObject * /*self*/, PyObject *args) { Document *pcDoc = 0; diff --git a/src/Gui/Document.cpp b/src/Gui/Document.cpp index fcf66ac9ed79..50d63efaea1a 100644 --- a/src/Gui/Document.cpp +++ b/src/Gui/Document.cpp @@ -24,6 +24,7 @@ #include "PreCompiled.h" #ifndef _PreComp_ +# include # include # include # include @@ -1389,6 +1390,36 @@ MDIView* Document::getActiveView(void) const return active; } +/** + * @brief Document::setActiveWindow + * If this document is active and the view is part of it then it will be + * activated. If the document is not active of the view is already active + * nothing is done. + * @param view + */ +void Document::setActiveWindow(Gui::MDIView* view) +{ + // get the main window's active view + MDIView* active = getMainWindow()->activeWindow(); + + // view is already active + if (active == view) + return; + + // get all MDI views of the document + std::list mdis = getMDIViews(); + + // this document is not active + if (std::find(mdis.begin(), mdis.end(), active) == mdis.end()) + return; + + // the view is not part of the document + if (std::find(mdis.begin(), mdis.end(), view) == mdis.end()) + return; + + getMainWindow()->setActiveWindow(view); +} + Gui::MDIView* Document::getViewOfNode(SoNode* node) const { std::list mdis = getMDIViewsOfType(View3DInventor::getClassTypeId()); diff --git a/src/Gui/Document.h b/src/Gui/Document.h index e2b65ce105a3..cd273f920640 100644 --- a/src/Gui/Document.h +++ b/src/Gui/Document.h @@ -159,6 +159,7 @@ class GuiExport Document : public Base::Persistence //@{ /// Getter for the active view Gui::MDIView* getActiveView(void) const; + void setActiveWindow(Gui::MDIView* view); Gui::MDIView* getEditingViewOfViewProvider(Gui::ViewProvider*) const; Gui::MDIView* getViewOfViewProvider(Gui::ViewProvider*) const; Gui::MDIView* getViewOfNode(SoNode*) const; diff --git a/src/Mod/PartDesign/Gui/Utils.cpp b/src/Mod/PartDesign/Gui/Utils.cpp index a71fdbdaa793..20c1ef8f5f51 100644 --- a/src/Mod/PartDesign/Gui/Utils.cpp +++ b/src/Mod/PartDesign/Gui/Utils.cpp @@ -115,6 +115,7 @@ PartDesign::Body * makeBody(App::Document *doc) "App.activeDocument().addObject('PartDesign::Body','%s')", bodyName.c_str() ); Gui::Command::doCommand( Gui::Command::Gui, + "Gui.activateView('Gui::View3DInventor', True)\n" "Gui.activeView().setActiveObject('%s', App.activeDocument().%s)", PDBODYKEY, bodyName.c_str() );