diff --git a/scwx-qt/source/scwx/qt/settings/settings_category.cpp b/scwx-qt/source/scwx/qt/settings/settings_category.cpp index 34859ae2..29b3a022 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_category.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_category.cpp @@ -4,6 +4,8 @@ #include +#include + namespace scwx { namespace qt @@ -27,6 +29,8 @@ public: subcategoryArrays_; std::vector subcategories_; std::vector variables_; + + boost::signals2::signal resetSignal_; }; SettingsCategory::SettingsCategory(const std::string& name) : @@ -96,6 +100,32 @@ bool SettingsCategory::Commit() return committed; } +void SettingsCategory::Reset() +{ + // Reset subcategory arrays + for (auto& subcategoryArray : p->subcategoryArrays_) + { + for (auto& subcategory : subcategoryArray.second) + { + subcategory->Reset(); + } + } + + // Reset subcategories + for (auto& subcategory : p->subcategories_) + { + subcategory->Reset(); + } + + // Reset variables + for (auto& variable : p->variables_) + { + variable->Reset(); + } + + p->resetSignal_(); +} + bool SettingsCategory::ReadJson(const boost::json::object& json) { bool validated = true; @@ -252,6 +282,12 @@ void SettingsCategory::RegisterVariables( p->variables_.end(), variables.cbegin(), variables.cend()); } +boost::signals2::connection +SettingsCategory::RegisterResetCallback(std::function callback) +{ + return p->resetSignal_.connect(callback); +} + } // namespace settings } // namespace qt } // namespace scwx diff --git a/scwx-qt/source/scwx/qt/settings/settings_category.hpp b/scwx-qt/source/scwx/qt/settings/settings_category.hpp index 9ccf7458..9b5613be 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_category.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_category.hpp @@ -6,6 +6,7 @@ #include #include +#include namespace scwx { @@ -34,14 +35,18 @@ public: void SetDefaults(); /** - * Sets the current value of all variables to the staged - * value. + * Sets the current value of all variables to the staged value. * * @return true if any staged value was committed, false if no staged values * are present. */ bool Commit(); + /** + * Clears the staged value of all variables. + */ + void Reset(); + /** * Reads the variables from the JSON object. * @@ -68,6 +73,14 @@ public: RegisterVariables(std::initializer_list variables); void RegisterVariables(std::vector variables); + /** + * Registers a function to be called when the category is reset. + * + * @param callback Function to be called + */ + boost::signals2::connection + RegisterResetCallback(std::function callback); + private: class Impl; std::unique_ptr p; diff --git a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp index 72d61dde..d2e6c949 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp @@ -29,7 +29,7 @@ public: typedef std::function ValueCallbackFunction; explicit SettingsVariable(const std::string& name); - ~SettingsVariable(); + virtual ~SettingsVariable(); SettingsVariable(const SettingsVariable&) = delete; SettingsVariable& operator=(const SettingsVariable&) = delete; @@ -96,7 +96,7 @@ public: /** * Clears the staged value of the settings variable. */ - void Reset(); + void Reset() override; /** * Gets the staged value of the settings variable, if defined. 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 f0444f45..d7211197 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable_base.hpp @@ -19,7 +19,7 @@ class SettingsVariableBase { protected: explicit SettingsVariableBase(const std::string& name); - ~SettingsVariableBase(); + virtual ~SettingsVariableBase(); public: SettingsVariableBase(const SettingsVariableBase&) = delete; @@ -48,6 +48,11 @@ public: */ virtual bool Commit() = 0; + /** + * Clears the staged value of the settings variable. + */ + virtual void Reset() = 0; + /** * Reads the value from the JSON object. If the read value is out of range, * the value is set to the minimum or maximum. If the read value fails 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 2de6f283..8e77bdb6 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 @@ -13,6 +13,8 @@ #include #include +#include + namespace scwx { namespace qt @@ -35,7 +37,13 @@ public: SetupUi(); ConnectSignals(); } - ~Impl() = default; + ~Impl() + { + for (auto& c : bs2Connections_) + { + c.disconnect(); + } + }; void AddPhenomenonLine(const std::string& name, settings::LineSettings& lineSettings, @@ -54,6 +62,8 @@ public: EditLineDialog* editLineDialog_; LineLabel* activeLineLabel_ {nullptr}; + std::vector bs2Connections_ {}; + boost::unordered_flat_map phenomenonPages_ {}; }; @@ -250,6 +260,11 @@ void AlertPaletteSettingsWidget::Impl::AddPhenomenonLine( self_->AddSettingsCategory(&lineSettings); + boost::signals2::connection c = lineSettings.RegisterResetCallback( + [lineLabel, &lineSettings]() + { lineLabel->set_line_settings(lineSettings); }); + bs2Connections_.push_back(c); + connect( toolButton, &QAbstractButton::clicked, diff --git a/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.cpp b/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.cpp index a7c77380..41c43817 100644 --- a/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.cpp @@ -61,6 +61,11 @@ bool SettingsPageWidget::CommitChanges() void SettingsPageWidget::DiscardChanges() { + for (auto& category : p->categories_) + { + category->Reset(); + } + for (auto& setting : p->settings_) { setting->Reset(); diff --git a/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.hpp b/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.hpp index 2fbdfc9e..39d8647a 100644 --- a/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.hpp +++ b/scwx-qt/source/scwx/qt/ui/settings/settings_page_widget.hpp @@ -60,9 +60,17 @@ public: void ResetToDefault(); protected: - void AddSettingsCategory(settings::SettingsCategory* category); void AddSettingsInterface(settings::SettingsInterfaceBase* setting); + /** + * Commits and resets all settings within a category upon page commit or + * reset. The use of SettingsInterface is preferred, as it allows the binding + * of widgets to these actions. + * + * @param [in] category Settings category + */ + void AddSettingsCategory(settings::SettingsCategory* category); + private: class Impl; std::shared_ptr p;