diff options
Diffstat (limited to 'opentrack-compat/options.cpp')
-rw-r--r-- | opentrack-compat/options.cpp | 127 |
1 files changed, 89 insertions, 38 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 |