summaryrefslogtreecommitdiffhomepage
path: root/options
diff options
context:
space:
mode:
Diffstat (limited to 'options')
-rw-r--r--options/bundle.cpp5
-rw-r--r--options/bundle.hpp10
-rw-r--r--options/connector.cpp16
-rw-r--r--options/connector.hpp27
-rw-r--r--options/value.cpp8
-rw-r--r--options/value.hpp6
6 files changed, 55 insertions, 17 deletions
diff --git a/options/bundle.cpp b/options/bundle.cpp
index 1eb69b60..0bfeef3b 100644
--- a/options/bundle.cpp
+++ b/options/bundle.cpp
@@ -49,6 +49,11 @@ void bundle::reload(std::shared_ptr<QSettings> settings)
}
}
+void bundle::set_all_to_default()
+{
+ forall([](const QString&, base_value* val) { set_base_value_to_default(val); });
+}
+
void bundle::store_kv(const QString& name, const QVariant& datum)
{
QMutexLocker l(&mtx);
diff --git a/options/bundle.hpp b/options/bundle.hpp
index a7897051..587b8a6b 100644
--- a/options/bundle.hpp
+++ b/options/bundle.hpp
@@ -32,6 +32,8 @@ namespace options {
namespace detail {
+void set_base_value_to_default(base_value* val);
+
struct bundler;
class OPENTRACK_OPTIONS_EXPORT bundle final : public QObject, public connector
@@ -66,11 +68,8 @@ public:
bundle(const QString& group_name);
~bundle() override;
QString name() const { return group_name; }
- void reload(std::shared_ptr<QSettings> settings = group::ini_file());
void store_kv(const QString& name, const QVariant& datum);
bool contains(const QString& name) const;
- void save();
- void save_deferred(QSettings& s);
bool is_modified() const;
template<typename t>
@@ -79,6 +78,11 @@ public:
QMutexLocker l(mtx);
return transient.get<t>(name);
}
+public slots:
+ void save();
+ void reload(std::shared_ptr<QSettings> settings = group::ini_file());
+ void save_deferred(QSettings& s);
+ void set_all_to_default();
};
OPENTRACK_OPTIONS_EXPORT bundler& singleton();
diff --git a/options/connector.cpp b/options/connector.cpp
index 242c07da..63d70ca7 100644
--- a/options/connector.cpp
+++ b/options/connector.cpp
@@ -34,7 +34,7 @@ bool connector::is_equal(const QString& name, const QVariant& val1, const QVaria
return generic_is_equal(val1, val2);
}
-bool connector::on_value_destructed_impl(const QString& name, const base_value* val)
+bool connector::on_value_destructed_impl(const QString& name, value_type val)
{
QMutexLocker l(get_mtx());
@@ -43,7 +43,7 @@ bool connector::on_value_destructed_impl(const QString& name, const base_value*
if (it != connected_values.end())
{
- std::vector<const base_value*>& values = std::get<0>((*it).second);
+ std::vector<value_type>& values = std::get<0>((*it).second);
for (auto it = values.begin(); it != values.end(); it++)
{
if (*it == val)
@@ -61,7 +61,7 @@ bool connector::on_value_destructed_impl(const QString& name, const base_value*
-void connector::on_value_destructed(const QString& name, const base_value* val)
+void connector::on_value_destructed(const QString& name, value_type val)
{
if (!name.size())
return;
@@ -76,7 +76,7 @@ void connector::on_value_destructed(const QString& name, const base_value* val)
<< "value-ptr" << quintptr(val);
}
-void connector::on_value_created(const QString& name, const base_value* val)
+void connector::on_value_created(const QString& name, value_type val)
{
if (!name.size())
return;
@@ -98,7 +98,7 @@ void connector::on_value_created(const QString& name, const base_value* val)
{
tt& tmp = (*it).second;
std::type_index& typeidx = std::get<2>(tmp);
- std::vector<const base_value*>& values = std::get<0>(tmp);
+ std::vector<value_type>& values = std::get<0>(tmp);
if (typeidx != val->type_index)
std::get<1>((*it).second) = generic_is_equal;
@@ -106,7 +106,7 @@ void connector::on_value_created(const QString& name, const base_value* val)
}
else
{
- std::vector<const base_value*> vec;
+ std::vector<value_type> vec;
vec.push_back(val);
connected_values.emplace(name, tt(vec, val->cmp, val->type_index));
}
@@ -117,7 +117,7 @@ void connector::notify_values(const QString& name) const
auto it = connected_values.find(name);
if (it != connected_values.cend())
{
- for (const base_value* val : std::get<0>((*it).second))
+ for (value_type val : std::get<0>((*it).second))
{
val->bundle_value_changed();
}
@@ -127,7 +127,7 @@ void connector::notify_values(const QString& name) const
void connector::notify_all_values() const
{
for (auto& pair : connected_values)
- for (const base_value* val : std::get<0>(pair.second))
+ for (value_type val : std::get<0>(pair.second))
val->bundle_value_changed();
}
diff --git a/options/connector.hpp b/options/connector.hpp
index dec98016..abe72154 100644
--- a/options/connector.hpp
+++ b/options/connector.hpp
@@ -30,20 +30,35 @@ class connector
{
friend class ::options::base_value;
- using value_vec = std::vector<const base_value*>;
+ using value_type = base_value*;
+ using value_vec = std::vector<value_type>;
using comparator = bool(*)(const QVariant&, const QVariant&);
using tt = std::tuple<value_vec, comparator, std::type_index>;
std::map<QString, tt> connected_values;
- void on_value_destructed(const QString& name, const base_value* val);
- void on_value_created(const QString& name, const base_value* val);
- bool on_value_destructed_impl(const QString& name, const base_value* val);
+ void on_value_destructed(const QString& name, value_type val);
+ void on_value_created(const QString& name, value_type val);
+ bool on_value_destructed_impl(const QString& name, value_type val);
protected:
void notify_values(const QString& name) const;
void notify_all_values() const;
virtual QMutex* get_mtx() const = 0;
+ template<typename F>
+ void forall(F&& fun)
+ {
+ QMutexLocker l(get_mtx());
+
+ for (auto& pair : connected_values)
+ {
+ for (auto& val : std::get<0>(pair.second))
+ {
+ fun(pair.first, val);
+ }
+ }
+ }
+
public:
connector();
virtual ~connector();
@@ -56,5 +71,5 @@ public:
connector& operator=(connector&&) = default;
};
-}
-}
+} // ns detail
+} // ns options
diff --git a/options/value.cpp b/options/value.cpp
index f5e92600..36e05dac 100644
--- a/options/value.cpp
+++ b/options/value.cpp
@@ -24,4 +24,12 @@ base_value::~base_value()
b->on_value_destructed(self_name, this);
}
+namespace detail
+{
+ void set_base_value_to_default(base_value* val)
+ {
+ val->set_to_default();
+ }
+}
+
}
diff --git a/options/value.hpp b/options/value.hpp
index 253da0c4..2021bd3e 100644
--- a/options/value.hpp
+++ b/options/value.hpp
@@ -90,6 +90,7 @@ public slots:
virtual void reload() = 0;
virtual void bundle_value_changed() const = 0;
+ virtual void set_to_default() = 0;
};
namespace detail {
@@ -171,6 +172,11 @@ public:
return def;
}
+ void set_to_default() override
+ {
+ *this = def;
+ }
+
operator t() const { return get(); }
void reload() override