From dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Mon, 5 May 2014 15:36:59 +0200 Subject: Cleaned up the api to eliminate some intermediate steps --- CMakeLists.txt | 8 ++++---- boost_any_qvariant_convert.hpp | 4 ++-- fusion_model.hpp | 27 +++++++++++++++++++-------- fusion_static_dispatch.hpp | 20 ++++++++++++++++++++ gui_item_delegate.cpp | 4 +++- main.cpp | 24 ++++++++++-------------- 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 to_qvariant_lambda() { return [](boost::any const& any) { - return convert(any); + return to_qvariant(any); }; } @@ -83,7 +83,7 @@ boost::any to_boost_any(QVariant const& x) #if defined( __GNUC__ ) && !defined( __clang__ ) template -std::function to_qvariant_lambda() +std::function 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 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 -struct fusion_model> : public FusionModelInterface +struct fusion_model> : public FusionModelInterface { static_assert(friendly_fusion::traits::is_sequence::type::value, "T is not a boost fusion sequence"); typedef std::vector data_type; typedef T row_type; - static constexpr bool has_header_h = true; - static constexpr bool has_header_v = false; - std::vector data; + fusion_model() = default; + + fusion_model(std::vector data) + : data(data) + {} + virtual size_t row_count() const override final { return data.size(); @@ -66,18 +74,21 @@ struct fusion_model> : public FusionModelInterface }; template -struct fusion_model> : public FusionModelInterface +struct fusion_model> : public FusionModelInterface { static_assert(boost::fusion::traits::is_sequence::type::value, "T is not a boost fusion sequence"); typedef std::map data_type; typedef T row_type; - static constexpr bool has_header_h = true; - static constexpr bool has_header_v = true; - std::map data; + fusion_model() = default; + + fusion_model(std::map 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()(x, index); } +#if defined( __GNUC__ ) && !defined( __clang__ ) +template +std::function make_set_nth_lambda() +{ + return [](T& seq, boost::any const& value){ + typedef friendly_fusion::result_of::begin begin; + typedef friendly_fusion::result_of::advance_c adv_it; + typedef friendly_fusion::result_of::deref deref; + typedef typename std::decay::type value_type; + + friendly_fusion::deref(friendly_fusion::advance_c(friendly_fusion::begin(seq))) = boost::any_cast(value); + }; +} +#endif //defined( __GNUC__ ) && !defined( __clang__ ) + template void set_nth_impl(T& seq, int index, boost::any const& value, indices) { typedef std::function element_type; static element_type table[] = { + #if defined( __GNUC__ ) && !defined( __clang__ ) + make_set_nth_lambda() + ... + #else [](T& seq, boost::any const& value) { typedef friendly_fusion::result_of::begin begin; @@ -70,6 +89,7 @@ void set_nth_impl(T& seq, int index, boost::any const& value, indices(friendly_fusion::begin(seq))) = boost::any_cast(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); diff --git a/main.cpp b/main.cpp index 6af725f..0e71544 100644 --- a/main.cpp +++ b/main.cpp @@ -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> { 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::type widget(adapter); - widget_type::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> template struct widget_type { -// typedef void type; -}; - -template -struct widget_type> { - typedef typename T::widget type; -}; - -template -struct widget_type&&> -{ - typedef typename T::widget type; -}; - -template -struct widget_type&> -{ typedef typename T::widget type; }; @@ -142,7 +125,11 @@ struct QtAdapter : public QAbstractTableModel }; template -std::shared_ptr::type>> make_qt_adapter(T value) { - typedef QtAdapter::type> type; - return std::make_shared(value); +std::shared_ptr::type>>::type> +make_qt_widget(T& x) +{ + typedef QtAdapter::type> adapter_type; + auto adapter_ptr = std::make_shared(x); + auto widget_ptr = std::make_shared::type>(adapter_ptr); + return widget_ptr; } -- cgit v1.2.3-70-g09d2