From 0a4eae24bc4a07447ef41c20d36561b5dcefcf1c Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 31 Oct 2018 14:27:28 +0100 Subject: [PATCH] add/extend helper classes to safely access documents, document objects or view providers --- src/Gui/DocumentObserver.cpp | 38 ++++++++++++++++++++++++------------ src/Gui/DocumentObserver.h | 12 ++++++++---- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/src/Gui/DocumentObserver.cpp b/src/Gui/DocumentObserver.cpp index 83a5690a38..7de3ccf928 100644 --- a/src/Gui/DocumentObserver.cpp +++ b/src/Gui/DocumentObserver.cpp @@ -83,7 +83,7 @@ std::string DocumentT::getDocumentName() const return document; } -std::string DocumentT::getDocumentPython() const +std::string DocumentT::getGuiDocumentPython() const { std::stringstream str; Document* doc = Application::Instance->activeDocument(); @@ -98,6 +98,21 @@ std::string DocumentT::getDocumentPython() const return str.str(); } +std::string DocumentT::getAppDocumentPython() const +{ + std::stringstream str; + Document* doc = Application::Instance->activeDocument(); + if (doc && document == doc->getDocument()->getName()) { + str << "App.ActiveDocument"; + } + else { + str << "App.getDocument(\"" + << document + << "\")"; + } + return str.str(); +} + // ----------------------------------------------------------------------------- ViewProviderT::ViewProviderT() @@ -138,19 +153,16 @@ std::string ViewProviderT::getDocumentName() const return document; } -std::string ViewProviderT::getDocumentPython() const +std::string ViewProviderT::getGuiDocumentPython() const { - std::stringstream str; - Document* doc = Application::Instance->activeDocument(); - if (doc && document == doc->getDocument()->getName()) { - str << "Gui.ActiveDocument"; - } - else { - str << "Gui.getDocument(\"" - << document - << "\")"; - } - return str.str(); + DocumentT doct(document); + return doct.getGuiDocumentPython(); +} + +std::string ViewProviderT::getAppDocumentPython() const +{ + DocumentT doct(document); + return doct.getAppDocumentPython(); } ViewProviderDocumentObject* ViewProviderT::getViewProvider() const diff --git a/src/Gui/DocumentObserver.h b/src/Gui/DocumentObserver.h index d61f67132b..9f62ed4532 100644 --- a/src/Gui/DocumentObserver.h +++ b/src/Gui/DocumentObserver.h @@ -62,8 +62,10 @@ public: Document* getDocument() const; /*! Get the name of the document. */ std::string getDocumentName() const; - /*! Get the document as Python command. */ - std::string getDocumentPython() const; + /*! Get the Gui::Document as Python command. */ + std::string getGuiDocumentPython() const; + /*! Get the App::Document as Python command. */ + std::string getAppDocumentPython() const; private: std::string document; @@ -94,8 +96,10 @@ public: Document* getDocument() const; /*! Get the name of the document. */ std::string getDocumentName() const; - /*! Get the document as Python command. */ - std::string getDocumentPython() const; + /*! Get the Gui::Document as Python command. */ + std::string getGuiDocumentPython() const; + /*! Get the App::Document as Python command. */ + std::string getAppDocumentPython() const; /*! Get a pointer to the document object or 0 if it doesn't exist any more. */ ViewProviderDocumentObject* getViewProvider() const; /*! Get the name of the document object. */