diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2016-06-12 21:51:37 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2016-06-14 18:14:46 +0200 |
commit | d3aac3ed3b9bd65b8ca4cdffd0c4f09fabbc583d (patch) | |
tree | 766de6dc53eba6b511ff395a2a4ff18fbd4af98d /opentrack-compat | |
parent | f4667d5cc9fbe8a5a65df136736a347c1fb37fd0 (diff) |
compat/options: slider_value improvements
- a comparison operator. Qt uses it with the metatype support.
- pretty print support for QDebug
Diffstat (limited to 'opentrack-compat')
-rw-r--r-- | opentrack-compat/options.cpp | 127 | ||||
-rw-r--r-- | opentrack-compat/options.hpp | 28 |
2 files changed, 104 insertions, 51 deletions
diff --git a/opentrack-compat/options.cpp b/opentrack-compat/options.cpp index 813e39ef..f86e32da 100644 --- a/opentrack-compat/options.cpp +++ b/opentrack-compat/options.cpp @@ -3,16 +3,6 @@ namespace options { -namespace detail -{ -OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() -{ - static opt_singleton ret; - return ret; -} - -} - group::group(const QString& name) : name(name) { auto conf = ini_file(); @@ -94,7 +84,7 @@ bool group::operator==(const group& other) const const QVariant val = other.get<QVariant>(kv.first); if (!other.contains(kv.first) || kv.second != val) { - qDebug() << "bundle" << name << "modified" << "key" << kv.first << "to" << kv.second << "from" << val; + qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << val << "<>" << kv.second; return false; } } @@ -104,7 +94,7 @@ bool group::operator==(const group& other) const const QVariant val = get<QVariant>(kv.first); if (!contains(kv.first) || kv.second != val) { - qDebug() << "bundle" << name << "modified" << "key" << kv.first << "to" << kv.second << "from" << val; + qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << kv.second << "<>" << val; return false; } } @@ -117,7 +107,8 @@ impl_bundle::impl_bundle(const QString& group_name) group_name(group_name), saved(group_name), transient(saved) -{} +{ +} void impl_bundle::reload() { @@ -167,22 +158,37 @@ bool impl_bundle::modifiedp() const // XXX unused return transient != saved; } -namespace detail +base_value::base_value(pbundle b, const QString &name) : + b(b), + self_name(name) { +} -void opt_singleton::bundle_decf(const opt_singleton::k& key) +opts::~opts() { - QMutexLocker l(&implsgl_mtx); + b->reload(); +} - if (--std::get<0>(implsgl_data[key]) == 0) - { - qDebug() << "bundle -" << key; +opts::opts(const QString &name) : b(bundle(name)) +{ +} - implsgl_data.erase(key); - } +pbundle bundle(const QString& name) +{ + return detail::singleton().bundle(name); } -opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) {} +custom_type_initializer::custom_type_initializer() +{ + qDebug() << "options: registering stream operators"; + + qRegisterMetaTypeStreamOperators<slider_value>("slider_value"); + QMetaType::registerDebugStreamOperator<slider_value>(); +} + +custom_type_initializer custom_type_initializer::singleton = custom_type_initializer(); + +namespace detail { opt_bundle::opt_bundle(const QString& group_name) : impl_bundle(group_name) @@ -194,6 +200,22 @@ opt_bundle::~opt_bundle() detail::singleton().bundle_decf(group_name); } +void opt_singleton::bundle_decf(const opt_singleton::k& key) +{ + QMutexLocker l(&implsgl_mtx); + + if (--std::get<0>(implsgl_data[key]) == 0) + { + qDebug() << "bundle -" << key; + + implsgl_data.erase(key); + } +} + +opt_singleton::opt_singleton() : implsgl_mtx(QMutex::Recursive) +{ +} + pbundle opt_singleton::bundle(const opt_singleton::k &key) { QMutexLocker l(&implsgl_mtx); @@ -212,40 +234,67 @@ pbundle opt_singleton::bundle(const opt_singleton::k &key) return shr; } +OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() +{ + static opt_singleton ret; + return ret; } -base_value::base_value(pbundle b, const QString &name) : - b(b), - self_name(name) -{} -opts::~opts() +} // end options::detail + +slider_value::operator double() const { - b->reload(); + return cur_; } -opts::opts(const QString &name) : b(bundle(name)) {} +slider_value::slider_value(double cur, double min, double max) : + cur_(cur), + min_(min), + max_(max) +{ +} -pbundle bundle(const QString& name) +slider_value::slider_value(const slider_value& v) : slider_value(v.cur(), v.min(), v.max()) { - return detail::singleton().bundle(name); } -slider_value::operator double() const +slider_value::slider_value() : slider_value(0, 0, 0) { - return cur_; } -custom_type_initializer::custom_type_initializer() +slider_value& slider_value::operator=(const slider_value& v) { - qDebug() << "options: registering stream operators"; + cur_ = v.cur_; + min_ = v.min_; + max_ = v.max_; - qRegisterMetaTypeStreamOperators<slider_value>("slider_value"); + return *this; } -custom_type_initializer custom_type_initializer::singleton = custom_type_initializer(); +bool slider_value::operator==(const slider_value& v) const +{ + using std::fabs; + + static constexpr double eps = 1e-3; + + return (fabs(v.cur_ - cur_) < eps && + fabs(v.min_ - min_) < eps && + fabs(v.max_ - max_) < eps); +} + +} // end options -} // end +QT_BEGIN_NAMESPACE + +QDebug operator << (QDebug dbg, const options::slider_value& val) +{ + dbg.nospace() << "cur=" << val.cur() + << ", min=" << val.min() + << ", max=" << val.max(); + + return dbg.space(); +} QDataStream& operator <<(QDataStream& out, const options::slider_value& v) { @@ -262,3 +311,5 @@ QDataStream& operator >>(QDataStream& in, options::slider_value& v) v = options::slider_value(cur, min, max); return in; } + +QT_END_NAMESPACE diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 76f36146..af36ea08 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -58,14 +58,11 @@ namespace options { double cur_, min_, max_; public: - slider_value(double cur, double min, double max) : - cur_(cur), - min_(min), - max_(max) - {} - slider_value(const slider_value& v) : slider_value(v.cur(), v.min(), v.max()) {} - slider_value() : slider_value(0, 0, 0) {} - slider_value& operator=(const slider_value& v) { cur_ = v.cur_; min_ = v.min_; max_ = v.max_; return *this; } + slider_value(double cur, double min, double max); + slider_value(const slider_value& v); + slider_value(); + slider_value& operator=(const slider_value& v); + bool operator==(const slider_value& v) const; operator double() const; double cur() const { return cur_; } double min() const { return min_; } @@ -73,12 +70,13 @@ namespace options }; } -Q_DECLARE_METATYPE(options::slider_value) +QDebug operator << (QDebug dbg, const options::slider_value& val); QDataStream& operator << (QDataStream& out, const options::slider_value& v); - QDataStream& operator >> (QDataStream& in, options::slider_value& v); +Q_DECLARE_METATYPE(options::slider_value) + namespace options { namespace { class custom_type_initializer @@ -258,7 +256,9 @@ namespace options { *this = def; } - value(pbundle b, const char* name, t def) : value(b, QString(name), def) {} + value(pbundle b, const char* name, t def) : value(b, QString(name), def) + { + } operator t() const { @@ -313,7 +313,8 @@ namespace options { v(v), cb(cb), enum_cases(enum_cases) - {} + { + } void operator()(int idx) { @@ -334,7 +335,8 @@ namespace options { v(v), cb(cb), enum_cases(enum_cases) - {} + { + } void operator()(int val) { |