Skip to content

Commit

Permalink
feat: add file and color controller setting types
Browse files Browse the repository at this point in the history
  • Loading branch information
acolombier committed Oct 27, 2024
1 parent bed48f8 commit d3b16c3
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 0 deletions.
112 changes: 112 additions & 0 deletions src/controllers/legacycontrollersettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
#include <util/assert.h>

#include <QCheckBox>
#include <QColorDialog>
#include <QComboBox>
#include <QDoubleSpinBox>
#include <QFileDialog>
#include <QLabel>
#include <QLayout>
#include <QPushButton>
#include <QSpinBox>

#include "moc_legacycontrollersettings.cpp"
Expand Down Expand Up @@ -54,6 +57,8 @@ LegacyControllerSettingBuilder::LegacyControllerSettingBuilder() {
registerType<LegacyControllerIntegerSetting>();
registerType<LegacyControllerRealSetting>();
registerType<LegacyControllerEnumSetting>();
registerType<LegacyControllerColorSetting>();
registerType<LegacyControllerFileSetting>();
}

AbstractLegacyControllerSetting::AbstractLegacyControllerSetting(const QDomElement& element) {
Expand Down Expand Up @@ -274,3 +279,110 @@ QWidget* LegacyControllerEnumSetting::buildInputWidget(QWidget* pParent) {

return pComboBox;
}

LegacyControllerColorSetting::LegacyControllerColorSetting(
const QDomElement& element)
: AbstractLegacyControllerSetting(element) {
m_defaultValue = QColor(element.attribute("default"));
m_savedValue = m_defaultValue;
m_editedValue = m_defaultValue;
reset();
save();
}

bool LegacyControllerColorSetting::match(const QDomElement& element) {
return element.hasAttribute("type") &&
QString::compare(element.attribute("type"),
"color",
Qt::CaseInsensitive) == 0;
}

void LegacyControllerColorSetting::parse(const QString& in, bool* ok) {
if (ok != nullptr) {
*ok = false;
}
reset();
save();

m_savedValue = QColor(in);
if (!m_editedValue.isValid()) {
*ok = false;
return;
}
m_editedValue = m_savedValue;
}

QWidget* LegacyControllerColorSetting::buildInputWidget(QWidget* pParent) {
auto* pPushButton = new QPushButton(tr("Change color"), pParent);

// connect(this, &AbstractLegacyControllerSetting::valueReset, pComboBox, [this, pComboBox]() {
// pComboBox->setCurrentIndex(static_cast<int>(m_editedValue));
// });

connect(pPushButton, &QPushButton::clicked, this, [this, pPushButton](bool) {
auto color = QColorDialog::getColor(m_editedValue, pPushButton, tr("Choose a new color"));
if (color.isValid()) {
m_editedValue = color;
emit changed();
}
});

return pPushButton;
}

LegacyControllerFileSetting::LegacyControllerFileSetting(
const QDomElement& element)
: AbstractLegacyControllerSetting(element) {
m_defaultValue = QFileInfo(element.attribute("default"));
m_fileFilter = element.attribute("pattern");
m_savedValue = m_defaultValue;
m_editedValue = m_defaultValue;
reset();
save();
}

bool LegacyControllerFileSetting::match(const QDomElement& element) {
return element.hasAttribute("type") &&
QString::compare(element.attribute("type"),
"file",
Qt::CaseInsensitive) == 0;
}

void LegacyControllerFileSetting::parse(const QString& in, bool* ok) {
if (ok != nullptr) {
*ok = false;
}
reset();
save();

m_editedValue = QFileInfo(in);
if (!m_editedValue.exists()) {
*ok = false;
return;
}
m_savedValue = m_editedValue;
}

QWidget* LegacyControllerFileSetting::buildInputWidget(QWidget* pParent) {
auto* pPushButton = new QPushButton(tr("Change file"), pParent);

// connect(this, &AbstractLegacyControllerSetting::valueReset, pComboBox, [this, pComboBox]() {
// pComboBox->setCurrentIndex(static_cast<int>(m_editedValue));
// });

connect(pPushButton,
&QPushButton::clicked,
this,
[this, pPushButton](bool) {
auto file = QFileInfo(QFileDialog::getOpenFileName(pPushButton,
tr("Select a file"),
QString(),
m_fileFilter));
if (file.exists()) {
m_editedValue = file;
emit changed();
}
});

return pPushButton;
}
137 changes: 137 additions & 0 deletions src/controllers/legacycontrollersettings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <QColor>
#include <QFileInfo>
#include <QJSValue>

#include "controllers/legacycontrollersettingsfactory.h"
Expand Down Expand Up @@ -439,6 +441,141 @@ class LegacyControllerEnumSetting
friend class ControllerS4MK3SettingTest_ensureLibrarySettingValueAndEnumEquals;
};

