##// END OF EJS Templates
Merge branch 'feature/VariableModel2' into develop
Alexandre Leroux -
r155:d5549194a792 merge
parent child
Show More
@@ -7,6 +7,8
7 7 #include <Common/spimpl.h>
8 8
9 9 class Variable;
10 class VariableModel;
11
10 12 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
11 13
12 14 /**
@@ -25,6 +27,8 public:
25 27 */
26 28 Variable *createVariable(const QString &name) noexcept;
27 29
30 VariableModel *variableModel() noexcept;
31
28 32 public slots:
29 33 void initialize();
30 34 void finalize();
@@ -3,6 +3,7
3 3
4 4 #include <Common/spimpl.h>
5 5
6 #include <QAbstractTableModel>
6 7 #include <QLoggingCategory>
7 8
8 9 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
@@ -12,9 +13,9 class Variable;
12 13 /**
13 14 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
14 15 */
15 class VariableModel {
16 class VariableModel : public QAbstractTableModel {
16 17 public:
17 explicit VariableModel();
18 explicit VariableModel(QObject *parent = nullptr);
18 19
19 20 /**
20 21 * Creates a new variable in the model
@@ -23,6 +24,15 public:
23 24 */
24 25 Variable *createVariable(const QString &name) noexcept;
25 26
27 // /////////////////////////// //
28 // QAbstractTableModel methods //
29 // /////////////////////////// //
30 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
31 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
32 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
33 virtual QVariant headerData(int section, Qt::Orientation orientation,
34 int role = Qt::DisplayRole) const override;
35
26 36 private:
27 37 class VariableModelPrivate;
28 38 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
@@ -35,6 +35,11 Variable *VariableController::createVariable(const QString &name) noexcept
35 35 return impl->m_VariableModel->createVariable(name);
36 36 }
37 37
38 VariableModel *VariableController::variableModel() noexcept
39 {
40 return impl->m_VariableModel.get();
41 }
42
38 43 void VariableController::initialize()
39 44 {
40 45 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
@@ -4,21 +4,114
4 4
5 5 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
6 6
7 namespace {
8
9 // Column indexes
10 const auto NAME_COLUMN = 0;
11 const auto UNIT_COLUMN = 1;
12 const auto MISSION_COLUMN = 2;
13 const auto NB_COLUMNS = 3;
14
15 } // namespace
16
7 17 struct VariableModel::VariableModelPrivate {
8 18 /// Variables created in SciQlop
9 19 std::vector<std::unique_ptr<Variable> > m_Variables;
10 20 };
11 21
12 VariableModel::VariableModel() : impl{spimpl::make_unique_impl<VariableModelPrivate>()}
22 VariableModel::VariableModel(QObject *parent)
23 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
13 24 {
14 25 }
15 26
16 27 Variable *VariableModel::createVariable(const QString &name) noexcept
17 28 {
29 auto insertIndex = rowCount();
30 beginInsertRows({}, insertIndex, insertIndex);
31
18 32 /// @todo For the moment, the other data of the variable is initialized with default values
19 33 auto variable
20 34 = std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission"));
21 35 impl->m_Variables.push_back(std::move(variable));
22 36
23 return impl->m_Variables.back().get();
37 endInsertRows();
38
39 return impl->m_Variables.at(insertIndex).get();
40 }
41
42 int VariableModel::columnCount(const QModelIndex &parent) const
43 {
44 Q_UNUSED(parent);
45
46 return NB_COLUMNS;
47 }
48
49 int VariableModel::rowCount(const QModelIndex &parent) const
50 {
51 Q_UNUSED(parent);
52
53 return impl->m_Variables.size();
54 }
55
56 QVariant VariableModel::data(const QModelIndex &index, int role) const
57 {
58 if (!index.isValid()) {
59 return QVariant{};
60 }
61
62 if (index.row() < 0 || index.row() >= rowCount()) {
63 return QVariant{};
64 }
65
66 if (role == Qt::DisplayRole) {
67 const auto &variable = impl->m_Variables.at(index.row());
68
69 if (variable) {
70 switch (index.column()) {
71 case NAME_COLUMN:
72 return variable->m_Name;
73 case UNIT_COLUMN:
74 return variable->m_Unit;
75 case MISSION_COLUMN:
76 return variable->m_Mission;
77 default:
78 // No action
79 break;
80 }
81
82 qWarning(LOG_VariableModel())
83 << tr("Can't get data (unknown column %1)").arg(index.column());
84 }
85 else {
86 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
87 }
88 }
89
90 return QVariant{};
91 }
92
93 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
94 {
95 if (role != Qt::DisplayRole) {
96 return QVariant{};
97 }
98
99 if (orientation == Qt::Horizontal) {
100 switch (section) {
101 case NAME_COLUMN:
102 return tr("Name");
103 case UNIT_COLUMN:
104 return tr("Unit");
105 case MISSION_COLUMN:
106 return tr("Mission");
107 default:
108 // No action
109 break;
110 }
111
112 qWarning(LOG_VariableModel())
113 << tr("Can't get header data (unknown column %1)").arg(section);
114 }
115
116 return QVariant{};
24 117 }
@@ -1,11 +1,23
1 #include <Variable/VariableController.h>
1 2 #include <Variable/VariableInspectorWidget.h>
3 #include <Variable/VariableModel.h>
2 4
3 5 #include <ui_VariableInspectorWidget.h>
4 6
7 #include <QSortFilterProxyModel>
8
9 #include <SqpApplication.h>
10
5 11 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
6 12 : QWidget{parent}, ui{new Ui::VariableInspectorWidget}
7 13 {
8 14 ui->setupUi(this);
15
16 // Sets model for table
17 auto sortFilterModel = new QSortFilterProxyModel{this};
18 sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
19
20 ui->tableView->setModel(sortFilterModel);
9 21 }
10 22
11 23 VariableInspectorWidget::~VariableInspectorWidget()
General Comments 0
You need to be logged in to leave comments. Login now