App: add new convenient class SubObjectT

Derived from App::DocumentObjectT to add support of sub object

Also some minor changes to App::DocumentObjectT

Changed Gui::SelectionChanges to make use of SubObjectT
This commit is contained in:
Zheng, Lei
2019-12-05 10:34:51 +08:00
committed by WandererFan
parent e8c2369828
commit e4978be7a4
6 changed files with 324 additions and 121 deletions

View File

@@ -29,10 +29,13 @@
#include <boost/bind.hpp>
#include <Base/Tools.h>
#include "Application.h"
#include "Document.h"
#include "DocumentObject.h"
#include "DocumentObserver.h"
#include "ComplexGeoData.h"
#include "GeoFeature.h"
using namespace App;
@@ -77,7 +80,7 @@ Document* DocumentT::getDocument() const
return GetApplication().getDocument(document.c_str());
}
std::string DocumentT::getDocumentName() const
const std::string &DocumentT::getDocumentName() const
{
return document;
}
@@ -85,15 +88,7 @@ std::string DocumentT::getDocumentName() const
std::string DocumentT::getDocumentPython() const
{
std::stringstream str;
Document* doc = GetApplication().getActiveDocument();
if (doc && document == doc->getName()) {
str << "App.ActiveDocument";
}
else {
str << "App.getDocument(\""
<< document
<< "\")";
}
str << "App.getDocument(\"" << document << "\")";
return str.str();
}
@@ -103,11 +98,19 @@ DocumentObjectT::DocumentObjectT()
{
}
DocumentObjectT::DocumentObjectT(const DocumentObjectT &other)
{
*this = other;
}
DocumentObjectT::DocumentObjectT(DocumentObjectT &&other)
{
*this = std::move(other);
}
DocumentObjectT::DocumentObjectT(const DocumentObject* obj)
{
object = obj->getNameInDocument();
label = obj->Label.getValue();
document = obj->getDocument()->getName();
*this = obj;
}
DocumentObjectT::DocumentObjectT(const Property* prop)
@@ -115,35 +118,78 @@ DocumentObjectT::DocumentObjectT(const Property* prop)
*this = prop;
}
DocumentObjectT::DocumentObjectT(const char *docName, const char *objName)
{
if(docName)
document = docName;
if(objName)
object = objName;
}
DocumentObjectT::~DocumentObjectT()
{
}
void DocumentObjectT::operator=(const DocumentObjectT& obj)
DocumentObjectT &DocumentObjectT::operator=(const DocumentObjectT& obj)
{
if (this == &obj)
return;
return *this;
object = obj.object;
label = obj.label;
document = obj.document;
property = obj.property;
return *this;
}
DocumentObjectT &DocumentObjectT::operator=(DocumentObjectT&& obj)
{
if (this == &obj)
return *this;
object = std::move(obj.object);
label = std::move(obj.label);
document = std::move(obj.document);
property = std::move(obj.property);
return *this;
}
void DocumentObjectT::operator=(const DocumentObject* obj)
{
object = obj->getNameInDocument();
label = obj->Label.getValue();
document = obj->getDocument()->getName();
property.clear();
if(!obj || !obj->getNameInDocument()) {
object.clear();
label.clear();
document.clear();
property.clear();
} else {
object = obj->getNameInDocument();
label = obj->Label.getValue();
document = obj->getDocument()->getName();
property.clear();
}
}
void DocumentObjectT::operator=(const Property *prop) {
auto obj = dynamic_cast<const DocumentObject*>(prop->getContainer());
assert(obj);
object = obj->getNameInDocument();
label = obj->Label.getValue();
document = obj->getDocument()->getName();
property = prop->getName();
if(!prop || !prop->getName()
|| !prop->getContainer()
|| !prop->getContainer()->isDerivedFrom(App::DocumentObject::getClassTypeId()))
{
object.clear();
label.clear();
document.clear();
property.clear();
} else {
auto obj = static_cast<App::DocumentObject*>(prop->getContainer());
object = obj->getNameInDocument();
label = obj->Label.getValue();
document = obj->getDocument()->getName();
property = prop->getName();
}
}
bool DocumentObjectT::operator==(const DocumentObjectT &other) const {
return document == other.document
&& object == other.object
&& label == other.label
&& property == other.property;
}
Document* DocumentObjectT::getDocument() const
@@ -151,7 +197,7 @@ Document* DocumentObjectT::getDocument() const
return GetApplication().getDocument(document.c_str());
}
std::string DocumentObjectT::getDocumentName() const
const std::string& DocumentObjectT::getDocumentName() const
{
return document;
}
@@ -159,15 +205,7 @@ std::string DocumentObjectT::getDocumentName() const
std::string DocumentObjectT::getDocumentPython() const
{
std::stringstream str;
Document* doc = GetApplication().getActiveDocument();
if (doc && document == doc->getName()) {
str << "App.ActiveDocument";
}
else {
str << "App.getDocument(\""
<< document
<< "\")";
}
str << "FreeCAD.getDocument(\"" << document << "\")";
return str.str();
}
@@ -181,12 +219,12 @@ DocumentObject* DocumentObjectT::getObject() const
return obj;
}
std::string DocumentObjectT::getObjectName() const
const std::string &DocumentObjectT::getObjectName() const
{
return object;
}
std::string DocumentObjectT::getObjectLabel() const
const std::string &DocumentObjectT::getObjectLabel() const
{
return label;
}
@@ -194,21 +232,11 @@ std::string DocumentObjectT::getObjectLabel() const
std::string DocumentObjectT::getObjectPython() const
{
std::stringstream str;
Document* doc = GetApplication().getActiveDocument();
if (doc && document == doc->getName()) {
str << "App.ActiveDocument.";
}
else {
str << "App.getDocument(\""
<< document
<< "\").";
}
str << object;
str << "FreeCAD.getDocument('" << document << "').getObject('" << object << "')";
return str.str();
}
std::string DocumentObjectT::getPropertyName() const {
const std::string &DocumentObjectT::getPropertyName() const {
return property;
}
@@ -231,6 +259,134 @@ Property *DocumentObjectT::getProperty() const {
}
// -----------------------------------------------------------------------------
SubObjectT::SubObjectT()
{}
SubObjectT::SubObjectT(const SubObjectT &other)
:DocumentObjectT(other), subname(other.subname)
{
}
SubObjectT::SubObjectT(SubObjectT &&other)
:DocumentObjectT(std::move(other)), subname(std::move(other.subname))
{
}
SubObjectT::SubObjectT(const DocumentObject *obj, const char *s)
:DocumentObjectT(obj),subname(s?s:"")
{}
SubObjectT::SubObjectT(const char *docName, const char *objName, const char *s)
:DocumentObjectT(docName,objName), subname(s?s:"")
{
}
bool SubObjectT::operator<(const SubObjectT &other) const {
if(getDocumentName() < other.getDocumentName())
return true;
if(getDocumentName() > other.getDocumentName())
return false;
if(getObjectName() < other.getObjectName())
return true;
if(getObjectName() > other.getObjectName())
return false;
if(getSubName() < other.getSubName())
return true;
if(getSubName() > other.getSubName())
return false;
return getPropertyName() < other.getPropertyName();
}
SubObjectT &SubObjectT::operator=(const SubObjectT& other)
{
if (this == &other)
return *this;
static_cast<DocumentObjectT&>(*this) = other;
subname = other.subname;
return *this;
}
SubObjectT &SubObjectT::operator=(SubObjectT &&other)
{
if (this == &other)
return *this;
static_cast<DocumentObjectT&>(*this) = std::move(other);
subname = std::move(other.subname);
return *this;
}
bool SubObjectT::operator==(const SubObjectT &other) const {
return static_cast<const DocumentObjectT&>(*this) == other
&& subname == other.subname;
}
void SubObjectT::setSubName(const char *s) {
subname = s?s:"";
}
const std::string &SubObjectT::getSubName() const {
return subname;
}
std::string SubObjectT::getSubNameNoElement() const {
return Data::ComplexGeoData::noElementName(subname.c_str());
}
const char *SubObjectT::getElementName() const {
return Data::ComplexGeoData::findElementName(subname.c_str());
}
std::string SubObjectT::getNewElementName() const {
std::pair<std::string, std::string> element;
auto obj = getObject();
if(!obj)
return std::string();
GeoFeature::resolveElement(obj,subname.c_str(),element);
return std::move(element.first);
}
std::string SubObjectT::getOldElementName(int *index) const {
std::pair<std::string, std::string> element;
auto obj = getObject();
if(!obj)
return std::string();
GeoFeature::resolveElement(obj,subname.c_str(),element);
if(!index)
return std::move(element.second);
std::size_t pos = element.second.find_first_of("0123456789");
if(pos == std::string::npos)
*index = -1;
else {
*index = std::atoi(element.second.c_str()+pos);
element.second.resize(pos);
}
return std::move(element.second);
}
App::DocumentObject *SubObjectT::getSubObject() const {
auto obj = getObject();
if(obj)
return obj->getSubObject(subname.c_str());
return 0;
}
std::string SubObjectT::getSubObjectPython(bool force) const {
if(!force && subname.empty())
return getObjectPython();
std::stringstream str;
str << "(" << getObjectPython() << ",u'"
<< Base::Tools::escapedUnicodeFromUtf8(subname.c_str()) << "')";
return str.str();
}
std::vector<App::DocumentObject*> SubObjectT::getSubObjectList() const {
auto obj = getObject();
if(obj)
return obj->getSubObjectList(subname.c_str());
return {};
}
// -----------------------------------------------------------------------------
DocumentObserver::DocumentObserver() : _document(0)
{
this->connectApplicationCreatedDocument = App::GetApplication().signalNewDocument.connect(boost::bind