From a8dea217258f5fad9164c69ad8706007d9504f9e Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 25 Aug 2021 20:05:31 +1000 Subject: [PATCH] Qt: Make advanced seting constructors return the widget --- src/duckstation-qt/advancedsettingswidget.cpp | 64 ++++++++++++------- 1 file changed, 42 insertions(+), 22 deletions(-) diff --git a/src/duckstation-qt/advancedsettingswidget.cpp b/src/duckstation-qt/advancedsettingswidget.cpp index 88707e132..d0884a70e 100644 --- a/src/duckstation-qt/advancedsettingswidget.cpp +++ b/src/duckstation-qt/advancedsettingswidget.cpp @@ -5,8 +5,8 @@ #include "settingsdialog.h" #include "settingwidgetbinder.h" -static void addBooleanTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, - std::string section, std::string key, bool default_value) +static QCheckBox* addBooleanTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, + std::string section, std::string key, bool default_value) { const int row = table->rowCount(); @@ -17,21 +17,25 @@ static void addBooleanTweakOption(QtHostInterface* host_interface, QTableWidget* table->setItem(row, 0, name_item); QCheckBox* cb = new QCheckBox(table); - SettingWidgetBinder::BindWidgetToBoolSetting(host_interface, cb, std::move(section), std::move(key), default_value); + if (!section.empty() || !key.empty()) + SettingWidgetBinder::BindWidgetToBoolSetting(host_interface, cb, std::move(section), std::move(key), default_value); + table->setCellWidget(row, 1, cb); + return cb; } -static void setBooleanTweakOption(QTableWidget* table, int row, bool value) +static QCheckBox* setBooleanTweakOption(QTableWidget* table, int row, bool value) { QWidget* widget = table->cellWidget(row, 1); QCheckBox* cb = qobject_cast(widget); Assert(cb); cb->setChecked(value); + return cb; } -static void addIntRangeTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, - std::string section, std::string key, int min_value, int max_value, - int default_value) +static QSpinBox* addIntRangeTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, + std::string section, std::string key, int min_value, int max_value, + int default_value) { const int row = table->rowCount(); @@ -44,21 +48,25 @@ static void addIntRangeTweakOption(QtHostInterface* host_interface, QTableWidget QSpinBox* cb = new QSpinBox(table); cb->setMinimum(min_value); cb->setMaximum(max_value); - SettingWidgetBinder::BindWidgetToIntSetting(host_interface, cb, std::move(section), std::move(key), default_value); + if (!section.empty() || !key.empty()) + SettingWidgetBinder::BindWidgetToIntSetting(host_interface, cb, std::move(section), std::move(key), default_value); + table->setCellWidget(row, 1, cb); + return cb; } -static void setIntRangeTweakOption(QTableWidget* table, int row, int value) +static QSpinBox* setIntRangeTweakOption(QTableWidget* table, int row, int value) { QWidget* widget = table->cellWidget(row, 1); QSpinBox* cb = qobject_cast(widget); Assert(cb); cb->setValue(value); + return cb; } -static void addFloatRangeTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, - std::string section, std::string key, float min_value, float max_value, - float step_value, float default_value) +static QDoubleSpinBox* addFloatRangeTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, + std::string section, std::string key, float min_value, float max_value, + float step_value, float default_value) { const int row = table->rowCount(); @@ -72,27 +80,34 @@ static void addFloatRangeTweakOption(QtHostInterface* host_interface, QTableWidg cb->setMinimum(min_value); cb->setMaximum(max_value); cb->setSingleStep(step_value); - SettingWidgetBinder::BindWidgetToFloatSetting(host_interface, cb, std::move(section), std::move(key), default_value); + + if (!section.empty() || !key.empty()) + { + SettingWidgetBinder::BindWidgetToFloatSetting(host_interface, cb, std::move(section), std::move(key), + default_value); + } + table->setCellWidget(row, 1, cb); + return cb; } -static void setFloatRangeTweakOption(QTableWidget* table, int row, float value) +static QDoubleSpinBox* setFloatRangeTweakOption(QTableWidget* table, int row, float value) { QWidget* widget = table->cellWidget(row, 1); QDoubleSpinBox* cb = qobject_cast(widget); Assert(cb); cb->setValue(value); + return cb; } template -static void addChoiceTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, - std::string section, std::string key, std::optional (*parse_callback)(const char*), - const char* (*get_value_callback)(T), const char* (*get_display_callback)(T), - const char* tr_context, u32 num_values, T default_value) +static QComboBox* addChoiceTweakOption(QtHostInterface* host_interface, QTableWidget* table, QString name, + std::string section, std::string key, + std::optional (*parse_callback)(const char*), + const char* (*get_value_callback)(T), const char* (*get_display_callback)(T), + const char* tr_context, u32 num_values, T default_value) { const int row = table->rowCount(); - const std::string current_value = - host_interface->GetStringSettingValue(section.c_str(), key.c_str(), get_value_callback(default_value)); table->insertRow(row); @@ -104,9 +119,14 @@ static void addChoiceTweakOption(QtHostInterface* host_interface, QTableWidget* for (u32 i = 0; i < num_values; i++) cb->addItem(qApp->translate(tr_context, get_display_callback(static_cast(i)))); - SettingWidgetBinder::BindWidgetToEnumSetting(host_interface, cb, std::move(section), std::move(key), parse_callback, - get_value_callback, default_value); + if (!section.empty() || !key.empty()) + { + SettingWidgetBinder::BindWidgetToEnumSetting(host_interface, cb, std::move(section), std::move(key), parse_callback, + get_value_callback, default_value); + } + table->setCellWidget(row, 1, cb); + return cb; } template