From bcc6d5d53dd06d530f6e2fde5385b5ebf127f1e4 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 1 Jul 2020 00:34:00 +1000 Subject: [PATCH] Qt: Support float settings in setting widget binder --- src/duckstation-qt/settingwidgetbinder.h | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/src/duckstation-qt/settingwidgetbinder.h b/src/duckstation-qt/settingwidgetbinder.h index 2495402fc..44cbdbf5d 100644 --- a/src/duckstation-qt/settingwidgetbinder.h +++ b/src/duckstation-qt/settingwidgetbinder.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,9 @@ struct SettingAccessor static int getIntValue(const T* widget); static void setIntValue(T* widget, int value); + static int getFloatValue(const T* widget); + static void setFloatValue(T* widget, int value); + static QString getStringValue(const T* widget); static void setStringValue(T* widget, const QString& value); @@ -42,6 +46,9 @@ struct SettingAccessor static int getIntValue(const QLineEdit* widget) { return widget->text().toInt(); } static void setIntValue(QLineEdit* widget, int value) { widget->setText(QStringLiteral("%1").arg(value)); } + static float getFloatValue(const QLineEdit* widget) { return widget->text().toFloat(); } + static void setFloatValue(QLineEdit* widget, float value) { widget->setText(QStringLiteral("%1").arg(value)); } + static QString getStringValue(const QLineEdit* widget) { return widget->text(); } static void setStringValue(QLineEdit* widget, const QString& value) { widget->setText(value); } @@ -61,6 +68,9 @@ struct SettingAccessor static int getIntValue(const QComboBox* widget) { return widget->currentIndex(); } static void setIntValue(QComboBox* widget, int value) { widget->setCurrentIndex(value); } + static float getFloatValue(const QComboBox* widget) { return static_cast(widget->currentIndex()); } + static void setFloatValue(QComboBox* widget, float value) { widget->setCurrentIndex(static_cast(value)); } + static QString getStringValue(const QComboBox* widget) { return widget->currentText(); } static void setStringValue(QComboBox* widget, const QString& value) { widget->setCurrentText(value); } @@ -80,6 +90,9 @@ struct SettingAccessor static int getIntValue(const QCheckBox* widget) { return widget->isChecked() ? 1 : 0; } static void setIntValue(QCheckBox* widget, int value) { widget->setChecked(value != 0); } + static float getFloatValue(const QCheckBox* widget) { return widget->isChecked() ? 1.0f : 0.0f; } + static void setFloatValue(QCheckBox* widget, float value) { widget->setChecked(value != 0.0f); } + static QString getStringValue(const QCheckBox* widget) { return widget->isChecked() ? QStringLiteral("1") : QStringLiteral("0"); @@ -102,6 +115,9 @@ struct SettingAccessor static int getIntValue(const QSlider* widget) { return widget->value(); } static void setIntValue(QSlider* widget, int value) { widget->setValue(value); } + static float getFloatValue(const QSlider* widget) { return static_cast(widget->value()); } + static void setFloatValue(QSlider* widget, float value) { widget->setValue(static_cast(value)); } + static QString getStringValue(const QSlider* widget) { return QStringLiteral("%1").arg(widget->value()); } static void setStringValue(QSlider* widget, const QString& value) { widget->setValue(value.toInt()); } @@ -121,6 +137,9 @@ struct SettingAccessor static int getIntValue(const QSpinBox* widget) { return widget->value(); } static void setIntValue(QSpinBox* widget, int value) { widget->setValue(value); } + static float getFloatValue(const QSpinBox* widget) { return static_cast(widget->value()); } + static void setFloatValue(QSpinBox* widget, float value) { widget->setValue(static_cast(value)); } + static QString getStringValue(const QSpinBox* widget) { return QStringLiteral("%1").arg(widget->value()); } static void setStringValue(QSpinBox* widget, const QString& value) { widget->setValue(value.toInt()); } @@ -131,6 +150,28 @@ struct SettingAccessor } }; +template<> +struct SettingAccessor +{ + static bool getBoolValue(const QDoubleSpinBox* widget) { return widget->value() > 0.0; } + static void setBoolValue(QDoubleSpinBox* widget, bool value) { widget->setValue(value ? 1.0 : 0.0); } + + static int getIntValue(const QDoubleSpinBox* widget) { return static_cast(widget->value()); } + static void setIntValue(QDoubleSpinBox* widget, int value) { widget->setValue(static_cast(value)); } + + static float getFloatValue(const QDoubleSpinBox* widget) { return static_cast(widget->value()); } + static void setFloatValue(QDoubleSpinBox* widget, float value) { widget->setValue(static_cast(value)); } + + static QString getStringValue(const QDoubleSpinBox* widget) { return QStringLiteral("%1").arg(widget->value()); } + static void setStringValue(QDoubleSpinBox* widget, const QString& value) { widget->setValue(value.toDouble()); } + + template + static void connectValueChanged(QDoubleSpinBox* widget, F func) + { + widget->connect(widget, QOverload::of(&QDoubleSpinBox::valueChanged), func); + } +}; + template<> struct SettingAccessor { @@ -140,6 +181,9 @@ struct SettingAccessor static int getIntValue(const QAction* widget) { return widget->isChecked() ? 1 : 0; } static void setIntValue(QAction* widget, int value) { widget->setChecked(value != 0); } + static float getFloatValue(const QAction* widget) { return widget->isChecked() ? 1.0f : 0.0f; } + static void setFloatValue(QAction* widget, float value) { widget->setChecked(value != 0.0f); } + static QString getStringValue(const QAction* widget) { return widget->isChecked() ? QStringLiteral("1") : QStringLiteral("0"); @@ -192,6 +236,25 @@ void BindWidgetToIntSetting(QtHostInterface* hi, WidgetType* widget, const QStri }); } +template +void BindWidgetToFloatSetting(QtHostInterface* hi, WidgetType* widget, const QString& setting_name, + float default_value = 0.0f) +{ + using Accessor = SettingAccessor; + + QVariant value = hi->getSettingValue(setting_name); + if (value.isValid()) + Accessor::setFloatValue(widget, value.toFloat()); + else + Accessor::setFloatValue(widget, default_value); + + Accessor::connectValueChanged(widget, [hi, widget, setting_name]() { + const float new_value = Accessor::getFloatValue(widget); + hi->putSettingValue(setting_name, new_value); + hi->applySettings(); + }); +} + template void BindWidgetToNormalizedSetting(QtHostInterface* hi, WidgetType* widget, const QString& setting_name, float range, float default_value = 0.0f)