From 969b43bb64b5241ede622a6872b59cabce346fa6 Mon Sep 17 00:00:00 2001 From: wmayer Date: Sun, 18 Oct 2015 19:35:15 +0200 Subject: [PATCH] + improve active document handling --- src/Gui/Application.cpp | 17 +++++++++- src/Gui/Application.h | 1 + src/Gui/ApplicationPy.cpp | 70 +++++++++++++++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index 0059f55a5d6e..772d41d45d9a 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -706,8 +706,23 @@ void Application::slotActiveDocument(const App::Document& Doc) { std::map::iterator doc = d->documents.find(&Doc); // this can happen when closing a document with two views opened - if (doc != d->documents.end()) + if (doc != d->documents.end()) { + // this can happen when calling App.setActiveDocument directly from Python + // because no MDI view will be activated + if (d->activeDocument != doc->second) { + d->activeDocument = doc->second; + if (d->activeDocument) { + Base::PyGILStateLocker lock; + Py::Object active(d->activeDocument->getPyObject(), true); + Py::Module("FreeCADGui").setAttr(std::string("ActiveDocument"),active); + } + else { + Base::PyGILStateLocker lock; + Py::Module("FreeCADGui").setAttr(std::string("ActiveDocument"),Py::None()); + } + } signalActiveDocument(*doc->second); + } } void Application::slotNewObject(const ViewProvider& vp) diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 29748a4dc360..50520bfb9c4e 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -231,6 +231,7 @@ class GuiExport Application PYFUNCDEF_S(sExport); PYFUNCDEF_S(sActiveDocument); + PYFUNCDEF_S(sSetActiveDocument); PYFUNCDEF_S(sGetDocument); PYFUNCDEF_S(sDoCommand); diff --git a/src/Gui/ApplicationPy.cpp b/src/Gui/ApplicationPy.cpp index 0672a6277e9a..d311b216d0a3 100644 --- a/src/Gui/ApplicationPy.cpp +++ b/src/Gui/ApplicationPy.cpp @@ -54,6 +54,7 @@ #include "Language/Translator.h" #include "DownloadManager.h" #include +#include #include #include #include @@ -139,6 +140,9 @@ PyMethodDef Application::Methods[] = { {"activeDocument", (PyCFunction) Application::sActiveDocument, 1, "activeDocument() -> object or None\n\n" "Return the active document or None if no one exists"}, + {"setActiveDocument", (PyCFunction) Application::sSetActiveDocument,1, + "setActiveDocument(string or App.Document) -> None\n\n" + "Activate the specified document"}, {"getDocument", (PyCFunction) Application::sGetDocument, 1, "getDocument(string) -> object\n\n" "Get a document by its name"}, @@ -171,19 +175,71 @@ PyObject* Gui::Application::sActiveDocument(PyObject * /*self*/, PyObject *args, } } -PyObject* Application::sGetDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) +PyObject* Gui::Application::sSetActiveDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) { - char *pstr=0; - if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C - return NULL; // NULL triggers exception + Document *pcDoc = 0; + + do { + char *pstr=0; + if (PyArg_ParseTuple(args, "s", &pstr)) { + pcDoc = Instance->getDocument(pstr); + if (!pcDoc) { + PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); + return 0; + } + break; + } + + PyErr_Clear(); + PyObject* doc; + if (PyArg_ParseTuple(args, "O!", &(App::DocumentPy::Type), &doc)) { + pcDoc = Instance->getDocument(static_cast(doc)->getDocumentPtr()); + if (!pcDoc) { + PyErr_Format(PyExc_KeyError, "Unknown document instance"); + return 0; + } + break; + } + } + while(false); - Document *pcDoc = Instance->getDocument(pstr); if (!pcDoc) { - PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); + PyErr_SetString(PyExc_TypeError, "Either string or App.Document expected"); return 0; } - return pcDoc->getPyObject(); + if (Instance->activeDocument() != pcDoc) { + Gui::MDIView* view = pcDoc->getActiveView(); + getMainWindow()->setActiveWindow(view); + } + Py_Return; +} + +PyObject* Application::sGetDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) +{ + char *pstr=0; + if (PyArg_ParseTuple(args, "s", &pstr)) { + Document *pcDoc = Instance->getDocument(pstr); + if (!pcDoc) { + PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); + return 0; + } + return pcDoc->getPyObject(); + } + + PyErr_Clear(); + PyObject* doc; + if (PyArg_ParseTuple(args, "O!", &(App::DocumentPy::Type), &doc)) { + Document *pcDoc = Instance->getDocument(static_cast(doc)->getDocumentPtr()); + if (!pcDoc) { + PyErr_Format(PyExc_KeyError, "Unknown document instance"); + return 0; + } + return pcDoc->getPyObject(); + } + + PyErr_SetString(PyExc_TypeError, "Either string or App.Document exprected"); + return 0; } PyObject* Application::sHide(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)