From cb33be1c50b68d6022f344ddac923c7aac3a11d5 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Wed, 17 Aug 2016 21:28:45 +0200 Subject: move options framework into its own library - adjust usages - add support for QList signals and metatype --- options/tie.hpp | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 options/tie.hpp (limited to 'options/tie.hpp') diff --git a/options/tie.hpp b/options/tie.hpp new file mode 100644 index 00000000..2969f8d4 --- /dev/null +++ b/options/tie.hpp @@ -0,0 +1,212 @@ +#pragma once + +#include "value.hpp" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace options { + +template +inline void tie_setting(value&, q*); + +template +inline +typename std::enable_if::value>::type +tie_setting(value& v, QComboBox* cb) +{ + cb->setCurrentIndex(cb->findData((unsigned)static_cast(v))); + v = static_cast(cb->currentData().toInt()); + + // QObject::connect plays badly with std::bind of std::shared_ptr. Data seems to get freed. + // Direct accesses of cb->currentData within arbitrary thread context cause crashes as well. + // Hence we go for a verbose implementation. + + std::vector enum_cases; + enum_cases.reserve(unsigned(cb->count())); + + for (int i = 0; i < cb->count(); i++) + enum_cases.push_back(cb->itemData(i).toInt()); + + struct fn1 + { + value& v; + QComboBox* cb; + std::vector enum_cases; + + fn1(value& v, QComboBox* cb, const std::vector& enum_cases) : + v(v), + cb(cb), + enum_cases(enum_cases) + { + } + + void operator()(int idx) + { + if (idx < 0 || idx >= (int)enum_cases.size()) + v = static_cast(-1); + else + v = static_cast(t(std::intptr_t(enum_cases[idx]))); + } + }; + + struct fn2 + { + value& v; + QComboBox* cb; + std::vector enum_cases; + + fn2(value& v, QComboBox* cb, const std::vector& enum_cases) : + v(v), + cb(cb), + enum_cases(enum_cases) + { + } + + void operator()(int val) + { + for (unsigned i = 0; i < enum_cases.size(); i++) + { + if (val == enum_cases[i]) + { + cb->setCurrentIndex(i); + return; + } + } + cb->setCurrentIndex(-1); + } + }; + + base_value::connect(cb, + static_cast(&QComboBox::currentIndexChanged), + &v, + fn1(v, cb, enum_cases), + v.DIRECT_CONNTYPE); + base_value::connect(&v, + static_cast(&base_value::valueChanged), + cb, + fn2(v, cb, enum_cases), + v.DIRECT_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QComboBox* cb) +{ + cb->setCurrentIndex(v); + v = cb->currentIndex(); + base_value::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QComboBox* cb) +{ + cb->setCurrentText(v); + v = cb->currentText(); + base_value::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(QString)), cb, SLOT(setCurrentText(QString)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QCheckBox* cb) +{ + cb->setChecked(v); + base_value::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QDoubleSpinBox* dsb) +{ + dsb->setValue(v); + base_value::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QSpinBox* sb) +{ + sb->setValue(v); + base_value::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QSlider* sl) +{ + sl->setValue(v); + v = sl->value(); + base_value::connect(sl, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), sl, SLOT(setValue(int)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QLineEdit* le) +{ + le->setText(v); + base_value::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(QString)),le, SLOT(setText(QString)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QLabel* lb) +{ + lb->setText(v); + base_value::connect(&v, SIGNAL(valueChanged(QString)), lb, SLOT(setText(QString)), v.DIRECT_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QTabWidget* t) +{ + t->setCurrentIndex(v); + base_value::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); + base_value::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); +} + +template<> +inline void tie_setting(value& v, QSlider* w) +{ + // we can't get these at runtime since signals cross threads + const int q_min = w->minimum(); + const int q_max = w->maximum(); + const int q_diff = q_max - q_min; + + slider_value sv(v); + + const double sv_c = sv.max() - sv.min(); + + using std::round; + + w->setValue(int((sv.cur() - sv.min()) / sv_c * q_diff + q_min)); + v = slider_value(q_diff <= 0 ? 0 : (w->value() - q_min) * sv_c / (double)q_diff + sv.min(), sv.min(), sv.max()); + + base_value::connect(w, + &QSlider::valueChanged, + &v, + [=, &v](int pos) { + const int q_diff = q_max - q_min; + if (q_diff <= 0 || pos <= 0) + v = slider_value(sv.min(), sv.min(), sv.max()); + else + { + const float c = float(sv.max()-sv.min()); + v = slider_value((pos - q_min) * c / (float)q_diff + float(sv.min()), sv.min(), sv.max()); + } + }, + v.DIRECT_CONNTYPE); + base_value::connect(&v, + static_cast(&base_value::valueChanged), + w, + [=](double value) { w->setValue(int(round(value * q_diff) + q_min)); }, + v.SAFE_CONNTYPE); +} + +} -- cgit v1.2.3