From 42601fc4e439a5bf3748bde70abc512f85c5edce Mon Sep 17 00:00:00 2001 From: wmayer Date: Wed, 31 Oct 2018 11:24:45 +0100 Subject: [PATCH] add/extend helper classes to savely access document, document objects or view providers --- src/App/DocumentObserver.cpp | 22 +++-- src/App/DocumentObserver.h | 4 + src/Gui/DocumentObserver.cpp | 150 +++++++++++++++++++++++++++++++++++ src/Gui/DocumentObserver.h | 81 +++++++++++++++++++ 4 files changed, 251 insertions(+), 6 deletions(-) diff --git a/src/App/DocumentObserver.cpp b/src/App/DocumentObserver.cpp index 818e1ddd37..53421d386d 100644 --- a/src/App/DocumentObserver.cpp +++ b/src/App/DocumentObserver.cpp @@ -46,6 +46,11 @@ DocumentT::DocumentT(Document* doc) document = doc->getName(); } +DocumentT::DocumentT(const std::string& name) +{ + document = name; +} + DocumentT::~DocumentT() { } @@ -62,6 +67,11 @@ void DocumentT::operator=(const Document* doc) document = doc->getName(); } +void DocumentT::operator=(const std::string& name) +{ + document = name; +} + Document* DocumentT::getDocument() const { return GetApplication().getDocument(document.c_str()); @@ -77,10 +87,10 @@ std::string DocumentT::getDocumentPython() const std::stringstream str; Document* doc = GetApplication().getActiveDocument(); if (doc && document == doc->getName()) { - str << "FreeCAD.ActiveDocument"; + str << "App.ActiveDocument"; } else { - str << "FreeCAD.getDocument(\"" + str << "App.getDocument(\"" << document << "\")"; } @@ -135,10 +145,10 @@ std::string DocumentObjectT::getDocumentPython() const std::stringstream str; Document* doc = GetApplication().getActiveDocument(); if (doc && document == doc->getName()) { - str << "FreeCAD.ActiveDocument"; + str << "App.ActiveDocument"; } else { - str << "FreeCAD.getDocument(\"" + str << "App.getDocument(\"" << document << "\")"; } @@ -170,10 +180,10 @@ std::string DocumentObjectT::getObjectPython() const std::stringstream str; Document* doc = GetApplication().getActiveDocument(); if (doc && document == doc->getName()) { - str << "FreeCAD.ActiveDocument."; + str << "App.ActiveDocument."; } else { - str << "FreeCAD.getDocument(\"" + str << "App.getDocument(\"" << document << "\")."; } diff --git a/src/App/DocumentObserver.h b/src/App/DocumentObserver.h index f97fd65d59..41627e3bab 100644 --- a/src/App/DocumentObserver.h +++ b/src/App/DocumentObserver.h @@ -48,12 +48,16 @@ public: DocumentT(); /*! Constructor */ DocumentT(Document*); + /*! Constructor */ + DocumentT(const std::string&); /*! Destructor */ ~DocumentT(); /*! Assignment operator */ void operator=(const DocumentT&); /*! Assignment operator */ void operator=(const Document*); + /*! Assignment operator */ + void operator=(const std::string&); /*! Get a pointer to the document or 0 if it doesn't exist any more. */ Document* getDocument() const; diff --git a/src/Gui/DocumentObserver.cpp b/src/Gui/DocumentObserver.cpp index d46c0b1a1f..83a5690a38 100644 --- a/src/Gui/DocumentObserver.cpp +++ b/src/Gui/DocumentObserver.cpp @@ -33,10 +33,160 @@ #include "Document.h" #include "ViewProviderDocumentObject.h" #include "DocumentObserver.h" +#include using namespace Gui; +DocumentT::DocumentT() +{ +} + +DocumentT::DocumentT(Document* doc) +{ + document = doc->getDocument()->getName(); +} + +DocumentT::DocumentT(const std::string& name) +{ + document = name; +} + +DocumentT::~DocumentT() +{ +} + +void DocumentT::operator=(const DocumentT& doc) +{ + if (this == &doc) + return; + document = doc.document; +} + +void DocumentT::operator=(const Document* doc) +{ + document = doc->getDocument()->getName(); +} + +void DocumentT::operator=(const std::string& name) +{ + document = name; +} + +Document* DocumentT::getDocument() const +{ + return Application::Instance->getDocument(document.c_str()); +} + +std::string DocumentT::getDocumentName() const +{ + return document; +} + +std::string DocumentT::getDocumentPython() 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(); +} + +// ----------------------------------------------------------------------------- + +ViewProviderT::ViewProviderT() +{ +} + +ViewProviderT::ViewProviderT(ViewProviderDocumentObject* obj) +{ + object = obj->getObject()->getNameInDocument(); + document = obj->getObject()->getDocument()->getName(); +} + +ViewProviderT::~ViewProviderT() +{ +} + +void ViewProviderT::operator=(const ViewProviderT& obj) +{ + if (this == &obj) + return; + object = obj.object; + document = obj.document; +} + +void ViewProviderT::operator=(const ViewProviderDocumentObject* obj) +{ + object = obj->getObject()->getNameInDocument(); + document = obj->getObject()->getDocument()->getName(); +} + +Document* ViewProviderT::getDocument() const +{ + return Application::Instance->getDocument(document.c_str()); +} + +std::string ViewProviderT::getDocumentName() const +{ + return document; +} + +std::string ViewProviderT::getDocumentPython() 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(); +} + +ViewProviderDocumentObject* ViewProviderT::getViewProvider() const +{ + ViewProviderDocumentObject* obj = 0; + Document* doc = getDocument(); + if (doc) { + obj = dynamic_cast(doc->getViewProviderByName(object.c_str())); + } + return obj; +} + +std::string ViewProviderT::getObjectName() const +{ + return object; +} + +std::string ViewProviderT::getObjectPython() const +{ + std::stringstream str; + Document* doc = Application::Instance->activeDocument(); + if (doc && document == doc->getDocument()->getName()) { + str << "Gui.ActiveDocument."; + } + else { + str << "Gui.getDocument(\"" + << document + << "\")."; + } + + str << object; + return str.str(); +} + +// ----------------------------------------------------------------------------- + DocumentObserver::DocumentObserver() { } diff --git a/src/Gui/DocumentObserver.h b/src/Gui/DocumentObserver.h index 08a9399e24..d61f67132b 100644 --- a/src/Gui/DocumentObserver.h +++ b/src/Gui/DocumentObserver.h @@ -33,6 +33,87 @@ namespace Gui class Document; class ViewProviderDocumentObject; +/** + * The DocumentT class is a helper class to store the name of a document. + * This can be useful when you cannot rely on that the document still exists when you have to + * access it. + * + * @author Werner Mayer + */ +class GuiExport DocumentT +{ +public: + /*! Constructor */ + DocumentT(); + /*! Constructor */ + DocumentT(Document*); + /*! Constructor */ + DocumentT(const std::string&); + /*! Destructor */ + ~DocumentT(); + /*! Assignment operator */ + void operator=(const DocumentT&); + /*! Assignment operator */ + void operator=(const Document*); + /*! Assignment operator */ + void operator=(const std::string&); + + /*! Get a pointer to the document or 0 if it doesn't exist any more. */ + Document* getDocument() const; + /*! Get the name of the document. */ + std::string getDocumentName() const; + /*! Get the document as Python command. */ + std::string getDocumentPython() const; + +private: + std::string document; +}; + +/** + * The ViewProviderT class is a helper class to store the names of a view provider and its document. + * This can be useful when you cannot rely on that the document or the object still exists when you have to + * access it. + * + * @author Werner Mayer + */ +class GuiExport ViewProviderT +{ +public: + /*! Constructor */ + ViewProviderT(); + /*! Constructor */ + ViewProviderT(ViewProviderDocumentObject*); + /*! Destructor */ + ~ViewProviderT(); + /*! Assignment operator */ + void operator=(const ViewProviderT&); + /*! Assignment operator */ + void operator=(const ViewProviderDocumentObject*); + + /*! Get a pointer to the document or 0 if it doesn't exist any more. */ + Document* getDocument() const; + /*! Get the name of the document. */ + std::string getDocumentName() const; + /*! Get the document as Python command. */ + std::string getDocumentPython() 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. */ + std::string getObjectName() const; + /*! Get the document object as Python command. */ + std::string getObjectPython() const; + /*! Get a pointer to the document or 0 if it doesn't exist any more or the type doesn't match. */ + template + inline T* getObjectAs() const + { + return Base::freecad_dynamic_cast(getViewProvider()); + } + +private: + std::string document; + std::string object; +}; + /** * The DocumentObserver class simplifies the step to write classes that listen * to what happens inside a document.