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;
}
template<class T>
SettingsVariable<T>* SettingsInterface<T>::GetSettingsVariable() const
{
return p->variable_;
}
template<class T>
bool SettingsInterface<T>::Commit()
{

View file

@ -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<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
* value.

View file

@ -29,6 +29,9 @@ public:
std::optional<T> minimum_ {};
std::optional<T> maximum_ {};
std::function<bool(const T&)> validator_ {nullptr};
std::vector<ValueCallbackFunction> valueChangedCallbackFunctions_ {};
std::vector<ValueCallbackFunction> valueStagedCallbackFunctions_ {};
};
template<class T>
@ -73,6 +76,15 @@ bool SettingsVariable<T>::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<T>::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<class T>
void SettingsVariable<T>::SetValueToDefault()
{
p->value_ = p->default_;
for (auto& callback : p->valueChangedCallbackFunctions_)
{
callback(p->value_);
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
}
template<class T>
@ -133,6 +163,11 @@ void SettingsVariable<T>::StageDefault()
{
p->staged_.reset();
}
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->default_);
}
}
template<class T>
@ -150,7 +185,13 @@ bool SettingsVariable<T>::StageValue(const T& value)
{
p->staged_.reset();
}
validated = true;
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(value);
}
}
return validated;
@ -166,6 +207,15 @@ bool SettingsVariable<T>::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<class T>
void SettingsVariable<T>::Reset()
{
p->staged_.reset();
for (auto& callback : p->valueStagedCallbackFunctions_)
{
callback(p->value_);
}
}
template<class T>
@ -263,6 +318,15 @@ bool SettingsVariable<T>::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<T>::WriteValue(boost::json::object& json) const
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>
bool SettingsVariable<T>::Equals(const SettingsVariableBase& o) const
{

View file

@ -19,6 +19,13 @@ template<class T>
class SettingsVariable : public SettingsVariableBase
{
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);
~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;

View file

@ -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<config::RadarSite> radarSite = config::RadarSite::Get(id);
if (radarSite != nullptr)
{
radarSiteDialog_->HandleMapUpdate(radarSite->latitude(),
radarSite->longitude());
}
}
void SettingsDialogImpl::ApplyChanges()
{
logger_->info("Apply settings changes");