class LegacyControllerColorSetting
: public LegacyControllerSettingFactory<LegacyControllerColorSetting>,
public AbstractLegacyControllerSetting {
public:
LegacyControllerColorSetting(const QDomElement& element);

virtual ~LegacyControllerColorSetting() = default;

QJSValue value() const override {
return QJSValue(stringify());
}

QString stringify() const override {
return m_savedValue.name(QColor::HexRgb);
}
void parse(const QString& in, bool* ok) override;
bool isDefault() const override {
return m_savedValue == m_defaultValue;
}
bool isDirty() const override {
return m_savedValue != m_editedValue;
}

virtual void save() override {
m_savedValue = m_editedValue;
}

virtual void reset() override {
m_editedValue = m_defaultValue;
emit valueReset();
}

/// @brief Whether or not this setting definition and its current state are
/// valid. Validity scope includes a known default/current/dirty option.
/// @return true if valid
bool valid() const override {
return AbstractLegacyControllerSetting::valid() &&
m_defaultValue.isValid() &&
m_savedValue.isValid();
}

static AbstractLegacyControllerSetting* createFrom(const QDomElement& element) {
return new LegacyControllerColorSetting(element);
}
static inline bool match(const QDomElement& element);

protected:
LegacyControllerColorSetting(const QDomElement& element,
QColor currentValue,
QColor defaultValue)
: AbstractLegacyControllerSetting(element),
m_savedValue(currentValue),
m_defaultValue(defaultValue) {
}

virtual QWidget* buildInputWidget(QWidget* parent) override;

private:
QColor m_savedValue;
QColor m_defaultValue;

QColor m_editedValue;

friend class LegacyControllerMappingSettingsTest_enumSettingEditing_Test;
friend class ControllerS4MK3SettingTest_ensureLibrarySettingValueAndEnumEquals;
};

class LegacyControllerFileSetting
: public LegacyControllerSettingFactory<LegacyControllerFileSetting>,
public AbstractLegacyControllerSetting {
public:
LegacyControllerFileSetting(const QDomElement& element);

virtual ~LegacyControllerFileSetting() = default;

QJSValue value() const override {
return QJSValue(stringify());
}

QString stringify() const override {
return m_savedValue.absoluteFilePath();
}
void parse(const QString& in, bool* ok) override;
bool isDefault() const override {
return m_savedValue == m_defaultValue;
}
bool isDirty() const override {
return m_savedValue != m_editedValue;
}

virtual void save() override {
m_savedValue = m_editedValue;
}

virtual void reset() override {
m_editedValue = m_defaultValue;
emit valueReset();
}

/// @brief Whether or not this setting definition and its current state are
/// valid. Validity scope includes a known default/current/dirty option.
/// @return true if valid
bool valid() const override {
return AbstractLegacyControllerSetting::valid() &&
(m_defaultValue == m_savedValue || m_savedValue.exists());
}

static AbstractLegacyControllerSetting* createFrom(const QDomElement& element) {
return new LegacyControllerFileSetting(element);
}
static inline bool match(const QDomElement& element);

protected:
LegacyControllerFileSetting(const QDomElement& element,
QFileInfo currentValue,

Check warning on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check warning on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 558 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]
QFileInfo defaultValue)

Check warning on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check warning on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]

Check failure on line 559 in src/controllers/legacycontrollersettings.h

View workflow job for this annotation

GitHub Actions / clazy

Missing reference on non-trivial type (class QFileInfo) [-Wclazy-function-args-by-ref]
: AbstractLegacyControllerSetting(element),
m_savedValue(currentValue),
m_defaultValue(defaultValue) {
}

virtual QWidget* buildInputWidget(QWidget* parent) override;

private:
QFileInfo m_savedValue;
QFileInfo m_defaultValue;

QString m_fileFilter;

QFileInfo m_editedValue;

friend class LegacyControllerMappingSettingsTest_enumSettingEditing_Test;
friend class ControllerS4MK3SettingTest_ensureLibrarySettingValueAndEnumEquals;
};

template<>
inline bool matchSetting<int>(const QDomElement& element) {
return element.hasAttribute("type") &&
Expand Down

0 comments on commit d3b16c3

Please sign in to comment.