#include "options.hpp" namespace options { group::group(const QString& name) : name(name) { auto conf = ini_file(); conf->beginGroup(name); for (auto& k_ : conf->childKeys()) { auto tmp = k_.toUtf8(); QString k(tmp); kvs[k] = conf->value(k_); } conf->endGroup(); } void group::save() const { auto s = ini_file(); s->beginGroup(name); for (auto& i : kvs) s->setValue(i.first, i.second); s->endGroup(); } void group::put(const QString &s, const QVariant &d) { kvs[s] = d; } bool group::contains(const QString &s) const { return kvs.count(s) != 0; } QString group::ini_directory() { const auto dirs = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation); if (dirs.size() == 0) return ""; if (QDir(dirs[0]).mkpath(OPENTRACK_ORG)) return dirs[0] + "/" OPENTRACK_ORG; return ""; } QString group::ini_filename() { QSettings settings(OPENTRACK_ORG); return settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); } QString group::ini_pathname() { const auto dir = ini_directory(); if (dir == "") return ""; QSettings settings(OPENTRACK_ORG); return dir + "/" + settings.value(OPENTRACK_CONFIG_FILENAME_KEY, OPENTRACK_DEFAULT_CONFIG).toString(); } const QStringList group::ini_list() { const auto dirname = ini_directory(); if (dirname == "") return QStringList(); QDir settings_dir(dirname); return settings_dir.entryList( QStringList { "*.ini" } , QDir::Files, QDir::Name ); } const mem group::ini_file() { const auto pathname = ini_pathname(); if (pathname != "") return std::make_shared(ini_pathname(), QSettings::IniFormat); return std::make_shared(); } bool group::operator==(const group& other) const { for (const auto& kv : kvs) { const QVariant val = other.get(kv.first); if (!other.contains(kv.first) || kv.second != val) { qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << val << "<>" << kv.second; return false; } } for (const auto& kv : other.kvs) { const QVariant val = get(kv.first); if (!contains(kv.first) || kv.second != val) { qDebug() << "bundle" << name << "modified" << "key" << kv.first << "-" << kv.second << "<>" << val; return false; } } return true; } impl_bundle::impl_bundle(const QString& group_name) : mtx(QMutex::Recursive), group_name(group_name), saved(group_name), transient(saved) { } void impl_bundle::reload() { { QMutexLocker l(&mtx); saved = group(group_name); transient = saved; } emit reloading(); } void impl_bundle::store_kv(const QString& name, const QVariant& datum) { QMutexLocker l(&mtx); transient.put(name, datum); } bool impl_bundle::contains(const QString &name) const { QMutexLocker l(const_cast(&mtx)); return transient.contains(name); } void impl_bundle::save() { bool modified_ = false; { QMutexLocker l(&mtx); if (saved != transient) { qDebug() << "bundle" << group_name << "changed, saving"; modified_ = true; saved = transient; saved.save(); } } if (modified_) emit saving(); } bool impl_bundle::modifiedp() const // XXX unused { QMutexLocker l(const_cast(&mtx)); return transient != saved; } base_value::base_value(pbundle b, const QString &name) : b(b), self_name(name) { } opts::~opts() { b->reload(); } opts::opts(const QString &name) : b(bundle(name)) { } pbundle bundle(const QString& name) { return detail::singleton().bundle(name); } custom_type_initializer::custom_type_initializer() { qDebug() << "options: registering stream operators"; qRegisterMetaTypeStreamOperators("slider_value"); QMetaType::registerDebugStreamOperator(); } custom_type_initializer custom_type_initializer::singleton = custom_type_initializer(); namespace detail { opt_bundle::opt_bundle(const QString& group_name) : impl_bundle(group_name) { } 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); if (implsgl_data.count(key) != 0) { auto shared = std::get<1>(implsgl_data[key]).lock(); if (shared != nullptr) return shared; } qDebug() << "bundle +" << key; auto shr = std::make_shared(key); implsgl_data[key] = tt(1, shr); return shr; } OPENTRACK_COMPAT_EXPORT opt_singleton& singleton() { static opt_singleton ret; return ret; } } // end options::detail slider_value::operator double() const { return cur_; } slider_value::slider_value(double cur, double min, double max) : cur_(cur), min_(min), max_(max) { } slider_value::slider_value(const slider_value& v) : slider_value(v.cur(), v.min(), v.max()) { } slider_value::slider_value() : slider_value(0, 0, 0) { } slider_value& slider_value::operator=(const slider_value& v) { cur_ = v.cur_; min_ = v.min_; max_ = v.max_; return *this; } 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 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) { out << v.cur() << v.min() << v.max(); return out; } QDataStream& operator >>(QDataStream& in, options::slider_value& v) { double cur, min, max; in >> cur; in >> min; in >> max; v = options::slider_value(cur, min, max); return in; } QT_END_NAMESPACE