From 97674bc88821b202aa7309208b71f2ab042fc5aa Mon Sep 17 00:00:00 2001 From: lat9nq <22451773+lat9nq@users.noreply.github.com> Date: Tue, 9 May 2023 15:46:07 -0400 Subject: [PATCH] shared_widget: Support checkbox + spinbox --- src/yuzu/configuration/configure_graphics.cpp | 2 +- src/yuzu/configuration/shared_widget.cpp | 50 +++++++++++++++++-- src/yuzu/configuration/shared_widget.h | 13 +++-- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/src/yuzu/configuration/configure_graphics.cpp b/src/yuzu/configuration/configure_graphics.cpp index 4c6d697033..5132c4796c 100644 --- a/src/yuzu/configuration/configure_graphics.cpp +++ b/src/yuzu/configuration/configure_graphics.cpp @@ -240,7 +240,7 @@ void ConfigureGraphics::Setup() { return new ConfigurationShared::Widget( setting, translations, this, runtime_lock, apply_funcs, ConfigurationShared::RequestType::LineEdit, true, 1.0f, - Settings::values.speed_limit.ToString()); + &Settings::values.speed_limit, QString::fromStdString("%")); } else { return new ConfigurationShared::Widget(setting, translations, this, runtime_lock, apply_funcs); diff --git a/src/yuzu/configuration/shared_widget.cpp b/src/yuzu/configuration/shared_widget.cpp index ba44d19735..841524b9ce 100644 --- a/src/yuzu/configuration/shared_widget.cpp +++ b/src/yuzu/configuration/shared_widget.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include "common/settings.h" @@ -234,7 +235,8 @@ void Widget::CreateSlider(const QString& name, bool reversed, float multiplier, } } -void Widget::CreateCheckBoxWithLineEdit(const QString& label, const std::string& text_box_default, +void Widget::CreateCheckBoxWithLineEdit(const QString& label, + const Settings::BasicSetting* other_setting, std::function& load_func) { created = true; @@ -243,7 +245,7 @@ void Widget::CreateCheckBoxWithLineEdit(const QString& label, const std::string& QHBoxLayout* layout = reinterpret_cast(this->layout()); line_edit = new QLineEdit(this); - line_edit->setText(QString::fromStdString(text_box_default)); + line_edit->setText(QString::fromStdString(other_setting->ToString())); checkbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); @@ -254,7 +256,7 @@ void Widget::CreateCheckBoxWithLineEdit(const QString& label, const std::string& if (!Settings::IsConfiguringGlobal()) { QObject::connect(restore_button, &QAbstractButton::clicked, [=](bool) { - line_edit->setText(QString::fromStdString(text_box_default)); + line_edit->setText(QString::fromStdString(other_setting->ToString())); }); QObject::connect(line_edit, &QLineEdit::textEdited, [=](const QString&) { @@ -264,6 +266,41 @@ void Widget::CreateCheckBoxWithLineEdit(const QString& label, const std::string& } } +void Widget::CreateCheckBoxWithSpinBox(const QString& label, + const Settings::BasicSetting* other_setting, + std::function& load_func, const QString& suffix) { + created = true; + + CreateCheckBox(label, load_func); + checkbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + + QHBoxLayout* layout = reinterpret_cast(this->layout()); + + spinbox = new QSpinBox(this); + const int min_val = std::stoi(other_setting->MinVal()); + const int max_val = std::stoi(other_setting->MaxVal()); + spinbox->setRange(min_val, max_val); + spinbox->setValue(std::stoi(other_setting->ToString())); + spinbox->setSuffix(suffix); + spinbox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred); + + layout->insertWidget(1, spinbox); + + QObject::connect(spinbox, QOverload::of(&QSpinBox::valueChanged), + [this](int) { checkbox->setCheckState(Qt::Checked); }); + + if (!Settings::IsConfiguringGlobal()) { + QObject::connect(restore_button, &QAbstractButton::clicked, [this, other_setting](bool) { + spinbox->setValue(std::stoi(other_setting->ToString())); + }); + + QObject::connect(spinbox, QOverload::of(&QSpinBox::valueChanged), [this](int) { + restore_button->setEnabled(true); + restore_button->setVisible(true); + }); + } +} + bool Widget::Valid() { return created; } @@ -273,7 +310,8 @@ Widget::~Widget() = default; Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translations_, QWidget* parent_, bool runtime_lock, std::forward_list>& apply_funcs, RequestType request, - bool managed, float multiplier, const std::string& text_box_default) + bool managed, float multiplier, const Settings::BasicSetting* other_setting, + const QString& format) : QWidget(parent_), parent{parent_}, translations{translations_}, setting{*setting_} { if (!Settings::IsConfiguringGlobal() && !setting.Switchable()) { LOG_DEBUG(Frontend, "\"{}\" is not switchable, skipping...", setting.GetLabel()); @@ -306,8 +344,10 @@ Widget::Widget(Settings::BasicSetting* setting_, const TranslationMap& translati CreateCheckBox(label, load_func); break; case RequestType::LineEdit: + CreateCheckBoxWithLineEdit(label, other_setting, load_func); + break; case RequestType::SpinBox: - CreateCheckBoxWithLineEdit(label, text_box_default, load_func); + CreateCheckBoxWithSpinBox(label, other_setting, load_func, format); break; case RequestType::ComboBox: case RequestType::Slider: diff --git a/src/yuzu/configuration/shared_widget.h b/src/yuzu/configuration/shared_widget.h index 8d2d7f2691..c25d819f0e 100644 --- a/src/yuzu/configuration/shared_widget.h +++ b/src/yuzu/configuration/shared_widget.h @@ -4,6 +4,7 @@ #include "yuzu/configuration/shared_translation.h" class QPushButton; +class QSpinBox; class QComboBox; class QLineEdit; class QSlider; @@ -32,7 +33,8 @@ public: Widget(Settings::BasicSetting* setting, const TranslationMap& translations, QWidget* parent, bool runtime_lock, std::forward_list>& apply_funcs, RequestType request = RequestType::Default, bool managed = true, float multiplier = 1.0f, - const std::string& text_box_default = ""); + const Settings::BasicSetting* other_setting = nullptr, + const QString& format = QStringLiteral("")); virtual ~Widget(); bool Valid(); @@ -42,16 +44,19 @@ public: QPushButton* restore_button{}; QLineEdit* line_edit{}; + QSpinBox* spinbox{}; QCheckBox* checkbox{}; QSlider* slider{}; QComboBox* combobox{}; private: void CreateCheckBox(const QString& label, std::function& load_func); - void CreateCheckBoxWithLineEdit(const QString& label, const std::string& text_box_default, + void CreateCheckBoxWithLineEdit(const QString& label, + const Settings::BasicSetting* other_setting, std::function& load_func); - void CreateCheckBoxWithSpinBox(const QString& label, const std::string& text_box_default, - std::function& load_func); + void CreateCheckBoxWithSpinBox(const QString& label, + const Settings::BasicSetting* other_setting, + std::function& load_func, const QString& suffix); void CreateCombobox(const QString& label, bool managed, std::function& load_func); void CreateLineEdit(const QString& label, bool managed, std::function& load_func); void CreateSlider(const QString& label, bool reversed, float multiplier,