From 74eebc4b9626c99f8f45286c0247b5e16eb19ce6 Mon Sep 17 00:00:00 2001 From: Antoine C Date: Tue, 8 Oct 2024 21:43:25 +0100 Subject: [PATCH] fixup! feat: add file and color controller setting types --- src/controllers/legacycontrollersettings.cpp | 59 ++++++++++++++++---- 1 file changed, 48 insertions(+), 11 deletions(-) diff --git a/src/controllers/legacycontrollersettings.cpp b/src/controllers/legacycontrollersettings.cpp index a135b9b503d..e99d8ce7f9b 100644 --- a/src/controllers/legacycontrollersettings.cpp +++ b/src/controllers/legacycontrollersettings.cpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -9,8 +10,11 @@ #include #include #include +#include +#include #include #include +#include #include "moc_legacycontrollersettings.cpp" @@ -315,18 +319,37 @@ void LegacyControllerColorSetting::parse(const QString& in, bool* ok) { QWidget* LegacyControllerColorSetting::buildInputWidget(QWidget* pParent) { auto* pPushButton = new QPushButton(tr("Change color"), pParent); - // connect(this, &AbstractLegacyControllerSetting::valueReset, pComboBox, [this, pComboBox]() { - // pComboBox->setCurrentIndex(static_cast(m_editedValue)); - // }); + auto setColorIcon = [pPushButton](const QColor& color) { + QPixmap icon(24, 24); + QPainter painter(&icon); + painter.fillRect(0, 0, 24, 24, color); + pPushButton->setIcon(QIcon(icon)); + }; + + connect(this, + &AbstractLegacyControllerSetting::valueReset, + pPushButton, + [this, pPushButton, setColorIcon]() { + if (m_editedValue.isValid()) { + setColorIcon(m_editedValue); + } else { + pPushButton->setIcon(QIcon()); + } + }); - connect(pPushButton, &QPushButton::clicked, this, [this, pPushButton](bool) { + connect(pPushButton, &QPushButton::clicked, this, [this, pPushButton, setColorIcon](bool) { auto color = QColorDialog::getColor(m_editedValue, pPushButton, tr("Choose a new color")); if (color.isValid()) { m_editedValue = color; + setColorIcon(color); emit changed(); } }); + if (m_savedValue.isValid()) { + setColorIcon(m_savedValue); + } + return pPushButton; } @@ -364,25 +387,39 @@ void LegacyControllerFileSetting::parse(const QString& in, bool* ok) { } QWidget* LegacyControllerFileSetting::buildInputWidget(QWidget* pParent) { - auto* pPushButton = new QPushButton(tr("Change file"), pParent); - - // connect(this, &AbstractLegacyControllerSetting::valueReset, pComboBox, [this, pComboBox]() { - // pComboBox->setCurrentIndex(static_cast(m_editedValue)); - // }); + auto* pWidget = new QWidget(pParent); + pWidget->setLayout(new QHBoxLayout); + auto* pPushButton = new QPushButton(tr("Browse..."), pWidget); + auto* pLabel = new QLabel(QStringLiteral("%1").arg(tr("No file selected")), pWidget); + pWidget->layout()->addWidget(pLabel); + pWidget->layout()->addWidget(pPushButton); + + connect(this, &AbstractLegacyControllerSetting::valueReset, pLabel, [this, pLabel]() { + if (m_editedValue.exists()) { + pLabel->setText(QStringLiteral("%1").arg(m_editedValue.absoluteFilePath())); + } else { + pLabel->setText(QStringLiteral("%1").arg(tr("No file selected"))); + } + }); connect(pPushButton, &QPushButton::clicked, this, - [this, pPushButton](bool) { + [this, pLabel, pPushButton](bool) { auto file = QFileInfo(QFileDialog::getOpenFileName(pPushButton, tr("Select a file"), QString(), m_fileFilter)); if (file.exists()) { m_editedValue = file; + pLabel->setText(QStringLiteral("%1").arg(file.absoluteFilePath())); emit changed(); } }); - return pPushButton; + if (m_savedValue.exists()) { + pLabel->setText(QStringLiteral("%1").arg(m_savedValue.absoluteFilePath())); + } + + return pWidget; }