Skip to content

Commit

Permalink
WIP: Added "delete" button to remove list items
Browse files Browse the repository at this point in the history
Unfortunately, VariantEditorFactory::createEditor is currently unable
to check whether the value is part of a list or not.
  • Loading branch information
bjorn committed Jul 18, 2024
1 parent c957f8e commit 6f0a900
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
26 changes: 24 additions & 2 deletions src/tiled/custompropertieshelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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<QtVariantProperty*>(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
Expand Down
1 change: 1 addition & 0 deletions src/tiled/custompropertieshelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
29 changes: 19 additions & 10 deletions src/tiled/varianteditorfactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ class ResetWidget : public QWidget

signals:
void resetProperty(QtProperty *property);
void removeProperty(QtProperty *property);

private:
void buttonClicked();

QtProperty *mProperty;
};

Expand All @@ -65,20 +64,28 @@ 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) {
layout->addWidget(editor, 1);
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);
});
}


Expand Down Expand Up @@ -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;
}

Expand Down
1 change: 1 addition & 0 deletions src/tiled/varianteditorfactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class VariantEditorFactory : public QtVariantEditorFactory

signals:
void resetProperty(QtProperty *property);
void removeProperty(QtProperty *property);

protected:
void connectPropertyManager(QtVariantPropertyManager *manager) override;
Expand Down

0 comments on commit 6f0a900

Please sign in to comment.