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 6731e83bf5
commit be6ec3fdfc
25 changed files with 1511 additions and 1579 deletions

View File

@@ -33,6 +33,7 @@
#include "PropertyContainer.h"
#include <Base/Exception.h>
#include "Application.h"
#include "DocumentObject.h"
using namespace App;
@@ -49,7 +50,7 @@ TYPESYSTEM_SOURCE_ABSTRACT(App::Property , Base::Persistence);
// Here is the implementation! Description should take place in the header file!
Property::Property()
:father(0)
:father(0), myName(0)
{
}
@@ -61,12 +62,45 @@ Property::~Property()
const char* Property::getName(void) const
{
return father->getPropertyName(this);
return myName;
}
std::string Property::getFullName() const {
std::string name;
if(myName) {
if(father)
name = father->getFullName() + ".";
name += myName;
}else
return "?";
return name;
}
short Property::getType(void) const
{
return father->getPropertyType(this);
short type = 0;
#define GET_PTYPE(_name) do {\
if(testStatus(App::Property::Prop##_name)) type|=Prop_##_name;\
}while(0)
GET_PTYPE(ReadOnly);
GET_PTYPE(Hidden);
GET_PTYPE(Output);
GET_PTYPE(Transient);
GET_PTYPE(NoRecompute);
GET_PTYPE(NoPersist);
return type;
}
void Property::syncType(unsigned type) {
#define SYNC_PTYPE(_name) do{\
if(type & Prop_##_name) StatusBits.set((size_t)Prop##_name);\
}while(0)
SYNC_PTYPE(ReadOnly);
SYNC_PTYPE(Transient);
SYNC_PTYPE(Hidden);
SYNC_PTYPE(Output);
SYNC_PTYPE(NoRecompute);
SYNC_PTYPE(NoPersist);
}
const char* Property::getGroup(void) const
@@ -99,7 +133,7 @@ void Property::getPaths(std::vector<ObjectIdentifier> &paths) const
paths.push_back(App::ObjectIdentifier(getContainer(), getName()));
}
const ObjectIdentifier Property::canonicalPath(const ObjectIdentifier &p) const
ObjectIdentifier Property::canonicalPath(const ObjectIdentifier &p) const
{
return p;
}
@@ -113,10 +147,7 @@ void Property::touch()
void Property::setReadOnly(bool readOnly)
{
unsigned long status = this->getStatus();
this->setStatus(App::Property::ReadOnly, readOnly);
if (status != this->getStatus())
App::GetApplication().signalChangePropertyEditor(*this);
}
void Property::hasSetValue(void)
@@ -134,12 +165,7 @@ void Property::aboutToSetValue(void)
void Property::verifyPath(const ObjectIdentifier &p) const
{
if (p.numSubComponents() != 1)
throw Base::ValueError("Invalid property path: single component expected");
if (!p.getPropertyComponent(0).isSimple())
throw Base::ValueError("Invalid property path: simple component expected");
if (p.getPropertyComponent(0).getName() != getName())
throw Base::ValueError("Invalid property path: name mismatch");
p.verify(*this);
}
Property *Property::Copy(void) const
@@ -155,6 +181,76 @@ void Property::Paste(const Property& /*from*/)
assert(0);
}
void Property::setStatusValue(unsigned long status) {
static const unsigned long mask =
(1<<PropDynamic)
|(1<<PropNoRecompute)
|(1<<PropReadOnly)
|(1<<PropTransient)
|(1<<PropOutput)
|(1<<PropHidden);
status &= ~mask;
status |= StatusBits.to_ulong() & mask;
unsigned long oldStatus = StatusBits.to_ulong();
StatusBits = decltype(StatusBits)(status);
if(father) {
static unsigned long _signalMask = (1<<ReadOnly) | (1<<Hidden);
if((status & _signalMask) != (oldStatus & _signalMask))
father->onPropertyStatusChanged(*this,oldStatus);
}
}
void Property::setStatus(Status pos, bool on) {
auto bits = StatusBits;
bits.set(pos,on);
setStatusValue(bits.to_ulong());
}
//**************************************************************************
//**************************************************************************
// PropertyListsBase
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void PropertyListsBase::_setPyObject(PyObject *value) {
std::vector<PyObject *> vals;
std::vector<int> indices;
if (PyDict_Check(value)) {
PyObject* keyList = PyDict_Keys(value);
PyObject* itemList = PyDict_Values(value);
Py_ssize_t nSize = PyList_Size(keyList);
vals.reserve(nSize);
indices.reserve(nSize);
int listSize = getSize();
for (Py_ssize_t i=0; i<nSize;++i) {
std::string keyStr;
PyObject* key = PyList_GetItem(keyList, i);
#if PY_MAJOR_VERSION < 3
if(!PyInt_Check(key))
#else
if(!PyLong_Check(key))
#endif
throw Base::TypeError("expect key type to be interger");
auto idx = PyLong_AsLong(key);
if(idx<-1 || idx>listSize)
throw Base::RuntimeError("index out of bound");
if(idx==-1 || idx==listSize) {
idx = listSize;
++listSize;
}
indices.push_back(idx);
vals.push_back(PyList_GetItem(itemList,i));
}
}else if (PySequence_Check(value)) {
Py_ssize_t nSize = PySequence_Size(value);
vals.reserve(nSize);
for (Py_ssize_t i=0; i<nSize;++i)
vals.push_back(PySequence_GetItem(value, i));
}else
vals.push_back(value);
setPyValues(vals,indices);
}
//**************************************************************************
//**************************************************************************
// PropertyLists