From fce9ee31ec0ded06230fb7bce5b8b959e9968826 Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 6 Apr 2018 06:04:28 +0200 Subject: options/tie: preliminary support for early conversions --- options/tie.hpp | 62 ++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/options/tie.hpp b/options/tie.hpp index 4ebc1846..014dc84d 100644 --- a/options/tie.hpp +++ b/options/tie.hpp @@ -8,9 +8,12 @@ #pragma once -#include "export.hpp" +#include "value-traits.hpp" #include "value.hpp" #include "compat/run-in-thread.hpp" +#include "compat/macros.hpp" + +#include #include #include @@ -23,6 +26,8 @@ #include +#include "export.hpp" + #if defined __GNUG__ # pragma GCC diagnostic push # pragma GCC diagnostic ignored "-Wattributes" @@ -30,25 +35,61 @@ namespace options { +namespace detail { + template -std::enable_if_t::value> -tie_setting(value& v, QComboBox* cb) +struct tie_setting_traits_helper +{ + using traits = detail::value_traits; + using value_type = typename traits::value_type; + using element_type = typename traits::element_type; + + static element_type to_element_type(const value& v) + { + return static_cast(static_cast(v)); + } +}; + +template +struct tie_setting_traits final : tie_setting_traits_helper { - cb->setCurrentIndex(cb->findData(int(static_cast(v)))); - v = static_cast(cb->currentData().toInt()); + static constexpr inline bool should_bind_to_itemdata() { return false; } +}; + +template +struct tie_setting_traits>> : tie_setting_traits_helper +{ + static constexpr inline bool should_bind_to_itemdata() { return true; } + + static t itemdata_to_value(int, const QVariant& var) + { + return static_cast(var.toInt()); + } +}; + +} // ns options::details + +template> +std::enable_if_t +tie_setting(value& v, QComboBox* cb, const traits_type& traits = traits_type()) +{ + using element_type = typename detail::value_traits::element_type; + + cb->setCurrentIndex(cb->findData(traits.to_element_type(v))); + v = traits.itemdata_to_value(cb->currentIndex(), cb->currentData()); base_value::connect(cb, static_cast(&QComboBox::currentIndexChanged), &v, - [&v, cb](int idx) + [&v, cb, traits](int idx) { run_in_thread_sync(cb, - [&]() { - v = static_cast(cb->itemData(idx).toInt()); + [&, traits]() { + v = traits.itemdata_to_value(idx, cb->currentData()); }); }, v.DIRECT_CONNTYPE); - base_value::connect(&v, base_value::value_changed(), + base_value::connect(&v, base_value::value_changed(), cb, [cb](int x) { run_in_thread_sync(cb, [&]() { cb->setCurrentIndex(cb->findData(x)); }); }, @@ -79,6 +120,9 @@ void tie_setting(value& 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& v, QComboBox* cb); OTR_OPTIONS_EXPORT void tie_setting(value& v, QComboBox* cb); OTR_OPTIONS_EXPORT void tie_setting(value& v, QComboBox* cb); -- cgit v1.2.3