diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp index 3a267129..f457953a 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.cpp @@ -67,6 +67,12 @@ void SettingsInterface::SetSettingsVariable(SettingsVariable& variable) p->variable_ = &variable; } +template +SettingsVariable* SettingsInterface::GetSettingsVariable() const +{ + return p->variable_; +} + template bool SettingsInterface::Commit() { diff --git a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp index cb177555..030b8996 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_interface.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_interface.hpp @@ -32,12 +32,19 @@ public: /** * Sets the settings variable associated with the interface. This must be - * set prior to setting any widgets. + * set prior to calling any other functions. * * @param variable Settings variable */ void SetSettingsVariable(SettingsVariable& variable); + /** + * Gets the settings variable associated with the interface. + * + * @return Settings variable + */ + SettingsVariable* GetSettingsVariable() const; + /** * Sets the current value of the associated settings variable 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 dca7005a..1dac32bd 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.cpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.cpp @@ -29,6 +29,9 @@ public: std::optional minimum_ {}; std::optional maximum_ {}; std::function validator_ {nullptr}; + + std::vector valueChangedCallbackFunctions_ {}; + std::vector valueStagedCallbackFunctions_ {}; }; template @@ -73,6 +76,15 @@ bool SettingsVariable::SetValue(const T& value) { p->value_ = value; validated = true; + + for (auto& callback : p->valueChangedCallbackFunctions_) + { + callback(p->value_); + } + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } } return validated; @@ -113,6 +125,15 @@ bool SettingsVariable::SetValueOrDefault(const T& value) p->value_ = p->default_; } + for (auto& callback : p->valueChangedCallbackFunctions_) + { + callback(p->value_); + } + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } + return validated; } @@ -120,6 +141,15 @@ template void SettingsVariable::SetValueToDefault() { p->value_ = p->default_; + + for (auto& callback : p->valueChangedCallbackFunctions_) + { + callback(p->value_); + } + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } } template @@ -133,6 +163,11 @@ void SettingsVariable::StageDefault() { p->staged_.reset(); } + + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->default_); + } } template @@ -150,7 +185,13 @@ bool SettingsVariable::StageValue(const T& value) { p->staged_.reset(); } + validated = true; + + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(value); + } } return validated; @@ -166,6 +207,15 @@ bool SettingsVariable::Commit() p->value_ = std::move(*p->staged_); p->staged_.reset(); committed = true; + + for (auto& callback : p->valueChangedCallbackFunctions_) + { + callback(p->value_); + } + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } } return committed; @@ -175,6 +225,11 @@ template void SettingsVariable::Reset() { p->staged_.reset(); + + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } } template @@ -263,6 +318,15 @@ bool SettingsVariable::ReadValue(const boost::json::object& json) p->value_ = p->default_; } + for (auto& callback : p->valueChangedCallbackFunctions_) + { + callback(p->value_); + } + for (auto& callback : p->valueStagedCallbackFunctions_) + { + callback(p->value_); + } + return validated; } @@ -272,6 +336,20 @@ void SettingsVariable::WriteValue(boost::json::object& json) const json[name()] = boost::json::value_from(p->value_); } +template +void SettingsVariable::RegisterValueChangedCallback( + ValueCallbackFunction callback) +{ + p->valueChangedCallbackFunctions_.push_back(std::move(callback)); +} + +template +void SettingsVariable::RegisterValueStagedCallback( + ValueCallbackFunction callback) +{ + p->valueStagedCallbackFunctions_.push_back(std::move(callback)); +} + 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 14940d8d..3d825189 100644 --- a/scwx-qt/source/scwx/qt/settings/settings_variable.hpp +++ b/scwx-qt/source/scwx/qt/settings/settings_variable.hpp @@ -19,6 +19,13 @@ template class SettingsVariable : public SettingsVariableBase { public: + /** + * Callback function for when a value changes. + * + * @param value New value + */ + typedef std::function ValueCallbackFunction; + explicit SettingsVariable(const std::string& name); ~SettingsVariable(); @@ -173,6 +180,24 @@ public: */ virtual void WriteValue(boost::json::object& json) const override; + /** + * Registers a function to be called when the current value changes. The + * current value is passed to the callback function. + * + * @param callback Function to be called + */ + void RegisterValueChangedCallback(ValueCallbackFunction callback); + + /** + * Registers a function to be called when the staged value changes (or a + * value is unstaged or committed). The staged value is passed to the + * callback function (or in the case of unstaging or committing, the current + * value). + * + * @param callback Function to be called + */ + void RegisterValueStagedCallback(ValueCallbackFunction callback); + 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 49620724..8126a960 100644 --- a/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp +++ b/scwx-qt/source/scwx/qt/ui/settings_dialog.cpp @@ -70,6 +70,8 @@ public: void SetupPalettesColorTablesTab(); void SetupPalettesAlertsTab(); + void UpdateRadarDialogLocation(const std::string& id); + void ApplyChanges(); void DiscardChanges(); void ResetToDefault(); @@ -146,8 +148,12 @@ void SettingsDialogImpl::ConnectSignals() } }); - // TODO: HandleMapUpdate for RadarSiteDialog, based on currently selected + // Update the Radar Site dialog "map" location with the currently selected // radar site + auto& defaultRadarSite = *defaultRadarSite_.GetSettingsVariable(); + defaultRadarSite.RegisterValueStagedCallback( + [this](const std::string& newValue) + { UpdateRadarDialogLocation(newValue); }); QObject::connect( self_->ui->buttonBox, @@ -232,6 +238,7 @@ void SettingsDialogImpl::SetupGeneralTab() }); defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox); defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton); + UpdateRadarDialogLocation(generalSettings.default_radar_site().GetValue()); fontSizes_.SetSettingsVariable(generalSettings.font_sizes()); fontSizes_.SetEditWidget(self_->ui->fontSizesLineEdit); @@ -353,6 +360,17 @@ void SettingsDialogImpl::SetupPalettesAlertsTab() } } +void SettingsDialogImpl::UpdateRadarDialogLocation(const std::string& id) +{ + std::shared_ptr radarSite = config::RadarSite::Get(id); + + if (radarSite != nullptr) + { + radarSiteDialog_->HandleMapUpdate(radarSite->latitude(), + radarSite->longitude()); + } +} + void SettingsDialogImpl::ApplyChanges() { logger_->info("Apply settings changes");