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/value.hpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'options/value.hpp') 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