From 47dec53450dec0264489cddb0005e13593f43399 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Thu, 26 May 2016 16:59:02 +0200 Subject: compat/options: finish slider value support more --- opentrack-compat/options.hpp | 45 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'opentrack-compat/options.hpp') diff --git a/opentrack-compat/options.hpp b/opentrack-compat/options.hpp index 19b36152..98d0dc83 100644 --- a/opentrack-compat/options.hpp +++ b/opentrack-compat/options.hpp @@ -34,6 +34,7 @@ #include #include #include +#include #include @@ -49,6 +50,25 @@ template using mem = std::shared_ptr; #define OPENTRACK_DEFAULT_CONFIG "default.ini" #define OPENTRACK_ORG "opentrack-2.3" +namespace options +{ + struct OPENTRACK_COMPAT_EXPORT slider_value + { + double cur, min, max; + slider_value(double cur, double min, double max) : + cur(cur), + min(min), + max(max) + {} + slider_value() : slider_value(0, 0, 0) {} + operator double() const; + double to_abs() const { return operator double(); } + static slider_value from_abs(double val, double min, double max); + }; +} + +Q_DECLARE_METATYPE(options::slider_value) + namespace options { template using map = std::map; @@ -144,11 +164,12 @@ namespace options { ~opt_bundle(); }; +#define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } +#define DEFINE_SIGNAL(t) void valueChanged(t) + class OPENTRACK_COMPAT_EXPORT base_value : public QObject { Q_OBJECT -#define DEFINE_SLOT(t) void setValue(t datum) { store(datum); } -#define DEFINE_SIGNAL(t) void valueChanged(t) public: QString name() const { return self_name; } base_value(pbundle b, const QString& name); @@ -157,6 +178,7 @@ namespace options { DEFINE_SIGNAL(int); DEFINE_SIGNAL(bool); DEFINE_SIGNAL(QString); + DEFINE_SIGNAL(slider_value); protected: pbundle b; QString self_name; @@ -172,6 +194,7 @@ namespace options { DEFINE_SLOT(int) DEFINE_SLOT(QString) DEFINE_SLOT(bool) + DEFINE_SLOT(slider_value) public slots: virtual void reload() = 0; }; @@ -198,8 +221,10 @@ namespace options { store(static_cast(datum)); return datum; } + static constexpr const Qt::ConnectionType DIRECT_CONNTYPE = Qt::AutoConnection; static constexpr const Qt::ConnectionType SAFE_CONNTYPE = Qt::QueuedConnection; + value(pbundle b, const QString& name, t def) : base_value(b, name), def(static_cast(def)) { QObject::connect(b.get(), SIGNAL(reloading()), @@ -208,13 +233,16 @@ namespace options { if (!b->contains(name) || b->get(name).type() == QVariant::Invalid) *this = def; } + value(pbundle b, const char* name, t def) : value(b, QString(name), def) {} operator t() const { return static_cast(b->contains(self_name) ? b->get(self_name) : def); } - void reload() override { + + void reload() override + { *this = static_cast(*this); } private: @@ -379,20 +407,22 @@ namespace options { } template<> - inline void tie_setting(value& v, QSlider* w) + inline void tie_setting(value& v, QSlider* w) { // we can't get these at runtime since signals cross threads const int min = w->minimum(); const int max = w->maximum(); const int max_ = max - min; - w->setValue(int(v * max_) + min); - v = max_ <= 0 ? 0 : (w->value() - min) / (double)max_; + slider_value sv(v); + + w->setValue(int(sv.cur * max_) + min); + v = slider_value(max_ <= 0 ? 0 : (w->value() - min) / (double)max_, sv.min, sv.max); base_value::connect(w, &QSlider::valueChanged, &v, [=, &v](int pos) -> void { - v = max_ <= 0 ? 0 : (pos - min) / (double)max_; + v = slider_value(max_ <= 0 ? 0 : (pos - min) / (double)max_, sv.min, sv.max); }, v.DIRECT_CONNTYPE); base_value::connect(&v, static_cast(&base_value::valueChanged), w, @@ -403,3 +433,4 @@ namespace options { v.DIRECT_CONNTYPE); } } + -- cgit v1.2.3