add/extend helper classes to savely access document, document objects or view providers
This commit is contained in:
@@ -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
|
||||
<< "\").";
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -33,10 +33,160 @@
|
||||
#include "Document.h"
|
||||
#include "ViewProviderDocumentObject.h"
|
||||
#include "DocumentObserver.h"
|
||||
#include <App/Document.h>
|
||||
|
||||
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<ViewProviderDocumentObject*>(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()
|
||||
{
|
||||
}
|
||||
|
||||
@@ -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<typename T>
|
||||
inline T* getObjectAs() const
|
||||
{
|
||||
return Base::freecad_dynamic_cast<T>(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.
|
||||
|
||||
Reference in New Issue
Block a user