summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDennis Brentjes <d.brentjes@gmail.com>2014-05-06 15:54:57 +0200
committerDennis Brentjes <d.brentjes@gmail.com>2014-05-06 15:54:57 +0200
commit6a1e6120c4efc46f1d192b0e5fabc06b780113cc (patch)
tree96966eb5723f60f225a6359777743ba540ed0534
parent7f6f356e4498cf545209f4cb9ee5588d898ebb51 (diff)
downloadgeneric-gui-6a1e6120c4efc46f1d192b0e5fabc06b780113cc.tar.gz
generic-gui-6a1e6120c4efc46f1d192b0e5fabc06b780113cc.tar.bz2
generic-gui-6a1e6120c4efc46f1d192b0e5fabc06b780113cc.zip
Made fusion model interface strict and the API now bypasses the QtAdapter and instantiates widgets with models instead of adapters
-rw-r--r--fusion_model.hpp52
-rw-r--r--main.cpp10
-rw-r--r--qt_adapter.hpp66
3 files changed, 92 insertions, 36 deletions
diff --git a/fusion_model.hpp b/fusion_model.hpp
index 3d71f8d..282be11 100644
--- a/fusion_model.hpp
+++ b/fusion_model.hpp
@@ -16,18 +16,46 @@ 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");}
- virtual std::string key(size_t section) const {throw std::runtime_error("\"key(size_t)\" not implemented for this model");}
- virtual boost::any get_cell(size_t row, size_t column) const {throw std::runtime_error("\"get_cell(size_t, size_t)\" not implemented for this model");}
- virtual void set_cell(size_t row, size_t column, boost::any const& value) {throw std::runtime_error("\"set_cell(size_t, size_t, boost::any const&)\" not implemented for this model");}
+template <bool>
+struct FusionModelWithHeaderH;
+
+template <>
+struct FusionModelWithHeaderH<false>
+{
+ static constexpr bool has_header_h = false;
+};
+
+template <>
+struct FusionModelWithHeaderH<true>
+{
+ static constexpr bool has_header_h = true;
+
+ virtual std::string field_name(size_t section) const = 0;
+};
+
+template <bool>
+struct FusionModelWithHeaderV;
+
+template <>
+struct FusionModelWithHeaderV<false>
+{
+ static constexpr bool has_header_v = false;
+};
+
+template <>
+struct FusionModelWithHeaderV<true>
+{
+ static constexpr bool has_header_v = true;
+
+ virtual std::string key(size_t section) const = 0;
+};
+
+template <bool has_header_h, bool has_header_v>
+struct FusionModelInterface : public FusionModelWithHeaderH<has_header_h>, public FusionModelWithHeaderV<has_header_v> {
+ virtual size_t row_count() const = 0;
+ virtual size_t column_count() const = 0;
+ virtual boost::any get_cell(size_t row, size_t column) const = 0;
+ virtual void set_cell(size_t row, size_t column, boost::any const& value) = 0;
};
template <typename T>
diff --git a/main.cpp b/main.cpp
index e378fcb..3ff490e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -42,8 +42,8 @@ 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)
+ CustomDataModelWidget(std::shared_ptr<DataModel> model)
+ : WidgetType<DataModel>::type(model)
{}
};
@@ -67,11 +67,13 @@ int main()
MainWindow w;
- auto widget1 = make_qt_widget(model);
- auto widget2 = make_qt_widget(mapping);
+ auto widget1 = std::make_shared<CustomDataModelWidget>(model);
+ auto widget2 = make_qt_widget(model);
+ auto widget3 = make_qt_widget(mapping);
w.add_widget(widget1.get());
w.add_widget(widget2.get());
+ w.add_widget(widget3.get());
int ret = w.show_and_run();
diff --git a/qt_adapter.hpp b/qt_adapter.hpp
index c0aba79..dea660f 100644
--- a/qt_adapter.hpp
+++ b/qt_adapter.hpp
@@ -33,16 +33,28 @@ struct QtModelType<std::map<std::string, V>>
typedef QAbstractTableModel type;
};
-template <typename T, typename U>
+template <typename T, typename Q>
+struct QtAdapter : public Q {
+
+ typedef void view;
+
+ QtAdapter(std::shared_ptr<T>)
+ {}
+};
+
+template <typename T>
+struct AdapterType;
+
+template <typename T, typename Model>
struct QtWidget : public T {
GuiItemDelegate delegate;
- std::shared_ptr<U> model;
+ std::shared_ptr<typename AdapterType<Model>::type> model;
- QtWidget(std::shared_ptr<U> model, QWidget* parent = nullptr)
+ QtWidget(std::shared_ptr<Model> model, QWidget* parent = nullptr)
: T(parent)
, delegate()
- , model(model)
+ , model(std::make_shared<typename AdapterType<Model>::type>(model))
{
T::setModel(this->model.get());
T::setItemDelegate(&delegate);
@@ -50,20 +62,11 @@ struct QtWidget : public T {
}
};
-template <typename T, typename Q>
-struct QtAdapter : public Q {
-
- typedef void view;
-
- QtAdapter(std::shared_ptr<T>)
- {}
-};
-
template <typename T>
struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel
{
typedef QTableView view;
- typedef QtWidget<view, QtAdapter<T, QAbstractTableModel>> widget;
+ typedef QtWidget<view, T> widget;
std::shared_ptr<T> model;
@@ -102,18 +105,42 @@ struct QtAdapter<T, QAbstractTableModel> : public QAbstractTableModel
return to_qvariant<typename T::row_type>(model->get_cell(index.row(), index.column()), index.column());
}
+ template <bool b>
+ typename std::enable_if<b, QVariant>::type get_key(int section) const
+ {
+ return QVariant(QString::fromStdString(model->key(section)));
+ }
+
+ template <bool b>
+ typename std::enable_if<!b, QVariant>::type get_key(int section) const
+ {
+ return QVariant();
+ }
+
+ template <bool b>
+ typename std::enable_if<b, QVariant>::type get_field_name(int section) const
+ {
+ return QVariant(QString::fromStdString(model->field_name(section)));
+ }
+
+ template <bool b>
+ typename std::enable_if<!b, QVariant>::type get_field_name(int section) const
+ {
+ return QVariant();
+ }
+
virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const override
{
if(role != Qt::DisplayRole) {
return QVariant();
}
- if(orientation == Qt::Horizontal && T::has_header_h) {
- return QVariant(QString::fromStdString(model->field_name(section)));
+ if(orientation == Qt::Horizontal) {
+ return get_field_name<T::has_header_h>(section);
}
- if(orientation == Qt::Vertical && T::has_header_v) {
- return QVariant(QString::fromStdString(model->key(section)));
+ if(orientation == Qt::Vertical) {
+ return get_key<T::has_header_v>(section);
}
return QVariant();
@@ -134,7 +161,6 @@ struct WidgetType {
template <typename Model>
std::shared_ptr<typename WidgetType<Model>::type> make_qt_widget(std::shared_ptr<Model> x)
{
- auto adapter_ptr = std::make_shared<typename AdapterType<Model>::type>(x);
- auto widget_ptr = std::make_shared<typename WidgetType<Model>::type>(adapter_ptr);
+ auto widget_ptr = std::make_shared<typename WidgetType<Model>::type>(x);
return widget_ptr;
}