From 4318375a51a66de8ef0cc168150ea65335f34dcb Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 28 Dec 2019 17:36:41 +0100 Subject: [PATCH] Gui: allow user to set an expression for property editor of vectors --- src/Gui/propertyeditor/PropertyItem.cpp | 56 ++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 2 deletions(-) diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index b9ca3bfdb3..0af57b952d 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -1266,6 +1266,40 @@ QVariant PropertyBoolItem::editorData(QWidget *editor) const // --------------------------------------------------------------- +namespace Gui { namespace PropertyEditor { +class VectorLineEdit : public Gui::ExpLineEdit +{ + int decimals; +public: + VectorLineEdit (int decimals, QWidget * parent=0, bool expressionOnly=false) + : Gui::ExpLineEdit(parent, expressionOnly) + , decimals(decimals) + { + } + + bool apply(const std::string &propName) { + if (!ExpressionBinding::apply(propName)) { + QVariant data = property("coords"); + if (data.canConvert()) { + const Base::Vector3d& value = data.value(); + + QString str = QString::fromLatin1("(%1, %2, %3)") + .arg(value.x,0,'f',decimals) + .arg(value.y,0,'f',decimals) + .arg(value.z,0,'f',decimals); + + Gui::Command::doCommand(Gui::Command::Doc, "%s = %s", propName.c_str(), str.toLatin1().constData()); + return true; + } + } + + return false; + } +}; +}} + +// --------------------------------------------------------------- + PROPERTYITEM_SOURCE(Gui::PropertyEditor::PropertyVectorItem) PropertyVectorItem::PropertyVectorItem() @@ -1291,6 +1325,8 @@ QVariant PropertyVectorItem::toString(const QVariant& prop) const .arg(QLocale::system().toString(value.x, 'f', 2), QLocale::system().toString(value.y, 'f', 2), QLocale::system().toString(value.z, 'f', 2)); + if (hasExpression()) + data += QString::fromLatin1(" ( %1 )").arg(QString::fromStdString(getExpressionString())); return QVariant(data); } @@ -1316,9 +1352,15 @@ void PropertyVectorItem::setValue(const QVariant& value) QWidget* PropertyVectorItem::createEditor(QWidget* parent, const QObject* /*receiver*/, const char* /*method*/) const { - QLineEdit *le = new QLineEdit(parent); + VectorLineEdit *le = new VectorLineEdit(decimals(), parent); le->setFrame(false); le->setReadOnly(true); + + if (isBound()) { + le->bind(getPath()); + le->setAutoApply(autoApply()); + } + return le; } @@ -1330,6 +1372,7 @@ void PropertyVectorItem::setEditorData(QWidget *editor, const QVariant& data) co .arg(QLocale::system().toString(value.x, 'f', 2), QLocale::system().toString(value.y, 'f', 2), QLocale::system().toString(value.z, 'f', 2)); + le->setProperty("coords", data); le->setText(text); } @@ -1403,6 +1446,8 @@ QVariant PropertyVectorDistanceItem::toString(const QVariant& prop) const Base::Quantity(value.x, Base::Unit::Length).getUserString() + QString::fromLatin1(" ") + Base::Quantity(value.y, Base::Unit::Length).getUserString() + QString::fromLatin1(" ") + Base::Quantity(value.z, Base::Unit::Length).getUserString() + QString::fromLatin1("]"); + if (hasExpression()) + data += QString::fromLatin1(" ( %1 )").arg(QString::fromStdString(getExpressionString())); return QVariant(data); } @@ -1434,14 +1479,21 @@ void PropertyVectorDistanceItem::setValue(const QVariant& variant) void PropertyVectorDistanceItem::setEditorData(QWidget *editor, const QVariant& data) const { QLineEdit* le = qobject_cast(editor); + le->setProperty("coords", data); le->setText(toString(data).toString()); } QWidget* PropertyVectorDistanceItem::createEditor(QWidget* parent, const QObject* /*receiver*/, const char* /*method*/) const { - QLineEdit *le = new QLineEdit(parent); + VectorLineEdit *le = new VectorLineEdit(decimals(), parent); le->setFrame(false); le->setReadOnly(true); + + if (isBound()) { + le->bind(getPath()); + le->setAutoApply(autoApply()); + } + return le; }