App: Property related API changes

Property:

* Extended property status bitset. Mirror most of PropertyType and
  allow dynamic change property type.

* Cache property name and type to improve performance

* Centralize property status change signalling

* Change aboutToSetValue()/hasSetValue() to virtual

* Add new API getFullName() to obtain full quanlified name of the property

AtomicPropertyChangeInterface:

* Allow calling aboutToSetValue()/hasSetValue() when actually changed

PropertyLists:

* Refactor implementation by an abstract class PropertyListBase and a
  template class PropertyListsT, to allow better code reuse.
  PropertyListT is derived from AtomicPropertyChangeInterface to allow
  more efficient change on individual elements.

* All list type property now accept setting python value as a dictionary
  with index as key to set individual element of a list.

* Add touch list for more efficient handling of value changes. The list
  contains the index of changed value. And empty touch list should be
  treated as the entire list is changed. PropertyContainerPy expose this
  functionality with getPropertyTouchList().

PropertyPersistentObject:

* New property to allow dynamic creation of any FreeCAD object derived
  from Base::Persistence, and use it as a property.

DynamicProperty:

* Use boost multi_index_container for efficient property lookup while
  keeping order.

* Modify to be allowed to use in PropertyContainer directly

PropertyContainer:

* Use boost multi_index_container for efficient property lookup while
  keeping order.

* Allow adding/removing dynamic property on all property container

* Modify Save/Restore() to persist property status, and better handle
  transient property which can now be dynamically enabled/disabled per
  object.

* Add new API getFullName() to obtain full quanlified name of the property.
  Implemented by Document, DocumentObject, and also
  ViewProviderDocumentObject if future patch

DocumentObject and FeaturePython are modified to accommondate the
dynamic property changes.

Removed get/setCustomAttribute() implementation from DocumentObjectPy,
and rely on PropertyContainerPy for the implementation, because of the
additional dynamic property support in property container.

Gui::ViewProviderDocumentObject, which is derived from
PropertyContainer, is also modified accordingly
This commit is contained in:
Zheng, Lei
2019-06-28 10:16:42 +08:00
committed by wmayer
parent ff1d1cd341
commit f4205130ae
25 changed files with 1511 additions and 1579 deletions

View File

