From a1d9b25f0bbc0b517188801c5a20fcf8a97d4a40 Mon Sep 17 00:00:00 2001 From: Dan Paulat Date: Sun, 29 Sep 2024 08:19:51 -0500 Subject: [PATCH] Add alert palette reset buttons --- .../scwx/qt/settings/settings_category.cpp | 89 +++++++++++++++++++ .../scwx/qt/settings/settings_category.hpp | 23 +++++ .../scwx/qt/settings/settings_variable.cpp | 12 +++ .../scwx/qt/settings/settings_variable.hpp | 18 ++++ .../qt/settings/settings_variable_base.hpp | 18 ++++ .../alert_palette_settings_widget.cpp | 17 ++++ 6 files changed, 177 insertions(+) diff --git a/scwx-qt/source/scwx/qt/settings/settings_category.cpp b/scwx-qt/source/scwx/qt/settings/settings_category.cpp index 3714c4ca..75a46bf8 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_category.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_category.cpp @@ -48,6 +48,62 @@ SettingsCategory::SettingsCategory(SettingsCategory&&) noexcept = default; SettingsCategory& SettingsCategory::operator=(SettingsCategory&&) noexcept = default; +bool SettingsCategory::IsDefault() const +{ + bool isDefault = true; + + // Get subcategory array defaults + for (auto& subcategoryArray : p->subcategoryArrays_) + { + for (auto& subcategory : subcategoryArray.second) + { + isDefault = isDefault && subcategory->IsDefault(); + } + } + + // Get subcategory defaults + for (auto& subcategory : p->subcategories_) + { + isDefault = isDefault && subcategory->IsDefault(); + } + + // Get variable defaults + for (auto& variable : p->variables_) + { + isDefault = isDefault && variable->IsDefault(); + } + + return isDefault; +} + +bool SettingsCategory::IsDefaultStaged() const +{ + bool isDefaultStaged = true; + + // Get subcategory array defaults + for (auto& subcategoryArray : p->subcategoryArrays_) + { + for (auto& subcategory : subcategoryArray.second) + { + isDefaultStaged = isDefaultStaged && subcategory->IsDefaultStaged(); + } + } + + // Get subcategory defaults + for (auto& subcategory : p->subcategories_) + { + isDefaultStaged = isDefaultStaged && subcategory->IsDefaultStaged(); + } + + // Get variable defaults + for (auto& variable : p->variables_) + { + isDefaultStaged = isDefaultStaged && variable->IsDefaultStaged(); + } + + return isDefaultStaged; +} + std::string SettingsCategory::name() const { return p->name_; @@ -102,6 +158,39 @@ void SettingsCategory::SetDefaults() p->stagedSignal_(); } +void SettingsCategory::StageDefaults() +{ + // Don't allow individual variables to invoke the signal when operating over + // the entire category + p->blockSignals_ = true; + + // Stage subcategory array defaults + for (auto& subcategoryArray : p->subcategoryArrays_) + { + for (auto& subcategory : subcategoryArray.second) + { + subcategory->StageDefaults(); + } + } + + // Stage subcategory defaults + for (auto& subcategory : p->subcategories_) + { + subcategory->StageDefaults(); + } + + // Stage variable defaults + for (auto& variable : p->variables_) + { + variable->StageDefault(); + } + + // Unblock signals + p->blockSignals_ = false; + + p->stagedSignal_(); +} + bool SettingsCategory::Commit() { bool committed = false; diff --git a/scwx-qt/source/scwx/qt/settings/settings_category.hpp b/scwx-qt/source/scwx/qt/settings/settings_category.hpp index ee80ba46..167af06a 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_category.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_category.hpp @@ -43,11 +43,34 @@ public: */ boost::signals2::signal& staged_signal(); + /** + * Gets whether or not all settings variables are currently set to default + * values. + * + * @return true if all settings variables are currently set to default + * values, otherwise false. + */ + bool IsDefault() const; + + /** + * Gets whether or not all settings variables currently have staged values + * set to default. + * + * @return true if all settings variables currently have staged values set + * to default, otherwise false. + */ + bool IsDefaultStaged() const; + /** * Set all variables to their defaults. */ void SetDefaults(); + /** + * Stage all variables to their defaults. + */ + void StageDefaults(); + /** * Sets the current value of all variables to the staged value. * diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp index c4a5f6c9..a5387937 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp @@ -65,6 +65,18 @@ inline auto FormatParameter(const T& value) } } +template +bool SettingsVariable::IsDefault() const +{ + return p->value_ == p->default_; +} + +template +bool SettingsVariable::IsDefaultStaged() const +{ + return p->staged_.value_or(p->value_) == p->default_; +} + template T SettingsVariable::GetValue() const { diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp index d2e6c949..df9184a1 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp @@ -37,6 +37,24 @@ public: SettingsVariable(SettingsVariable&&) noexcept; SettingsVariable& operator=(SettingsVariable&&) noexcept; + /** + * Gets whether or not the settings variable is currently set to its default + * value. + * + * @return true if the settings variable is currently set to its default + * value, otherwise false. + */ + bool IsDefault() const; + + /** + * Gets whether or not the settings variable currently has its staged value + * set to default. + * + * @return true if the settings variable currently has its staged value set + * to default, otherwise false. + */ + bool IsDefaultStaged() const; + /** * Gets the current value of the settings variable. * diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp b/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp index fba1eff9..f4e48934 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp @@ -45,6 +45,24 @@ public: */ boost::signals2::signal& staged_signal(); + /** + * Gets whether or not the settings variable is currently set to its default + * value. + * + * @return true if the settings variable is currently set to its default + * value, otherwise false. + */ + virtual bool IsDefault() const = 0; + + /** + * Gets whether or not the settings variable currently has its staged value + * set to default. + * + * @return true if the settings variable currently has its staged value set + * to default, otherwise false. + */ + virtual bool IsDefaultStaged() const = 0; + /** * Sets the current value of the settings variable to default. */ diff --git a/scwx-qt/source/scwx/qt/ui/settings/alert_palette_settings_widget.cpp b/scwx-qt/source/scwx/qt/ui/settings/alert_palette_settings_widget.cpp index 5d974225..a3ae4642 100644 --- a/scwx-qt/source/scwx/qt/ui/settings/alert_palette_settings_widget.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings/alert_palette_settings_widget.cpp @@ -57,6 +57,8 @@ public: settings::LineSettings* activeLineSettings_ {nullptr}; boost::unordered_flat_map phenomenonPages_ {}; + + std::vector connections_ {}; }; AlertPaletteSettingsWidget::AlertPaletteSettingsWidget(QWidget* parent) : @@ -243,9 +245,15 @@ void AlertPaletteSettingsWidget::Impl::AddPhenomenonLine( LineLabel* lineLabel = new LineLabel(self_); lineLabel->set_line_settings(lineSettings); + QToolButton* resetButton = new QToolButton(self_); + resetButton->setIcon( + QIcon {":/res/icons/font-awesome-6/rotate-left-solid.svg"}); + resetButton->setVisible(!lineSettings.IsDefaultStaged()); + layout->addWidget(new QLabel(tr(name.c_str()), self_), row, 0); layout->addWidget(lineLabel, row, 1); layout->addWidget(toolButton, row, 2); + layout->addWidget(resetButton, row, 3); self_->AddSettingsCategory(&lineSettings); @@ -268,6 +276,15 @@ void AlertPaletteSettingsWidget::Impl::AddPhenomenonLine( // Show the dialog editLineDialog_->show(); }); + + connect(resetButton, + &QAbstractButton::clicked, + self_, + [&lineSettings]() { lineSettings.StageDefaults(); }); + + connections_.emplace_back(lineSettings.staged_signal().connect( + [resetButton, &lineSettings]() + { resetButton->setVisible(!lineSettings.IsDefaultStaged()); })); } } // namespace ui