From 6a1e6120c4efc46f1d192b0e5fabc06b780113cc Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Tue, 6 May 2014 15:54:57 +0200 Subject: Made fusion model interface strict and the API now bypasses the QtAdapter and instantiates widgets with models instead of adapters --- qt_adapter.hpp | 66 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 20 deletions(-) (limited to 'qt_adapter.hpp') 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> typedef QAbstractTableModel type; }; -template +template +struct QtAdapter : public Q { + + typedef void view; + + QtAdapter(std::shared_ptr) + {} +}; + +template +struct AdapterType; + +template struct QtWidget : public T { GuiItemDelegate delegate; - std::shared_ptr model; + std::shared_ptr::type> model; - QtWidget(std::shared_ptr model, QWidget* parent = nullptr) + QtWidget(std::shared_ptr model, QWidget* parent = nullptr) : T(parent) , delegate() - , model(model) + , model(std::make_shared::type>(model)) { T::setModel(this->model.get()); T::setItemDelegate(&delegate); @@ -50,20 +62,11 @@ struct QtWidget : public T { } }; -template -struct QtAdapter : public Q { - - typedef void view; - - QtAdapter(std::shared_ptr) - {} -}; - template struct QtAdapter : public QAbstractTableModel { typedef QTableView view; - typedef QtWidget> widget; + typedef QtWidget widget; std::shared_ptr model; @@ -102,18 +105,42 @@ struct QtAdapter : public QAbstractTableModel return to_qvariant(model->get_cell(index.row(), index.column()), index.column()); } + template + typename std::enable_if::type get_key(int section) const + { + return QVariant(QString::fromStdString(model->key(section))); + } + + template + typename std::enable_if::type get_key(int section) const + { + return QVariant(); + } + + template + typename std::enable_if::type get_field_name(int section) const + { + return QVariant(QString::fromStdString(model->field_name(section))); + } + + template + typename std::enable_if::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(section); } - if(orientation == Qt::Vertical && T::has_header_v) { - return QVariant(QString::fromStdString(model->key(section))); + if(orientation == Qt::Vertical) { + return get_key(section); } return QVariant(); @@ -134,7 +161,6 @@ struct WidgetType { template std::shared_ptr::type> make_qt_widget(std::shared_ptr x) { - auto adapter_ptr = std::make_shared::type>(x); - auto widget_ptr = std::make_shared::type>(adapter_ptr); + auto widget_ptr = std::make_shared::type>(x); return widget_ptr; } -- cgit v1.2.3-70-g09d2