diff options
| author | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-06 12:35:50 +0200 |
|---|---|---|
| committer | Dennis Brentjes <d.brentjes@gmail.com> | 2014-05-06 12:35:50 +0200 |
| commit | a8d753c769fb98dc27066aed0f4df8fe101ad842 (patch) | |
| tree | c38bc3c6a2ee513a565364a6686f5b3f2c2d899e | |
| parent | dfefe42d0fe40dd97260d3ec0b3e1cb7690cf669 (diff) | |
| download | generic-gui-a8d753c769fb98dc27066aed0f4df8fe101ad842.tar.gz generic-gui-a8d753c769fb98dc27066aed0f4df8fe101ad842.tar.bz2 generic-gui-a8d753c769fb98dc27066aed0f4df8fe101ad842.zip | |
Cleaned up the interface a bit more.
| -rw-r--r-- | CMakeLists.txt | 1 | ||||
| -rw-r--r-- | fusion_model.hpp | 8 | ||||
| -rw-r--r-- | fusion_outputter.hpp | 78 | ||||
| -rw-r--r-- | main.cpp | 43 | ||||
| -rw-r--r-- | qt_adapter.hpp | 44 |
5 files changed, 132 insertions, 42 deletions
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<std::vector<T>> : public FusionModelInterface<true, false> fusion_model() = default; - fusion_model(std::vector<T> data) - : data(data) - {} - virtual size_t row_count() const override final { return data.size(); @@ -85,10 +81,6 @@ struct fusion_model<std::map<std::string, T>> : public FusionModelInterface<true 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_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 <boost/fusion/include/pair.hpp> +#include <boost/fusion/include/map.hpp> +#include <boost/fusion/include/vector.hpp> + +#include <iostream> +#include <ostream> +#include <vector> +#include <string> + +//Workaround for argument dependent lookup +namespace std +{ + +struct Outputter +{ + template <typename I, typename E> + static typename std::enable_if<!std::is_same<I,E>::value, std::ostream&>::type + output(std::ostream& os, I const& it, E const& end) + { + if(std::is_same<typename std::decay<typename friendly_fusion::result_of::deref<I>::type>::type, bool>::value) { + os << std::boolalpha; + } + + os << friendly_fusion::deref(it); + + if(!std::is_same<typename friendly_fusion::result_of::advance_c<I, 1>::type, E>::value) { + os << ", "; + } + + return Outputter::output(os, friendly_fusion::advance_c<1>(it), end); + } + + template <typename I, typename E> + static typename std::enable_if<std::is_same<I,E>::value, std::ostream&>::type + output(std::ostream& os, I const&, E const&) + { + return os; + } + + +}; + +template <typename T, typename U> +std::ostream& operator<<(std::ostream& os, std::map<T, U> map) +{ + os << "{" << std::endl; + for (auto&& x : map) { + os << "\t{" << x.first << ", " << x.second << "}" << std::endl; + } + os << "}" << std::endl; + return os; +} + +template <typename T> +std::ostream& operator<<(std::ostream& os, std::vector<T> vec) +{ + os << "{" << std::endl; + for (auto&& x : vec) { + os << "\t" << x << std::endl; + } + os << "}" << std::endl; + return os; +} + +template <typename T> +typename std::enable_if<friendly_fusion::traits::is_sequence<T>::value, std::ostream&>::type +operator<<(std::ostream& os, T x) +{ + return std::Outputter::output(os, friendly_fusion::begin(x), friendly_fusion::end(x)); +} + +} + + @@ -4,6 +4,7 @@ #include "qt_adapter.hpp" #include "gui_item_delegate.hpp" #include "meta_types.hpp" +#include "fusion_outputter.hpp" #include <boost/fusion/adapted.hpp> @@ -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<std::map<std::string, Data>> { } }; +struct CustomDataModelWidget : public WidgetType<DataModel>::type +{ + CustomDataModelWidget(std::shared_ptr<AdapterType<DataModel>::type> adapter) + : WidgetType<DataModel>::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<DataModel>(); - 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<DataMapping>(); - 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<std::map<std::string, V>> typedef QAbstractTableModel type; }; -template <typename T> -struct widget_type { - typedef typename T::widget type; -}; - template <typename T, typename U> struct QtWidget : public T { @@ -59,7 +54,7 @@ struct QtAdapter : public Q { typedef void view; - QtAdapter(T) + QtAdapter(std::shared_ptr<T>) {} }; @@ -69,27 +64,27 @@ struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel typedef QTableView view; typedef QtWidget<view, QtAdapter<T, QAbstractTableModel>> widget; - T model; + std::shared_ptr<T> model; - QtAdapter(T model) + QtAdapter(std::shared_ptr<T> 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<typename T::row_type>(value, index.column())); + model->set_cell(index.row(), index.column(), to_boost_any<typename T::row_type>(value, index.column())); return true; } @@ -103,7 +98,7 @@ struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel { if(role != Qt::DisplayRole && role != Qt::EditRole) return QVariant(); - return to_qvariant<typename T::row_type>(model.get_cell(index.row(), index.column()), index.column()); + return to_qvariant<typename T::row_type>(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<T, QAbstractTableModel> : 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 <typename T> -std::shared_ptr<typename widget_type<QtAdapter<T, typename QtModelType<typename T::data_type>::type>>::type> -make_qt_widget(T& x) +template <typename Model> +struct AdapterType +{ + typedef QtAdapter<Model, typename QtModelType<typename Model::data_type>::type> type; +}; + +template <typename Model> +struct WidgetType { + typedef typename AdapterType<Model>::type::widget type; +}; + +template <typename Model> +std::shared_ptr<typename WidgetType<Model>::type> make_qt_widget(std::shared_ptr<Model> 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); + auto adapter_ptr = std::make_shared<typename AdapterType<Model>::type>(x); + auto widget_ptr = std::make_shared<typename WidgetType<Model>::type>(adapter_ptr); return widget_ptr; } |
