Qt: Fix several issues in SettingWidgetBinder

- Possible nullopt value().
 - Incorrect loading of some folder values.
 - Enable per-game setting of spinboxes.
This commit is contained in:
Connor McLaughlin 2022-08-05 22:44:01 +10:00 committed by refractionpcsx2
parent cd2d7c91c3
commit 307028734f
5 changed files with 231 additions and 41 deletions

View File

@ -26,6 +26,7 @@
#include <QtWidgets/QDoubleSpinBox>
#include <QtWidgets/QFileDialog>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QMenu>
#include <QtWidgets/QSlider>
#include <QtWidgets/QSpinBox>
@ -41,6 +42,10 @@
namespace SettingWidgetBinder
{
static constexpr const char* NULLABLE_PROPERTY = "SettingWidgetBinder_isNullable";
static constexpr const char* IS_NULL_PROPERTY = "SettingWidgetBinder_isNull";
static constexpr const char* GLOBAL_VALUE_PROPERTY = "SettingWidgetBinder_globalValue";
template <typename T>
struct SettingAccessor
{
@ -179,7 +184,7 @@ namespace SettingWidgetBinder
}
static void setNullableStringValue(QComboBox* widget, std::optional<QString> value)
{
isNullValue(widget) ? widget->setCurrentIndex(0) : setStringValue(widget, value.value());
value.has_value() ? setStringValue(widget, value.value()) : widget->setCurrentIndex(0);
}
template <typename F>
@ -286,71 +291,256 @@ namespace SettingWidgetBinder
template <>
struct SettingAccessor<QSpinBox>
{
static bool isNullable(const QSpinBox* widget) { return widget->property(NULLABLE_PROPERTY).toBool(); }
static void updateNullState(QSpinBox* widget, bool isNull)
{
widget->setPrefix(isNull ? qApp->translate("SettingWidgetBinder", "Default: ") : QString());
}
static bool getBoolValue(const QSpinBox* widget) { return widget->value() > 0; }
static void setBoolValue(QSpinBox* widget, bool value) { widget->setValue(value ? 1 : 0); }
static void makeNullableBool(QSpinBox* widget, bool globalSetting) { widget->setEnabled(false); }
static std::optional<bool> getNullableBoolValue(const QSpinBox* widget) { return getBoolValue(widget); }
static void setNullableBoolValue(QSpinBox* widget, std::optional<bool> value) { setBoolValue(widget, value.value_or(false)); }
static void makeNullableBool(QSpinBox* widget, bool globalSetting)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalSetting));
}
static std::optional<bool> getNullableBoolValue(const QSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getBoolValue(widget);
}
static void setNullableBoolValue(QSpinBox* widget, std::optional<bool> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setBoolValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toBool());
updateNullState(widget, !value.has_value());
}
static int getIntValue(const QSpinBox* widget) { return widget->value(); }
static void setIntValue(QSpinBox* widget, int value) { widget->setValue(value); }
static void makeNullableInt(QSpinBox* widget, int globalValue) { widget->setEnabled(false); }
static std::optional<int> getNullableIntValue(const QSpinBox* widget) { return getIntValue(widget); }
static void setNullableIntValue(QSpinBox* widget, std::optional<int> value) { setIntValue(widget, value.value_or(0)); }
static void makeNullableInt(QSpinBox* widget, int globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<int> getNullableIntValue(const QSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getIntValue(widget);
}
static void setNullableIntValue(QSpinBox* widget, std::optional<int> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setIntValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toInt());
updateNullState(widget, !value.has_value());
}
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)); }
static void makeNullableFloat(QSpinBox* widget, float globalValue) { widget->setEnabled(false); }
static std::optional<float> getNullableFloatValue(const QSpinBox* widget) { return getFloatValue(widget); }
static void setNullableFloatValue(QSpinBox* widget, std::optional<float> value) { setFloatValue(widget, value.value_or(0.0f)); }
static void makeNullableFloat(QSpinBox* widget, float globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<float> getNullableFloatValue(const QSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getFloatValue(widget);
}
static void setNullableFloatValue(QSpinBox* widget, std::optional<float> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setFloatValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toFloat());
updateNullState(widget, !value.has_value());
}
static QString getStringValue(const QSpinBox* widget) { return QString::number(widget->value()); }
static void setStringValue(QSpinBox* widget, const QString& value) { widget->setValue(value.toInt()); }
static void makeNullableString(QSpinBox* widget, const QString& globalValue) { widget->setEnabled(false); }
static std::optional<QString> getNullableStringValue(const QSpinBox* widget) { return getStringValue(widget); }
static void setNullableStringValue(QSpinBox* widget, std::optional<QString> value) { setStringValue(widget, value.value_or(QString())); }
static void makeNullableString(QSpinBox* widget, const QString& globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<QString> getNullableStringValue(const QSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getStringValue(widget);
}
static void setNullableStringValue(QSpinBox* widget, std::optional<QString> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setStringValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toString());
updateNullState(widget, !value.has_value());
}
template <typename F>
static void connectValueChanged(QSpinBox* widget, F func)
{
widget->connect(widget, QOverload<int>::of(&QSpinBox::valueChanged), func);
if (!isNullable(widget))
{
widget->connect(widget, QOverload<int>::of(&QSpinBox::valueChanged), func);
}
else
{
widget->setContextMenuPolicy(Qt::CustomContextMenu);
widget->connect(widget, &QSpinBox::customContextMenuRequested, widget, [widget, func](const QPoint& pt) {
QMenu menu(widget);
widget->connect(menu.addAction(qApp->translate("SettingWidgetBinder", "Reset")), &QAction::triggered, widget,
[widget, func = std::move(func)]() {
const bool old = widget->blockSignals(true);
setNullableIntValue(widget, std::nullopt);
widget->blockSignals(old);
updateNullState(widget, true);
func();
});
menu.exec(widget->mapToGlobal(pt));
});
widget->connect(widget, &QSpinBox::valueChanged, widget, [widget, func = std::move(func)]() {
if (widget->property(IS_NULL_PROPERTY).toBool())
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(false));
updateNullState(widget, false);
}
func();
});
}
}
};
template <>
struct SettingAccessor<QDoubleSpinBox>
{
static bool isNullable(const QDoubleSpinBox* widget) { return widget->property(NULLABLE_PROPERTY).toBool(); }
static void updateNullState(QDoubleSpinBox* widget, bool isNull)
{
widget->setPrefix(isNull ? qApp->translate("SettingWidgetBinder", "Default: ") : QString());
}
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 void makeNullableBool(QDoubleSpinBox* widget, bool globalSetting) { widget->setEnabled(false); }
static std::optional<bool> getNullableBoolValue(const QDoubleSpinBox* widget) { return getBoolValue(widget); }
static void setNullableBoolValue(QDoubleSpinBox* widget, std::optional<bool> value) { setBoolValue(widget, value.value_or(false)); }
static void makeNullableBool(QDoubleSpinBox* widget, bool globalSetting)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalSetting));
}
static std::optional<bool> getNullableBoolValue(const QDoubleSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getBoolValue(widget);
}
static void setNullableBoolValue(QDoubleSpinBox* widget, std::optional<bool> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setBoolValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toBool());
updateNullState(widget, !value.has_value());
}
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 void makeNullableInt(QDoubleSpinBox* widget, int globalValue) { widget->setEnabled(false); }
static std::optional<int> getNullableIntValue(const QDoubleSpinBox* widget) { return getIntValue(widget); }
static void setNullableIntValue(QDoubleSpinBox* widget, std::optional<int> value) { setIntValue(widget, value.value_or(0)); }
static void makeNullableInt(QDoubleSpinBox* widget, int globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<int> getNullableIntValue(const QDoubleSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getIntValue(widget);
}
static void setNullableIntValue(QDoubleSpinBox* widget, std::optional<int> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setIntValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toInt());
updateNullState(widget, !value.has_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 void makeNullableFloat(QDoubleSpinBox* widget, float globalValue) { widget->setEnabled(false); }
static std::optional<float> getNullableFloatValue(const QDoubleSpinBox* widget) { return getFloatValue(widget); }
static void setNullableFloatValue(QDoubleSpinBox* widget, std::optional<float> value) { setFloatValue(widget, value.value_or(0.0f)); }
static void makeNullableFloat(QDoubleSpinBox* widget, float globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<float> getNullableFloatValue(const QDoubleSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getFloatValue(widget);
}
static void setNullableFloatValue(QDoubleSpinBox* widget, std::optional<float> value)
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setFloatValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toFloat());
updateNullState(widget, !value.has_value());
}
static QString getStringValue(const QDoubleSpinBox* widget) { return QString::number(widget->value()); }
static void setStringValue(QDoubleSpinBox* widget, const QString& value) { widget->setValue(value.toDouble()); }
static void makeNullableString(QDoubleSpinBox* widget, const QString& globalValue) { widget->setEnabled(false); }
static std::optional<QString> getNullableStringValue(const QDoubleSpinBox* widget) { return getStringValue(widget); }
static void makeNullableString(QDoubleSpinBox* widget, const QString& globalValue)
{
widget->setProperty(NULLABLE_PROPERTY, QVariant(true));
widget->setProperty(GLOBAL_VALUE_PROPERTY, QVariant(globalValue));
}
static std::optional<QString> getNullableStringValue(const QDoubleSpinBox* widget)
{
if (widget->property(IS_NULL_PROPERTY).toBool())
return std::nullopt;
return getStringValue(widget);
}
static void setNullableStringValue(QDoubleSpinBox* widget, std::optional<QString> value)
{
setStringValue(widget, value.value_or(QString()));
widget->setProperty(IS_NULL_PROPERTY, QVariant(!value.has_value()));
setStringValue(widget, value.has_value() ? value.value() : widget->property(GLOBAL_VALUE_PROPERTY).toString());
updateNullState(widget, !value.has_value());
}
template <typename F>
static void connectValueChanged(QDoubleSpinBox* widget, F func)
{
widget->connect(widget, QOverload<double>::of(&QDoubleSpinBox::valueChanged), func);
if (!isNullable(widget))
{
widget->connect(widget, QOverload<double>::of(&QDoubleSpinBox::valueChanged), func);
}
else
{
widget->setContextMenuPolicy(Qt::CustomContextMenu);
widget->connect(widget, &QDoubleSpinBox::customContextMenuRequested, widget, [widget, func](const QPoint& pt) {
QMenu menu(widget);
widget->connect(menu.addAction(qApp->translate("SettingWidgetBinder", "Reset")), &QAction::triggered, widget,
[widget, func = std::move(func)]() {
const bool old = widget->blockSignals(true);
setNullableFloatValue(widget, std::nullopt);
widget->blockSignals(old);
updateNullState(widget, true);
func();
});
menu.exec(widget->mapToGlobal(pt));
});
widget->connect(widget, QOverload<double>::of(&QDoubleSpinBox::valueChanged), widget, [widget, func = std::move(func)]() {
if (widget->property(IS_NULL_PROPERTY).toBool())
{
widget->setProperty(IS_NULL_PROPERTY, QVariant(false));
updateNullState(widget, false);
}
func();
});
}
}
};
@ -776,14 +966,15 @@ namespace SettingWidgetBinder
}
template <typename WidgetType>
static void BindWidgetToFolderSetting(SettingsInterface* sif, WidgetType* widget,
QAbstractButton* browse_button, QAbstractButton* open_button, QAbstractButton* reset_button,
std::string section, std::string key, std::string default_value)
static void BindWidgetToFolderSetting(SettingsInterface* sif, WidgetType* widget, QAbstractButton* browse_button, QAbstractButton* open_button,
QAbstractButton* reset_button, std::string section, std::string key, std::string default_value)
{
using Accessor = SettingAccessor<WidgetType>;
std::string current_path(Host::GetBaseStringSettingValue(section.c_str(), key.c_str(), default_value.c_str()));
if (!Path::IsAbsolute(current_path))
if (current_path.empty())
current_path = default_value;
else if (!Path::IsAbsolute(current_path))
current_path = Path::Combine(EmuFolders::DataRoot, current_path);
const QString value(QString::fromStdString(current_path));
@ -836,9 +1027,8 @@ namespace SettingWidgetBinder
}
if (reset_button)
{
QObject::connect(reset_button, &QAbstractButton::clicked, reset_button, [widget, default_value = std::move(default_value)]() {
Accessor::setStringValue(widget, QString::fromStdString(Path::Combine(EmuFolders::AppRoot, default_value)));
});
QObject::connect(reset_button, &QAbstractButton::clicked, reset_button,
[widget, default_value = std::move(default_value)]() { Accessor::setStringValue(widget, QString::fromStdString(default_value)); });
}
}
} // namespace SettingWidgetBinder

