Add callbacks to settings variables, use to update radar dialog location in settings dialog

This commit is contained in:
Dan Paulat 2022-12-22 00:56:52 -06:00
parent 87f611e026
commit f7cc902eef
5 changed files with 136 additions and 2 deletions

View file

@ -67,6 +67,12 @@ void SettingsInterface<T>::SetSettingsVariable(SettingsVariable<T>& variable)
p->variable_ = &variable; p->variable_ = &variable;
} }
template<class T>
SettingsVariable<T>* SettingsInterface<T>::GetSettingsVariable() const
{
return p->variable_;
}
template<class T> template<class T>
bool SettingsInterface<T>::Commit() bool SettingsInterface<T>::Commit()
{ {

View file

@ -32,12 +32,19 @@ public:
/** /**
* Sets the settings variable associated with the interface. This must be * 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 * @param variable Settings variable
*/ */
void SetSettingsVariable(SettingsVariable<T>& variable); void SetSettingsVariable(SettingsVariable<T>& variable);
/**
* Gets the settings variable associated with the interface.
*
* @return Settings variable
*/
SettingsVariable<T>* GetSettingsVariable() const;
/** /**
* Sets the current value of the associated settings variable to the staged * Sets the current value of the associated settings variable to the staged
* value. * value.

View file

@ -29,6 +29,9 @@ public:
std::optional<T> minimum_ {}; std::optional<T> minimum_ {};
std::optional<T> maximum_ {}; std::optional<T> maximum_ {};
std::function<bool(const T&)> validator_ {nullptr}; std::function<bool(const T&)> validator_ {nullptr};
std::vector<ValueCallbackFunction> valueChangedCallbackFunctions_ {};
std::vector<ValueCallbackFunction> valueStagedCallbackFunctions_ {};
}; };
template<class T> template<class T>
@ -73,6 +76,15 @@ bool SettingsVariable<T>::SetValue(const T& value)
{ {
p->value_ = value; p->value_ = value;
validated = true; validated = true;
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
} }
return validated; return validated;
@ -113,6 +125,15 @@ bool SettingsVariable<T>::SetValueOrDefault(const T& value)
p->value_ = p->default_; p->value_ = p->default_;
} }
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
return validated; return validated;
} }
@ -120,6 +141,15 @@ template<class T>
void SettingsVariable<T>::SetValueToDefault() void SettingsVariable<T>::SetValueToDefault()
{ {
p->value_ = p->default_; p->value_ = p->default_;
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
} }
template<class T> template<class T>
@ -133,6 +163,11 @@ void SettingsVariable<T>::StageDefault()
{ {
p->staged_.reset(); p->staged_.reset();
} }
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->default_);
}
} }
template<class T> template<class T>
@ -150,7 +185,13 @@ bool SettingsVariable<T>::StageValue(const T& value)
{ {
p->staged_.reset(); p->staged_.reset();
} }
validated = true; validated = true;
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(value);
}
} }
return validated; return validated;
@ -166,6 +207,15 @@ bool SettingsVariable<T>::Commit()
p->value_ = std::move(*p->staged_); p->value_ = std::move(*p->staged_);
p->staged_.reset(); p->staged_.reset();
committed = true; committed = true;
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
} }
return committed; return committed;
@ -175,6 +225,11 @@ template<class T>
void SettingsVariable<T>::Reset() void SettingsVariable<T>::Reset()
{ {
p->staged_.reset(); p->staged_.reset();
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
} }
template<class T> template<class T>
@ -263,6 +318,15 @@ bool SettingsVariable<T>::ReadValue(const boost::json::object& json)
p->value_ = p->default_; p->value_ = p->default_;
} }
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
return validated; return validated;
} }
@ -272,6 +336,20 @@ void SettingsVariable<T>::WriteValue(boost::json::object& json) const
json[name()] = boost::json::value_from<T&>(p->value_); json[name()] = boost::json::value_from<T&>(p->value_);
} }
template<class T>
void SettingsVariable<T>::RegisterValueChangedCallback(
ValueCallbackFunction callback)
{
p->valueChangedCallbackFunctions_.push_back(std::move(callback));
}
template<class T>
void SettingsVariable<T>::RegisterValueStagedCallback(
ValueCallbackFunction callback)
{
p->valueStagedCallbackFunctions_.push_back(std::move(callback));
}
template<class T> template<class T>
bool SettingsVariable<T>::Equals(const SettingsVariableBase& o) const bool SettingsVariable<T>::Equals(const SettingsVariableBase& o) const
{ {

View file

@ -19,6 +19,13 @@ template<class T>
class SettingsVariable : public SettingsVariableBase class SettingsVariable : public SettingsVariableBase
{ {
public: public:
/**
* Callback function for when a value changes.
*
* @param value New value
*/
typedef std::function<void(const T& value)> ValueCallbackFunction;
explicit SettingsVariable(const std::string& name); explicit SettingsVariable(const std::string& name);
~SettingsVariable(); ~SettingsVariable();
@ -173,6 +180,24 @@ public:
*/ */
virtual void WriteValue(boost::json::object& json) const override; 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: protected:
virtual bool Equals(const SettingsVariableBase& o) const override; virtual bool Equals(const SettingsVariableBase& o) const override;

View file

@ -70,6 +70,8 @@ public:
void SetupPalettesColorTablesTab(); void SetupPalettesColorTablesTab();
void SetupPalettesAlertsTab(); void SetupPalettesAlertsTab();
void UpdateRadarDialogLocation(const std::string& id);
void ApplyChanges(); void ApplyChanges();
void DiscardChanges(); void DiscardChanges();
void ResetToDefault(); 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 // radar site
auto& defaultRadarSite = *defaultRadarSite_.GetSettingsVariable();
defaultRadarSite.RegisterValueStagedCallback(
[this](const std::string& newValue)
{ UpdateRadarDialogLocation(newValue); });
QObject::connect( QObject::connect(
self_->ui->buttonBox, self_->ui->buttonBox,
@ -232,6 +238,7 @@ void SettingsDialogImpl::SetupGeneralTab()
}); });
defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox); defaultRadarSite_.SetEditWidget(self_->ui->radarSiteComboBox);
defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton); defaultRadarSite_.SetResetButton(self_->ui->resetRadarSiteButton);
UpdateRadarDialogLocation(generalSettings.default_radar_site().GetValue());
fontSizes_.SetSettingsVariable(generalSettings.font_sizes()); fontSizes_.SetSettingsVariable(generalSettings.font_sizes());
fontSizes_.SetEditWidget(self_->ui->fontSizesLineEdit); fontSizes_.SetEditWidget(self_->ui->fontSizesLineEdit);
@ -353,6 +360,17 @@ void SettingsDialogImpl::SetupPalettesAlertsTab()
} }
} }
void SettingsDialogImpl::UpdateRadarDialogLocation(const std::string& id)
{
std::shared_ptr<config::RadarSite> radarSite = config::RadarSite::Get(id);
if (radarSite != nullptr)
{
radarSiteDialog_->HandleMapUpdate(radarSite->latitude(),
radarSite->longitude());
}
}
void SettingsDialogImpl::ApplyChanges() void SettingsDialogImpl::ApplyChanges()
{ {
logger_->info("Apply settings changes"); logger_->info("Apply settings changes");