summaryrefslogtreecommitdiffhomepage
path: root/options/value.hpp
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2022-07-01 18:07:03 +0200
committerStanislaw Halik <sthalik@misaki.pl>2022-07-01 18:07:03 +0200
commita67e8630caf20e7f48151024e9e68dd9271d75c7 (patch)
treee7418eabdfb4abd6ca88c0ba6a427ca5a40e7c32 /options/value.hpp
parent74dff816c07737b7e697e5d63222e59a9fefe23e (diff)
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<t>` instance. Also fix a bug in `options/tie.hpp` where `QComboBox::setCurrentIndex` was erroneously called as `Qt::DirectConnection`.
Diffstat (limited to 'options/value.hpp')
-rw-r--r--options/value.hpp23
1 files changed, 21 insertions, 2 deletions
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<typename t, typename...> /*MSVC workaround*/ static constexpr bool is_enum_v = std::is_enum_v<t>;
} // ns options::detail
namespace options {
@@ -38,7 +38,7 @@ namespace options {
template<typename t>
class value final : public value_
{
- static_assert(std::is_same_v<t, remove_cvref_t<t>>);
+ static_assert(std::is_same_v<t, std::remove_cvref_t<t>>);
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_<t>;
+ using slot_sig = typename value_::slot_sig_<t>;
+
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<u>(get());
}
+
+ template<typename Q, typename F>
+ QMetaObject::Connection
+ connect_to(Q* qobject, F&& writer, Qt::ConnectionType conn = Qt::QueuedConnection) {
+ return QObject::connect(this, static_cast<signal_sig>(&value<t>::valueChanged), qobject, std::forward<F>(writer), conn);
+ }
+ template<typename Q, typename F>
+ QMetaObject::Connection
+ connect_from(Q* qobject, F&& reader, Qt::ConnectionType conn = Qt::DirectConnection) {
+ return QObject::connect(qobject, std::forward<F>(reader), this, static_cast<slot_sig>(&value<t>::setValue), conn);
+ }
+ template<typename Q, typename F, typename G>
+ QMetaObject::Connection
+ connect_from(Q* qobject, F&& reader, G&& fn, Qt::ConnectionType conn = Qt::DirectConnection) {
+ return QObject::connect(qobject, std::forward<F>(reader), this, std::forward<G>(fn), conn);
+ }
};
#if !defined OTR_OPTIONS_INST_VALUE