+ improve active document handling

This commit is contained in:
wmayer 2015-10-18 19:35:15 +02:00
parent 00cdea7b74
commit 969b43bb64
3 changed files with 80 additions and 8 deletions

View File

@ -706,8 +706,23 @@ void Application::slotActiveDocument(const App::Document& Doc)
{ {
std::map<const App::Document*, Gui::Document*>::iterator doc = d->documents.find(&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 // 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); signalActiveDocument(*doc->second);
}
} }
void Application::slotNewObject(const ViewProvider& vp) void Application::slotNewObject(const ViewProvider& vp)

View File

@ -231,6 +231,7 @@ public:
PYFUNCDEF_S(sExport); PYFUNCDEF_S(sExport);
PYFUNCDEF_S(sActiveDocument); PYFUNCDEF_S(sActiveDocument);
PYFUNCDEF_S(sSetActiveDocument);
PYFUNCDEF_S(sGetDocument); PYFUNCDEF_S(sGetDocument);
PYFUNCDEF_S(sDoCommand); PYFUNCDEF_S(sDoCommand);

View File

@ -54,6 +54,7 @@
#include "Language/Translator.h" #include "Language/Translator.h"
#include "DownloadManager.h" #include "DownloadManager.h"
#include <App/DocumentObjectPy.h> #include <App/DocumentObjectPy.h>
#include <App/DocumentPy.h>
#include <App/PropertyFile.h> #include <App/PropertyFile.h>
#include <Base/Interpreter.h> #include <Base/Interpreter.h>
#include <Base/Console.h> #include <Base/Console.h>
@ -139,6 +140,9 @@ PyMethodDef Application::Methods[] = {
{"activeDocument", (PyCFunction) Application::sActiveDocument, 1, {"activeDocument", (PyCFunction) Application::sActiveDocument, 1,
"activeDocument() -> object or None\n\n" "activeDocument() -> object or None\n\n"
"Return the active document or None if no one exists"}, "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", (PyCFunction) Application::sGetDocument, 1,
"getDocument(string) -> object\n\n" "getDocument(string) -> object\n\n"
"Get a document by its name"}, "Get a document by its name"},
@ -171,19 +175,71 @@ PyObject* Gui::Application::sActiveDocument(PyObject * /*self*/, PyObject *args,
} }
} }
PyObject* Gui::Application::sSetActiveDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{
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);
if (!pcDoc) {
PyErr_SetString(PyExc_TypeError, "Either string or App.Document expected");
return 0;
}
if (Instance->activeDocument() != pcDoc) {
Gui::MDIView* view = pcDoc->getActiveView();
getMainWindow()->setActiveWindow(view);
}
Py_Return;
}
PyObject* Application::sGetDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/) PyObject* Application::sGetDocument(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)
{ {
char *pstr=0; char *pstr=0;
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C if (PyArg_ParseTuple(args, "s", &pstr)) {
return NULL; // NULL triggers exception
Document *pcDoc = Instance->getDocument(pstr); Document *pcDoc = Instance->getDocument(pstr);
if (!pcDoc) { if (!pcDoc) {
PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr); PyErr_Format(PyExc_NameError, "Unknown document '%s'", pstr);
return 0; return 0;
} }
return pcDoc->getPyObject(); 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*/) PyObject* Application::sHide(PyObject * /*self*/, PyObject *args,PyObject * /*kwd*/)