From 920815fe503f809e17b13f1551c71cd6a501483e Mon Sep 17 00:00:00 2001 From: Dennis Brentjes Date: Thu, 15 May 2014 16:22:25 +0200 Subject: Moved all the Adapters function to a common base --- qt_adapter.hpp | 96 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 66 insertions(+), 30 deletions(-) diff --git a/qt_adapter.hpp b/qt_adapter.hpp index 23a8c9f..9a5c383 100644 --- a/qt_adapter.hpp +++ b/qt_adapter.hpp @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -21,10 +22,27 @@ struct QtModelType typedef void type; }; +template +struct QtVectorModels; + +template <> +struct QtVectorModels +{ + typedef QAbstractListModel type; +}; + +template <> +struct QtVectorModels +{ + typedef QAbstractTableModel type; +}; + template struct QtModelType> { - typedef QAbstractTableModel type; + typedef typename QtVectorModels< + friendly_fusion::result_of::size::type::value == 1 + >::type type; }; template @@ -62,47 +80,27 @@ struct QtWidget : public T { } }; -template +template struct QtAdapterBase : public Model, public FusionModelObserver { - virtual void cell_changed(int row, int column) override - { - emit this->dataChanged(this->createIndex(row, column), this->createIndex(row, column)); - } - - virtual void append_row_begin() override - { - this->beginInsertRows(QModelIndex(), this->rowCount(QModelIndex()), this->rowCount(QModelIndex())); - } - - virtual void append_row_end() override - { - this->endInsertRows(); - } -}; - -template -struct QtAdapter : public QtAdapterBase -{ - typedef QTableView view; - typedef QtWidget widget; - std::shared_ptr model; - QtAdapter(std::shared_ptr model) + QtAdapterBase(std::shared_ptr model) : model(model) {} - virtual int rowCount(QModelIndex const&) const override - { - return model->row_count(); - } + //ModelInterface virtual int columnCount(QModelIndex const&) const override { return model->column_count(); } + virtual int rowCount(QModelIndex const&) const override + { + return model->row_count(); + } + bool setData(QModelIndex const& index, QVariant const& value, int role) override final { if(role != Qt::EditRole) return false; @@ -116,7 +114,7 @@ struct QtAdapter : public QtAdapterBase(index.column()); - return QAbstractTableModel::flags(index) | (constness ? Qt::NoItemFlags : Qt::ItemIsEditable); + return Model::flags(index) | (constness ? Qt::NoItemFlags : Qt::ItemIsEditable); } virtual QVariant data(QModelIndex const& index, int role) const override @@ -166,6 +164,44 @@ struct QtAdapter : public QtAdapterBasedataChanged(this->createIndex(row, column), this->createIndex(row, column)); + } + + virtual void append_row_begin() override + { + this->beginInsertRows(QModelIndex(), this->rowCount(QModelIndex()), this->rowCount(QModelIndex())); + } + + virtual void append_row_end() override + { + this->endInsertRows(); + } +}; + +template +struct QtAdapter : public QtAdapterBase +{ + typedef QtWidget widget; + + QtAdapter(std::shared_ptr model) + : QtAdapterBase(model) + {} +}; + +template +struct QtAdapter : public QtAdapterBase +{ + typedef QtWidget widget; + + QtAdapter(std::shared_ptr model) + : QtAdapterBase(model) + {} + }; template -- cgit v1.2.3-70-g09d2