From 5430c986f8cc42bb57135a5a848fc469db87af1e Mon Sep 17 00:00:00 2001 From: wmayer Date: Tue, 1 Mar 2022 17:15:41 +0100 Subject: [PATCH] Gui: cleanup of PR #5516 * instead of breaking encapsulation of PropertyItem apply the attorney idiom * refactoring of PropertyItemDelegate::createEditor by moving the code to PropertyItem::createPropertyEditorWidget --- src/Gui/propertyeditor/PropertyItem.cpp | 27 ++++++++++++++- src/Gui/propertyeditor/PropertyItem.h | 13 ++++++-- .../propertyeditor/PropertyItemDelegate.cpp | 33 ++++++++----------- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/Gui/propertyeditor/PropertyItem.cpp b/src/Gui/propertyeditor/PropertyItem.cpp index 3aa6446bf7..d51824f00d 100644 --- a/src/Gui/propertyeditor/PropertyItem.cpp +++ b/src/Gui/propertyeditor/PropertyItem.cpp @@ -91,6 +91,14 @@ PropertyItem* PropertyItemFactory::createPropertyItem (const char* sName) const } // ---------------------------------------------------- + +QVariant PropertyItemAttorney::toString(PropertyItem* item, const QVariant& v) +{ + return item->toString(v); +} + +// ---------------------------------------------------- + Q_DECLARE_METATYPE(Py::Object) PROPERTYITEM_SOURCE(Gui::PropertyEditor::PropertyItem) @@ -476,6 +484,22 @@ QVariant PropertyItem::expressionEditorData(QWidget *editor) const return QVariant(); } +PropertyEditorWidget* PropertyItem::createPropertyEditorWidget(QWidget* parent) const +{ + PropertyEditorWidget* editor = new PropertyEditorWidget(parent); + connect(editor, &PropertyEditorWidget::buttonClick, this, [this]() { + const auto &props = this->getPropertyData(); + if (!props.empty() + && props[0]->getName() + && props[0]->testStatus(App::Property::UserEdit) + && props[0]->getContainer()) + { + props[0]->getContainer()->editProperty(props[0]->getName()); + } + }); + return editor; +} + QString PropertyItem::propertyName() const { if (propName.isEmpty()) @@ -1540,7 +1564,8 @@ void PropertyEditorWidget::setValue(const QVariant& val) // --------------------------------------------------------------- VectorListWidget::VectorListWidget(int decimals, QWidget *parent) - :PropertyEditorWidget(parent), decimals(decimals) + : PropertyEditorWidget(parent) + , decimals(decimals) { connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked())); } diff --git a/src/Gui/propertyeditor/PropertyItem.h b/src/Gui/propertyeditor/PropertyItem.h index 5da33fb7a8..9de08a51d4 100644 --- a/src/Gui/propertyeditor/PropertyItem.h +++ b/src/Gui/propertyeditor/PropertyItem.h @@ -77,6 +77,7 @@ namespace PropertyEditor { class PropertyItem; class PropertyModel; +class PropertyEditorWidget; /** * The PropertyItemFactory provides methods for the dynamic creation of property items. @@ -111,6 +112,11 @@ public: } }; +class PropertyItemAttorney { +public: + static QVariant toString(PropertyItem* item, const QVariant& v); +}; + class GuiExport PropertyItem : public QObject, public ExpressionBinding { Q_OBJECT @@ -139,6 +145,8 @@ public: void setExpressionEditorData(QWidget *editor, const QVariant& data) const; QVariant expressionEditorData(QWidget *editor) const; + PropertyEditorWidget* createPropertyEditorWidget(QWidget* parent) const; + /**override the bind functions to ensure we issue the propertyBound() call, which is then overloaded by childs which like to be informed of a binding*/ virtual void bind(const App::Property& prop); @@ -180,14 +188,13 @@ public: bool hasAnyExpression() const; - virtual QVariant toString(const QVariant&) const; - protected: PropertyItem(); virtual QVariant displayName() const; virtual QVariant decoration(const QVariant&) const; virtual QVariant toolTip(const App::Property*) const; + virtual QVariant toString(const QVariant&) const; virtual QVariant value(const App::Property*) const; virtual void setValue(const QVariant&); virtual void initialize(); @@ -205,6 +212,8 @@ protected: int precision; bool linked; bool expanded; + + friend class PropertyItemAttorney; }; /** diff --git a/src/Gui/propertyeditor/PropertyItemDelegate.cpp b/src/Gui/propertyeditor/PropertyItemDelegate.cpp index 43676168a1..f7fb69bbdd 100644 --- a/src/Gui/propertyeditor/PropertyItemDelegate.cpp +++ b/src/Gui/propertyeditor/PropertyItemDelegate.cpp @@ -182,30 +182,25 @@ QWidget * PropertyItemDelegate::createEditor (QWidget * parent, const QStyleOpti QWidget* editor; expressionEditor = 0; userEditor = nullptr; - if(parentEditor && parentEditor->isBinding()) + if (parentEditor && parentEditor->isBinding()) { expressionEditor = editor = childItem->createExpressionEditor(parent, this, SLOT(valueChanged())); + } else { const auto &props = childItem->getPropertyData(); - if (props.size() && props[0]->testStatus(App::Property::UserEdit)) { - editor = userEditor = new PropertyEditorWidget(parent); - QObject::connect(userEditor, &PropertyEditorWidget::buttonClick, childItem, - [childItem]() { - const auto &props = childItem->getPropertyData(); - if (props.size() - && props[0]->getName() - && props[0]->testStatus(App::Property::UserEdit) - && props[0]->getContainer()) - { - props[0]->getContainer()->editProperty(props[0]->getName()); - } - }); - } else + if (!props.empty() && props[0]->testStatus(App::Property::UserEdit)) { + editor = userEditor = childItem->createPropertyEditorWidget(parent); + } + else { editor = childItem->createEditor(parent, this, SLOT(valueChanged())); + } } - if (editor) // Make sure the editor background is painted so the cell content doesn't show through + if (editor) { + // Make sure the editor background is painted so the cell content doesn't show through editor->setAutoFillBackground(true); - if (editor && childItem->isReadOnly()) + } + if (editor && childItem->isReadOnly()) { editor->setDisabled(true); + } else if (editor /*&& this->pressed*/) { // We changed the way editor is activated in PropertyEditor (in response // of signal activated and clicked), so now we should grab focus @@ -246,10 +241,10 @@ void PropertyItemDelegate::setEditorData(QWidget *editor, const QModelIndex &ind QVariant data = index.data(Qt::EditRole); PropertyItem *childItem = static_cast(index.internalPointer()); editor->blockSignals(true); - if(expressionEditor == editor) + if (expressionEditor == editor) childItem->setExpressionEditorData(editor, data); else if (userEditor == editor) - userEditor->setValue(childItem->toString(data)); + userEditor->setValue(PropertyItemAttorney::toString(childItem, data)); else childItem->setEditorData(editor, data); editor->blockSignals(false);