[Qt] Support initializing SettingsWidget by reference to cvar

This prevents a situation where one developer changes the name of a cvar but doesn't update calls to Config::FindCvar*
This commit is contained in:
Satori 2020-08-30 23:09:16 +01:00
parent e990505609
commit cca959e760
4 changed files with 60 additions and 19 deletions

View File

@ -8,7 +8,9 @@
#include "xenia/ui/qt/widgets/groupbox.h"
#include "xenia/ui/qt/widgets/scroll_area.h"
DECLARE_bool(show_debug_tab)
DECLARE_bool(show_debug_tab);
DECLARE_bool(discord);
DECLARE_bool(use_game_icon);
namespace xe {
namespace ui {
@ -53,7 +55,7 @@ DECLARE_bool(show_debug_tab)
groupbox_layout->setContentsMargins(16, 16, 16, 16);
groupbox->setLayout(groupbox_layout);
auto discord_presence_checkbox = new SettingsCheckBox("discord");
auto discord_presence_checkbox = new SettingsCheckBox(cvars::discord);
discord_presence_checkbox->setText("Discord Rich Presence");
groupbox_layout->addWidget(discord_presence_checkbox);

View File

@ -0,0 +1,36 @@
/**
******************************************************************************
* Xenia : Xbox 360 Emulator Research Project *
******************************************************************************
* Copyright 2020 Ben Vanik. All rights reserved. *
* Released under the BSD license - see LICENSE in the root for more details. *
******************************************************************************
*/
#include "settings_checkbox.h"
namespace xe {
namespace ui {
namespace qt {
void SettingsCheckBox::Initialize() {
if (!cvar_) {
return;
}
setChecked(*cvar_->current_value());
connect(this, &SettingsCheckBox::stateChanged, [&](int state) {
if (state == Qt::Checked) {
UpdateValue(true);
} else if (state == Qt::Unchecked) {
UpdateValue(false);
} else {
XELOGW("PartiallyChecked state not supported for SettingsCheckBox");
}
});
}
} // namespace qt
} // namespace ui
} // namespace xe

View File

@ -18,27 +18,23 @@ namespace xe {
namespace ui {
namespace qt {
class SettingsCheckBox : public SettingsWidget<bool, XCheckBox> {
using SettingsType = bool;
class SettingsCheckBox : public SettingsWidget<SettingsType, XCheckBox> {
public:
explicit SettingsCheckBox(const std::string& config_name,
QWidget* parent = nullptr)
: SettingsWidget(config_name, parent) {
if (!cvar_) {
return;
}
setChecked(*cvar_->current_value());
connect(this, &SettingsCheckBox::stateChanged, [&](int state) {
if (state == Qt::Checked) {
UpdateValue(true);
} else if (state == Qt::Unchecked) {
UpdateValue(false);
} else {
XELOGW("PartiallyChecked state not supported for SettingsCheckBox");
}
});
Initialize();
}
explicit SettingsCheckBox(const SettingsType& config_var,
QWidget* parent = nullptr)
: SettingsWidget(config_var, parent) {
Initialize();
}
void Initialize();
};
} // namespace qt

View File

@ -25,7 +25,14 @@ class SettingsWidget : public Widget {
SettingsWidget(const std::string& config_name, Args... args)
: Widget(args...), cvar_(nullptr) {
cvar_ = dynamic_cast<cvar::ConfigVar<T>*>(
Config::Instance().FindConfigVar(config_name));
Config::Instance().FindConfigVarByName(config_name));
}
template <typename... Args>
SettingsWidget(const T& config_ref, Args... args)
: Widget(args...), cvar_(nullptr) {
cvar_ = dynamic_cast<cvar::ConfigVar<T>*>(
Config::Instance().FindConfigVar(config_ref));
}
void UpdateValue(T val) {