[Qt] SettingsWidget stores IConfigVar* instead of ConfigVar<T>

This commit is contained in:
Satori 2020-10-09 23:04:01 +01:00
parent 90520e57e6
commit 8768796b05
4 changed files with 85 additions and 11 deletions

View File

@ -18,7 +18,12 @@ void SettingsCheckBox::Initialize() {
return; return;
} }
setChecked(*cvar_->current_value()); auto cvar = cvar_->as<bool>();
if (!cvar) {
return;
}
setChecked(*cvar->current_value());
connect(this, &SettingsCheckBox::stateChanged, [this](int state) { connect(this, &SettingsCheckBox::stateChanged, [this](int state) {
if (state == Qt::Checked) { if (state == Qt::Checked) {

View File

@ -18,11 +18,58 @@ namespace ui {
namespace qt { namespace qt {
template <typename T> template <typename T>
class SettingsComboBox : SettingsWidget<T, XComboBox> { class SettingsComboBox : public SettingsWidget<T, XComboBox> {
static_assert(std::is_same_v<T, std::string> || std::is_same_v<T, int>, using SettingsCvar = cvar::ConfigVar<T>;
"Settings TextEdit must use std::string or int");
public:
SettingsComboBox(SettingsCvar* config_var = nullptr, QLabel* label = nullptr,
QWidget* parent = nullptr)
: SettingsWidget(config_var, label, parent) {}
void Initialize();
}; };
template <>
inline void SettingsComboBox<int>::Initialize() {
if (!cvar_) {
return;
}
if (!cvar_) {
return;
}
auto cvar = cvar_->as<int>();
if (!cvar) {
return;
}
setCurrentIndex(*cvar->current_value());
connect(this, QOverload<int>::of(&QComboBox::currentIndexChanged),
[this](int index) { UpdateValue(index); });
}
template <>
inline void SettingsComboBox<std::string>::Initialize() {
if (!cvar_) {
return;
}
if (!cvar_) {
return;
}
auto cvar = cvar_->as<std::string>();
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 qt
} // namespace ui } // namespace ui
} // namespace xe } // namespace xe

View File

@ -41,7 +41,26 @@ SettingsCheckBox* SettingsGroupBox::CreateCheckBox(const QString& text,
SettingsComboBox<int>* SettingsGroupBox::CreateComboBox(const QString& text, SettingsComboBox<int>* SettingsGroupBox::CreateComboBox(const QString& text,
cvar::ConfigVar<int>* target) { cvar::ConfigVar<int>* 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<int>();
combobox_layout->addWidget(combobox);
combobox_layout->addWidget(widget_label);
layout_->addLayout(combobox_layout);
return combobox;
} }
SettingsComboBox<std::string>* SettingsGroupBox::CreateComboBox( SettingsComboBox<std::string>* SettingsGroupBox::CreateComboBox(

View File

@ -28,7 +28,7 @@ class SettingsWidget : public Widget {
public: public:
template <typename... Args> template <typename... Args>
SettingsWidget(cvar::ConfigVar<T>* config_var, QLabel* label = nullptr, SettingsWidget(cvar::IConfigVar* config_var, QLabel* label = nullptr,
Args... args) Args... args)
: Widget(args...), cvar_(config_var), label_(label) { : Widget(args...), cvar_(config_var), label_(label) {
// default config update function // default config update function
@ -46,8 +46,8 @@ class SettingsWidget : public Widget {
} }
} }
cvar::ConfigVar<T>* config_var() const { return cvar_; } cvar::IConfigVar* config_var() const { return cvar_; }
void set_config_var(cvar::ConfigVar<T>* cvar) { cvar_ = cvar; } void set_config_var(cvar::IConfigVar* cvar) { cvar_ = cvar; }
void set_update_config_fn( void set_update_config_fn(
const std::function<void(T, cvar::ConfigVar<T>&)>& fn) { const std::function<void(T, cvar::ConfigVar<T>&)>& fn) {
@ -56,15 +56,18 @@ class SettingsWidget : public Widget {
void UpdateValue(T val) { void UpdateValue(T val) {
if (cvar_) { if (cvar_) {
update_config_fn_(val, *cvar_); auto cvar = cvar_->as<T>();
SaveToConfig(); if (cvar) {
update_config_fn_(val, *cvar);
SaveToConfig();
}
} }
} }
void SaveToConfig() { Config::Instance().SaveConfig(); } void SaveToConfig() { Config::Instance().SaveConfig(); }
protected: protected:
cvar::ConfigVar<T>* cvar_; cvar::IConfigVar* cvar_;
std::function<void(T, cvar::ConfigVar<T>&)> update_config_fn_; std::function<void(T, cvar::ConfigVar<T>&)> update_config_fn_;
QLabel* label_; QLabel* label_;
}; };