From ce5092c068b1e180a6bce0adeb413974bb122ce1 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Tue, 20 Dec 2022 00:38:16 -0600 Subject: [PATCH] Adding common edit widget and reset button processing to settings variable --- .../scwx/qt/settings/settings_variable.cpp | 130 +++++++++++++++++- .../scwx/qt/settings/settings_variable.hpp | 22 +++ scwx-qt/source/scwx/qt/ui/settings_dialog.cpp | 27 +++- 3 files changed, 169 insertions(+), 10 deletions(-) diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp index b1688684..b09be78b 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp @@ -7,6 +7,10 @@ #include #include +#include +#include +#include +#include namespace scwx { @@ -22,16 +26,28 @@ template class SettingsVariable::Impl { public: - explicit Impl() {} + explicit Impl() + { + context_->moveToThread(QCoreApplication::instance()->thread()); + } ~Impl() {} + void UpdateEditWidget(); + void UpdateResetButton(); + T value_ {}; T default_ {}; std::optional staged_ {}; std::optional minimum_ {}; std::optional maximum_ {}; std::function validator_ {nullptr}; + + bool stagedValid_ {true}; + + std::unique_ptr context_ {std::make_unique()}; + QWidget* editWidget_ {nullptr}; + QAbstractButton* resetButton_ {nullptr}; }; template @@ -128,15 +144,19 @@ void SettingsVariable::SetValueToDefault() template bool SettingsVariable::StageValue(const T& value) { - bool validated = false; - if (Validate(value)) { - p->staged_ = value; - validated = true; + p->staged_ = value; + p->stagedValid_ = true; + } + else + { + p->stagedValid_ = false; } - return validated; + p->UpdateResetButton(); + + return p->stagedValid_; } template @@ -146,9 +166,20 @@ void SettingsVariable::Commit() { p->value_ = std::move(*p->staged_); p->staged_.reset(); + p->stagedValid_ = true; } } +template +void SettingsVariable::Reset() +{ + p->staged_.reset(); + p->stagedValid_ = true; + + p->UpdateEditWidget(); + p->UpdateResetButton(); +} + template T SettingsVariable::GetDefault() const { @@ -226,6 +257,93 @@ void SettingsVariable::WriteValue(boost::json::object& json) const json[name()] = boost::json::value_from(p->value_); } +template +void SettingsVariable::SetEditWidget(QWidget* widget) +{ + p->editWidget_ = widget; + + if (QLineEdit* lineEdit = dynamic_cast(widget)) + { + if constexpr (std::is_same_v) + { + // If the line is edited (not programatically changed), stage the new + // value + QObject::connect(lineEdit, + &QLineEdit::textEdited, + p->context_.get(), + [this](const QString& text) + { + // Attempt to stage the value + StageValue(text.toStdString()); + }); + } + } + + p->UpdateEditWidget(); +} + +template +void SettingsVariable::SetResetButton(QAbstractButton* button) +{ + p->resetButton_ = button; + + QObject::connect(p->resetButton_, + &QAbstractButton::clicked, + p->context_.get(), + [this]() + { + if (p->value_ == p->default_) + { + // If the current value is default, reset the staged + // value + Reset(); + } + else + { + // Stage the default value + StageValue(p->default_); + p->UpdateEditWidget(); + } + }); + + p->UpdateResetButton(); +} + +template +void SettingsVariable::Impl::UpdateEditWidget() +{ + // Use the staged value if present, otherwise the current value + T& value = staged_.has_value() ? *staged_ : value_; + + if (QLineEdit* lineEdit = dynamic_cast(editWidget_)) + { + if constexpr (std::is_integral_v) + { + lineEdit->setText(QString::number(value)); + } + else if constexpr (std::is_same_v) + { + lineEdit->setText(QString::fromStdString(value)); + } + } +} + +template +void SettingsVariable::Impl::UpdateResetButton() +{ + if (resetButton_ != nullptr) + { + if (staged_.has_value()) + { + resetButton_->setVisible(!stagedValid_ || *staged_ != default_); + } + else + { + resetButton_->setVisible(value_ != default_); + } + } +} + template bool SettingsVariable::Equals(const SettingsVariableBase& o) const { diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp index 5bb258ed..6c49d865 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp @@ -4,6 +4,9 @@ #include +class QAbstractButton; +class QWidget; + namespace scwx { namespace qt @@ -72,6 +75,11 @@ public: */ void Commit(); + /** + * Clears the staged value of the settings variable. + */ + void Reset(); + /** * Validate the value against the defined parameters of the settings * variable. @@ -135,6 +143,20 @@ public: */ virtual void WriteValue(boost::json::object& json) const override; + /** + * Sets the edit widget from the settings dialog. + * + * @param widget Edit widget + */ + void SetEditWidget(QWidget* widget); + + /** + * Sets the reset button from the settings dialog. + * + * @param button Reset button + */ + void SetResetButton(QAbstractButton* button); + protected: virtual bool Equals(const SettingsVariableBase& o) const override; diff --git a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp index 1de9f997..359bd0b2 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp @@ -2,6 +2,7 @@ #include "ui_settings_dialog.h" #include +#include #include @@ -80,10 +81,28 @@ SettingsDialog::~SettingsDialog() void SettingsDialogImpl::SetupGeneralTab() { - self_->ui->resetRadarSiteButton->setVisible(false); - self_->ui->resetGridWidthButton->setVisible(false); - self_->ui->resetGridHeightButton->setVisible(false); - self_->ui->resetMapboxApiKeyButton->setVisible(false); + settings::GeneralSettings& generalSettings = + manager::SettingsManager::general_settings(); + + generalSettings.default_radar_site().SetEditWidget( + self_->ui->radarSiteComboBox); + generalSettings.default_radar_site().SetResetButton( + self_->ui->resetRadarSiteButton); + + generalSettings.grid_width().SetEditWidget(self_->ui->gridWidthSpinBox); + generalSettings.grid_width().SetResetButton(self_->ui->resetGridWidthButton); + + generalSettings.grid_height().SetEditWidget(self_->ui->gridHeightSpinBox); + generalSettings.grid_height().SetResetButton( + self_->ui->resetGridHeightButton); + + generalSettings.mapbox_api_key().SetEditWidget( + self_->ui->mapboxApiKeyLineEdit); + generalSettings.mapbox_api_key().SetResetButton( + self_->ui->resetMapboxApiKeyButton); + + generalSettings.debug_enabled().SetEditWidget( + self_->ui->debugEnabledCheckBox); } void SettingsDialogImpl::SetupPalettesColorTablesTab()