@@ -37,202 +37,117 @@
#include <Base/Exception.h>
#include <Base/Tools.h>
FC_LOG_LEVEL_INIT("DynamicProperty",true,true)
using namespace App;
DynamicProperty::DynamicProperty(PropertyContainer* p) : pc(p)
DynamicProperty::DynamicProperty()
{
}
DynamicProperty::~DynamicProperty()
{
clear();
}
void DynamicProperty::clear() {
auto &index = props.get<0>();
for(auto &v : index)
delete v.property;
index.clear();
}
void DynamicProperty::getPropertyList(std::vector<Property*> &List) const
{
// get the properties of the base class first and insert the dynamic properties afterwards
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyList(List);
else
this->pc->PropertyContainer::getPropertyList(List);
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it)
List.push_back(it->second.property);
for (auto &v : props.get<0>())
List.push_back(v.property);
}
void DynamicProperty::getPropertyMap(std::map<std::string,Property*> &Map) const
{
// get the properties of the base class first and insert the dynamic properties afterwards
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyMap(Map);
else
this->pc->PropertyContainer::getPropertyMap(Map);
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it)
Map[it->first] = it->second.property;
}
Property *DynamicProperty::getPropertyByName(const char* name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.property;
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyByName(name);
return this->pc->PropertyContainer::getPropertyByName(name);
for (auto &v : props.get<0>())
Map[v.name] = v.property;
}
Property *DynamicProperty::getDynamicPropertyByName(const char* name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.property;
auto &index = props.get<0>();
auto it = index.find(name);
if (it != index.end())
return it->property;
return 0;
}
std::vector<std::string> DynamicProperty::getDynamicPropertyNames() const
{
std::vector<std::string> names;
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
names.push_back(it->first);
}
auto &index = props.get<0>();
names.reserve(index.size());
for(auto &v : index)
names.push_back(v.name);
return names;
}
void DynamicProperty::addDynamicProperties(const PropertyContainer* cont)
{
std::vector<std::string> names = cont->getDynamicPropertyNames();
for (std::vector<std::string>::iterator it = names.begin(); it != names.end(); ++it) {
App::Property* prop = cont->getDynamicPropertyByName(it->c_str());
if (prop) {
addDynamicProperty(
prop->getTypeId().getName(),
prop->getName(),
prop->getGroup(),
prop->getDocumentation(),
prop->getType(),
cont->isReadOnly(prop),
cont->isHidden(prop));
}
}
}
const char* DynamicProperty::getPropertyName(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->first.c_str();
}
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyName(prop);
return this->pc->PropertyContainer::getPropertyName(prop);
}
unsigned int DynamicProperty::getMemSize (void) const
{
std::map<std::string,Property*> Map;
getPropertyMap(Map);
std::map<std::string,Property*>::const_iterator It;
unsigned int size = 0;
for (It = Map.begin(); It != Map.end();++It)
size += It->second->getMemSize();
return size;
}
short DynamicProperty::getPropertyType(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop) {
short attr = it->second.attr;
if (it->second.hidden)
attr |= Prop_Hidden;
if (it->second.readonly)
attr |= Prop_ReadOnly;
return attr;
}
}
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyType(prop);
return this->pc->PropertyContainer::getPropertyType(prop);
return prop?prop->getType():0;
}
short DynamicProperty::getPropertyType(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end()) {
short attr = it->second.attr;
if (it->second.hidden)
auto &index = props.get<0>();
auto it = index.find(name);
if (it != index.end()) {
short attr = it->attr;
if (it->hidden)
attr |= Prop_Hidden;
if (it->second.readonly)
if (it->readonly)
attr |= Prop_ReadOnly;
return attr;
}
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyType(name);
return this->pc->PropertyContainer::getPropertyType(name);
return 0;
}
const char* DynamicProperty::getPropertyGroup(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.group.c_str();
}
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyGroup(prop);
return this->pc->PropertyContainer::getPropertyGroup(prop);
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if(it!=index.end())
return it->group.c_str();
return 0;
}
const char* DynamicProperty::getPropertyGroup(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.group.c_str();
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyGroup(name);
return this->pc->PropertyContainer::getPropertyGroup(name);
auto &index = props.get<0>();
auto it = index.find(name);
if (it != index.end())
return it->group.c_str();
return 0;
}
const char* DynamicProperty::getPropertyDocumentation(const Property* prop) const
{
for (std::map<std::string,PropData>::const_iterator it = props.begin(); it != props.end(); ++it) {
if (it->second.property == prop)
return it->second.doc.c_str();
}
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyDocumentation(prop);
return this->pc->PropertyContainer::getPropertyDocumentation(prop);
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if(it!=index.end())
return it->doc.c_str();
return 0;
}
const char* DynamicProperty::getPropertyDocumentation(const char *name) const
{
std::map<std::string,PropData>::const_iterator it = props.find(name);
if (it != props.end())
return it->second.doc.c_str();
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
return static_cast<App::ExtensionContainer*>(this->pc)->ExtensionContainer::getPropertyDocumentation(name);
return this->pc->PropertyContainer::getPropertyDocumentation(name);
auto &index = props.get<0>();
auto it = index.find(name);
if (it != index.end())
return it->doc.c_str();
return 0;
}
Property* DynamicProperty::addDynamicProperty(const char* type, const char* name, const char* group,
const char* doc, short attr, bool ro, bool hidden)
Property* DynamicProperty::addDynamicProperty(PropertyContainer &pc, const char* type,
const char* name, const char* group, const char* doc, short attr, bool ro, bool hidden)
{
Base::BaseClass* base = static_cast<Base::BaseClass*>(Base::Type::createInstanceByName(type,true));
if (!base)
@@ -246,49 +161,76 @@ Property* DynamicProperty::addDynamicProperty(const char* type, const char* name
// get unique name
Property* pcProperty = static_cast<Property*>(base);
std::string ObjectName;
if (name && *name != '\0')
ObjectName = getUniquePropertyName(name);
else
ObjectName = getUniquePropertyName(type);
if (!name || !name[0])
name = type;
pcProperty->setContainer(this->pc);
PropData data;
data.property = pcProperty;
data.group = (group ? group : "");
data.doc = (doc ? doc : "");
data.attr = attr;
data.readonly = ro;
data.hidden = hidden;
props[ObjectName] = data;
auto res = props.get<0>().emplace(pcProperty,
getUniquePropertyName(pc,name), nullptr, group, doc, attr, ro, hidden);
pcProperty->setContainer(&pc);
pcProperty->myName = res.first->name.c_str();
if(ro) attr |= Prop_ReadOnly;
if(hidden) attr |= Prop_Hidden;
pcProperty->syncType(attr);
pcProperty->StatusBits.set((size_t)Property::PropDynamic);
GetApplication().signalAppendDynamicProperty(*pcProperty);
return pcProperty;
}
bool DynamicProperty::addProperty(Property *prop)
{
if(!prop || !prop->getName())
return false;
auto &index = props.get<0>();
if(index.count(prop->getName()))
return false;
index.emplace(prop,std::string(),prop->getName(),
prop->getGroup(),prop->getDocumentation(),prop->getType(),false,false);
return true;
}
bool DynamicProperty::removeProperty(const Property *prop)
{
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if (it != index.end()) {
index.erase(it);
return true;
}
return false;
}
bool DynamicProperty::removeDynamicProperty(const char* name)
{
std::map<std::string,PropData>::iterator it = props.find(name);
if (it != props.end()) {
GetApplication().signalRemoveDynamicProperty(*it->second.property);
delete it->second.property;
props.erase(it);
auto &index = props.get<0>();
auto it = index.find(name);
if (it != index.end()) {
if(it->property->testStatus(Property::LockDynamic))
throw Base::RuntimeError("property is locked");
else if(!it->property->testStatus(Property::PropDynamic))
throw Base::RuntimeError("property is not dynamic");
Property *prop = it->property;
GetApplication().signalRemoveDynamicProperty(*prop);
delete prop;
index.erase(it);
return true;
}
return false;
}
std::string DynamicProperty::getUniquePropertyName(const char *Name) const
std::string DynamicProperty::getUniquePropertyName(PropertyContainer &pc, const char *Name) const
{
std::string CleanName = Base::Tools::getIdentifier(Name);
// name in use?
std::map<std::string,Property*> objectProps;
getPropertyMap(objectProps);
std::map<std::string,Property*>::const_iterator pos;
pos = objectProps.find(CleanName);
pc.getPropertyMap(objectProps);
auto pos = objectProps.find(CleanName);
if (pos == objectProps.end()) {
// if not, name is OK
@@ -304,151 +246,60 @@ std::string DynamicProperty::getUniquePropertyName(const char *Name) const
}
}
void DynamicProperty::Save (Base::Writer &writer) const
void DynamicProperty::save(const Property *prop, Base::Writer &writer) const
{
//extensions must be saved first, as they need to be read and initialised before properties (as
//they have their own properties which they need to handle on restore)
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
static_cast<App::ExtensionContainer*>(this->pc)->saveExtensions(writer);
std::map<std::string,Property*> Map;
getPropertyMap(Map);
writer.incInd(); // indentation for 'Properties Count'
writer.Stream() << writer.ind() << "<Properties Count=\"" << Map.size() << "\">" << std::endl;
std::map<std::string,Property*>::iterator it;
for (it = Map.begin(); it != Map.end(); ++it)
{
writer.incInd(); // indentation for 'Property name'
// check whether a static or dynamic property
std::map<std::string,PropData>::const_iterator pt = props.find(it->first);
if (pt == props.end()) {
writer.Stream() << writer.ind() << "<Property name=\"" << it->first << "\" type=\""
<< it->second->getTypeId().getName() << "\">" << std::endl;
}
else {
writer.Stream() << writer.ind() << "<Property name=\"" << it->first
<< "\" type=\"" << it->second->getTypeId().getName()
<< "\" group=\"" << encodeAttribute(pt->second.group)
<< "\" doc=\"" << encodeAttribute(pt->second.doc)
<< "\" attr=\"" << pt->second.attr << "\" ro=\"" << pt->second.readonly
<< "\" hide=\"" << pt->second.hidden << "\">" << std::endl;
}
writer.incInd(); // indentation for the actual property
try {
// We must make sure to handle all exceptions accordingly so that
// the project file doesn't get invalidated. In the error case this
// means to proceed instead of aborting the write operation.
// Don't write transient properties
if (!(getPropertyType(it->second) & Prop_Transient))
it->second->Save(writer);
}
catch (const Base::Exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const std::exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const char* e) {
Base::Console().Error("%s\n", e);
}
#ifndef FC_DEBUG
catch (...) {
Base::Console().Error("DynamicProperty::Save: Unknown C++ exception thrown. Try to continue...\n");
}
#endif
writer.decInd(); // indentation for the actual property
writer.Stream() << writer.ind() << "</Property>" << std::endl;
writer.decInd(); // indentation for 'Property name'
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if(it != index.end()) {
auto &data = *it;
writer.Stream() << "\" group=\"" << Base::Persistence::encodeAttribute(data.group)
<< "\" doc=\"" << Base::Persistence::encodeAttribute(data.doc)
<< "\" attr=\"" << data.attr << "\" ro=\"" << data.readonly
<< "\" hide=\"" << data.hidden;
}
writer.Stream() << writer.ind() << "</Properties>" << std::endl;
writer.decInd(); // indentation for 'Properties Count'
}
void DynamicProperty::Restore(Base::XMLReader &reader)
Property *DynamicProperty::restore(PropertyContainer &pc,
const char *PropName, const char *TypeName, Base::XMLReader &reader)
{
//first all extensions must be initialised so that they can handle their properties
if (this->pc->isDerivedFrom(App::ExtensionContainer::getClassTypeId()))
static_cast<App::ExtensionContainer*>(this->pc)->restoreExtensions(reader);
if (!reader.hasAttribute("group"))
return 0;
reader.readElement("Properties");
int Cnt = reader.getAttributeAsInteger("Count");
for (int i=0 ;i<Cnt ;i++) {
reader.readElement("Property");
const char* PropName = reader.getAttribute("name");
const char* TypeName = reader.getAttribute("type");
Property* prop = getPropertyByName(PropName);
try {
if (!prop) {
short attribute = 0;
bool readonly = false, hidden = false;
const char *group=0, *doc=0, *attr=0, *ro=0, *hide=0;
if (reader.hasAttribute("group"))
group = reader.getAttribute("group");
if (reader.hasAttribute("doc"))
doc = reader.getAttribute("doc");
if (reader.hasAttribute("attr")) {
attr = reader.getAttribute("attr");
if (attr) attribute = attr[0]-48;
}
if (reader.hasAttribute("ro")) {
ro = reader.getAttribute("ro");
if (ro) readonly = (ro[0]-48) != 0;
}
if (reader.hasAttribute("hide")) {
hide = reader.getAttribute("hide");
if (hide) hidden = (hide[0]-48) != 0;
}
prop = addDynamicProperty(TypeName, PropName, group, doc, attribute, readonly, hidden);
}
}
catch(const Base::Exception& e) {
// only handle this exception type
Base::Console().Warning(e.what());
}
//NOTE: We must also check the type of the current property because a subclass of
//PropertyContainer might change the type of a property but not its name. In this
//case we would force to read-in a wrong property type and the behaviour would be
//undefined.
// Don't read transient properties
if (!(getPropertyType(prop) & Prop_Transient)) {
if (prop && strcmp(prop->getTypeId().getName(), TypeName) == 0) {
try {
prop->Restore(reader);
}
catch (const Base::XMLParseException&) {
throw; // re-throw
}
catch (const Base::Exception &e) {
Base::Console().Error("%s\n", e.what());
}
catch (const std::exception &e) {
Base::Console().Error("%s\n", e.what());
}
#ifndef FC_DEBUG
catch (...) {
Base::Console().Error("DynamicProperty::Restore: Unknown C++ exception thrown\n");
}
#endif
}
else if (prop) {
//Base::Console().Warning("%s: Overread data for property %s of type %s, expected type is %s\n",
// pc->getTypeId().getName(), prop->getName(), prop->getTypeId().getName(), TypeName);
pc->handleChangedPropertyType(reader, TypeName, prop);
}
else {
//Base::Console().Warning("%s: No property found with name %s and type %s\n",
// pc->getTypeId().getName(), PropName, TypeName);
pc->handleChangedPropertyName(reader, TypeName, PropName);
}
}
reader.readEndElement("Property");
short attribute = 0;
bool readonly = false, hidden = false;
const char *group=0, *doc=0, *attr=0, *ro=0, *hide=0;
group = reader.getAttribute("group");
if (reader.hasAttribute("doc"))
doc = reader.getAttribute("doc");
if (reader.hasAttribute("attr")) {
attr = reader.getAttribute("attr");
if (attr) attribute = attr[0]-48;
}
reader.readEndElement("Properties");
if (reader.hasAttribute("ro")) {
ro = reader.getAttribute("ro");
if (ro) readonly = (ro[0]-48) != 0;
}
if (reader.hasAttribute("hide")) {
hide = reader.getAttribute("hide");
if (hide) hidden = (hide[0]-48) != 0;
}
return addDynamicProperty(pc,TypeName, PropName, group, doc, attribute, readonly, hidden);
}
DynamicProperty::PropData DynamicProperty::getDynamicPropertyData(const Property *prop) const
{
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if(it != index.end())
return *it;
return PropData();
}
const char *DynamicProperty::getPropertyName(const Property *prop) const
{
auto &index = props.get<1>();
auto it = index.find(const_cast<Property*>(prop));
if(it != index.end())
return it->getName();
return 0;
}