diff options
Diffstat (limited to 'options')
-rw-r--r-- | options/tie.hpp | 94 |
1 files changed, 34 insertions, 60 deletions
diff --git a/options/tie.hpp b/options/tie.hpp index 307f5930..ca5eae5e 100644 --- a/options/tie.hpp +++ b/options/tie.hpp @@ -8,12 +8,10 @@ #pragma once -#include "value-traits.hpp" +#include "export.hpp" #include "value.hpp" #include "compat/run-in-thread.hpp" -#include "compat/macros.hpp" - -#include <type_traits> +#include "compat/meta.hpp" #include <QComboBox> #include <QCheckBox> @@ -26,8 +24,6 @@ #include <cmath> -#include "export.hpp" - #if defined __GNUG__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wattributes" @@ -35,67 +31,48 @@ namespace options { -namespace detail { - template<typename t> -struct tie_setting_traits_helper -{ - using traits = detail::value_traits<t>; - using value_type = typename traits::value_type; - using element_type = typename traits::element_type; - - static element_type to_element_type(const value<t>& v) - { - return static_cast<element_type>(static_cast<value_type>(v)); - } -}; - -template<typename t, typename Enable = void> -struct tie_setting_traits final : tie_setting_traits_helper<t> +std::enable_if_t<std::is_enum_v<t>> tie_setting(value<t>& v, QComboBox* cb) { - static constexpr inline bool should_bind_to_itemdata() { return false; } -}; - -template<typename t> -struct tie_setting_traits<t, std::enable_if_t<std::is_enum_v<t>>> : tie_setting_traits_helper<t> -{ - static constexpr inline bool should_bind_to_itemdata() { return true; } - - static t itemdata_to_value(int, const QVariant& var) - { - return static_cast<t>(var.toInt()); - } -}; - -} // ns options::detail - -template<typename t, typename traits_type = detail::tie_setting_traits<t>> -std::enable_if_t<traits_type::should_bind_to_itemdata()> -tie_setting(value<t>& v, QComboBox* cb, const traits_type& traits = traits_type()) -{ - using element_type = typename detail::value_traits<t>::element_type; - - cb->setCurrentIndex(cb->findData(traits.to_element_type(v))); - v = traits.itemdata_to_value(cb->currentIndex(), cb->currentData()); + 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, traits](int idx) - { - run_in_thread_sync(cb, - [&, traits]() { - v = traits.itemdata_to_value(idx, cb->currentData()); - }); - }, - v.DIRECT_CONNTYPE); - base_value::connect(&v, base_value::value_changed<element_type>(), + &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, base_value::value_changed<int>(), cb, [cb](int x) { - run_in_thread_sync(cb, [&]() { cb->setCurrentIndex(cb->findData(x)); }); + run_in_thread_sync(cb, [=]() { cb->setCurrentIndex(cb->findData(x)); }); }, v.DIRECT_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()); + + base_value::connect(cb, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), + &v, [&v, cb, fn_to_value](int idx) { + run_in_thread_sync(cb, [&]() { + v = fn_to_value(idx, cb->currentData()); + }); + }, v.DIRECT_CONNTYPE); + base_value::connect(&v, base_value::value_changed<t>(), + cb, [&v, cb, fn_to_index](cv_qualified<t>& v) { + run_in_thread_sync(cb, [&]() { + cb->setCurrentIndex(fn_to_index(v)); + }); + }); +} + template<typename t, typename F> void tie_setting(value<t>& v, QLabel* lb, F&& fun) { @@ -120,9 +97,6 @@ void tie_setting(value<t>& v, QObject* obj, F&& fun) v.DIRECT_CONNTYPE); } -// XXX TODO add combobox transform both ways via std::function -// need for non-translated `module_settings' dylib names - 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); |