@@ -1,117 +1,115 | |||||
1 | #include <Variable/VariableModel.h> |
|
1 | #include <Variable/VariableModel.h> | |
2 |
|
2 | |||
3 | #include <Variable/Variable.h> |
|
3 | #include <Variable/Variable.h> | |
4 |
|
4 | |||
5 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
5 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | |
6 |
|
6 | |||
7 | namespace { |
|
7 | namespace { | |
8 |
|
8 | |||
9 | // Column indexes |
|
9 | // Column indexes | |
10 | const auto NAME_COLUMN = 0; |
|
10 | const auto NAME_COLUMN = 0; | |
11 | const auto UNIT_COLUMN = 1; |
|
11 | const auto UNIT_COLUMN = 1; | |
12 | const auto MISSION_COLUMN = 2; |
|
12 | const auto MISSION_COLUMN = 2; | |
13 | const auto NB_COLUMNS = 3; |
|
13 | const auto NB_COLUMNS = 3; | |
14 |
|
14 | |||
15 | } // namespace |
|
15 | } // namespace | |
16 |
|
16 | |||
17 | struct VariableModel::VariableModelPrivate { |
|
17 | struct VariableModel::VariableModelPrivate { | |
18 | /// Variables created in SciQlop |
|
18 | /// Variables created in SciQlop | |
19 | std::vector<std::unique_ptr<Variable> > m_Variables; |
|
19 | std::vector<std::unique_ptr<Variable> > m_Variables; | |
20 | }; |
|
20 | }; | |
21 |
|
21 | |||
22 | VariableModel::VariableModel(QObject *parent) |
|
22 | VariableModel::VariableModel(QObject *parent) | |
23 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
23 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | |
24 | { |
|
24 | { | |
25 | } |
|
25 | } | |
26 |
|
26 | |||
27 | Variable *VariableModel::createVariable(const QString &name) noexcept |
|
27 | Variable *VariableModel::createVariable(const QString &name) noexcept | |
28 | { |
|
28 | { | |
29 | auto insertIndex = rowCount(); |
|
29 | auto insertIndex = rowCount(); | |
30 | beginInsertRows({}, insertIndex, insertIndex); |
|
30 | beginInsertRows({}, insertIndex, insertIndex); | |
31 |
|
31 | |||
32 | /// @todo For the moment, the other data of the variable is initialized with default values |
|
32 | /// @todo For the moment, the other data of the variable is initialized with default values | |
33 | auto variable |
|
33 | auto variable | |
34 | = std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission")); |
|
34 | = std::make_unique<Variable>(name, QStringLiteral("unit"), QStringLiteral("mission")); | |
35 | impl->m_Variables.push_back(std::move(variable)); |
|
35 | impl->m_Variables.push_back(std::move(variable)); | |
36 |
|
36 | |||
37 | endInsertRows(); |
|
37 | endInsertRows(); | |
38 |
|
38 | |||
39 | return impl->m_Variables.at(insertIndex).get(); |
|
39 | return impl->m_Variables.at(insertIndex).get(); | |
40 | } |
|
40 | } | |
41 |
|
41 | |||
42 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
42 | int VariableModel::columnCount(const QModelIndex &parent) const | |
43 | { |
|
43 | { | |
44 | Q_UNUSED(parent); |
|
44 | Q_UNUSED(parent); | |
45 |
|
45 | |||
46 | return NB_COLUMNS; |
|
46 | return NB_COLUMNS; | |
47 | } |
|
47 | } | |
48 |
|
48 | |||
49 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
49 | int VariableModel::rowCount(const QModelIndex &parent) const | |
50 | { |
|
50 | { | |
51 | Q_UNUSED(parent); |
|
51 | Q_UNUSED(parent); | |
52 |
|
52 | |||
53 | return impl->m_Variables.size(); |
|
53 | return impl->m_Variables.size(); | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
56 | QVariant VariableModel::data(const QModelIndex &index, int role) const | |
57 | { |
|
57 | { | |
58 | if (!index.isValid()) { |
|
58 | if (!index.isValid()) { | |
59 | return QVariant{}; |
|
59 | return QVariant{}; | |
60 | } |
|
60 | } | |
61 |
|
61 | |||
62 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
62 | if (index.row() < 0 || index.row() >= rowCount()) { | |
63 | return QVariant{}; |
|
63 | return QVariant{}; | |
64 | } |
|
64 | } | |
65 |
|
65 | |||
66 | if (role == Qt::DisplayRole) { |
|
66 | if (role == Qt::DisplayRole) { | |
67 |
|
|
67 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | |
68 |
|
||||
69 | if (variable) { |
|
|||
70 | switch (index.column()) { |
|
68 | switch (index.column()) { | |
71 | case NAME_COLUMN: |
|
69 | case NAME_COLUMN: | |
72 | return variable->m_Name; |
|
70 | return variable->m_Name; | |
73 | case UNIT_COLUMN: |
|
71 | case UNIT_COLUMN: | |
74 | return variable->m_Unit; |
|
72 | return variable->m_Unit; | |
75 | case MISSION_COLUMN: |
|
73 | case MISSION_COLUMN: | |
76 | return variable->m_Mission; |
|
74 | return variable->m_Mission; | |
77 | default: |
|
75 | default: | |
78 | // No action |
|
76 | // No action | |
79 | break; |
|
77 | break; | |
80 | } |
|
78 | } | |
81 |
|
79 | |||
82 | qWarning(LOG_VariableModel()) |
|
80 | qWarning(LOG_VariableModel()) | |
83 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
81 | << tr("Can't get data (unknown column %1)").arg(index.column()); | |
84 | } |
|
82 | } | |
85 | else { |
|
83 | else { | |
86 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
84 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); | |
87 | } |
|
85 | } | |
88 | } |
|
86 | } | |
89 |
|
87 | |||
90 | return QVariant{}; |
|
88 | return QVariant{}; | |
91 | } |
|
89 | } | |
92 |
|
90 | |||
93 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
91 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | |
94 | { |
|
92 | { | |
95 | if (role != Qt::DisplayRole) { |
|
93 | if (role != Qt::DisplayRole) { | |
96 | return QVariant{}; |
|
94 | return QVariant{}; | |
97 | } |
|
95 | } | |
98 |
|
96 | |||
99 | if (orientation == Qt::Horizontal) { |
|
97 | if (orientation == Qt::Horizontal) { | |
100 | switch (section) { |
|
98 | switch (section) { | |
101 | case NAME_COLUMN: |
|
99 | case NAME_COLUMN: | |
102 | return tr("Name"); |
|
100 | return tr("Name"); | |
103 | case UNIT_COLUMN: |
|
101 | case UNIT_COLUMN: | |
104 | return tr("Unit"); |
|
102 | return tr("Unit"); | |
105 | case MISSION_COLUMN: |
|
103 | case MISSION_COLUMN: | |
106 | return tr("Mission"); |
|
104 | return tr("Mission"); | |
107 | default: |
|
105 | default: | |
108 | // No action |
|
106 | // No action | |
109 | break; |
|
107 | break; | |
110 | } |
|
108 | } | |
111 |
|
109 | |||
112 | qWarning(LOG_VariableModel()) |
|
110 | qWarning(LOG_VariableModel()) | |
113 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
111 | << tr("Can't get header data (unknown column %1)").arg(section); | |
114 | } |
|
112 | } | |
115 |
|
113 | |||
116 | return QVariant{}; |
|
114 | return QVariant{}; | |
117 | } |
|
115 | } |
General Comments 0
You need to be logged in to leave comments.
Login now