From ca847749c03b88d8e482396a9c4311ef02b054b9 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Tue, 21 Mar 2017 08:22:36 +0100 Subject: options/{bundle,connector,value}: allow setting to default values This is complicated by Qt's rejection of template classes. Also move some stuff to slots where makes sense. --- options/bundle.cpp | 5 +++++ options/bundle.hpp | 10 +++++++--- options/connector.cpp | 16 ++++++++-------- options/connector.hpp | 27 +++++++++++++++++++++------ options/value.cpp | 8 ++++++++ options/value.hpp | 6 ++++++ 6 files changed, 55 insertions(+), 17 deletions(-) (limited to 'options') 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 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 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 @@ -79,6 +78,11 @@ public: QMutexLocker l(mtx); return transient.get(name); } +public slots: + void save(); + void reload(std::shared_ptr 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& values = std::get<0>((*it).second); + std::vector& 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& values = std::get<0>(tmp); + std::vector& 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 vec; + std::vector 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; + using value_type = base_value*; + using value_vec = std::vector; using comparator = bool(*)(const QVariant&, const QVariant&); using tt = std::tuple; std::map 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 + 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 -- cgit v1.2.3