Expressions: Fix property editor behavior

- change responsibility of python code emition
- Correct python code handling for expressions
- handle constraints expressions handling
This commit is contained in:
Stefan Tröger
2015-10-13 08:32:23 +02:00
committed by wmayer
parent 08b1c4619e
commit 2c249e69f2
6 changed files with 77 additions and 63 deletions

View File

@@ -317,7 +317,8 @@ void PropertyExpressionEngine::setValue(const ObjectIdentifier & path, boost::sh
prop->getPathValue(usePath);
// Check if the current expression equals the new one and do nothing if so to reduce unneeded computations
if(expressions.find(usePath) != expressions.end() && expr == expressions[usePath].expression)
ExpressionMap::iterator it = expressions.find(usePath);
if(it != expressions.end() && expr == it->second.expression)
return;
if (expr) {

View File

@@ -180,7 +180,7 @@ bool ExpressionBinding::apply()
std::string name = docObj->getNameInDocument();
return apply("App.ActiveDocument." + name + "." + std::string(prop->getName()));
return apply("App.ActiveDocument." + name + "." + getPath().toEscapedString());
}
void ExpressionBinding::expressionChange(const ObjectIdentifier& id) {

View File

@@ -52,7 +52,7 @@ public:
//auto apply means that the python code is issues not only on aplly() but
//also on setExpression
bool autoApply() {return m_autoApply;};
bool autoApply() const {return m_autoApply;};
void setAutoApply(bool value) {m_autoApply = value;};
protected:

View File

@@ -317,10 +317,7 @@ void Gui::QuantitySpinBox::onChange() {
QPalette p(lineEdit()->palette());
p.setColor(QPalette::Active, QPalette::Text, defaultPalette.color(QPalette::Text));
lineEdit()->setPalette(p);
<<<<<<< 175351b02ea3a586e1dbe0dc5e993966714ea236
=======
iconLabel->setToolTip(QString());
>>>>>>> further expression integration for property editor
}
iconLabel->setToolTip(QString());
}

View File

@@ -62,6 +62,7 @@ TYPESYSTEM_SOURCE(Gui::PropertyEditor::PropertyItem, Base::BaseClass);
PropertyItem::PropertyItem() : parentItem(0), readonly(false), cleared(false)
{
precision = Base::UnitsApi::getDecimals();
setAutoApply(true);
}
PropertyItem::~PropertyItem()
@@ -86,19 +87,21 @@ void PropertyItem::setPropertyData(const std::vector<App::Property*>& items)
const App::Property& p = *items.front();
if(!(p.getContainer()->getPropertyType(&p) & App::Prop_ReadOnly)) {
App::ObjectIdentifier id(p);
std::vector<App::ObjectIdentifier> paths;
p.getPaths(paths);
//there may be no paths available in this property (for example an empty constraint list)
if(id.getProperty() && !paths.empty())
bind(id);
try {
if(!(p.getContainer()->isReadOnly(&p))) {
App::ObjectIdentifier id(p);
std::vector<App::ObjectIdentifier> paths;
p.getPaths(paths);
//there may be no paths available in this property (for example an empty constraint list)
if(id.getProperty() && !paths.empty())
bind(id);
}
}
else
setReadOnly(true);
//it may happen that setting properties is not possible
catch(...) {};
}
propertyItems = items;
@@ -247,26 +250,26 @@ QString PropertyItem::pythonIdentifier(const App::Property* prop) const
if (parent->getTypeId() == App::Document::getClassTypeId()) {
App::Document* doc = static_cast<App::Document*>(parent);
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCAD.getDocument(\"%1\").%2").arg(docName).arg(propName);
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCAD.getDocument(\"%1\").%2").arg(docName).arg(propName);
}
if (parent->getTypeId().isDerivedFrom(App::DocumentObject::getClassTypeId())) {
App::DocumentObject* obj = static_cast<App::DocumentObject*>(parent);
App::Document* doc = obj->getDocument();
QString propName = QString::fromAscii(parent->getPropertyName(prop));
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString objName = QString::fromAscii(obj->getNameInDocument());
.arg(docName).arg(objName).arg(propName);
QString propName = QString::fromAscii(parent->getPropertyName(prop));
return QString::fromAscii("FreeCAD.getDocument(\"%1\").getObject(\"%2\").%3")
.arg(docName).arg(objName).arg(propName);
}
if (parent->getTypeId().isDerivedFrom(Gui::ViewProviderDocumentObject::getClassTypeId())) {
QString propName = QString::fromAscii(parent->getPropertyName(prop));
App::DocumentObject* obj = static_cast<Gui::ViewProviderDocumentObject*>(parent)->getObject();
App::Document* doc = obj->getDocument();
.arg(docName).arg(objName).arg(propName);
QString docName = QString::fromAscii(App::GetApplication().getDocumentName(doc));
QString objName = QString::fromAscii(obj->getNameInDocument());
QString propName = QString::fromAscii(parent->getPropertyName(prop));
@@ -366,12 +369,7 @@ QVariant PropertyItem::data(int column, int role) const
return toString(value(propertyItems[0]));
else if (role == Qt::ToolTipRole)
return toolTip(propertyItems[0]);
{
//check if we have an expression set. If so we do nothing, as than the editor is responsible
//for issuing the relevant python code
if(hasExpression())
return true;
else
return QVariant();
}
}
@@ -545,11 +543,14 @@ QVariant PropertyIntegerItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyInteger::getClassTypeId()));
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
QString data = QString::fromAscii("%1").arg(val);
setPropertyValue(data);
int value = (int)static_cast<const App::PropertyInteger*>(prop)->getValue();
return QVariant(value);
}
void PropertyIntegerItem::setValue(const QVariant& value)
{
//if the item has an expression it issues the python code
if(!hasExpression()) {
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
@@ -561,8 +562,9 @@ QWidget* PropertyIntegerItem::createEditor(QWidget* parent, const QObject* recei
QWidget* PropertyIntegerItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::IntSpinBox *sb = new Gui::IntSpinBox(parent);
sb->setAutoApply(true);
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(int)), receiver, method);
if(isBound()) {
sb->bind(getPath());
@@ -608,11 +610,14 @@ QVariant PropertyIntegerConstraintItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyIntegerConstraint::getClassTypeId()));
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
QString data = QString::fromAscii("%1").arg(val);
setPropertyValue(data);
int value = (int)static_cast<const App::PropertyIntegerConstraint*>(prop)->getValue();
return QVariant(value);
}
void PropertyIntegerConstraintItem::setValue(const QVariant& value)
{
//if the item has an expression it issues the python code
if(!hasExpression()) {
if (!value.canConvert(QVariant::Int))
return;
int val = value.toInt();
@@ -624,8 +629,8 @@ QWidget* PropertyIntegerConstraintItem::createEditor(QWidget* parent, const QObj
QWidget* PropertyIntegerConstraintItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const
{
Gui::IntSpinBox *sb = new Gui::IntSpinBox(parent);
sb->setAutoApply(true);
}
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(int)), receiver, method);
if(isBound()) {
@@ -695,11 +700,14 @@ QVariant PropertyFloatItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyFloat::getClassTypeId()));
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
QString data = QString::fromAscii("%1").arg(val,0,'f',decimals());
setPropertyValue(data);
double value = static_cast<const App::PropertyFloat*>(prop)->getValue();
return QVariant(value);
}
void PropertyFloatItem::setValue(const QVariant& value)
{
//if the item has an expression it issues the python code
if(!hasExpression()) {
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
@@ -712,7 +720,7 @@ QWidget* PropertyFloatItem::createEditor(QWidget* parent, const QObject* receive
{
Gui::DoubleSpinBox *sb = new Gui::DoubleSpinBox(parent);
sb->setFrame(false);
sb->setAutoApply(true);
sb->setDecimals(decimals());
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method);
@@ -760,12 +768,15 @@ QVariant PropertyUnitItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyQuantity::getClassTypeId()));
if (!value.canConvert<Base::Quantity>())
return;
const Base::Quantity& val = value.value<Base::Quantity>();
Base::Quantity value = static_cast<const App::PropertyQuantity*>(prop)->getQuantityValue();
return QVariant::fromValue<Base::Quantity>(value);
}
void PropertyUnitItem::setValue(const QVariant& value)
{
QString unit = QString::fromLatin1("'%1 %2'").arg(val.getValue()).arg(val.getUnit().getString());
setPropertyValue(unit);
//if the item has an expression it handles the python code
if(!hasExpression()) {
if (!value.canConvert<Base::Quantity>())
return;
const Base::Quantity& val = value.value<Base::Quantity>();
@@ -778,8 +789,9 @@ QWidget* PropertyUnitItem::createEditor(QWidget* parent, const QObject* receiver
{
Gui::QuantitySpinBox *infield = new Gui::QuantitySpinBox(parent);
infield->setFrame(false);
infield->setAutoApply(true);
infield->setMinimumHeight(0);
infield->setReadOnly(isReadOnly());
//if we are bound to an expression we need to bind it to the input field
if(isBound()) {
infield->bind(getPath());
@@ -860,11 +872,14 @@ QVariant PropertyFloatConstraintItem::value(const App::Property* prop) const
{
assert(prop && prop->getTypeId().isDerivedFrom(App::PropertyFloatConstraint::getClassTypeId()));
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
QString data = QString::fromAscii("%1").arg(val,0,'f',decimals());
setPropertyValue(data);
double value = static_cast<const App::PropertyFloatConstraint*>(prop)->getValue();
return QVariant(value);
}
void PropertyFloatConstraintItem::setValue(const QVariant& value)
{
//if the item has an expression it issues the python code
if(!hasExpression()) {
if (!value.canConvert(QVariant::Double))
return;
double val = value.toDouble();
@@ -877,8 +892,9 @@ QWidget* PropertyFloatConstraintItem::createEditor(QWidget* parent, const QObjec
{
Gui::DoubleSpinBox *sb = new Gui::DoubleSpinBox(parent);
sb->setDecimals(decimals());
sb->setAutoApply(true);
sb->setFrame(false);
sb->setReadOnly(isReadOnly());
QObject::connect(sb, SIGNAL(valueChanged(double)), receiver, method);
if(isBound()) {
sb->bind(getPath());
@@ -2317,7 +2333,7 @@ QVariant PropertyPathItem::toolTip(const App::Property* prop) const
QVariant PropertyPathItem::toolTip(const App::Property* prop) const
{
fc->setMode(FileChooser::Directory);
return value(prop);
}
QWidget* PropertyPathItem::createEditor(QWidget* parent, const QObject* receiver, const char* method) const

View File

@@ -103,7 +103,7 @@ void PropertyConstraintListItem::initialize()
}
item->bind(list->createPath(id-1));
item->setAutoApply(true);
item->setAutoApply(false);
}
}