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