From d5549194a792e78ff7201acd0ae1a55859d0bb05 2017-06-15 10:10:32 From: Alexandre Leroux Date: 2017-06-15 10:10:32 Subject: [PATCH] Merge branch 'feature/VariableModel2' into develop --- diff --git a/core/include/Variable/VariableController.h b/core/include/Variable/VariableController.h index 0ec574b..d25da2b 100644 --- a/core/include/Variable/VariableController.h +++ b/core/include/Variable/VariableController.h @@ -7,6 +7,8 @@ #include class Variable; +class VariableModel; + Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController) /** @@ -25,6 +27,8 @@ public: */ Variable *createVariable(const QString &name) noexcept; + VariableModel *variableModel() noexcept; + public slots: void initialize(); void finalize(); diff --git a/core/include/Variable/VariableModel.h b/core/include/Variable/VariableModel.h index ad2e627..074da18 100644 --- a/core/include/Variable/VariableModel.h +++ b/core/include/Variable/VariableModel.h @@ -3,6 +3,7 @@ #include +#include #include Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel) @@ -12,9 +13,9 @@ class Variable; /** * @brief The VariableModel class aims to hold the variables that have been created in SciQlop */ -class VariableModel { +class VariableModel : public QAbstractTableModel { public: - explicit VariableModel(); + explicit VariableModel(QObject *parent = nullptr); /** * Creates a new variable in the model @@ -23,6 +24,15 @@ public: */ Variable *createVariable(const QString &name) noexcept; + // /////////////////////////// // + // QAbstractTableModel methods // + // /////////////////////////// // + virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override; + virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override; + virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; + virtual QVariant headerData(int section, Qt::Orientation orientation, + int role = Qt::DisplayRole) const override; + private: class VariableModelPrivate; spimpl::unique_impl_ptr impl; diff --git a/core/src/Variable/VariableController.cpp b/core/src/Variable/VariableController.cpp index 007c5c0..ce36585 100644 --- a/core/src/Variable/VariableController.cpp +++ b/core/src/Variable/VariableController.cpp @@ -35,6 +35,11 @@ Variable *VariableController::createVariable(const QString &name) noexcept return impl->m_VariableModel->createVariable(name); } +VariableModel *VariableController::variableModel() noexcept +{ + return impl->m_VariableModel.get(); +} + void VariableController::initialize() { qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); diff --git a/core/src/Variable/VariableModel.cpp b/core/src/Variable/VariableModel.cpp index 7758670..c7b7684 100644 --- a/core/src/Variable/VariableModel.cpp +++ b/core/src/Variable/VariableModel.cpp @@ -4,21 +4,114 @@ Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") +namespace { + +// Column indexes +const auto NAME_COLUMN = 0; +const auto UNIT_COLUMN = 1; +const auto MISSION_COLUMN = 2; +const auto NB_COLUMNS = 3; + +} // namespace + struct VariableModel::VariableModelPrivate { /// Variables created in SciQlop std::vector > m_Variables; }; -VariableModel::VariableModel() : impl{spimpl::make_unique_impl()} +VariableModel::VariableModel(QObject *parent) + : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl()} { } Variable *VariableModel::createVariable(const QString &name) noexcept { + auto insertIndex = rowCount(); + beginInsertRows({}, insertIndex, insertIndex); + /// @todo For the moment, the other data of the variable is initialized with default values auto variable = std::make_unique(name, QStringLiteral("unit"), QStringLiteral("mission")); impl->m_Variables.push_back(std::move(variable)); - return impl->m_Variables.back().get(); + endInsertRows(); + + return impl->m_Variables.at(insertIndex).get(); +} + +int VariableModel::columnCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + + return NB_COLUMNS; +} + +int VariableModel::rowCount(const QModelIndex &parent) const +{ + Q_UNUSED(parent); + + return impl->m_Variables.size(); +} + +QVariant VariableModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + return QVariant{}; + } + + if (index.row() < 0 || index.row() >= rowCount()) { + return QVariant{}; + } + + if (role == Qt::DisplayRole) { + const auto &variable = impl->m_Variables.at(index.row()); + + if (variable) { + switch (index.column()) { + case NAME_COLUMN: + return variable->m_Name; + case UNIT_COLUMN: + return variable->m_Unit; + case MISSION_COLUMN: + return variable->m_Mission; + default: + // No action + break; + } + + qWarning(LOG_VariableModel()) + << tr("Can't get data (unknown column %1)").arg(index.column()); + } + else { + qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); + } + } + + return QVariant{}; +} + +QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const +{ + if (role != Qt::DisplayRole) { + return QVariant{}; + } + + if (orientation == Qt::Horizontal) { + switch (section) { + case NAME_COLUMN: + return tr("Name"); + case UNIT_COLUMN: + return tr("Unit"); + case MISSION_COLUMN: + return tr("Mission"); + default: + // No action + break; + } + + qWarning(LOG_VariableModel()) + << tr("Can't get header data (unknown column %1)").arg(section); + } + + return QVariant{}; } diff --git a/gui/src/Variable/VariableInspectorWidget.cpp b/gui/src/Variable/VariableInspectorWidget.cpp index 724016c..7a788b7 100644 --- a/gui/src/Variable/VariableInspectorWidget.cpp +++ b/gui/src/Variable/VariableInspectorWidget.cpp @@ -1,11 +1,23 @@ +#include #include +#include #include +#include + +#include + VariableInspectorWidget::VariableInspectorWidget(QWidget *parent) : QWidget{parent}, ui{new Ui::VariableInspectorWidget} { ui->setupUi(this); + + // Sets model for table + auto sortFilterModel = new QSortFilterProxyModel{this}; + sortFilterModel->setSourceModel(sqpApp->variableController().variableModel()); + + ui->tableView->setModel(sortFilterModel); } VariableInspectorWidget::~VariableInspectorWidget()