shared_widget: Internalize component restoring

This commit is contained in:
lat9nq 2023-06-21 04:04:48 -04:00
parent 21723879e7
commit d1de1c3bed
3 changed files with 50 additions and 62 deletions

View File

@ -52,20 +52,6 @@ ConfigureSystem::ConfigureSystem(
Setup(builder);
connect(rng_seed_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
rng_seed_edit->setEnabled(state == Qt::Checked);
if (state != Qt::Checked) {
rng_seed_edit->setText(QStringLiteral("00000000"));
}
});
connect(custom_rtc_checkbox, &QCheckBox::stateChanged, this, [this](int state) {
custom_rtc_edit->setEnabled(state == Qt::Checked);
if (state != Qt::Checked) {
custom_rtc_edit->setDateTime(QDateTime::currentDateTime());
}
});
const auto locale_check = [this]() {
const auto region_index = combo_region->currentIndex();
const auto language_index = combo_language->currentIndex();
@ -149,19 +135,7 @@ void ConfigureSystem::Setup(const ConfigurationShared::Builder& builder) {
continue;
}
if (setting->Id() == Settings::values.rng_seed.Id()) {
// Keep track of rng_seed's widgets to reset it with the checkbox state
rng_seed_checkbox = widget->checkbox;
rng_seed_edit = widget->line_edit;
rng_seed_edit->setEnabled(Settings::values.rng_seed_enabled.GetValue());
} else if (setting->Id() == Settings::values.custom_rtc.Id()) {
// Keep track of custom_rtc's widgets to reset it with the checkbox state
custom_rtc_checkbox = widget->checkbox;
custom_rtc_edit = widget->date_time_edit;
custom_rtc_edit->setEnabled(Settings::values.custom_rtc_enabled.GetValue());
} else if (setting->Id() == Settings::values.region_index.Id()) {
if (setting->Id() == Settings::values.region_index.Id()) {
// Keep track of the region_index (and langauge_index) combobox to validate the selected
// settings
combo_region = widget->combobox;

View File

@ -50,10 +50,6 @@ private:
Core::System& system;
QCheckBox* rng_seed_checkbox;
QLineEdit* rng_seed_edit;
QCheckBox* custom_rtc_checkbox;
QDateTimeEdit* custom_rtc_edit;
QComboBox* combo_region;
QComboBox* combo_language;
};

View File

@ -46,6 +46,10 @@ namespace ConfigurationShared {
static int restore_button_count = 0;
static std::string RelevantDefault(const Settings::BasicSetting& setting) {
return Settings::IsConfiguringGlobal() ? setting.DefaultToString() : setting.ToStringGlobal();
}
QPushButton* Widget::CreateRestoreGlobalButton(bool using_global, QWidget* parent) {
restore_button_count++;
@ -92,12 +96,12 @@ QWidget* Widget::CreateCheckBox(Settings::BasicSetting* bool_setting, const QStr
return checkbox->checkState() == Qt::CheckState::Checked ? "true" : "false";
};
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this, bool_setting]() {
checkbox->setCheckState(bool_setting->ToStringGlobal() == "true" ? Qt::Checked
: Qt::Unchecked);
};
restore_func = [this, bool_setting]() {
checkbox->setCheckState(RelevantDefault(*bool_setting) == "true" ? Qt::Checked
: Qt::Unchecked);
};
if (!Settings::IsConfiguringGlobal()) {
QObject::connect(checkbox, &QCheckBox::clicked, [touch]() { touch(); });
}
@ -139,12 +143,12 @@ QWidget* Widget::CreateCombobox(std::function<std::string()>& serializer,
return std::to_string(enumeration->at(current).first);
};
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this, find_index]() {
const u32 global_value = std::stoi(setting.ToStringGlobal());
combobox->setCurrentIndex(find_index(global_value));
};
restore_func = [this, find_index]() {
const u32 global_value = std::stoi(RelevantDefault(setting));
combobox->setCurrentIndex(find_index(global_value));
};
if (!Settings::IsConfiguringGlobal()) {
QObject::connect(combobox, QOverload<int>::of(&QComboBox::activated),
[touch]() { touch(); });
}
@ -165,11 +169,11 @@ QWidget* Widget::CreateLineEdit(std::function<std::string()>& serializer,
return line_edit;
}
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this]() {
line_edit->setText(QString::fromStdString(setting.ToStringGlobal()));
};
restore_func = [this]() {
line_edit->setText(QString::fromStdString(RelevantDefault(setting)));
};
if (!Settings::IsConfiguringGlobal()) {
QObject::connect(line_edit, &QLineEdit::textChanged, [touch]() { touch(); });
}
@ -215,10 +219,9 @@ QWidget* Widget::CreateSlider(bool reversed, float multiplier, const QString& fo
slider->setInvertedAppearance(reversed);
serializer = [this]() { return std::to_string(slider->value()); };
restore_func = [this]() { slider->setValue(std::stoi(RelevantDefault(setting))); };
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this]() { slider->setValue(std::stoi(setting.ToStringGlobal())); };
QObject::connect(slider, &QAbstractSlider::actionTriggered, [touch]() { touch(); });
}
@ -242,9 +245,12 @@ QWidget* Widget::CreateSpinBox(const QString& suffix, std::function<std::string(
serializer = [this]() { return std::to_string(spinbox->value()); };
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this]() { spinbox->setValue(std::stoi(setting.ToStringGlobal())); };
restore_func = [this]() {
auto value{std::stol(RelevantDefault(setting))};
spinbox->setValue(value);
};
if (!Settings::IsConfiguringGlobal()) {
QObject::connect(spinbox, QOverload<int>::of(&QSpinBox::valueChanged), [this, touch]() {
if (spinbox->value() != std::stoi(setting.ToStringGlobal())) {
touch();
@ -264,7 +270,7 @@ QWidget* Widget::CreateHexEdit(std::function<std::string()>& serializer,
}
auto to_hex = [=](const std::string& input) {
return QString::fromStdString(fmt::format("{:08x}", std::stoi(input)));
return QString::fromStdString(fmt::format("{:08x}", std::stoul(input)));
};
QRegExpValidator* regex =
@ -282,8 +288,9 @@ QWidget* Widget::CreateHexEdit(std::function<std::string()>& serializer,
serializer = [hex_to_dec]() { return hex_to_dec(); };
restore_func = [this, to_hex]() { line_edit->setText(to_hex(RelevantDefault(setting))); };
if (!Settings::IsConfiguringGlobal()) {
restore_func = [this, to_hex]() { line_edit->setText(to_hex(setting.ToStringGlobal())); };
QObject::connect(line_edit, &QLineEdit::textChanged, [touch]() { touch(); });
}
@ -306,18 +313,18 @@ QWidget* Widget::CreateDateTimeEdit(bool disabled, bool restrict,
serializer = [this]() { return std::to_string(date_time_edit->dateTime().toSecsSinceEpoch()); };
auto get_clear_val = [this, restrict, current_time]() {
return QDateTime::fromSecsSinceEpoch([this, restrict, current_time]() {
if (restrict && checkbox->checkState() == Qt::Checked) {
return std::stoll(RelevantDefault(setting));
}
return current_time;
}());
};
restore_func = [this, get_clear_val]() { date_time_edit->setDateTime(get_clear_val()); };
if (!Settings::IsConfiguringGlobal()) {
auto get_clear_val = [this, restrict, current_time]() {
return QDateTime::fromSecsSinceEpoch([this, restrict, current_time]() {
if (restrict && checkbox->checkState() == Qt::Checked) {
return std::stoll(setting.ToStringGlobal());
}
return current_time;
}());
};
restore_func = [this, get_clear_val]() { date_time_edit->setDateTime(get_clear_val()); };
QObject::connect(date_time_edit, &QDateTimeEdit::editingFinished,
[this, get_clear_val, touch]() {
if (date_time_edit->dateTime() != get_clear_val()) {
@ -493,6 +500,17 @@ void Widget::SetupComponent(const QString& label, std::function<void()>& load_fu
}
};
}
if (other_setting != nullptr) {
const auto reset = [restore_func, data_component](int state) {
data_component->setEnabled(state == Qt::Checked);
if (state != Qt::Checked) {
restore_func();
}
};
connect(checkbox, &QCheckBox::stateChanged, reset);
reset(checkbox->checkState());
}
}
bool Widget::Valid() const {