From 8768796b05c79967b941390e87f0e47f0bd05108 Mon Sep 17 00:00:00 2001 From: Satori Date: Fri, 9 Oct 2020 23:04:01 +0100 Subject: [PATCH] [Qt] SettingsWidget stores IConfigVar* instead of ConfigVar --- .../qt/settings/widgets/settings_checkbox.cc | 7 ++- .../qt/settings/widgets/settings_combobox.h | 53 +++++++++++++++++-- .../qt/settings/widgets/settings_groupbox.cc | 21 +++++++- .../ui/qt/settings/widgets/settings_widget.h | 15 +++--- 4 files changed, 85 insertions(+), 11 deletions(-) diff --git a/src/xenia/ui/qt/settings/widgets/settings_checkbox.cc b/src/xenia/ui/qt/settings/widgets/settings_checkbox.cc index 76c10efa3..8f1c49e46 100644 --- a/src/xenia/ui/qt/settings/widgets/settings_checkbox.cc +++ b/src/xenia/ui/qt/settings/widgets/settings_checkbox.cc @@ -18,7 +18,12 @@ void SettingsCheckBox::Initialize() { return; } - setChecked(*cvar_->current_value()); + auto cvar = cvar_->as(); + if (!cvar) { + return; + } + + setChecked(*cvar->current_value()); connect(this, &SettingsCheckBox::stateChanged, [this](int state) { if (state == Qt::Checked) { diff --git a/src/xenia/ui/qt/settings/widgets/settings_combobox.h b/src/xenia/ui/qt/settings/widgets/settings_combobox.h index 367a2cc59..8beeb373d 100644 --- a/src/xenia/ui/qt/settings/widgets/settings_combobox.h +++ b/src/xenia/ui/qt/settings/widgets/settings_combobox.h @@ -18,11 +18,58 @@ namespace ui { namespace qt { template -class SettingsComboBox : SettingsWidget { - static_assert(std::is_same_v || std::is_same_v, - "Settings TextEdit must use std::string or int"); +class SettingsComboBox : public SettingsWidget { + using SettingsCvar = cvar::ConfigVar; + + public: + SettingsComboBox(SettingsCvar* config_var = nullptr, QLabel* label = nullptr, + QWidget* parent = nullptr) + : SettingsWidget(config_var, label, parent) {} + + void Initialize(); }; +template <> +inline void SettingsComboBox::Initialize() { + if (!cvar_) { + return; + } + if (!cvar_) { + return; + } + + auto cvar = cvar_->as(); + if (!cvar) { + return; + } + + setCurrentIndex(*cvar->current_value()); + + connect(this, QOverload::of(&QComboBox::currentIndexChanged), + [this](int index) { UpdateValue(index); }); +} + +template <> +inline void SettingsComboBox::Initialize() { + if (!cvar_) { + return; + } + if (!cvar_) { + return; + } + + auto cvar = cvar_->as(); + if (!cvar) { + return; + } + + setCurrentText(QString(*cvar->current_value()->c_str())); + + connect(this, &QComboBox::currentTextChanged, [this](const QString& text) { + UpdateValue(std::string(text.toUtf8())); + }); +} + } // namespace qt } // namespace ui } // namespace xe diff --git a/src/xenia/ui/qt/settings/widgets/settings_groupbox.cc b/src/xenia/ui/qt/settings/widgets/settings_groupbox.cc index 2534cd806..e3d7ee32d 100644 --- a/src/xenia/ui/qt/settings/widgets/settings_groupbox.cc +++ b/src/xenia/ui/qt/settings/widgets/settings_groupbox.cc @@ -41,7 +41,26 @@ SettingsCheckBox* SettingsGroupBox::CreateCheckBox(const QString& text, SettingsComboBox* SettingsGroupBox::CreateComboBox(const QString& text, cvar::ConfigVar* target) { - return nullptr; + auto combobox_layout = new QVBoxLayout(); + combobox_layout->setContentsMargins(0, 0, 0, 0); + combobox_layout->setSpacing(0); + + auto widget_label = new QLabel(); + widget_label->setObjectName("subLabel"); + widget_label->setProperty("type", "warning"); + auto font = widget_label->font(); + font.setPointSizeF(kSubLabelSize); + widget_label->setFont(font); + widget_label->setVisible(false); + + auto combobox = new SettingsComboBox(); + combobox_layout->addWidget(combobox); + + combobox_layout->addWidget(widget_label); + + layout_->addLayout(combobox_layout); + + return combobox; } SettingsComboBox* SettingsGroupBox::CreateComboBox( diff --git a/src/xenia/ui/qt/settings/widgets/settings_widget.h b/src/xenia/ui/qt/settings/widgets/settings_widget.h index 23dec68ee..716561363 100644 --- a/src/xenia/ui/qt/settings/widgets/settings_widget.h +++ b/src/xenia/ui/qt/settings/widgets/settings_widget.h @@ -28,7 +28,7 @@ class SettingsWidget : public Widget { public: template - SettingsWidget(cvar::ConfigVar* config_var, QLabel* label = nullptr, + SettingsWidget(cvar::IConfigVar* config_var, QLabel* label = nullptr, Args... args) : Widget(args...), cvar_(config_var), label_(label) { // default config update function @@ -46,8 +46,8 @@ class SettingsWidget : public Widget { } } - cvar::ConfigVar* config_var() const { return cvar_; } - void set_config_var(cvar::ConfigVar* cvar) { cvar_ = cvar; } + cvar::IConfigVar* config_var() const { return cvar_; } + void set_config_var(cvar::IConfigVar* cvar) { cvar_ = cvar; } void set_update_config_fn( const std::function&)>& fn) { @@ -56,15 +56,18 @@ class SettingsWidget : public Widget { void UpdateValue(T val) { if (cvar_) { - update_config_fn_(val, *cvar_); - SaveToConfig(); + auto cvar = cvar_->as(); + if (cvar) { + update_config_fn_(val, *cvar); + SaveToConfig(); + } } } void SaveToConfig() { Config::Instance().SaveConfig(); } protected: - cvar::ConfigVar* cvar_; + cvar::IConfigVar* cvar_; std::function&)> update_config_fn_; QLabel* label_; };