Implement discard functionality for line settings

This commit is contained in:
Dan Paulat 2024-09-22 09:54:32 -05:00
parent 6063de2095
commit 76809de2df
7 changed files with 89 additions and 7 deletions

View file

@ -4,6 +4,8 @@
#include <algorithm>
#include <boost/signals2/signal.hpp>
namespace scwx
{
namespace qt
@ -27,6 +29,8 @@ public:
subcategoryArrays_;
std::vector<SettingsCategory*> subcategories_;
std::vector<SettingsVariableBase*> variables_;
boost::signals2::signal<void()> 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<void()> callback)
{
return p->resetSignal_.connect(callback);
}
} // namespace settings
} // namespace qt
} // namespace scwx

View file

@ -6,6 +6,7 @@
#include <string>
#include <boost/json/object.hpp>
#include <boost/signals2/connection.hpp>
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<SettingsVariableBase*> variables);
void RegisterVariables(std::vector<SettingsVariableBase*> variables);
/**
* Registers a function to be called when the category is reset.
*
* @param callback Function to be called
*/
boost::signals2::connection
RegisterResetCallback(std::function<void()> callback);
private:
class Impl;
std::unique_ptr<Impl> p;

View file

@ -29,7 +29,7 @@ public:
typedef std::function<void(const T& value)> 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.

View file

@ -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

View file

@ -13,6 +13,8 @@
#include <QToolButton>
#include <QVBoxLayout>
#include <boost/signals2.hpp>
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<boost::signals2::connection> bs2Connections_ {};
boost::unordered_flat_map<awips::Phenomenon, QWidget*> 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,

View file

@ -61,6 +61,11 @@ bool SettingsPageWidget::CommitChanges()
void SettingsPageWidget::DiscardChanges()
{
for (auto& category : p->categories_)
{
category->Reset();
}
for (auto& setting : p->settings_)
{
setting->Reset();

View file

@ -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<Impl> p;