Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for list property types #4002

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem here is the if (true of course. It should be replaced with a check on whether the property is a value in a list (so whether its parent is a list property). Unfortunately, we don't have access to the parent property (they're private, and in theory there could even be multiple in this properties framework).

If we find a way to make this work, it should of course also only determine the visibility of the "Delete" button, and not affect the visibility of the "Reset" button.

// 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