Skip to content

Commit

Permalink
add method to activate 3d view when setting active object list
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Sep 12, 2018
1 parent 23b0c89 commit 876339b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 15 deletions.
40 changes: 31 additions & 9 deletions src/Gui/Application.cpp
Expand Up @@ -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)
Expand Down Expand Up @@ -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<MDIView*> 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
{
Expand Down
3 changes: 3 additions & 0 deletions src/Gui/Application.h
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
25 changes: 19 additions & 6 deletions src/Gui/ApplicationPy.cpp
Expand Up @@ -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"},
Expand Down Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions src/Gui/Document.cpp
Expand Up @@ -24,6 +24,7 @@
#include "PreCompiled.h"

#ifndef _PreComp_
# include <algorithm>
# include <QAbstractButton>
# include <qapplication.h>
# include <qdir.h>
Expand Down Expand Up @@ -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<MDIView*> 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<MDIView*> mdis = getMDIViewsOfType(View3DInventor::getClassTypeId());
Expand Down
1 change: 1 addition & 0 deletions src/Gui/Document.h
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Mod/PartDesign/Gui/Utils.cpp
Expand Up @@ -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() );

Expand Down

0 comments on commit 876339b

Please sign in to comment.