From a67e8630caf20e7f48151024e9e68dd9271d75c7 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 1 Jul 2022 18:07:03 +0200 Subject: options/value: add `QObject::connect` wrapper This is useful not just to save on complexity in call sites, but also because I plan on using the Verdigris library to remove needless `valueChanged()` and `setValue()` overloads from each `value` instance. Also fix a bug in `options/tie.hpp` where `QComboBox::setCurrentIndex` was erroneously called as `Qt::DirectConnection`. --- options/base-value.hpp | 13 +++------- options/tie.cpp | 66 ++++++++++++++++++++---------------------------- options/tie.hpp | 32 ++++++++--------------- options/value-traits.hpp | 11 ++++++++ options/value.hpp | 23 +++++++++++++++-- 5 files changed, 74 insertions(+), 71 deletions(-) (limited to 'options') diff --git a/options/base-value.hpp b/options/base-value.hpp index eb8c89b7..81da0513 100644 --- a/options/base-value.hpp +++ b/options/base-value.hpp @@ -10,6 +10,7 @@ #include "value-traits.hpp" #include +#include #include #include @@ -26,21 +27,15 @@ class OTR_OPTIONS_EXPORT value_ : public QObject { Q_OBJECT - template using cv_qualified = detail::cv_qualified; - template - using signal_sig = void(value_::*)(cv_qualified) const; +protected: + template using signal_sig_ = void(value_::*)(detail::cv_qualified>) const; + template using slot_sig_ = void(value_::*)(detail::cv_qualified>); public: QString name() const { return self_name; } value_(bundle const& b, const QString& name) noexcept; ~value_() override; - template - static constexpr auto value_changed() - { - return static_cast>(&value_::valueChanged); - } - static const bool TRACE_NOTIFY; signals: diff --git a/options/tie.cpp b/options/tie.cpp index 493ff551..0ac1c5bf 100644 --- a/options/tie.cpp +++ b/options/tie.cpp @@ -8,7 +8,6 @@ #include "tie.hpp" #include "compat/run-in-thread.hpp" -#include "compat/macros.hpp" #include "value-traits.hpp" @@ -20,16 +19,16 @@ void tie_setting(value& v, QComboBox* cb) { cb->setCurrentIndex(v); v = cb->currentIndex(); - value_::connect(cb, SIGNAL(currentIndexChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(int)), cb, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); + v.connect_to(cb, &QComboBox::setCurrentIndex); + v.connect_from(cb, static_cast(&QComboBox::currentIndexChanged)); } void tie_setting(value& v, QComboBox* cb) { cb->setCurrentText(v); v = cb->currentText(); - value_::connect(cb, SIGNAL(currentTextChanged(QString)), &v, SLOT(setValue(const QString&)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(const QString&)), cb, SLOT(setCurrentText(const QString&)), v.SAFE_CONNTYPE); + v.connect_to(cb, &QComboBox::currentTextChanged); + v.connect_from(cb, &QComboBox::setCurrentText); } void tie_setting(value& v, QComboBox* cb) @@ -57,60 +56,57 @@ void tie_setting(value& v, QComboBox* cb) else v = {}; - value_::connect(cb, static_cast(&QComboBox::currentIndexChanged), - &v, [cb, &v](int idx) { v = cb->itemData(idx); }, - v.DIRECT_CONNTYPE); - value_::connect(&v, value_::value_changed(), - cb, [set_idx](const QVariant& var) { set_idx(var); }, - v.SAFE_CONNTYPE); + v.connect_from(cb, static_cast(&QComboBox::currentIndexChanged), + [cb, &v](int idx) { v = cb->itemData(idx); }); + v.connect_to(cb, [fn = std::move(set_idx)](const QVariant& var) { fn(var); }); } void tie_setting(value& v, QRadioButton* cb) { cb->setChecked(v); - value_::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE); + v.connect_to(cb, &QRadioButton::setChecked); + v.connect_from(cb, &QRadioButton::toggled); } void tie_setting(value& v, QCheckBox* cb) { cb->setChecked(v); - value_::connect(cb, SIGNAL(toggled(bool)), &v, SLOT(setValue(bool)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(bool)), cb, SLOT(setChecked(bool)), v.SAFE_CONNTYPE); + v.connect_to(cb, &QCheckBox::setChecked); + v.connect_from(cb, &QCheckBox::toggled); } void tie_setting(value& v, QDoubleSpinBox* dsb) { dsb->setValue(v); - value_::connect(dsb, SIGNAL(valueChanged(double)), &v, SLOT(setValue(double)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(double)), dsb, SLOT(setValue(double)), v.SAFE_CONNTYPE); + v.connect_to(dsb, &QDoubleSpinBox::setValue); + v.connect_from(dsb, static_cast(&QDoubleSpinBox::valueChanged)); } void tie_setting(value& v, QSpinBox* sb) { sb->setValue(v); - value_::connect(sb, SIGNAL(valueChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(int)), sb, SLOT(setValue(int)), v.SAFE_CONNTYPE); + v.connect_to(sb, &QSpinBox::setValue); + v.connect_from(sb, static_cast(&QSpinBox::valueChanged)); } void tie_setting(value& v, QLineEdit* le) { le->setText(v); - value_::connect(le, SIGNAL(textChanged(QString)), &v, SLOT(setValue(QString)), v.DIRECT_CONNTYPE); - value_::connect(&v, value_::value_changed(), le, &QLineEdit::setText, v.SAFE_CONNTYPE); + v.connect_to(le, &QLineEdit::setText); + v.connect_from(le, &QLineEdit::textChanged); } void tie_setting(value& v, QLabel* lb) { lb->setText(v); - value_::connect(&v, value_::value_changed(), lb, &QLabel::setText, v.SAFE_CONNTYPE); + v.connect_to(lb, &QLabel::setText); } void tie_setting(value& v, QTabWidget* t) { t->setCurrentIndex(v); - value_::connect(t, SIGNAL(currentChanged(int)), &v, SLOT(setValue(int)), v.DIRECT_CONNTYPE); - value_::connect(&v, SIGNAL(valueChanged(int)), t, SLOT(setCurrentIndex(int)), v.SAFE_CONNTYPE); + v.connect_to(t, &QTabWidget::setCurrentIndex); + v.connect_from(t, &QTabWidget::currentChanged); } void tie_setting(value& v, QSlider* w) @@ -123,32 +119,24 @@ void tie_setting(value& v, QSlider* w) v = v().update_from_slider(w->value(), q_min, q_max); } - value_::connect(w, &QSlider::valueChanged, &v, [=, &v](int pos) - { - run_in_thread_sync(w, [&]() - { + v.connect_from(w, &QSlider::valueChanged, [=, &v](int pos) { + run_in_thread_sync(w, [&]() { const int q_min = w->minimum(); const int q_max = w->maximum(); v = v().update_from_slider(pos, q_min, q_max); w->setValue(v().to_slider_pos(q_min, q_max)); }); - }, - v.DIRECT_CONNTYPE); - - value_::connect(&v, - value_::value_changed(), - w, - [=, &v](double) { - run_in_thread_sync(w, [=, &v]() - { + }, v.DIRECT_CONNTYPE); + + v.connect_to(w, [=, &v](double) { + run_in_thread_sync(w, [=, &v]() { const int q_min = w->minimum(); const int q_max = w->maximum(); const int pos = v->to_slider_pos(q_min, q_max); v = v->update_from_slider(pos, q_min, q_max); w->setValue(pos); }); - }, - v.DIRECT_CONNTYPE); + }, v.DIRECT_CONNTYPE); } } // ns options diff --git a/options/tie.hpp b/options/tie.hpp index 46ade075..81567139 100644 --- a/options/tie.hpp +++ b/options/tie.hpp @@ -37,13 +37,9 @@ std::enable_if_t> tie_setting(value& v, QComboBox* cb) cb->setCurrentIndex(cb->findData(int(static_cast(v)))); v = static_cast(cb->currentData().toInt()); - value_::connect(cb, static_cast(&QComboBox::currentIndexChanged), - &v, [&v, cb](int idx) { v = static_cast(cb->itemData(idx).toInt()); }, - v.DIRECT_CONNTYPE); - - value_::connect(&v, value_::value_changed(), - cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); }, - v.SAFE_CONNTYPE); + v.connect_from(cb, static_cast(&QComboBox::currentIndexChanged), + [&v, cb](int idx) { v = static_cast(cb->itemData(idx).toInt()); }); + v.connect_to(cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); }); } template @@ -52,23 +48,20 @@ void tie_setting(value& v, QComboBox* cb, From&& fn_to_index, To&& fn_to_valu cb->setCurrentIndex(fn_to_index(v)); v = fn_to_value(cb->currentIndex(), cb->currentData()); - value_::connect(cb, static_cast(&QComboBox::currentIndexChanged), - &v, [&v, cb, fn_to_value](int idx) { v = fn_to_value(idx, cb->currentData()); }, - v.DIRECT_CONNTYPE); - value_::connect(&v, value_::value_changed(), - cb, [cb, fn_to_index](detail::cv_qualified& v) { cb->setCurrentIndex(fn_to_index(v)); }, - v.DIRECT_CONNTYPE); + v.connect_from(cb, static_cast(&QComboBox::currentIndexChanged), + [&v, cb, fn = std::forward(fn_to_value)](int idx) { v = fn(idx, cb->currentData()); }); + v.connect_to(cb, [cb, fn = std::forward(fn_to_index)](detail::cv_qualified v) { cb->setCurrentIndex(fn(v)); }); } template void tie_setting(value& v, QLabel* lb, F&& fun) { - auto closure = [lb, fun](detail::cv_qualified v) { lb->setText(fun(v)); }; + auto closure = [lb, fn = std::forward(fun)](detail::cv_qualified v) { + lb->setText(fn(v)); + }; closure(v()); - value_::connect(&v, value_::value_changed(), - lb, closure, - v.SAFE_CONNTYPE); + v.connect_to(lb, std::move(closure)); } template @@ -78,10 +71,7 @@ void tie_setting(value& v, QObject* obj, F&& fun) abort(); fun(v()); - - value_::connect(&v, value_::value_changed(), - obj, fun, - v.DIRECT_CONNTYPE); + v.connect_to(obj, std::forward(fun)); } OTR_OPTIONS_EXPORT void tie_setting(value& v, QComboBox* cb); diff --git a/options/value-traits.hpp b/options/value-traits.hpp index 145cd924..e676f942 100644 --- a/options/value-traits.hpp +++ b/options/value-traits.hpp @@ -15,6 +15,17 @@ using cv_qualified = std::remove_cvref_t, std::add_lvalue_reference_t>>>; +template +struct maybe_enum_type { + using type = t; +}; +template +struct maybe_enum_type>> { + using type = int; +}; + +template using maybe_enum_type_t = typename maybe_enum_type::type; + template struct default_value_traits { diff --git a/options/value.hpp b/options/value.hpp index 090a7bdf..4476f8b7 100644 --- a/options/value.hpp +++ b/options/value.hpp @@ -27,10 +27,10 @@ namespace options::detail { { t x; public: - constexpr t const* operator->() const { return &x; } constexpr t* operator->() { return &x; } constexpr explicit dereference_wrapper(t&& x) : x(x) {} }; + template /*MSVC workaround*/ static constexpr bool is_enum_v = std::is_enum_v; } // ns options::detail namespace options { @@ -38,7 +38,7 @@ namespace options { template class value final : public value_ { - static_assert(std::is_same_v>); + static_assert(std::is_same_v>); mutable QMutex mtx; const t def; mutable t cached_value; @@ -86,6 +86,9 @@ class value final : public value_ } public: + using signal_sig = typename value_::signal_sig_; + using slot_sig = typename value_::slot_sig_; + QVariant get_variant() const noexcept override { if (QVariant ret{b->get_variant(self_name)}; ret.isValid() && !ret.isNull()) @@ -195,6 +198,22 @@ public: { return static_cast(get()); } + + template + QMetaObject::Connection + connect_to(Q* qobject, F&& writer, Qt::ConnectionType conn = Qt::QueuedConnection) { + return QObject::connect(this, static_cast(&value::valueChanged), qobject, std::forward(writer), conn); + } + template + QMetaObject::Connection + connect_from(Q* qobject, F&& reader, Qt::ConnectionType conn = Qt::DirectConnection) { + return QObject::connect(qobject, std::forward(reader), this, static_cast(&value::setValue), conn); + } + template + QMetaObject::Connection + connect_from(Q* qobject, F&& reader, G&& fn, Qt::ConnectionType conn = Qt::DirectConnection) { + return QObject::connect(qobject, std::forward(reader), this, std::forward(fn), conn); + } }; #if !defined OTR_OPTIONS_INST_VALUE -- cgit v1.2.3