Qt: implement EnhanceSpinBox and EnhanceDoubleSpinBox

This commit is contained in:
Megamouse 2018-06-17 14:00:50 +02:00
parent 525c257c6a
commit 50dd704f2b
2 changed files with 91 additions and 0 deletions

View File

@ -358,6 +358,90 @@ void emu_settings::EnhanceSlider(QSlider* slider, SettingsType type)
});
}
void emu_settings::EnhanceSpinBox(QSpinBox* spinbox, SettingsType type, const QString& prefix, const QString& suffix)
{
if (!spinbox)
{
LOG_FATAL(GENERAL, "EnhanceSpinBox '%s' was used with an invalid object", GetSettingName(type));
return;
}
QStringList range = GetSettingOptions(type);
bool ok_def, ok_sel, ok_min, ok_max;
int def = qstr(GetSettingDefault(type)).toInt(&ok_def);
int min = range.first().toInt(&ok_min);
int max = range.last().toInt(&ok_max);
if (!ok_def || !ok_min || !ok_max)
{
LOG_FATAL(GENERAL, "EnhanceSpinBox '%s' was used with an invalid type", GetSettingName(type));
return;
}
std::string selected = GetSetting(type);
int val = qstr(selected).toInt(&ok_sel);
if (!ok_sel || val < min || val > max)
{
LOG_ERROR(GENERAL, "EnhanceSpinBox '%s' tried to set an invalid value: %d. Setting to default: %d. Allowed range: [%d, %d]", GetSettingName(type), selected, def, min, max);
val = def;
m_broken_types.insert(type);
}
spinbox->setPrefix(prefix);
spinbox->setSuffix(suffix);
spinbox->setRange(min, max);
spinbox->setValue(val);
connect(spinbox, QOverload<const QString &>::of(&QSpinBox::valueChanged), [=](const QString&/* value*/)
{
SetSetting(type, sstr(spinbox->cleanText()));
});
}
void emu_settings::EnhanceDoubleSpinBox(QDoubleSpinBox* spinbox, SettingsType type, const QString& prefix, const QString& suffix)
{
if (!spinbox)
{
LOG_FATAL(GENERAL, "EnhanceDoubleSpinBox '%s' was used with an invalid object", GetSettingName(type));
return;
}
QStringList range = GetSettingOptions(type);
bool ok_def, ok_sel, ok_min, ok_max;
double def = qstr(GetSettingDefault(type)).toDouble(&ok_def);
double min = range.first().toDouble(&ok_min);
double max = range.last().toDouble(&ok_max);
if (!ok_def || !ok_min || !ok_max)
{
LOG_FATAL(GENERAL, "EnhanceDoubleSpinBox '%s' was used with an invalid type", GetSettingName(type));
return;
}
std::string selected = GetSetting(type);
double val = qstr(selected).toDouble(&ok_sel);
if (!ok_sel || val < min || val > max)
{
LOG_ERROR(GENERAL, "EnhanceDoubleSpinBox '%s' tried to set an invalid value: %f. Setting to default: %f. Allowed range: [%f, %f]", GetSettingName(type), val, def, min, max);
val = def;
m_broken_types.insert(type);
}
spinbox->setPrefix(prefix);
spinbox->setSuffix(suffix);
spinbox->setRange(min, max);
spinbox->setValue(val);
connect(spinbox, QOverload<const QString &>::of(&QDoubleSpinBox::valueChanged), [=](const QString&/* value*/)
{
SetSetting(type, sstr(spinbox->cleanText()));
});
}
std::vector<std::string> emu_settings::GetLoadedLibraries()
{
return m_currentSettings["Core"]["Load libraries"].as<std::vector<std::string>, std::initializer_list<std::string>>({});

View File

@ -10,6 +10,7 @@
#include <QMap>
#include <QObject>
#include <QComboBox>
#include <QSpinBox>
constexpr auto qstr = QString::fromStdString;
@ -173,6 +174,12 @@ public:
/** Connects a slider with the target settings type*/
void EnhanceSlider(QSlider* slider, SettingsType type);
/** Connects an integer spin box with the target settings type*/
void EnhanceSpinBox(QSpinBox* slider, SettingsType type, const QString& prefix = "", const QString& suffix = "");
/** Connects a double spin box with the target settings type*/
void EnhanceDoubleSpinBox(QDoubleSpinBox* slider, SettingsType type, const QString& prefix = "", const QString& suffix = "");
std::vector<std::string> GetLoadedLibraries();
void SaveSelectedLibraries(const std::vector<std::string>& libs);