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:
@@ -30,7 +30,9 @@
|
||||
|
||||
#include "PropertyContainer.h"
|
||||
#include "Property.h"
|
||||
#include "PropertyLinks.h"
|
||||
#include "Application.h"
|
||||
#include "DocumentObject.h"
|
||||
|
||||
#include <boost/iostreams/device/array.hpp>
|
||||
#include <boost/iostreams/stream.hpp>
|
||||
@@ -39,6 +41,8 @@
|
||||
#include "PropertyContainerPy.h"
|
||||
#include "PropertyContainerPy.cpp"
|
||||
|
||||
FC_LOG_LEVEL_INIT("Property", true, 2);
|
||||
|
||||
using namespace App;
|
||||
|
||||
// returns a string which represent the object e.g. when printed in python
|
||||
@@ -62,6 +66,27 @@ PyObject* PropertyContainerPy::getPropertyByName(PyObject *args)
|
||||
}
|
||||
}
|
||||
|
||||
PyObject* PropertyContainerPy::getPropertyTouchList(PyObject *args)
|
||||
{
|
||||
char *pstr;
|
||||
if (!PyArg_ParseTuple(args, "s", &pstr)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(pstr);
|
||||
if (prop && prop->isDerivedFrom(PropertyLists::getClassTypeId())) {
|
||||
const auto &touched = static_cast<PropertyLists*>(prop)->getTouchList();
|
||||
Py::Tuple ret(touched.size());
|
||||
int i=0;
|
||||
for(int idx : touched)
|
||||
ret.setItem(i++,Py::Long(idx));
|
||||
return Py::new_reference_to(ret);
|
||||
}
|
||||
else if(!prop)
|
||||
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", pstr);
|
||||
else
|
||||
PyErr_Format(PyExc_AttributeError, "Property '%s' is not of list type", pstr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject* PropertyContainerPy::getTypeOfProperty(PyObject *args)
|
||||
{
|
||||
Py::List ret;
|
||||
@@ -117,12 +142,10 @@ PyObject* PropertyContainerPy::setEditorMode(PyObject *args)
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned long status = prop->getStatus();
|
||||
prop->setStatus(Property::ReadOnly,(type & 1) > 0);
|
||||
prop->setStatus(Property::Hidden,(type & 2) > 0);
|
||||
|
||||
if (status != prop->getStatus())
|
||||
GetApplication().signalChangePropertyEditor(*prop);
|
||||
std::bitset<32> status(prop->getStatus());
|
||||
status.set(Property::ReadOnly, (type & 1) > 0);
|
||||
status.set(Property::Hidden, (type & 2) > 0);
|
||||
prop->setStatusValue(status.to_ulong());
|
||||
|
||||
Py_Return;
|
||||
}
|
||||
@@ -139,20 +162,17 @@ PyObject* PropertyContainerPy::setEditorMode(PyObject *args)
|
||||
}
|
||||
|
||||
// reset all bits first
|
||||
unsigned long status = prop->getStatus();
|
||||
prop->setStatus(Property::ReadOnly, false);
|
||||
prop->setStatus(Property::Hidden, false);
|
||||
|
||||
std::bitset<32> status(prop->getStatus());
|
||||
status.reset(Property::ReadOnly);
|
||||
status.reset(Property::Hidden);
|
||||
for (Py::Sequence::iterator it = seq.begin();it!=seq.end();++it) {
|
||||
std::string str = (std::string)Py::String(*it);
|
||||
if (str == "ReadOnly")
|
||||
prop->setStatus(Property::ReadOnly, true);
|
||||
status.set(Property::ReadOnly);
|
||||
else if (str == "Hidden")
|
||||
prop->setStatus(Property::Hidden, true);
|
||||
status.set(Property::Hidden);
|
||||
}
|
||||
|
||||
if (status != prop->getStatus())
|
||||
GetApplication().signalChangePropertyEditor(*prop);
|
||||
prop->setStatusValue(status.to_ulong());
|
||||
|
||||
Py_Return;
|
||||
}
|
||||
@@ -162,6 +182,124 @@ PyObject* PropertyContainerPy::setEditorMode(PyObject *args)
|
||||
return 0;
|
||||
}
|
||||
|
||||
static const std::map<std::string, int> &getStatusMap() {
|
||||
static std::map<std::string,int> statusMap;
|
||||
if(statusMap.empty()) {
|
||||
statusMap["Immutable"] = Property::Immutable;
|
||||
statusMap["ReadOnly"] = Property::ReadOnly;
|
||||
statusMap["Hidden"] = Property::Hidden;
|
||||
statusMap["Transient"] = Property::Transient;
|
||||
statusMap["MaterialEdit"] = Property::MaterialEdit;
|
||||
statusMap["NoMaterialListEdit"] = Property::NoMaterialListEdit;
|
||||
statusMap["Output"] = Property::Output;
|
||||
statusMap["LockDynamic"] = Property::LockDynamic;
|
||||
statusMap["NoModify"] = Property::NoModify;
|
||||
statusMap["PartialTrigger"] = Property::PartialTrigger;
|
||||
}
|
||||
return statusMap;
|
||||
}
|
||||
|
||||
PyObject* PropertyContainerPy::setPropertyStatus(PyObject *args)
|
||||
{
|
||||
char* name;
|
||||
PyObject *pyValue;
|
||||
if (!PyArg_ParseTuple(args, "sO", &name, &pyValue))
|
||||
return 0;
|
||||
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name);
|
||||
if (!prop) {
|
||||
PyErr_Format(PyExc_AttributeError, "Property container has no property '%s'", name);
|
||||
return 0;
|
||||
}
|
||||
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
|
||||
|
||||
std::bitset<32> status(prop->getStatus());
|
||||
size_t count = 1;
|
||||
bool isSeq = false;
|
||||
if(PyList_Check(pyValue) || PyTuple_Check(pyValue)) {
|
||||
isSeq = true;
|
||||
count = PySequence_Size(pyValue);
|
||||
}
|
||||
for(size_t i=0;i<count;++i) {
|
||||
Py::Object item;
|
||||
if(isSeq)
|
||||
item = Py::Object(PySequence_GetItem(pyValue,i));
|
||||
else
|
||||
item = Py::Object(pyValue);
|
||||
bool value = true;
|
||||
if(item.isString()) {
|
||||
const auto &statusMap = getStatusMap();
|
||||
auto v = (std::string)Py::String(item);
|
||||
if(v.size()>1 && v[0] == '-') {
|
||||
value = false;
|
||||
v = v.substr(1);
|
||||
}
|
||||
auto it = statusMap.find(v);
|
||||
if(it == statusMap.end()) {
|
||||
if(linkProp && v == "AllowPartial") {
|
||||
linkProp->setAllowPartial(value);
|
||||
continue;
|
||||
}
|
||||
PyErr_Format(PyExc_ValueError, "Unknown property status '%s'", v.c_str());
|
||||
return 0;
|
||||
}
|
||||
status.set(it->second,value);
|
||||
}else if(item.isNumeric()) {
|
||||
int v = Py::Int(item);
|
||||
if(v<0) {
|
||||
value = false;
|
||||
v = -v;
|
||||
}
|
||||
if(v==0 || v>31)
|
||||
PyErr_Format(PyExc_ValueError, "Status value out of range '%d'", v);
|
||||
status.set(v,value);
|
||||
} else {
|
||||
PyErr_SetString(PyExc_TypeError, "Expects status type to be Int or String");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
prop->setStatusValue(status.to_ulong());
|
||||
Py_Return;
|
||||
}
|
||||
|
||||
PyObject* PropertyContainerPy::getPropertyStatus(PyObject *args)
|
||||
{
|
||||
char* name = "";
|
||||
if (!PyArg_ParseTuple(args, "|s", &name)) // convert args: Python->C
|
||||
return NULL; // NULL triggers exception
|
||||
|
||||
Py::List ret;
|
||||
const auto &statusMap = getStatusMap();
|
||||
if(!name[0]) {
|
||||
for(auto &v : statusMap)
|
||||
ret.append(Py::String(v.first.c_str()));
|
||||
}else{
|
||||
App::Property* prop = getPropertyContainerPtr()->getPropertyByName(name);
|
||||
if (prop) {
|
||||
|
||||
auto linkProp = Base::freecad_dynamic_cast<App::PropertyLinkBase>(prop);
|
||||
if(linkProp && linkProp->testFlag(App::PropertyLinkBase::LinkAllowPartial))
|
||||
ret.append(Py::String("AllowPartial"));
|
||||
|
||||
std::bitset<32> bits(prop->getStatus());
|
||||
for(size_t i=1;i<bits.size();++i) {
|
||||
if(!bits[i]) continue;
|
||||
bool found = false;
|
||||
for(auto &v : statusMap) {
|
||||
if(v.second == (int)i) {
|
||||
ret.append(Py::String(v.first.c_str()));
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!found)
|
||||
ret.append(Py::Int((long)i));
|
||||
}
|
||||
}
|
||||
}
|
||||
return Py::new_reference_to(ret);
|
||||
}
|
||||
|
||||
PyObject* PropertyContainerPy::getEditorMode(PyObject *args)
|
||||
{
|
||||
char* name;
|
||||
@@ -336,14 +474,19 @@ PyObject* PropertyContainerPy::restorePropertyContent(PyObject *args)
|
||||
PyObject *PropertyContainerPy::getCustomAttributes(const char* attr) const
|
||||
{
|
||||
// search in PropertyList
|
||||
if(FC_LOG_INSTANCE.level()>FC_LOGLEVEL_TRACE) {
|
||||
FC_TRACE("Get property " << attr);
|
||||
}
|
||||
Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
|
||||
if (prop) {
|
||||
PyObject* pyobj = prop->getPyObject();
|
||||
if (!pyobj && PyErr_Occurred()) {
|
||||
// the Python exception is already set
|
||||
throw Py::Exception();
|
||||
}
|
||||
return pyobj;
|
||||
PY_TRY {
|
||||
PyObject* pyobj = prop->getPyObject();
|
||||
if (!pyobj && PyErr_Occurred()) {
|
||||
// the Python exception is already set
|
||||
throw Py::Exception();
|
||||
}
|
||||
return pyobj;
|
||||
}PY_CATCH
|
||||
}
|
||||
else if (Base::streq(attr, "__dict__")) {
|
||||
// get the properties to the C++ PropertyContainer class
|
||||
@@ -374,14 +517,17 @@ int PropertyContainerPy::setCustomAttributes(const char* attr, PyObject *obj)
|
||||
Property *prop = getPropertyContainerPtr()->getPropertyByName(attr);
|
||||
if (prop) {
|
||||
// Read-only attributes must not be set over its Python interface
|
||||
short Type = getPropertyContainerPtr()->getPropertyType(prop);
|
||||
if (Type & Prop_ReadOnly) {
|
||||
if(prop->testStatus(Property::Immutable)) {
|
||||
std::stringstream s;
|
||||
s << "Object attribute '" << attr << "' is read-only";
|
||||
throw Py::AttributeError(s.str());
|
||||
}
|
||||
|
||||
prop->setPyObject(obj);
|
||||
PY_TRY {
|
||||
FC_TRACE("Set property " << prop->getFullName());
|
||||
prop->setPyObject(obj);
|
||||
}_PY_CATCH(return(-1))
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user