diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-05 15:36:59 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-05 15:36:59 +0200 |
| commit | dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669 (patch) | |
| tree | 38db4835d86d4f056395ed731affb9afc2e558aa | |
| parent | 13cfd05f9b2bb3c8d7d1bf47fcef037a745bacf8 (diff) | |
| download | generic-gui-dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669.tar.gz generic-gui-dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669.tar.bz2 generic-gui-dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669.zip | |
Cleaned up the api to eliminate some intermediate steps
| -rw-r--r-- | CMakeLists.txt | 8 | ||||
| -rw-r--r-- | boost_any_qvariant_convert.hpp | 4 | ||||
| -rw-r--r-- | fusion_model.hpp | 27 | ||||
| -rw-r--r-- | fusion_static_dispatch.hpp | 20 | ||||
| -rw-r--r-- | gui_item_delegate.cpp | 4 | ||||
| -rw-r--r-- | main.cpp | 24 | ||||
| -rw-r--r-- | qt_adapter.hpp | 27 |
7 files changed, 65 insertions, 49 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a880f9..c5a8f5c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ - -project(msc-scriptie) +cmake_minimum_required(VERSION 2.8.12.2) +project(generic_gui) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --std=c++11 -g3") @@ -43,7 +43,7 @@ qt5_wrap_cpp(qt_sources gui_item_delegate.hpp ) -add_executable(msc-scriptie +add_executable(generic_gui fusion_model.hpp index_list.hpp qt_adapter.hpp @@ -58,7 +58,7 @@ add_executable(msc-scriptie ) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS} ${Qt5Gui_EXECUTABLE_COMPILE_FLAGS} ${Qt5Core_EXECUTABLE_COMPILE_FLAGS}") -target_link_libraries(msc-scriptie ${Qt5Widgets_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Core_LIBRARIES}) +target_link_libraries(generic_gui ${Qt5Widgets_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Core_LIBRARIES}) diff --git a/boost_any_qvariant_convert.hpp b/boost_any_qvariant_convert.hpp index 859623b..1f6e2be 100644 --- a/boost_any_qvariant_convert.hpp +++ b/boost_any_qvariant_convert.hpp @@ -31,7 +31,7 @@ std::function<QVariant(boost::any const&)> to_qvariant_lambda() { return [](boost::any const& any) { - return convert<T, index>(any); + return to_qvariant<T, index>(any); }; } @@ -83,7 +83,7 @@ boost::any to_boost_any(QVariant const& x) #if defined( __GNUC__ ) && !defined( __clang__ ) template <typename T, int index> -std::function<boost::any(QVariant const&)> to_qvariant_lambda() +std::function<boost::any(QVariant const&)> to_boost_any_lambda() { return [](QVariant const& value) { diff --git a/fusion_model.hpp b/fusion_model.hpp index 4ccaa5b..3d68d94 100644 --- a/fusion_model.hpp +++ b/fusion_model.hpp @@ -17,7 +17,12 @@ private: fusion_model(T); }; +template <bool header_h, bool header_v> struct FusionModelInterface { + + static constexpr bool has_header_h = header_h; + static constexpr bool has_header_v = header_v; + virtual size_t row_count() const {throw std::runtime_error("\"row_count()\" not implemented for this model");} virtual size_t column_count() const {throw std::runtime_error("\"column_count()\" not implemented for this model");} virtual std::string field_name(size_t section) const {throw std::runtime_error("\"field_name(size_t)\" not implemented for this model");} @@ -27,18 +32,21 @@ struct FusionModelInterface { }; template <typename T> -struct fusion_model<std::vector<T>> : public FusionModelInterface +struct fusion_model<std::vector<T>> : public FusionModelInterface<true, false> { static_assert(friendly_fusion::traits::is_sequence<T>::type::value, "T is not a boost fusion sequence"); typedef std::vector<T> data_type; typedef T row_type; - static constexpr bool has_header_h = true; - static constexpr bool has_header_v = false; - std::vector<T> data; + fusion_model() = default; + + fusion_model(std::vector<T> data) + : data(data) + {} + virtual size_t row_count() const override final { return data.size(); @@ -66,18 +74,21 @@ struct fusion_model<std::vector<T>> : public FusionModelInterface }; template <typename T> -struct fusion_model<std::map<std::string, T>> : public FusionModelInterface +struct fusion_model<std::map<std::string, T>> : public FusionModelInterface<true, true> { static_assert(boost::fusion::traits::is_sequence<T>::type::value, "T is not a boost fusion sequence"); typedef std::map<std::string, T> data_type; typedef T row_type; - static constexpr bool has_header_h = true; - static constexpr bool has_header_v = true; - std::map<std::string, T> data; + fusion_model() = default; + + fusion_model(std::map<std::string, T> data) + : data(data) + {} + virtual size_t row_count() const override final { return data.size(); diff --git a/fusion_static_dispatch.hpp b/fusion_static_dispatch.hpp index b0037a6..ce81c90 100644 --- a/fusion_static_dispatch.hpp +++ b/fusion_static_dispatch.hpp @@ -54,12 +54,31 @@ boost::any get_nth(T x, int index) return get_nth_functor<T>()(x, index); } +#if defined( __GNUC__ ) && !defined( __clang__ ) +template<int index, typename T> +std::function<void(T&, boost::any const&)> make_set_nth_lambda() +{ + return [](T& seq, boost::any const& value){ + typedef friendly_fusion::result_of::begin<T> begin; + typedef friendly_fusion::result_of::advance_c<typename begin::type, index> adv_it; + typedef friendly_fusion::result_of::deref<typename adv_it::type> deref; + typedef typename std::decay<typename deref::type>::type value_type; + + friendly_fusion::deref(friendly_fusion::advance_c<index>(friendly_fusion::begin(seq))) = boost::any_cast<value_type>(value); + }; +} +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + template <typename T, int... Indices> void set_nth_impl(T& seq, int index, boost::any const& value, indices<Indices...>) { typedef std::function<void(T&, boost::any const&)> element_type; static element_type table[] = { + #if defined( __GNUC__ ) && !defined( __clang__ ) + make_set_nth_lambda<Indices, T>() + ... + #else [](T& seq, boost::any const& value) { typedef friendly_fusion::result_of::begin<T> begin; @@ -70,6 +89,7 @@ void set_nth_impl(T& seq, int index, boost::any const& value, indices<Indices... friendly_fusion::deref(friendly_fusion::advance_c<Indices>(friendly_fusion::begin(seq))) = boost::any_cast<value_type>(value); } ... + #endif //defined( __GNUC__ ) && !defined( __clang__ ) }; table[index](seq, value); diff --git a/gui_item_delegate.cpp b/gui_item_delegate.cpp index 7527020..4a3633f 100644 --- a/gui_item_delegate.cpp +++ b/gui_item_delegate.cpp @@ -16,7 +16,9 @@ void GuiItemDelegate::setModelData(QWidget* widget, QAbstractItemModel* model, c QByteArray n = widget->metaObject()->userProperty().name(); QVariant variant = widget->property(n); - if(variant.type() == QVariant::String) { + std::cout << std::string(widget->metaObject()->userProperty().typeName()) << std::endl; + if(variant.userType() == QVariant::String && variant.userType() != QMetaType::Float) { + std::cout << "lolwut" << std::endl; model->setData(index, QVariant::fromValue(variant.toString().toStdString()), Qt::EditRole); } else { QStyledItemDelegate::setModelData(widget, model, index); @@ -12,7 +12,7 @@ struct Data { std::string name; uint32_t number; - float ratio; + double ratio; bool boolean; }; @@ -20,7 +20,7 @@ BOOST_FUSION_ADAPT_STRUCT( Data, (std::string, name) (uint32_t, number) - (float, ratio) + (double, ratio) (bool, boolean) ) @@ -40,9 +40,9 @@ struct DataMapping :public fusion_model<std::map<std::string, Data>> { int main() { - Data d1{"Pietje", 2, 3.333f, true}; - Data d2{"Jantje", 3, 1.5f, false}; - Data d3{"Sjaakje", 1, 0.1337f, false}; + Data d1{"Pietje", 2, 3.333, true}; + Data d2{"Jantje", 3, 1.5, false}; + Data d3{"Sjaakje", 1, 0.1337, false}; DataModel model; @@ -50,23 +50,19 @@ int main() model.add_data(d2); model.add_data(d3); - auto adapter = make_qt_adapter(model); - DataMapping mapping; mapping.add_data("nummer1", d1); mapping.add_data("nummer2", d2); mapping.add_data("nummer3", d3); - - auto adapter2 = make_qt_adapter(mapping); MainWindow w; - widget_type<decltype(adapter)>::type widget(adapter); - widget_type<decltype(adapter2)>::type widget2(adapter2); - - w.add_widget(&widget); - w.add_widget(&widget2); + auto widget1 = make_qt_widget(model); + auto widget2 = make_qt_widget(mapping); + + w.add_widget(widget1.get()); + w.add_widget(widget2.get()); return w.show_and_run(); } diff --git a/qt_adapter.hpp b/qt_adapter.hpp index 8af44d7..6ce5ca1 100644 --- a/qt_adapter.hpp +++ b/qt_adapter.hpp @@ -34,23 +34,6 @@ struct QtModelType<std::map<std::string, V>> template <typename T> struct widget_type { -// typedef void type; -}; - -template <typename T> -struct widget_type<std::shared_ptr<T>> { - typedef typename T::widget type; -}; - -template <typename T> -struct widget_type<std::shared_ptr<T>&&> -{ - typedef typename T::widget type; -}; - -template <typename T> -struct widget_type<std::shared_ptr<T>&> -{ typedef typename T::widget type; }; @@ -142,7 +125,11 @@ struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel }; template <typename T> -std::shared_ptr<QtAdapter<T, typename QtModelType<typename T::data_type>::type>> make_qt_adapter(T value) { - typedef QtAdapter<T, typename QtModelType<typename T::data_type>::type> type; - return std::make_shared<type>(value); +std::shared_ptr<typename widget_type<QtAdapter<T, typename QtModelType<typename T::data_type>::type>>::type> +make_qt_widget(T& x) +{ + typedef QtAdapter<T, typename QtModelType<typename T::data_type>::type> adapter_type; + auto adapter_ptr = std::make_shared<adapter_type>(x); + auto widget_ptr = std::make_shared<typename widget_type<adapter_type>::type>(adapter_ptr); + return widget_ptr; } |
