Skip to content

Commit

Permalink
fixup! 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 16, 2024
1 parent 866cc92 commit e46c745
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 41 deletions.
42 changes: 16 additions & 26 deletions src/controllers/legacycontrollersettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ LegacyControllerEnumSetting::LegacyControllerEnumSetting(
value = value.nextSiblingElement("value")) {
QString val = value.text();
QColor color = QColor(value.attribute("color"));
m_options.append(Item{val, value.attribute("label", val), color});
m_options.emplace_back(Item{val, value.attribute("label", val), color});
if (value.hasAttribute("default")) {
m_defaultValue = pos;
}
Expand Down Expand Up @@ -294,21 +294,14 @@ QWidget* LegacyControllerEnumSetting::buildInputWidget(QWidget* pParent) {

LegacyControllerColorSetting::LegacyControllerColorSetting(
const QDomElement& element)
: AbstractLegacyControllerSetting(element) {
m_defaultValue = QColor(element.attribute("default"));
m_savedValue = m_defaultValue;
m_editedValue = m_defaultValue;
: 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;
Expand All @@ -317,8 +310,10 @@ void LegacyControllerColorSetting::parse(const QString& in, bool* ok) {
save();

m_savedValue = QColor(in);
if (ok != nullptr) {
*ok = m_editedValue.isValid();
}
if (!m_editedValue.isValid()) {
*ok = false;
return;
}
m_editedValue = m_savedValue;
Expand Down Expand Up @@ -363,22 +358,15 @@ QWidget* LegacyControllerColorSetting::buildInputWidget(QWidget* pParent) {

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;
: AbstractLegacyControllerSetting(element),
m_fileFilter(element.attribute("pattern")),
m_defaultValue(QFileInfo(element.attribute("default"))),
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;
Expand All @@ -387,8 +375,10 @@ void LegacyControllerFileSetting::parse(const QString& in, bool* ok) {
save();

m_editedValue = QFileInfo(in);
if (ok != nullptr) {
*ok = m_editedValue.exists();
}
if (!m_editedValue.exists()) {
*ok = false;
return;
}
m_savedValue = m_editedValue;
Expand Down
41 changes: 26 additions & 15 deletions src/controllers/legacycontrollersettings.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <gtest/gtest_prod.h>

#include <QColor>
#include <QFileInfo>
#include <QJSValue>
Expand Down Expand Up @@ -437,7 +439,7 @@ class LegacyControllerColorSetting
public:
LegacyControllerColorSetting(const QDomElement& element);

virtual ~LegacyControllerColorSetting() = default;
~LegacyControllerColorSetting() override;

QJSValue value() const override {
return QJSValue(stringify());
Expand Down Expand Up @@ -475,27 +477,32 @@ class LegacyControllerColorSetting
static AbstractLegacyControllerSetting* createFrom(const QDomElement& element) {
return new LegacyControllerColorSetting(element);
}
static inline bool match(const QDomElement& element);
static inline bool match(const QDomElement& element) {
return element.hasAttribute("type") &&
QString::compare(element.attribute("type"),
"color",
Qt::CaseInsensitive) == 0;
}

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

virtual QWidget* buildInputWidget(QWidget* parent) override;

private:
QColor m_savedValue;
QColor m_defaultValue;
QColor m_savedValue;

QColor m_editedValue;

friend class LegacyControllerMappingSettingsTest_enumSettingEditing_Test;
friend class ControllerS4MK3SettingTest_ensureLibrarySettingValueAndEnumEquals;
FRIEND_TEST(ControllerS4MK3SettingTest, ensureLibrarySettingValueAndEnumEquals);
FRIEND_TEST(LegacyControllerMappingSettingsTest, enumSettingEditing);
};

class LegacyControllerFileSetting
Expand Down Expand Up @@ -541,29 +548,33 @@ class LegacyControllerFileSetting
static AbstractLegacyControllerSetting* createFrom(const QDomElement& element) {
return new LegacyControllerFileSetting(element);
}
static inline bool match(const QDomElement& element);
static inline bool match(const QDomElement& element) {
return element.hasAttribute("type") &&
QString::compare(element.attribute("type"),
"file",
Qt::CaseInsensitive) == 0;
}

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

virtual QWidget* buildInputWidget(QWidget* parent) override;

private:
QFileInfo m_savedValue;
QFileInfo m_defaultValue;

QString m_fileFilter;
QFileInfo m_defaultValue;
QFileInfo m_savedValue;

QFileInfo m_editedValue;

friend class LegacyControllerMappingSettingsTest_enumSettingEditing_Test;
friend class ControllerS4MK3SettingTest_ensureLibrarySettingValueAndEnumEquals;
FRIEND_TEST(LegacyControllerMappingSettingsTest, enumSettingEditing);
FRIEND_TEST(ControllerS4MK3SettingTest, ensureLibrarySettingValueAndEnumEquals);
};

template<>
Expand Down

0 comments on commit e46c745

Please sign in to comment.