+ add helper classes to store names of doument and object and not a pointer
This commit is contained in:
parent
8645d56acf
commit
d6bbc0ad0f
|
@ -24,6 +24,7 @@
|
|||
#include "PreCompiled.h"
|
||||
|
||||
#ifndef _PreComp_
|
||||
# include <sstream>
|
||||
#endif
|
||||
|
||||
#include <boost/signals.hpp>
|
||||
|
@ -36,6 +37,154 @@
|
|||
|
||||
using namespace App;
|
||||
|
||||
|
||||
DocumentT::DocumentT()
|
||||
{
|
||||
}
|
||||
|
||||
DocumentT::DocumentT(Document* doc)
|
||||
{
|
||||
document = doc->getName();
|
||||
}
|
||||
|
||||
DocumentT::~DocumentT()
|
||||
{
|
||||
}
|
||||
|
||||
void DocumentT::operator=(const DocumentT& doc)
|
||||
{
|
||||
if (this == &doc)
|
||||
return;
|
||||
document = doc.document;
|
||||
}
|
||||
|
||||
void DocumentT::operator=(const Document* doc)
|
||||
{
|
||||
document = doc->getName();
|
||||
}
|
||||
|
||||
Document* DocumentT::getDocument() const
|
||||
{
|
||||
return GetApplication().getDocument(document.c_str());
|
||||
}
|
||||
|
||||
std::string DocumentT::getDocumentName() const
|
||||
{
|
||||
return document;
|
||||
}
|
||||
|
||||
std::string DocumentT::getDocumentPython() const
|
||||
{
|
||||
std::stringstream str;
|
||||
Document* doc = GetApplication().getActiveDocument();
|
||||
if (doc && document == doc->getName()) {
|
||||
str << "FreeCAD.ActiveDocument";
|
||||
}
|
||||
else {
|
||||
str << "FreeCAD.getDocument(\""
|
||||
<< document
|
||||
<< "\")";
|
||||
}
|
||||
return str.str();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
DocumentObjectT::DocumentObjectT()
|
||||
{
|
||||
}
|
||||
|
||||
DocumentObjectT::DocumentObjectT(DocumentObject* obj)
|
||||
{
|
||||
object = obj->getNameInDocument();
|
||||
label = obj->Label.getValue();
|
||||
document = obj->getDocument()->getName();
|
||||
}
|
||||
|
||||
DocumentObjectT::~DocumentObjectT()
|
||||
{
|
||||
}
|
||||
|
||||
void DocumentObjectT::operator=(const DocumentObjectT& obj)
|
||||
{
|
||||
if (this == &obj)
|
||||
return;
|
||||
object = obj.object;
|
||||
label = obj.label;
|
||||
document = obj.document;
|
||||
}
|
||||
|
||||
void DocumentObjectT::operator=(const DocumentObject* obj)
|
||||
{
|
||||
object = obj->getNameInDocument();
|
||||
label = obj->Label.getValue();
|
||||
document = obj->getDocument()->getName();
|
||||
}
|
||||
|
||||
Document* DocumentObjectT::getDocument() const
|
||||
{
|
||||
return GetApplication().getDocument(document.c_str());
|
||||
}
|
||||
|
||||
std::string DocumentObjectT::getDocumentName() const
|
||||
{
|
||||
return document;
|
||||
}
|
||||
|
||||
std::string DocumentObjectT::getDocumentPython() const
|
||||
{
|
||||
std::stringstream str;
|
||||
Document* doc = GetApplication().getActiveDocument();
|
||||
if (doc && document == doc->getName()) {
|
||||
str << "FreeCAD.ActiveDocument";
|
||||
}
|
||||
else {
|
||||
str << "FreeCAD.getDocument(\""
|
||||
<< document
|
||||
<< "\")";
|
||||
}
|
||||
return str.str();
|
||||
}
|
||||
|
||||
DocumentObject* DocumentObjectT::getObject() const
|
||||
{
|
||||
DocumentObject* obj = 0;
|
||||
Document* doc = getDocument();
|
||||
if (doc) {
|
||||
obj = doc->getObject(object.c_str());
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
|
||||
std::string DocumentObjectT::getObjectName() const
|
||||
{
|
||||
return object;
|
||||
}
|
||||
|
||||
std::string DocumentObjectT::getObjectLabel() const
|
||||
{
|
||||
return label;
|
||||
}
|
||||
|
||||
std::string DocumentObjectT::getObjectPython() const
|
||||
{
|
||||
std::stringstream str;
|
||||
Document* doc = GetApplication().getActiveDocument();
|
||||
if (doc && document == doc->getName()) {
|
||||
str << "FreeCAD.ActiveDocument.";
|
||||
}
|
||||
else {
|
||||
str << "FreeCAD.getDocument(\""
|
||||
<< document
|
||||
<< "\").";
|
||||
}
|
||||
|
||||
str << object;
|
||||
return str.str();
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
DocumentObserver::DocumentObserver() : _document(0)
|
||||
{
|
||||
this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind
|
||||
|
|
|
@ -33,6 +33,86 @@ class Document;
|
|||
class DocumentObject;
|
||||
class Property;
|
||||
|
||||
/**
|
||||
* 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 AppExport DocumentT
|
||||
{
|
||||
public:
|
||||
/*! Constructor */
|
||||
DocumentT();
|
||||
/*! Constructor */
|
||||
DocumentT(Document*);
|
||||
/*! Destructor */
|
||||
~DocumentT();
|
||||
/*! Assignment operator */
|
||||
void operator=(const DocumentT&);
|
||||
/*! Assignment operator */
|
||||
void operator=(const Document*);
|
||||
|
||||
/*! 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 DocumentObjectT class is a helper class to store the names of a document object 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 AppExport DocumentObjectT
|
||||
{
|
||||
public:
|
||||
/*! Constructor */
|
||||
DocumentObjectT();
|
||||
/*! Constructor */
|
||||
DocumentObjectT(DocumentObject*);
|
||||
/*! Destructor */
|
||||
~DocumentObjectT();
|
||||
/*! Assignment operator */
|
||||
void operator=(const DocumentObjectT&);
|
||||
/*! Assignment operator */
|
||||
void operator=(const DocumentObject*);
|
||||
|
||||
/*! 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. */
|
||||
DocumentObject* getObject() const;
|
||||
/*! Get the name of the document object. */
|
||||
std::string getObjectName() const;
|
||||
/*! Get the label of the document object. */
|
||||
std::string getObjectLabel() 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 freecad_dynamic_cast<T>(getObject());
|
||||
}
|
||||
|
||||
private:
|
||||
std::string document;
|
||||
std::string object;
|
||||
std::string label;
|
||||
};
|
||||
|
||||
/**
|
||||
* The DocumentObserver class simplfies the step to write classes that listen
|
||||
* to what happens inside a document.
|
||||
|
|
Loading…
Reference in New Issue
Block a user