2020-01-01 04:01:58 +00:00
|
|
|
#pragma once
|
2020-01-24 04:49:49 +00:00
|
|
|
#include <optional>
|
2020-01-01 04:01:58 +00:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include "core/settings.h"
|
|
|
|
#include "qthostinterface.h"
|
|
|
|
|
2020-02-04 06:22:39 +00:00
|
|
|
#include <QtWidgets/QAction>
|
2020-01-01 04:01:58 +00:00
|
|
|
#include <QtWidgets/QCheckBox>
|
|
|
|
#include <QtWidgets/QComboBox>
|
2020-06-30 14:34:00 +00:00
|
|
|
#include <QtWidgets/QDoubleSpinBox>
|
2020-01-01 04:01:58 +00:00
|
|
|
#include <QtWidgets/QLineEdit>
|
2020-01-28 14:01:35 +00:00
|
|
|
#include <QtWidgets/QSlider>
|
2020-06-14 21:43:16 +00:00
|
|
|
#include <QtWidgets/QSpinBox>
|
2020-01-01 04:01:58 +00:00
|
|
|
|
|
|
|
namespace SettingWidgetBinder {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct SettingAccessor
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const T* widget);
|
|
|
|
static void setBoolValue(T* widget, bool value);
|
|
|
|
|
|
|
|
static int getIntValue(const T* widget);
|
|
|
|
static void setIntValue(T* widget, int value);
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
static int getFloatValue(const T* widget);
|
|
|
|
static void setFloatValue(T* widget, int value);
|
|
|
|
|
2020-01-01 04:01:58 +00:00
|
|
|
static QString getStringValue(const T* widget);
|
|
|
|
static void setStringValue(T* widget, const QString& value);
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(T* widget, F func);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QLineEdit>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QLineEdit* widget) { return widget->text().toInt() != 0; }
|
|
|
|
static void setBoolValue(QLineEdit* widget, bool value)
|
|
|
|
{
|
|
|
|
widget->setText(value ? QStringLiteral("1") : QStringLiteral("0"));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int getIntValue(const QLineEdit* widget) { return widget->text().toInt(); }
|
|
|
|
static void setIntValue(QLineEdit* widget, int value) { widget->setText(QStringLiteral("%1").arg(value)); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
static float getFloatValue(const QLineEdit* widget) { return widget->text().toFloat(); }
|
|
|
|
static void setFloatValue(QLineEdit* widget, float value) { widget->setText(QStringLiteral("%1").arg(value)); }
|
|
|
|
|
2020-01-01 04:01:58 +00:00
|
|
|
static QString getStringValue(const QLineEdit* widget) { return widget->text(); }
|
|
|
|
static void setStringValue(QLineEdit* widget, const QString& value) { widget->setText(value); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QLineEdit* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, &QLineEdit::textChanged, func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QComboBox>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QComboBox* widget) { return widget->currentText() > 0; }
|
|
|
|
static void setBoolValue(QComboBox* widget, bool value) { widget->setCurrentIndex(value ? 1 : 0); }
|
|
|
|
|
|
|
|
static int getIntValue(const QComboBox* widget) { return widget->currentIndex(); }
|
|
|
|
static void setIntValue(QComboBox* widget, int value) { widget->setCurrentIndex(value); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
static float getFloatValue(const QComboBox* widget) { return static_cast<float>(widget->currentIndex()); }
|
|
|
|
static void setFloatValue(QComboBox* widget, float value) { widget->setCurrentIndex(static_cast<int>(value)); }
|
|
|
|
|
2020-01-01 04:01:58 +00:00
|
|
|
static QString getStringValue(const QComboBox* widget) { return widget->currentText(); }
|
|
|
|
static void setStringValue(QComboBox* widget, const QString& value) { widget->setCurrentText(value); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QComboBox* widget, F func)
|
|
|
|
{
|
2020-01-24 04:49:49 +00:00
|
|
|
widget->connect(widget, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), func);
|
2020-01-01 04:01:58 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QCheckBox>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QCheckBox* widget) { return widget->isChecked(); }
|
|
|
|
static void setBoolValue(QCheckBox* widget, bool value) { widget->setChecked(value); }
|
|
|
|
|
|
|
|
static int getIntValue(const QCheckBox* widget) { return widget->isChecked() ? 1 : 0; }
|
|
|
|
static void setIntValue(QCheckBox* widget, int value) { widget->setChecked(value != 0); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
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); }
|
|
|
|
|
2020-01-01 04:01:58 +00:00
|
|
|
static QString getStringValue(const QCheckBox* widget)
|
|
|
|
{
|
|
|
|
return widget->isChecked() ? QStringLiteral("1") : QStringLiteral("0");
|
|
|
|
}
|
|
|
|
static void setStringValue(QCheckBox* widget, const QString& value) { widget->setChecked(value.toInt() != 0); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QCheckBox* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, &QCheckBox::stateChanged, func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-28 14:01:35 +00:00
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QSlider>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QSlider* widget) { return widget->value() > 0; }
|
|
|
|
static void setBoolValue(QSlider* widget, bool value) { widget->setValue(value ? 1 : 0); }
|
|
|
|
|
|
|
|
static int getIntValue(const QSlider* widget) { return widget->value(); }
|
|
|
|
static void setIntValue(QSlider* widget, int value) { widget->setValue(value); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
static float getFloatValue(const QSlider* widget) { return static_cast<float>(widget->value()); }
|
|
|
|
static void setFloatValue(QSlider* widget, float value) { widget->setValue(static_cast<int>(value)); }
|
|
|
|
|
2020-01-28 14:01:35 +00:00
|
|
|
static QString getStringValue(const QSlider* widget) { return QStringLiteral("%1").arg(widget->value()); }
|
|
|
|
static void setStringValue(QSlider* widget, const QString& value) { widget->setValue(value.toInt()); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QSlider* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, &QSlider::valueChanged, func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-14 21:43:16 +00:00
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QSpinBox>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QSpinBox* widget) { return widget->value() > 0; }
|
|
|
|
static void setBoolValue(QSpinBox* widget, bool value) { widget->setValue(value ? 1 : 0); }
|
|
|
|
|
|
|
|
static int getIntValue(const QSpinBox* widget) { return widget->value(); }
|
|
|
|
static void setIntValue(QSpinBox* widget, int value) { widget->setValue(value); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
static float getFloatValue(const QSpinBox* widget) { return static_cast<float>(widget->value()); }
|
|
|
|
static void setFloatValue(QSpinBox* widget, float value) { widget->setValue(static_cast<int>(value)); }
|
|
|
|
|
2020-06-14 21:43:16 +00:00
|
|
|
static QString getStringValue(const QSpinBox* widget) { return QStringLiteral("%1").arg(widget->value()); }
|
|
|
|
static void setStringValue(QSpinBox* widget, const QString& value) { widget->setValue(value.toInt()); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QSpinBox* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, QOverload<int>::of(&QSpinBox::valueChanged), func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QDoubleSpinBox>
|
|
|
|
{
|
|
|
|
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<int>(widget->value()); }
|
|
|
|
static void setIntValue(QDoubleSpinBox* widget, int value) { widget->setValue(static_cast<double>(value)); }
|
|
|
|
|
|
|
|
static float getFloatValue(const QDoubleSpinBox* widget) { return static_cast<float>(widget->value()); }
|
|
|
|
static void setFloatValue(QDoubleSpinBox* widget, float value) { widget->setValue(static_cast<double>(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<typename F>
|
|
|
|
static void connectValueChanged(QDoubleSpinBox* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, QOverload<double>::of(&QDoubleSpinBox::valueChanged), func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-04 06:22:39 +00:00
|
|
|
template<>
|
|
|
|
struct SettingAccessor<QAction>
|
|
|
|
{
|
|
|
|
static bool getBoolValue(const QAction* widget) { return widget->isChecked(); }
|
|
|
|
static void setBoolValue(QAction* widget, bool value) { widget->setChecked(value); }
|
|
|
|
|
|
|
|
static int getIntValue(const QAction* widget) { return widget->isChecked() ? 1 : 0; }
|
|
|
|
static void setIntValue(QAction* widget, int value) { widget->setChecked(value != 0); }
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
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); }
|
|
|
|
|
2020-02-04 06:22:39 +00:00
|
|
|
static QString getStringValue(const QAction* widget)
|
|
|
|
{
|
|
|
|
return widget->isChecked() ? QStringLiteral("1") : QStringLiteral("0");
|
|
|
|
}
|
|
|
|
static void setStringValue(QAction* widget, const QString& value) { widget->setChecked(value.toInt() != 0); }
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static void connectValueChanged(QAction* widget, F func)
|
|
|
|
{
|
|
|
|
widget->connect(widget, &QAction::toggled, func);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-01 04:01:58 +00:00
|
|
|
/// Binds a widget's value to a setting, updating it when the value changes.
|
|
|
|
|
|
|
|
template<typename WidgetType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToBoolSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
bool default_value = false)
|
2020-01-01 04:01:58 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
bool value = hi->GetBoolSettingValue(section.c_str(), key.c_str(), default_value);
|
2020-07-13 16:24:11 +00:00
|
|
|
|
|
|
|
Accessor::setBoolValue(widget, value);
|
2020-01-01 04:01:58 +00:00
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key]() {
|
2020-01-24 04:49:49 +00:00
|
|
|
const bool new_value = Accessor::getBoolValue(widget);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetBoolSettingValue(section.c_str(), key.c_str(), new_value);
|
2020-01-24 04:49:49 +00:00
|
|
|
hi->applySettings();
|
2020-01-01 04:01:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename WidgetType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToIntSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
int default_value = 0)
|
2020-01-01 04:01:58 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
s32 value = hi->GetIntSettingValue(section.c_str(), key.c_str(), static_cast<s32>(default_value));
|
2020-01-01 04:01:58 +00:00
|
|
|
|
2020-07-13 16:24:11 +00:00
|
|
|
Accessor::setIntValue(widget, static_cast<int>(value));
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key]() {
|
2020-01-24 04:49:49 +00:00
|
|
|
const int new_value = Accessor::getIntValue(widget);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetIntSettingValue(section.c_str(), key.c_str(), new_value);
|
2020-01-24 04:49:49 +00:00
|
|
|
hi->applySettings();
|
2020-01-01 04:01:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-30 14:34:00 +00:00
|
|
|
template<typename WidgetType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToFloatSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
float default_value = 0.0f)
|
2020-06-30 14:34:00 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
float value = hi->GetFloatSettingValue(section.c_str(), key.c_str(), default_value);
|
2020-06-30 14:34:00 +00:00
|
|
|
|
2020-07-13 16:24:11 +00:00
|
|
|
Accessor::setFloatValue(widget, value);
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key]() {
|
2020-06-30 14:34:00 +00:00
|
|
|
const float new_value = Accessor::getFloatValue(widget);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetFloatSettingValue(section.c_str(), key.c_str(), new_value);
|
2020-06-30 14:34:00 +00:00
|
|
|
hi->applySettings();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-10 13:44:00 +00:00
|
|
|
template<typename WidgetType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToNormalizedSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
float range, float default_value = 0.0f)
|
2020-02-10 13:44:00 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
float value = hi->GetFloatSettingValue(section.c_str(), key.c_str(), default_value);
|
2020-07-13 16:24:11 +00:00
|
|
|
|
|
|
|
Accessor::setIntValue(widget, static_cast<int>(value * range));
|
2020-02-10 13:44:00 +00:00
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key, range]() {
|
2020-02-10 13:44:00 +00:00
|
|
|
const float new_value = (static_cast<float>(Accessor::getIntValue(widget)) / range);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetFloatSettingValue(section.c_str(), key.c_str(), new_value);
|
2020-02-10 13:44:00 +00:00
|
|
|
hi->applySettings();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-24 04:49:49 +00:00
|
|
|
template<typename WidgetType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToStringSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
std::string default_value = std::string())
|
2020-01-01 04:01:58 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
std::string value = hi->GetStringSettingValue(section.c_str(), key.c_str(), default_value.c_str());
|
2020-07-13 16:24:11 +00:00
|
|
|
|
|
|
|
Accessor::setStringValue(widget, QString::fromStdString(value));
|
2020-01-01 04:01:58 +00:00
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key]() {
|
2020-01-24 04:49:49 +00:00
|
|
|
const QString new_value = Accessor::getStringValue(widget);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetStringSettingValue(section.c_str(), key.c_str(), new_value.toUtf8().constData());
|
2020-01-24 04:49:49 +00:00
|
|
|
hi->applySettings();
|
2020-01-01 04:01:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-03 07:51:58 +00:00
|
|
|
template<typename WidgetType, typename DataType>
|
2020-07-21 09:49:04 +00:00
|
|
|
void BindWidgetToEnumSetting(QtHostInterface* hi, WidgetType* widget, std::string section, std::string key,
|
|
|
|
std::optional<DataType> (*from_string_function)(const char* str),
|
2020-06-30 14:33:53 +00:00
|
|
|
const char* (*to_string_function)(DataType value), DataType default_value)
|
2020-01-03 07:51:58 +00:00
|
|
|
{
|
|
|
|
using Accessor = SettingAccessor<WidgetType>;
|
2020-01-24 04:49:49 +00:00
|
|
|
using UnderlyingType = std::underlying_type_t<DataType>;
|
2020-01-03 07:51:58 +00:00
|
|
|
|
2020-07-13 16:24:11 +00:00
|
|
|
// TODO: Clean this up?
|
2020-07-21 09:49:04 +00:00
|
|
|
const std::string old_setting_string_value =
|
|
|
|
hi->GetStringSettingValue(section.c_str(), key.c_str(), to_string_function(default_value));
|
2020-07-13 16:24:11 +00:00
|
|
|
const std::optional<DataType> old_setting_value = from_string_function(old_setting_string_value.c_str());
|
2020-01-24 04:49:49 +00:00
|
|
|
if (old_setting_value.has_value())
|
|
|
|
Accessor::setIntValue(widget, static_cast<int>(static_cast<UnderlyingType>(old_setting_value.value())));
|
2020-06-30 14:33:53 +00:00
|
|
|
else
|
|
|
|
Accessor::setIntValue(widget, static_cast<int>(static_cast<UnderlyingType>(default_value)));
|
2020-01-24 04:49:49 +00:00
|
|
|
|
2020-07-21 09:49:04 +00:00
|
|
|
Accessor::connectValueChanged(widget, [hi, widget, section, key, to_string_function]() {
|
2020-01-24 04:49:49 +00:00
|
|
|
const DataType value = static_cast<DataType>(static_cast<UnderlyingType>(Accessor::getIntValue(widget)));
|
|
|
|
const char* string_value = to_string_function(value);
|
2020-07-21 09:49:04 +00:00
|
|
|
hi->SetStringSettingValue(section.c_str(), key.c_str(), string_value);
|
2020-01-24 04:49:49 +00:00
|
|
|
hi->applySettings();
|
2020-01-03 07:51:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-14 21:43:16 +00:00
|
|
|
} // namespace SettingWidgetBinder
|