Skip to content

Commit

Permalink
+ improve active document handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Oct 18, 2015
1 parent 00cdea7 commit 969b43b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/Gui/Application.cpp
Expand Up @@ -706,8 +706,23 @@ void Application::slotActiveDocument(const App::Document& Doc)
{
std::map<const App::Document*, Gui::Document*>::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)
Expand Down
1 change: 1 addition & 0 deletions src/Gui/Application.h
Expand Up @@ -231,6 +231,7 @@ class GuiExport Application
PYFUNCDEF_S(sExport);

PYFUNCDEF_S(sActiveDocument);
PYFUNCDEF_S(sSetActiveDocument);
PYFUNCDEF_S(sGetDocument);

PYFUNCDEF_S(sDoCommand);
Expand Down
70 changes: 63 additions & 7 deletions src/Gui/ApplicationPy.cpp
Expand Up @@ -54,6 +54,7 @@
#include "Language/Translator.h"
#include "DownloadManager.h"
#include <App/DocumentObjectPy.h>
#include <App/DocumentPy.h>
#include <App/PropertyFile.h>
#include <Base/Interpreter.h>
#include <Base/Console.h>
Expand Down Expand Up @@ -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"},
Expand Down Expand Up @@ -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<App::DocumentPy*>(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<App::DocumentPy*>(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*/)
Expand Down

0 comments on commit 969b43b

Please sign in to comment.