From 6f0a9009039802aae6e760f248910599a1290ea6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 18 Jul 2024 18:01:27 +0200 Subject: [PATCH] WIP: Added "delete" button to remove list items Unfortunately, VariantEditorFactory::createEditor is currently unable to check whether the value is part of a list or not. --- src/tiled/custompropertieshelper.cpp | 26 +++++++++++++++++++++++-- src/tiled/custompropertieshelper.h | 1 + src/tiled/varianteditorfactory.cpp | 29 ++++++++++++++++++---------- src/tiled/varianteditorfactory.h | 1 + 4 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/tiled/custompropertieshelper.cpp b/src/tiled/custompropertieshelper.cpp index 0b0fa5c091..6c96b01ada 100644 --- a/src/tiled/custompropertieshelper.cpp +++ b/src/tiled/custompropertieshelper.cpp @@ -53,6 +53,8 @@ CustomPropertiesHelper::CustomPropertiesHelper(QtAbstractPropertyBrowser *proper connect(variantEditorFactory, &VariantEditorFactory::resetProperty, this, &CustomPropertiesHelper::resetProperty); + connect(variantEditorFactory, &VariantEditorFactory::removeProperty, + this, &CustomPropertiesHelper::removeProperty); connect(Preferences::instance(), &Preferences::propertyTypesChanged, this, &CustomPropertiesHelper::propertyTypesChanged); @@ -403,8 +405,8 @@ void CustomPropertiesHelper::resetProperty(QtProperty *property) auto variantMap = parent->value().toMap(); variantMap.remove(property->propertyName()); - // Not setting mApplyingToParent here since we need this change - // to be applied to the children as well. + // Not setting mNoApplyToChildren here since we need this + // change to be applied to the children as well. parent->setValue(variantMap); } } else { @@ -426,6 +428,26 @@ void CustomPropertiesHelper::resetProperty(QtProperty *property) } } +void CustomPropertiesHelper::removeProperty(QtProperty *property) +{ + // Removing is only supported for list items for now + auto parent = static_cast(mPropertyParents.value(property)); + if (!parent) + return; + if (parent->propertyType() != QMetaType::QVariantList) + return; + + auto variantList = parent->value().toList(); + const int index = parent->subProperties().indexOf(property); + if (index != -1) { + variantList.removeAt(index); + + // Not setting mNoApplyToChildren here since we need this + // change to be applied to the children as well. + parent->setValue(variantList); + } +} + void CustomPropertiesHelper::propertyTypesChanged() { // When this happens in response to emitting propertyValueChanged, it means diff --git a/src/tiled/custompropertieshelper.h b/src/tiled/custompropertieshelper.h index aa3e668964..342c5ba8c3 100644 --- a/src/tiled/custompropertieshelper.h +++ b/src/tiled/custompropertieshelper.h @@ -77,6 +77,7 @@ class CustomPropertiesHelper : public QObject void onValueChanged(QtProperty *property, const QVariant &value); void resetProperty(QtProperty *property); + void removeProperty(QtProperty *property); void propertyTypesChanged(); void setPropertyAttributes(QtVariantProperty *property, diff --git a/src/tiled/varianteditorfactory.cpp b/src/tiled/varianteditorfactory.cpp index 85111b568d..779baeba2f 100644 --- a/src/tiled/varianteditorfactory.cpp +++ b/src/tiled/varianteditorfactory.cpp @@ -45,10 +45,9 @@ class ResetWidget : public QWidget signals: void resetProperty(QtProperty *property); + void removeProperty(QtProperty *property); private: - void buttonClicked(); - QtProperty *mProperty; }; @@ -65,6 +64,13 @@ ResetWidget::ResetWidget(QtProperty *property, QWidget *editor, QWidget *parent) resetButton->setToolTip(tr("Reset")); Utils::setThemeIcon(resetButton, "edit-clear"); + auto removeButton = new QToolButton(this); + removeButton->setIcon(QIcon(QLatin1String(":/images/16/edit-delete.png"))); + removeButton->setIconSize(Utils::smallIconSize()); + removeButton->setAutoRaise(true); + removeButton->setToolTip(tr("Remove")); + Utils::setThemeIcon(removeButton, "edit-delete"); + layout->setContentsMargins(0, 0, 0, 0); layout->setSpacing(0); if (editor) { @@ -72,13 +78,14 @@ ResetWidget::ResetWidget(QtProperty *property, QWidget *editor, QWidget *parent) setFocusProxy(editor); } layout->addWidget(resetButton, 0, Qt::AlignRight); - - connect(resetButton, &QToolButton::clicked, this, &ResetWidget::buttonClicked); -} - -void ResetWidget::buttonClicked() -{ - emit resetProperty(mProperty); + layout->addWidget(removeButton, 0, Qt::AlignRight); + + connect(resetButton, &QToolButton::clicked, this, [this] { + emit resetProperty(mProperty); + }); + connect(removeButton, &QToolButton::clicked, this, [this] { + emit removeProperty(mProperty); + }); } @@ -196,12 +203,14 @@ QWidget *VariantEditorFactory::createEditor(QtVariantPropertyManager *manager, if (!editor) editor = QtVariantEditorFactory::createEditor(manager, property, parent); - if (type == QMetaType::QColor || type == VariantPropertyManager::displayObjectRefTypeId() || property->isModified()) { + if (true || type == QMetaType::QColor || type == VariantPropertyManager::displayObjectRefTypeId() || property->isModified()) { // Allow resetting color and object reference properties, or allow // unsetting a class member (todo: resolve conflict...). auto resetWidget = new ResetWidget(property, editor, parent); connect(resetWidget, &ResetWidget::resetProperty, this, &VariantEditorFactory::resetProperty); + connect(resetWidget, &ResetWidget::removeProperty, + this, &VariantEditorFactory::removeProperty); editor = resetWidget; } diff --git a/src/tiled/varianteditorfactory.h b/src/tiled/varianteditorfactory.h index 9c4b486f1d..f05450e8ac 100644 --- a/src/tiled/varianteditorfactory.h +++ b/src/tiled/varianteditorfactory.h @@ -55,6 +55,7 @@ class VariantEditorFactory : public QtVariantEditorFactory signals: void resetProperty(QtProperty *property); + void removeProperty(QtProperty *property); protected: void connectPropertyManager(QtVariantPropertyManager *manager) override;