From a8d753c769fb98dc27066aed0f4df8fe101ad842 Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 6 May 2014 12:35:50 +0200 Subject: Cleaned up the interface a bit more. --- CMakeLists.txt | 1 + fusion_model.hpp | 8 ------ fusion_outputter.hpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ main.cpp | 43 +++++++++++++++++++---------- qt_adapter.hpp | 44 +++++++++++++++-------------- 5 files changed, 132 insertions(+), 42 deletions(-) create mode 100644 fusion_outputter.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index c5a8f5c..307d2a7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ add_executable(generic_gui fusion_static_dispatch.hpp meta_types.hpp boost_any_qvariant_convert.hpp + fusion_outputter.hpp gui_item_delegate.hpp gui_item_delegate.cpp main_window.hpp main_window.cpp ${qt_sources} diff --git a/fusion_model.hpp b/fusion_model.hpp index 3d68d94..116e49f 100644 --- a/fusion_model.hpp +++ b/fusion_model.hpp @@ -43,10 +43,6 @@ struct fusion_model> : public FusionModelInterface fusion_model() = default; - fusion_model(std::vector data) - : data(data) - {} - virtual size_t row_count() const override final { return data.size(); @@ -85,10 +81,6 @@ struct fusion_model> : public FusionModelInterface data) - : data(data) - {} - virtual size_t row_count() const override final { return data.size(); diff --git a/fusion_outputter.hpp b/fusion_outputter.hpp new file mode 100644 index 0000000..d148c8d --- /dev/null +++ b/fusion_outputter.hpp @@ -0,0 +1,78 @@ +#pragma once + +#include "friendly_fusion.hpp" + +#include +#include +#include + +#include +#include +#include +#include + +//Workaround for argument dependent lookup +namespace std +{ + +struct Outputter +{ + template + static typename std::enable_if::value, std::ostream&>::type + output(std::ostream& os, I const& it, E const& end) + { + if(std::is_same::type>::type, bool>::value) { + os << std::boolalpha; + } + + os << friendly_fusion::deref(it); + + if(!std::is_same::type, E>::value) { + os << ", "; + } + + return Outputter::output(os, friendly_fusion::advance_c<1>(it), end); + } + + template + static typename std::enable_if::value, std::ostream&>::type + output(std::ostream& os, I const&, E const&) + { + return os; + } + + +}; + +template +std::ostream& operator<<(std::ostream& os, std::map map) +{ + os << "{" << std::endl; + for (auto&& x : map) { + os << "\t{" << x.first << ", " << x.second << "}" << std::endl; + } + os << "}" << std::endl; + return os; +} + +template +std::ostream& operator<<(std::ostream& os, std::vector vec) +{ + os << "{" << std::endl; + for (auto&& x : vec) { + os << "\t" << x << std::endl; + } + os << "}" << std::endl; + return os; +} + +template +typename std::enable_if::value, std::ostream&>::type +operator<<(std::ostream& os, T x) +{ + return std::Outputter::output(os, friendly_fusion::begin(x), friendly_fusion::end(x)); +} + +} + + diff --git a/main.cpp b/main.cpp index 0e71544..262c1e9 100644 --- a/main.cpp +++ b/main.cpp @@ -4,6 +4,7 @@ #include "qt_adapter.hpp" #include "gui_item_delegate.hpp" #include "meta_types.hpp" +#include "fusion_outputter.hpp" #include @@ -12,7 +13,8 @@ struct Data { std::string name; uint32_t number; - double ratio; + double ratio1; + float ratio2; bool boolean; }; @@ -20,7 +22,8 @@ BOOST_FUSION_ADAPT_STRUCT( Data, (std::string, name) (uint32_t, number) - (double, ratio) + (double, ratio1) + (float,ratio2) (bool, boolean) ) @@ -38,23 +41,30 @@ struct DataMapping :public fusion_model> { } }; +struct CustomDataModelWidget : public WidgetType::type +{ + CustomDataModelWidget(std::shared_ptr::type> adapter) + : WidgetType::type(adapter) + {} +}; + int main() { - Data d1{"Pietje", 2, 3.333, true}; - Data d2{"Jantje", 3, 1.5, false}; - Data d3{"Sjaakje", 1, 0.1337, false}; + Data d1{"Pietje", 2, 3.333, 0.333, true}; + Data d2{"Jantje", 3, 1.5, 0.5, false}; + Data d3{"Sjaakje", 1, 0.1337, 0.0337, false}; - DataModel model; + auto model = std::make_shared(); - model.add_data(d1); - model.add_data(d2); - model.add_data(d3); + model->add_data(d1); + model->add_data(d2); + model->add_data(d3); - DataMapping mapping; + auto mapping = std::make_shared(); - mapping.add_data("nummer1", d1); - mapping.add_data("nummer2", d2); - mapping.add_data("nummer3", d3); + mapping->add_data("nummer1", d1); + mapping->add_data("nummer2", d2); + mapping->add_data("nummer3", d3); MainWindow w; @@ -64,5 +74,10 @@ int main() w.add_widget(widget1.get()); w.add_widget(widget2.get()); - return w.show_and_run(); + int ret = w.show_and_run(); + + std::cout << "model: " << std::endl << model->data << std::endl; + std::cout << "mapping: " << std::endl << mapping->data << std::endl; + + return ret; } diff --git a/qt_adapter.hpp b/qt_adapter.hpp index 6ce5ca1..2e6548a 100644 --- a/qt_adapter.hpp +++ b/qt_adapter.hpp @@ -32,11 +32,6 @@ struct QtModelType> typedef QAbstractTableModel type; }; -template -struct widget_type { - typedef typename T::widget type; -}; - template struct QtWidget : public T { @@ -59,7 +54,7 @@ struct QtAdapter : public Q { typedef void view; - QtAdapter(T) + QtAdapter(std::shared_ptr) {} }; @@ -69,27 +64,27 @@ struct QtAdapter : public QAbstractTableModel typedef QTableView view; typedef QtWidget> widget; - T model; + std::shared_ptr model; - QtAdapter(T model) + QtAdapter(std::shared_ptr model) : model(model) {} virtual int rowCount(QModelIndex const&) const override { - return model.row_count(); + return model->row_count(); } virtual int columnCount(QModelIndex const&) const override { - return model.column_count(); + return model->column_count(); } bool setData(QModelIndex const& index, QVariant const& value, int role) override final { if(role != Qt::EditRole) return false; - model.set_cell(index.row(), index.column(), to_boost_any(value, index.column())); + model->set_cell(index.row(), index.column(), to_boost_any(value, index.column())); return true; } @@ -103,7 +98,7 @@ struct QtAdapter : public QAbstractTableModel { if(role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); - return to_qvariant(model.get_cell(index.row(), index.column()), index.column()); + return to_qvariant(model->get_cell(index.row(), index.column()), index.column()); } virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override @@ -113,23 +108,32 @@ struct QtAdapter : public QAbstractTableModel } if(orientation == Qt::Horizontal && T::has_header_h) { - return QVariant(QString::fromStdString(model.field_name(section))); + return QVariant(QString::fromStdString(model->field_name(section))); } if(orientation == Qt::Vertical && T::has_header_v) { - return QVariant(QString::fromStdString(model.key(section))); + return QVariant(QString::fromStdString(model->key(section))); } return QVariant(); } }; -template -std::shared_ptr::type>>::type> -make_qt_widget(T& x) +template +struct AdapterType +{ + typedef QtAdapter::type> type; +}; + +template +struct WidgetType { + typedef typename AdapterType::type::widget type; +}; + +template +std::shared_ptr::type> make_qt_widget(std::shared_ptr x) { - typedef QtAdapter::type> adapter_type; - auto adapter_ptr = std::make_shared(x); - auto widget_ptr = std::make_shared::type>(adapter_ptr); + auto adapter_ptr = std::make_shared::type>(x); + auto widget_ptr = std::make_shared::type>(adapter_ptr); return widget_ptr; } -- cgit v1.2.3-70-g09d2