summaryrefslogtreecommitdiffhomepage
path: root/options/tie.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'options/tie.hpp')
-rw-r--r--options/tie.hpp75
1 files changed, 58 insertions, 17 deletions
diff --git a/options/tie.hpp b/options/tie.hpp
index 93004e0f..194a3a5d 100644
--- a/options/tie.hpp
+++ b/options/tie.hpp
@@ -10,6 +10,7 @@
#include "export.hpp"
#include "value.hpp"
+#include "compat/run-in-thread.hpp"
#include <QComboBox>
#include <QCheckBox>
@@ -19,44 +20,84 @@
#include <QLineEdit>
#include <QLabel>
#include <QTabWidget>
+#include <QRadioButton>
#include <cmath>
+#if defined __GNUG__
+# pragma GCC diagnostic push
+# pragma GCC diagnostic ignored "-Wattributes"
+#endif
+
namespace options {
template<typename t>
-typename std::enable_if<std::is_enum<t>::value>::type
-tie_setting(value<t>& v, QComboBox* cb)
+std::enable_if_t<std::is_enum_v<t>> tie_setting(value<t>& v, QComboBox* cb)
{
cb->setCurrentIndex(cb->findData(int(static_cast<t>(v))));
v = static_cast<t>(cb->currentData().toInt());
- base_value::connect(cb,
- static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
- &v,
- [&v, cb](int idx)
- {
- run_in_thread_sync(cb,
- [&]() {
- v = static_cast<t>(cb->itemData(idx).toInt());
- });
- },
- v.DIRECT_CONNTYPE);
- base_value::connect(&v, static_cast<void (base_value::*)(int) const>(&base_value::valueChanged),
- cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); },
- v.SAFE_CONNTYPE);
+ value_::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
+ &v, [&v, cb](int idx) { v = static_cast<t>(cb->itemData(idx).toInt()); },
+ v.DIRECT_CONNTYPE);
+
+ value_::connect(&v, value_::value_changed<int>(),
+ cb, [cb](int x) { cb->setCurrentIndex(cb->findData(x)); },
+ v.SAFE_CONNTYPE);
+}
+
+template<typename t, typename From, typename To>
+void tie_setting(value<t>& v, QComboBox* cb, From&& fn_to_index, To&& fn_to_value)
+{
+ cb->setCurrentIndex(fn_to_index(v));
+ v = fn_to_value(cb->currentIndex(), cb->currentData());
+
+ value_::connect(cb, static_cast<void (QComboBox::*)(int)>(&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<t>(),
+ cb, [cb, fn_to_index](detail::cv_qualified<t>& v) { cb->setCurrentIndex(fn_to_index(v)); },
+ v.SAFE_CONNTYPE);
+}
+
+template<typename t, typename F>
+void tie_setting(value<t>& v, QLabel* lb, F&& fun)
+{
+ auto closure = [lb, fun](detail::cv_qualified<t> v) { lb->setText(fun(v)); };
+
+ closure(v());
+ value_::connect(&v, value_::value_changed<t>(),
+ lb, closure,
+ v.SAFE_CONNTYPE);
+}
+
+template<typename t, typename F>
+void tie_setting(value<t>& v, QObject* obj, F&& fun)
+{
+ if (obj == nullptr)
+ abort();
+
+ fun(v());
+
+ value_::connect(&v, value_::value_changed<t>(),
+ obj, fun,
+ v.DIRECT_CONNTYPE);
}
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<QVariant>& v, QComboBox* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<bool>& v, QCheckBox* cb);
+OTR_OPTIONS_EXPORT void tie_setting(value<bool>& v, QRadioButton* cb);
OTR_OPTIONS_EXPORT void tie_setting(value<double>& v, QDoubleSpinBox* dsb);
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSpinBox* sb);
-OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QSlider* sl);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QLineEdit* le);
OTR_OPTIONS_EXPORT void tie_setting(value<QString>& v, QLabel* lb);
OTR_OPTIONS_EXPORT void tie_setting(value<int>& v, QTabWidget* t);
OTR_OPTIONS_EXPORT void tie_setting(value<slider_value>& v, QSlider* w);
} // ns options
+
+#if defined __GNUG__
+# pragma GCC diagnostic pop
+#endif