View File

@ -39,7 +39,7 @@ BIOSSettingsWidget::BIOSSettingsWidget(SettingsDialog* dialog, QWidget* parent)
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.patchRegion, "EmuCore", "PatchBios", false);
SettingWidgetBinder::BindWidgetToEnumSetting(sif, m_ui.regionComboBox, "EmuCore", "PatchRegion", BiosZoneStrings, BiosZoneBytes, BiosZoneBytes[0]);
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.searchDirectory, m_ui.browseSearchDirectory, m_ui.openSearchDirectory,
m_ui.resetSearchDirectory, "Folders", "Bios", "bios");
m_ui.resetSearchDirectory, "Folders", "Bios", Path::Combine(EmuFolders::DataRoot, "bios"));
dialog->registerWidgetHelp(m_ui.patchRegion, tr("Patch Region"), tr("Unchecked"),
tr("Patches the BIOS region byte in ROM. Not recommended unless you really know what you're doing."));

View File

@ -29,10 +29,10 @@ FolderSettingsWidget::FolderSettingsWidget(SettingsDialog* dialog, QWidget* pare
m_ui.setupUi(this);
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.cache, m_ui.cacheBrowse, m_ui.cacheOpen, m_ui.cacheReset, "Folders", "Cache", "cache");
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.covers, m_ui.coversBrowse, m_ui.coversOpen, m_ui.coversReset, "Folders", "Covers", "covers");
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.snapshots, m_ui.snapshotsBrowse, m_ui.snapshotsOpen, m_ui.snapshotsReset, "Folders", "Snapshots", "snaps");
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.saveStates, m_ui.saveStatesBrowse, m_ui.saveStatesOpen, m_ui.saveStatesReset, "Folders", "SaveStates", "sstates");
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.cache, m_ui.cacheBrowse, m_ui.cacheOpen, m_ui.cacheReset, "Folders", "Cache", Path::Combine(EmuFolders::DataRoot, "cache"));
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.covers, m_ui.coversBrowse, m_ui.coversOpen, m_ui.coversReset, "Folders", "Covers", Path::Combine(EmuFolders::DataRoot, "covers"));
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.snapshots, m_ui.snapshotsBrowse, m_ui.snapshotsOpen, m_ui.snapshotsReset, "Folders", "Snapshots", Path::Combine(EmuFolders::DataRoot, "snaps"));
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.saveStates, m_ui.saveStatesBrowse, m_ui.saveStatesOpen, m_ui.saveStatesReset, "Folders", "SaveStates", Path::Combine(EmuFolders::DataRoot, "sstates"));
}
FolderSettingsWidget::~FolderSettingsWidget() = default;

