@@ -1,243 +1,248 | |||||
1 | #include <Variable/Variable.h> |
|
1 | #include <Variable/Variable.h> | |
2 | #include <Variable/VariableModel.h> |
|
2 | #include <Variable/VariableModel.h> | |
3 |
|
3 | |||
4 | #include <Data/IDataSeries.h> |
|
4 | #include <Data/IDataSeries.h> | |
5 |
|
5 | |||
6 | #include <QDateTime> |
|
6 | #include <QDateTime> | |
7 | #include <QSize> |
|
7 | #include <QSize> | |
8 | #include <unordered_map> |
|
8 | #include <unordered_map> | |
9 |
|
9 | |||
10 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") |
|
10 | Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel") | |
11 |
|
11 | |||
12 | namespace { |
|
12 | namespace { | |
13 |
|
13 | |||
14 | // Column indexes |
|
14 | // Column indexes | |
15 | const auto NAME_COLUMN = 0; |
|
15 | const auto NAME_COLUMN = 0; | |
16 | const auto TSTART_COLUMN = 1; |
|
16 | const auto TSTART_COLUMN = 1; | |
17 | const auto TEND_COLUMN = 2; |
|
17 | const auto TEND_COLUMN = 2; | |
18 | const auto NB_COLUMNS = 3; |
|
18 | const auto NB_COLUMNS = 3; | |
19 |
|
19 | |||
20 | // Column properties |
|
20 | // Column properties | |
21 | const auto DEFAULT_HEIGHT = 25; |
|
21 | const auto DEFAULT_HEIGHT = 25; | |
22 | const auto DEFAULT_WIDTH = 100; |
|
22 | const auto DEFAULT_WIDTH = 100; | |
23 |
|
23 | |||
24 | struct ColumnProperties { |
|
24 | struct ColumnProperties { | |
25 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, |
|
25 | ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH, | |
26 | int height = DEFAULT_HEIGHT) |
|
26 | int height = DEFAULT_HEIGHT) | |
27 | : m_Name{name}, m_Width{width}, m_Height{height} |
|
27 | : m_Name{name}, m_Width{width}, m_Height{height} | |
28 | { |
|
28 | { | |
29 | } |
|
29 | } | |
30 |
|
30 | |||
31 | QString m_Name; |
|
31 | QString m_Name; | |
32 | int m_Width; |
|
32 | int m_Width; | |
33 | int m_Height; |
|
33 | int m_Height; | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 | const auto COLUMN_PROPERTIES |
|
36 | const auto COLUMN_PROPERTIES | |
37 | = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}}, |
|
37 | = QHash<int, ColumnProperties>{{NAME_COLUMN, {QObject::tr("Name")}}, | |
38 | {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, |
|
38 | {TSTART_COLUMN, {QObject::tr("tStart"), 180}}, | |
39 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}}; |
|
39 | {TEND_COLUMN, {QObject::tr("tEnd"), 180}}}; | |
40 |
|
40 | |||
41 | /// Format for datetimes |
|
41 | /// Format for datetimes | |
42 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); |
|
42 | const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz"); | |
43 |
|
43 | |||
44 |
|
44 | |||
45 | } // namespace |
|
45 | } // namespace | |
46 |
|
46 | |||
47 | struct VariableModel::VariableModelPrivate { |
|
47 | struct VariableModel::VariableModelPrivate { | |
48 | /// Variables created in SciQlop |
|
48 | /// Variables created in SciQlop | |
49 | std::vector<std::shared_ptr<Variable> > m_Variables; |
|
49 | std::vector<std::shared_ptr<Variable> > m_Variables; | |
50 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; |
|
50 | std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress; | |
51 |
|
51 | |||
52 | /// Return the row index of the variable. -1 if it's not found |
|
52 | /// Return the row index of the variable. -1 if it's not found | |
53 | int indexOfVariable(Variable *variable) const noexcept; |
|
53 | int indexOfVariable(Variable *variable) const noexcept; | |
54 | }; |
|
54 | }; | |
55 |
|
55 | |||
56 | VariableModel::VariableModel(QObject *parent) |
|
56 | VariableModel::VariableModel(QObject *parent) | |
57 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} |
|
57 | : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()} | |
58 | { |
|
58 | { | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, |
|
61 | std::shared_ptr<Variable> VariableModel::createVariable(const QString &name, | |
62 | const SqpDateTime &dateTime, |
|
62 | const SqpDateTime &dateTime, | |
63 | const QVariantHash &metadata) noexcept |
|
63 | const QVariantHash &metadata) noexcept | |
64 | { |
|
64 | { | |
65 | auto insertIndex = rowCount(); |
|
65 | auto insertIndex = rowCount(); | |
66 | beginInsertRows({}, insertIndex, insertIndex); |
|
66 | beginInsertRows({}, insertIndex, insertIndex); | |
67 |
|
67 | |||
68 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); |
|
68 | auto variable = std::make_shared<Variable>(name, dateTime, metadata); | |
69 |
|
69 | |||
70 | impl->m_Variables.push_back(variable); |
|
70 | impl->m_Variables.push_back(variable); | |
71 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); |
|
71 | connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated); | |
72 |
|
72 | |||
73 | endInsertRows(); |
|
73 | endInsertRows(); | |
74 |
|
74 | |||
75 | return variable; |
|
75 | return variable; | |
76 | } |
|
76 | } | |
77 |
|
77 | |||
78 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
78 | void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept | |
79 | { |
|
79 | { | |
80 | if (!variable) { |
|
80 | if (!variable) { | |
81 | qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; |
|
81 | qCCritical(LOG_Variable()) << "Can't delete a null variable from the model"; | |
82 | return; |
|
82 | return; | |
83 | } |
|
83 | } | |
84 |
|
84 | |||
85 | // Finds variable in the model |
|
85 | // Finds variable in the model | |
86 | auto begin = impl->m_Variables.cbegin(); |
|
86 | auto begin = impl->m_Variables.cbegin(); | |
87 | auto end = impl->m_Variables.cend(); |
|
87 | auto end = impl->m_Variables.cend(); | |
88 | auto it = std::find(begin, end, variable); |
|
88 | auto it = std::find(begin, end, variable); | |
89 | if (it != end) { |
|
89 | if (it != end) { | |
90 | auto removeIndex = std::distance(begin, it); |
|
90 | auto removeIndex = std::distance(begin, it); | |
91 |
|
91 | |||
92 | // Deletes variable |
|
92 | // Deletes variable | |
93 | beginRemoveRows({}, removeIndex, removeIndex); |
|
93 | beginRemoveRows({}, removeIndex, removeIndex); | |
94 | impl->m_Variables.erase(it); |
|
94 | impl->m_Variables.erase(it); | |
95 | endRemoveRows(); |
|
95 | endRemoveRows(); | |
96 | } |
|
96 | } | |
97 | else { |
|
97 | else { | |
98 | qCritical(LOG_VariableModel()) |
|
98 | qCritical(LOG_VariableModel()) | |
99 | << tr("Can't delete variable %1 from the model: the variable is not in the model") |
|
99 | << tr("Can't delete variable %1 from the model: the variable is not in the model") | |
100 | .arg(variable->name()); |
|
100 | .arg(variable->name()); | |
101 | } |
|
101 | } | |
102 |
|
102 | |||
103 | // Removes variable from progress map |
|
103 | // Removes variable from progress map | |
104 | impl->m_VariableToProgress.erase(variable); |
|
104 | impl->m_VariableToProgress.erase(variable); | |
105 | } |
|
105 | } | |
106 |
|
106 | |||
107 |
|
107 | |||
108 | std::shared_ptr<Variable> VariableModel::variable(int index) const |
|
108 | std::shared_ptr<Variable> VariableModel::variable(int index) const | |
109 | { |
|
109 | { | |
110 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; |
|
110 | return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr; | |
111 | } |
|
111 | } | |
112 |
|
112 | |||
113 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) |
|
113 | void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress) | |
114 | { |
|
114 | { | |
|
115 | if (progress > 0.0) { | |||
115 | impl->m_VariableToProgress[variable] = progress; |
|
116 | impl->m_VariableToProgress[variable] = progress; | |
|
117 | } | |||
|
118 | else { | |||
|
119 | impl->m_VariableToProgress.erase(variable); | |||
|
120 | } | |||
116 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); |
|
121 | auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN); | |
117 |
|
122 | |||
118 | emit dataChanged(modelIndex, modelIndex); |
|
123 | emit dataChanged(modelIndex, modelIndex); | |
119 | } |
|
124 | } | |
120 |
|
125 | |||
121 | int VariableModel::columnCount(const QModelIndex &parent) const |
|
126 | int VariableModel::columnCount(const QModelIndex &parent) const | |
122 | { |
|
127 | { | |
123 | Q_UNUSED(parent); |
|
128 | Q_UNUSED(parent); | |
124 |
|
129 | |||
125 | return NB_COLUMNS; |
|
130 | return NB_COLUMNS; | |
126 | } |
|
131 | } | |
127 |
|
132 | |||
128 | int VariableModel::rowCount(const QModelIndex &parent) const |
|
133 | int VariableModel::rowCount(const QModelIndex &parent) const | |
129 | { |
|
134 | { | |
130 | Q_UNUSED(parent); |
|
135 | Q_UNUSED(parent); | |
131 |
|
136 | |||
132 | return impl->m_Variables.size(); |
|
137 | return impl->m_Variables.size(); | |
133 | } |
|
138 | } | |
134 |
|
139 | |||
135 | QVariant VariableModel::data(const QModelIndex &index, int role) const |
|
140 | QVariant VariableModel::data(const QModelIndex &index, int role) const | |
136 | { |
|
141 | { | |
137 | if (!index.isValid()) { |
|
142 | if (!index.isValid()) { | |
138 | return QVariant{}; |
|
143 | return QVariant{}; | |
139 | } |
|
144 | } | |
140 |
|
145 | |||
141 | if (index.row() < 0 || index.row() >= rowCount()) { |
|
146 | if (index.row() < 0 || index.row() >= rowCount()) { | |
142 | return QVariant{}; |
|
147 | return QVariant{}; | |
143 | } |
|
148 | } | |
144 |
|
149 | |||
145 | if (role == Qt::DisplayRole) { |
|
150 | if (role == Qt::DisplayRole) { | |
146 | if (auto variable = impl->m_Variables.at(index.row()).get()) { |
|
151 | if (auto variable = impl->m_Variables.at(index.row()).get()) { | |
147 | /// Lambda function that builds the variant to return for a time value |
|
152 | /// Lambda function that builds the variant to return for a time value | |
148 | auto dateTimeVariant = [](double time) { |
|
153 | auto dateTimeVariant = [](double time) { | |
149 | auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); |
|
154 | auto dateTime = QDateTime::fromMSecsSinceEpoch(time * 1000.); | |
150 | return dateTime.toString(DATETIME_FORMAT); |
|
155 | return dateTime.toString(DATETIME_FORMAT); | |
151 | }; |
|
156 | }; | |
152 |
|
157 | |||
153 | switch (index.column()) { |
|
158 | switch (index.column()) { | |
154 | case NAME_COLUMN: |
|
159 | case NAME_COLUMN: | |
155 | return variable->name(); |
|
160 | return variable->name(); | |
156 | case TSTART_COLUMN: |
|
161 | case TSTART_COLUMN: | |
157 | return dateTimeVariant(variable->dateTime().m_TStart); |
|
162 | return dateTimeVariant(variable->dateTime().m_TStart); | |
158 | case TEND_COLUMN: |
|
163 | case TEND_COLUMN: | |
159 | return dateTimeVariant(variable->dateTime().m_TEnd); |
|
164 | return dateTimeVariant(variable->dateTime().m_TEnd); | |
160 | default: |
|
165 | default: | |
161 | // No action |
|
166 | // No action | |
162 | break; |
|
167 | break; | |
163 | } |
|
168 | } | |
164 |
|
169 | |||
165 | qWarning(LOG_VariableModel()) |
|
170 | qWarning(LOG_VariableModel()) | |
166 | << tr("Can't get data (unknown column %1)").arg(index.column()); |
|
171 | << tr("Can't get data (unknown column %1)").arg(index.column()); | |
167 | } |
|
172 | } | |
168 | else { |
|
173 | else { | |
169 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); |
|
174 | qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)"); | |
170 | } |
|
175 | } | |
171 | } |
|
176 | } | |
172 | else if (role == VariableRoles::ProgressRole) { |
|
177 | else if (role == VariableRoles::ProgressRole) { | |
173 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
178 | if (auto variable = impl->m_Variables.at(index.row())) { | |
174 |
|
179 | |||
175 | auto it = impl->m_VariableToProgress.find(variable); |
|
180 | auto it = impl->m_VariableToProgress.find(variable); | |
176 | if (it != impl->m_VariableToProgress.cend()) { |
|
181 | if (it != impl->m_VariableToProgress.cend()) { | |
177 | return it->second; |
|
182 | return it->second; | |
178 | } |
|
183 | } | |
179 | } |
|
184 | } | |
180 | } |
|
185 | } | |
181 |
|
186 | |||
182 | return QVariant{}; |
|
187 | return QVariant{}; | |
183 | } |
|
188 | } | |
184 |
|
189 | |||
185 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const |
|
190 | QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const | |
186 | { |
|
191 | { | |
187 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { |
|
192 | if (role != Qt::DisplayRole && role != Qt::SizeHintRole) { | |
188 | return QVariant{}; |
|
193 | return QVariant{}; | |
189 | } |
|
194 | } | |
190 |
|
195 | |||
191 | if (orientation == Qt::Horizontal) { |
|
196 | if (orientation == Qt::Horizontal) { | |
192 | auto propertiesIt = COLUMN_PROPERTIES.find(section); |
|
197 | auto propertiesIt = COLUMN_PROPERTIES.find(section); | |
193 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { |
|
198 | if (propertiesIt != COLUMN_PROPERTIES.cend()) { | |
194 | // Role is either DisplayRole or SizeHintRole |
|
199 | // Role is either DisplayRole or SizeHintRole | |
195 | return (role == Qt::DisplayRole) |
|
200 | return (role == Qt::DisplayRole) | |
196 | ? QVariant{propertiesIt->m_Name} |
|
201 | ? QVariant{propertiesIt->m_Name} | |
197 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; |
|
202 | : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}}; | |
198 | } |
|
203 | } | |
199 | else { |
|
204 | else { | |
200 | qWarning(LOG_VariableModel()) |
|
205 | qWarning(LOG_VariableModel()) | |
201 | << tr("Can't get header data (unknown column %1)").arg(section); |
|
206 | << tr("Can't get header data (unknown column %1)").arg(section); | |
202 | } |
|
207 | } | |
203 | } |
|
208 | } | |
204 |
|
209 | |||
205 | return QVariant{}; |
|
210 | return QVariant{}; | |
206 | } |
|
211 | } | |
207 |
|
212 | |||
208 | void VariableModel::abortProgress(const QModelIndex &index) |
|
213 | void VariableModel::abortProgress(const QModelIndex &index) | |
209 | { |
|
214 | { | |
210 | if (auto variable = impl->m_Variables.at(index.row())) { |
|
215 | if (auto variable = impl->m_Variables.at(index.row())) { | |
211 | emit abortProgessRequested(variable); |
|
216 | emit abortProgessRequested(variable); | |
212 | } |
|
217 | } | |
213 | } |
|
218 | } | |
214 |
|
219 | |||
215 | void VariableModel::onVariableUpdated() noexcept |
|
220 | void VariableModel::onVariableUpdated() noexcept | |
216 | { |
|
221 | { | |
217 | // Finds variable that has been updated in the model |
|
222 | // Finds variable that has been updated in the model | |
218 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { |
|
223 | if (auto updatedVariable = dynamic_cast<Variable *>(sender())) { | |
219 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); |
|
224 | auto updatedVariableIndex = impl->indexOfVariable(updatedVariable); | |
220 |
|
225 | |||
221 | if (updatedVariableIndex > -1) { |
|
226 | if (updatedVariableIndex > -1) { | |
222 | emit dataChanged(createIndex(updatedVariableIndex, 0), |
|
227 | emit dataChanged(createIndex(updatedVariableIndex, 0), | |
223 | createIndex(updatedVariableIndex, columnCount() - 1)); |
|
228 | createIndex(updatedVariableIndex, columnCount() - 1)); | |
224 | } |
|
229 | } | |
225 | } |
|
230 | } | |
226 | } |
|
231 | } | |
227 |
|
232 | |||
228 | int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept |
|
233 | int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept | |
229 | { |
|
234 | { | |
230 | auto begin = std::cbegin(m_Variables); |
|
235 | auto begin = std::cbegin(m_Variables); | |
231 | auto end = std::cend(m_Variables); |
|
236 | auto end = std::cend(m_Variables); | |
232 | auto it |
|
237 | auto it | |
233 | = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); |
|
238 | = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; }); | |
234 |
|
239 | |||
235 | if (it != end) { |
|
240 | if (it != end) { | |
236 | // Gets the index of the variable in the model: we assume here that views have the same |
|
241 | // Gets the index of the variable in the model: we assume here that views have the same | |
237 | // order as the model |
|
242 | // order as the model | |
238 | return std::distance(begin, it); |
|
243 | return std::distance(begin, it); | |
239 | } |
|
244 | } | |
240 | else { |
|
245 | else { | |
241 | return -1; |
|
246 | return -1; | |
242 | } |
|
247 | } | |
243 | } |
|
248 | } |
1 | NO CONTENT: modified file |
|
NO CONTENT: modified file |
General Comments 0
You need to be logged in to leave comments.
Login now