From 2568cb4ccd7d2cce8ef09e66de298fc01f324bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Fri, 15 Nov 2024 16:42:33 +0100 Subject: [PATCH] Added clear button to the editor for color properties --- src/tiled/varianteditor.cpp | 40 +++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/src/tiled/varianteditor.cpp b/src/tiled/varianteditor.cpp index 962c155ba9..4c9a83db3a 100644 --- a/src/tiled/varianteditor.cpp +++ b/src/tiled/varianteditor.cpp @@ -430,22 +430,46 @@ QWidget *RectFProperty::createEditor(QWidget *parent) return editor; } -// todo: needs to allow setting color back to invalid (unset) QWidget *ColorProperty::createEditor(QWidget *parent) { - auto editor = new ColorButton(parent); - editor->setShowAlphaChannel(m_alpha); + auto editor = new QWidget(parent); + auto layout = new QHBoxLayout(editor); + layout->setContentsMargins(QMargins()); + layout->setSpacing(0); + + auto colorButton = new ColorButton(editor); + colorButton->setShowAlphaChannel(m_alpha); + colorButton->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed); + + QIcon resetIcon(QStringLiteral(":/images/16/edit-clear.png")); + resetIcon.addFile(QStringLiteral(":/images/24/edit-clear.png")); + + auto resetButton = new QToolButton(editor); + resetButton->setIcon(resetIcon); + resetButton->setToolTip(tr("Clear Color")); + Utils::setThemeIcon(resetButton, "edit-clear"); + + layout->addWidget(colorButton); + layout->addWidget(resetButton); auto syncEditor = [=] { - const QSignalBlocker blocker(editor); - editor->setColor(value()); + const QSignalBlocker blocker(colorButton); + auto v = value(); + colorButton->setColor(v); + resetButton->setEnabled(v.isValid()); }; syncEditor(); - connect(this, &Property::valueChanged, editor, syncEditor); - connect(editor, &ColorButton::colorChanged, this, [this, editor] { - setValue(editor->color()); + connect(resetButton, &QToolButton::clicked, colorButton, [colorButton] { + colorButton->setColor(QColor()); }); + connect(this, &Property::valueChanged, colorButton, syncEditor); + connect(colorButton, &ColorButton::colorChanged, this, [=] { + resetButton->setEnabled(colorButton->color().isValid()); + setValue(colorButton->color()); + }); + + editor->setFocusProxy(colorButton); return editor; }