View File

@ -232,7 +232,7 @@ GraphicsSettingsWidget::GraphicsSettingsWidget(SettingsDialog* dialog, QWidget*
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.loadTextureReplacementsAsync, "EmuCore/GS", "LoadTextureReplacementsAsync", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.precacheTextureReplacements, "EmuCore/GS", "PrecacheTextureReplacements", false);
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.texturesDirectory, m_ui.texturesBrowse, m_ui.texturesOpen, m_ui.texturesReset,
"Folders", "Textures", "textures");
"Folders", "Textures", Path::Combine(EmuFolders::DataRoot, "textures"));
//////////////////////////////////////////////////////////////////////////
// Advanced Settings

View File

@ -50,7 +50,7 @@ MemoryCardSettingsWidget::MemoryCardSettingsWidget(SettingsDialog* dialog, QWidg
// since the group box hasn't been resized at that point.
m_ui.cardGroupBox->installEventFilter(this);
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.directory, m_ui.browse, m_ui.open, m_ui.reset, "Folders", "MemoryCards", "memcards");
SettingWidgetBinder::BindWidgetToFolderSetting(sif, m_ui.directory, m_ui.browse, m_ui.open, m_ui.reset, "Folders", "MemoryCards", Path::Combine(EmuFolders::DataRoot, "memcards"));
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.autoEject, "EmuCore", "McdEnableEjection", true);
SettingWidgetBinder::BindWidgetToBoolSetting(sif, m_ui.automaticManagement, "EmuCore", "McdFolderAutoManage", true);