##// END OF EJS Templates
Handles StateRole in variable model
Alexandre Leroux -
r809:892af30fdefb
parent child
Show More
@@ -1,95 +1,94
1 #ifndef SCIQLOP_VARIABLEMODEL_H
1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
2 #define SCIQLOP_VARIABLEMODEL_H
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Data/SqpRange.h>
6 #include <Data/SqpRange.h>
7
7
8 #include <QAbstractTableModel>
8 #include <QAbstractTableModel>
9 #include <QLoggingCategory>
9 #include <QLoggingCategory>
10
10
11 #include <Common/MetaTypes.h>
11 #include <Common/MetaTypes.h>
12 #include <Common/spimpl.h>
12 #include <Common/spimpl.h>
13
13
14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
15
15
16 enum VariableRoles { ProgressRole = Qt::UserRole };
16 enum VariableRoles { ProgressRole = Qt::UserRole, StateRole };
17
18
17
19 class IDataSeries;
18 class IDataSeries;
20 class Variable;
19 class Variable;
21
20
22 /**
21 /**
23 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
22 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
24 */
23 */
25 class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel {
24 class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel {
26 Q_OBJECT
25 Q_OBJECT
27 public:
26 public:
28 explicit VariableModel(QObject *parent = nullptr);
27 explicit VariableModel(QObject *parent = nullptr);
29
28
30 /**
29 /**
31 * Adds an existing variable in the model.
30 * Adds an existing variable in the model.
32 * @param variable the variable to add.
31 * @param variable the variable to add.
33 * @remarks the variable's name is modified to avoid name duplicates
32 * @remarks the variable's name is modified to avoid name duplicates
34 * @remarks this method does nothing if the variable already exists in the model
33 * @remarks this method does nothing if the variable already exists in the model
35 */
34 */
36 void addVariable(std::shared_ptr<Variable> variable) noexcept;
35 void addVariable(std::shared_ptr<Variable> variable) noexcept;
37
36
38 /**
37 /**
39 * Checks that a variable is contained in the model
38 * Checks that a variable is contained in the model
40 * @param variable the variable to check
39 * @param variable the variable to check
41 * @return true if the variable is in the model, false otherwise
40 * @return true if the variable is in the model, false otherwise
42 */
41 */
43 bool containsVariable(std::shared_ptr<Variable> variable) const noexcept;
42 bool containsVariable(std::shared_ptr<Variable> variable) const noexcept;
44
43
45 /**
44 /**
46 * Creates a new variable in the model
45 * Creates a new variable in the model
47 * @param name the name of the new variable
46 * @param name the name of the new variable
48 * @param metadata the metadata associated to the new variable
47 * @param metadata the metadata associated to the new variable
49 * @return the pointer to the new variable
48 * @return the pointer to the new variable
50 */
49 */
51 std::shared_ptr<Variable> createVariable(const QString &name,
50 std::shared_ptr<Variable> createVariable(const QString &name,
52 const QVariantHash &metadata) noexcept;
51 const QVariantHash &metadata) noexcept;
53
52
54 /**
53 /**
55 * Deletes a variable from the model, if it exists
54 * Deletes a variable from the model, if it exists
56 * @param variable the variable to delete
55 * @param variable the variable to delete
57 */
56 */
58 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
57 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
59
58
60
59
61 std::shared_ptr<Variable> variable(int index) const;
60 std::shared_ptr<Variable> variable(int index) const;
62 std::vector<std::shared_ptr<Variable> > variables() const;
61 std::vector<std::shared_ptr<Variable> > variables() const;
63
62
64 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
63 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
65
64
66
65
67 // /////////////////////////// //
66 // /////////////////////////// //
68 // QAbstractTableModel methods //
67 // QAbstractTableModel methods //
69 // /////////////////////////// //
68 // /////////////////////////// //
70
69
71 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
70 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
72 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
71 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
73 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
72 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
74 virtual QVariant headerData(int section, Qt::Orientation orientation,
73 virtual QVariant headerData(int section, Qt::Orientation orientation,
75 int role = Qt::DisplayRole) const override;
74 int role = Qt::DisplayRole) const override;
76
75
77
76
78 void abortProgress(const QModelIndex &index);
77 void abortProgress(const QModelIndex &index);
79
78
80 signals:
79 signals:
81 void abortProgessRequested(std::shared_ptr<Variable> variable);
80 void abortProgessRequested(std::shared_ptr<Variable> variable);
82
81
83 private:
82 private:
84 class VariableModelPrivate;
83 class VariableModelPrivate;
85 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
84 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
86
85
87 private slots:
86 private slots:
88 /// Slot called when data of a variable has been updated
87 /// Slot called when data of a variable has been updated
89 void onVariableUpdated() noexcept;
88 void onVariableUpdated() noexcept;
90 };
89 };
91
90
92 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
91 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
93 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
92 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
94
93
95 #endif // SCIQLOP_VARIABLEMODEL_H
94 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,293 +1,298
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableModel.h>
2 #include <Variable/VariableModel.h>
3
3
4 #include <Common/DateUtils.h>
4 #include <Common/DateUtils.h>
5 #include <Common/StringUtils.h>
5 #include <Common/StringUtils.h>
6
6
7 #include <Data/IDataSeries.h>
7 #include <Data/IDataSeries.h>
8
8
9 #include <QSize>
9 #include <QSize>
10 #include <unordered_map>
10 #include <unordered_map>
11
11
12 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
12 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
13
13
14 namespace {
14 namespace {
15
15
16 // Column indexes
16 // Column indexes
17 const auto NAME_COLUMN = 0;
17 const auto NAME_COLUMN = 0;
18 const auto TSTART_COLUMN = 1;
18 const auto TSTART_COLUMN = 1;
19 const auto TEND_COLUMN = 2;
19 const auto TEND_COLUMN = 2;
20 const auto NBPOINTS_COLUMN = 3;
20 const auto NBPOINTS_COLUMN = 3;
21 const auto UNIT_COLUMN = 4;
21 const auto UNIT_COLUMN = 4;
22 const auto MISSION_COLUMN = 5;
22 const auto MISSION_COLUMN = 5;
23 const auto PLUGIN_COLUMN = 6;
23 const auto PLUGIN_COLUMN = 6;
24 const auto NB_COLUMNS = 7;
24 const auto NB_COLUMNS = 7;
25
25
26 // Column properties
26 // Column properties
27 const auto DEFAULT_HEIGHT = 25;
27 const auto DEFAULT_HEIGHT = 25;
28 const auto DEFAULT_WIDTH = 100;
28 const auto DEFAULT_WIDTH = 100;
29
29
30 struct ColumnProperties {
30 struct ColumnProperties {
31 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
31 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
32 int height = DEFAULT_HEIGHT)
32 int height = DEFAULT_HEIGHT)
33 : m_Name{name}, m_Width{width}, m_Height{height}
33 : m_Name{name}, m_Width{width}, m_Height{height}
34 {
34 {
35 }
35 }
36
36
37 QString m_Name;
37 QString m_Name;
38 int m_Width;
38 int m_Width;
39 int m_Height;
39 int m_Height;
40 };
40 };
41
41
42 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
42 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
43 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
43 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
44 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
44 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
45 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
45 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
46 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
46 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
47
47
48 /// Format for datetimes
48 /// Format for datetimes
49 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
49 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
50
50
51 QString uniqueName(const QString &defaultName,
51 QString uniqueName(const QString &defaultName,
52 const std::vector<std::shared_ptr<Variable> > &variables)
52 const std::vector<std::shared_ptr<Variable> > &variables)
53 {
53 {
54 auto forbiddenNames = std::vector<QString>(variables.size());
54 auto forbiddenNames = std::vector<QString>(variables.size());
55 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
55 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
56 [](const auto &variable) { return variable->name(); });
56 [](const auto &variable) { return variable->name(); });
57 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
57 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
58 Q_ASSERT(!uniqueName.isEmpty());
58 Q_ASSERT(!uniqueName.isEmpty());
59
59
60 return uniqueName;
60 return uniqueName;
61 }
61 }
62
62
63 } // namespace
63 } // namespace
64
64
65 struct VariableModel::VariableModelPrivate {
65 struct VariableModel::VariableModelPrivate {
66 /// Variables created in SciQlop
66 /// Variables created in SciQlop
67 std::vector<std::shared_ptr<Variable> > m_Variables;
67 std::vector<std::shared_ptr<Variable> > m_Variables;
68 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
68 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
69
69
70 /// Return the row index of the variable. -1 if it's not found
70 /// Return the row index of the variable. -1 if it's not found
71 int indexOfVariable(Variable *variable) const noexcept;
71 int indexOfVariable(Variable *variable) const noexcept;
72 };
72 };
73
73
74 VariableModel::VariableModel(QObject *parent)
74 VariableModel::VariableModel(QObject *parent)
75 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
75 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
76 {
76 {
77 }
77 }
78
78
79 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
79 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
80 {
80 {
81 auto insertIndex = rowCount();
81 auto insertIndex = rowCount();
82 beginInsertRows({}, insertIndex, insertIndex);
82 beginInsertRows({}, insertIndex, insertIndex);
83
83
84 // Generates unique name for the variable
84 // Generates unique name for the variable
85 variable->setName(uniqueName(variable->name(), impl->m_Variables));
85 variable->setName(uniqueName(variable->name(), impl->m_Variables));
86
86
87 impl->m_Variables.push_back(variable);
87 impl->m_Variables.push_back(variable);
88 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
88 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
89
89
90 endInsertRows();
90 endInsertRows();
91 }
91 }
92
92
93 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
93 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
94 {
94 {
95 auto end = impl->m_Variables.cend();
95 auto end = impl->m_Variables.cend();
96 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
96 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
97 }
97 }
98
98
99 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
99 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
100 const QVariantHash &metadata) noexcept
100 const QVariantHash &metadata) noexcept
101 {
101 {
102 auto variable = std::make_shared<Variable>(name, metadata);
102 auto variable = std::make_shared<Variable>(name, metadata);
103 addVariable(variable);
103 addVariable(variable);
104
104
105 return variable;
105 return variable;
106 }
106 }
107
107
108 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
108 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
109 {
109 {
110 if (!variable) {
110 if (!variable) {
111 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
111 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
112 return;
112 return;
113 }
113 }
114
114
115 // Finds variable in the model
115 // Finds variable in the model
116 auto begin = impl->m_Variables.cbegin();
116 auto begin = impl->m_Variables.cbegin();
117 auto end = impl->m_Variables.cend();
117 auto end = impl->m_Variables.cend();
118 auto it = std::find(begin, end, variable);
118 auto it = std::find(begin, end, variable);
119 if (it != end) {
119 if (it != end) {
120 auto removeIndex = std::distance(begin, it);
120 auto removeIndex = std::distance(begin, it);
121
121
122 // Deletes variable
122 // Deletes variable
123 beginRemoveRows({}, removeIndex, removeIndex);
123 beginRemoveRows({}, removeIndex, removeIndex);
124 impl->m_Variables.erase(it);
124 impl->m_Variables.erase(it);
125 endRemoveRows();
125 endRemoveRows();
126 }
126 }
127 else {
127 else {
128 qCritical(LOG_VariableModel())
128 qCritical(LOG_VariableModel())
129 << tr("Can't delete variable %1 from the model: the variable is not in the model")
129 << tr("Can't delete variable %1 from the model: the variable is not in the model")
130 .arg(variable->name());
130 .arg(variable->name());
131 }
131 }
132
132
133 // Removes variable from progress map
133 // Removes variable from progress map
134 impl->m_VariableToProgress.erase(variable);
134 impl->m_VariableToProgress.erase(variable);
135 }
135 }
136
136
137
137
138 std::shared_ptr<Variable> VariableModel::variable(int index) const
138 std::shared_ptr<Variable> VariableModel::variable(int index) const
139 {
139 {
140 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
140 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
141 }
141 }
142
142
143 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
143 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
144 {
144 {
145 return impl->m_Variables;
145 return impl->m_Variables;
146 }
146 }
147
147
148 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
148 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
149 {
149 {
150 if (progress > 0.0) {
150 if (progress > 0.0) {
151 impl->m_VariableToProgress[variable] = progress;
151 impl->m_VariableToProgress[variable] = progress;
152 }
152 }
153 else {
153 else {
154 impl->m_VariableToProgress.erase(variable);
154 impl->m_VariableToProgress.erase(variable);
155 }
155 }
156 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
156 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
157
157
158 emit dataChanged(modelIndex, modelIndex);
158 emit dataChanged(modelIndex, modelIndex);
159 }
159 }
160
160
161 int VariableModel::columnCount(const QModelIndex &parent) const
161 int VariableModel::columnCount(const QModelIndex &parent) const
162 {
162 {
163 Q_UNUSED(parent);
163 Q_UNUSED(parent);
164
164
165 return NB_COLUMNS;
165 return NB_COLUMNS;
166 }
166 }
167
167
168 int VariableModel::rowCount(const QModelIndex &parent) const
168 int VariableModel::rowCount(const QModelIndex &parent) const
169 {
169 {
170 Q_UNUSED(parent);
170 Q_UNUSED(parent);
171
171
172 return impl->m_Variables.size();
172 return impl->m_Variables.size();
173 }
173 }
174
174
175 QVariant VariableModel::data(const QModelIndex &index, int role) const
175 QVariant VariableModel::data(const QModelIndex &index, int role) const
176 {
176 {
177 if (!index.isValid()) {
177 if (!index.isValid()) {
178 return QVariant{};
178 return QVariant{};
179 }
179 }
180
180
181 if (index.row() < 0 || index.row() >= rowCount()) {
181 if (index.row() < 0 || index.row() >= rowCount()) {
182 return QVariant{};
182 return QVariant{};
183 }
183 }
184
184
185 if (role == Qt::DisplayRole) {
185 if (role == Qt::DisplayRole) {
186 if (auto variable = impl->m_Variables.at(index.row()).get()) {
186 if (auto variable = impl->m_Variables.at(index.row()).get()) {
187 switch (index.column()) {
187 switch (index.column()) {
188 case NAME_COLUMN:
188 case NAME_COLUMN:
189 return variable->name();
189 return variable->name();
190 case TSTART_COLUMN: {
190 case TSTART_COLUMN: {
191 auto range = variable->realRange();
191 auto range = variable->realRange();
192 return range != INVALID_RANGE
192 return range != INVALID_RANGE
193 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
193 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
194 : QVariant{};
194 : QVariant{};
195 }
195 }
196 case TEND_COLUMN: {
196 case TEND_COLUMN: {
197 auto range = variable->realRange();
197 auto range = variable->realRange();
198 return range != INVALID_RANGE
198 return range != INVALID_RANGE
199 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
199 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
200 : QVariant{};
200 : QVariant{};
201 }
201 }
202 case NBPOINTS_COLUMN:
202 case NBPOINTS_COLUMN:
203 return variable->nbPoints();
203 return variable->nbPoints();
204 case UNIT_COLUMN:
204 case UNIT_COLUMN:
205 return variable->metadata().value(QStringLiteral("units"));
205 return variable->metadata().value(QStringLiteral("units"));
206 case MISSION_COLUMN:
206 case MISSION_COLUMN:
207 return variable->metadata().value(QStringLiteral("mission"));
207 return variable->metadata().value(QStringLiteral("mission"));
208 case PLUGIN_COLUMN:
208 case PLUGIN_COLUMN:
209 return variable->metadata().value(QStringLiteral("plugin"));
209 return variable->metadata().value(QStringLiteral("plugin"));
210 default:
210 default:
211 // No action
211 // No action
212 break;
212 break;
213 }
213 }
214
214
215 qWarning(LOG_VariableModel())
215 qWarning(LOG_VariableModel())
216 << tr("Can't get data (unknown column %1)").arg(index.column());
216 << tr("Can't get data (unknown column %1)").arg(index.column());
217 }
217 }
218 else {
218 else {
219 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
219 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
220 }
220 }
221 }
221 }
222 else if (role == VariableRoles::ProgressRole) {
222 else if (role == VariableRoles::ProgressRole) {
223 if (auto variable = impl->m_Variables.at(index.row())) {
223 if (auto variable = impl->m_Variables.at(index.row())) {
224
224
225 auto it = impl->m_VariableToProgress.find(variable);
225 auto it = impl->m_VariableToProgress.find(variable);
226 if (it != impl->m_VariableToProgress.cend()) {
226 if (it != impl->m_VariableToProgress.cend()) {
227 return it->second;
227 return it->second;
228 }
228 }
229 }
229 }
230 }
230 }
231 else if (role == VariableRoles::StateRole) {
232 if (auto variable = impl->m_Variables.at(index.row())) {
233 return variable->stateData();
234 }
235 }
231
236
232 return QVariant{};
237 return QVariant{};
233 }
238 }
234
239
235 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
240 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
236 {
241 {
237 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
242 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
238 return QVariant{};
243 return QVariant{};
239 }
244 }
240
245
241 if (orientation == Qt::Horizontal) {
246 if (orientation == Qt::Horizontal) {
242 auto propertiesIt = COLUMN_PROPERTIES.find(section);
247 auto propertiesIt = COLUMN_PROPERTIES.find(section);
243 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
248 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
244 // Role is either DisplayRole or SizeHintRole
249 // Role is either DisplayRole or SizeHintRole
245 return (role == Qt::DisplayRole)
250 return (role == Qt::DisplayRole)
246 ? QVariant{propertiesIt->m_Name}
251 ? QVariant{propertiesIt->m_Name}
247 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
252 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
248 }
253 }
249 else {
254 else {
250 qWarning(LOG_VariableModel())
255 qWarning(LOG_VariableModel())
251 << tr("Can't get header data (unknown column %1)").arg(section);
256 << tr("Can't get header data (unknown column %1)").arg(section);
252 }
257 }
253 }
258 }
254
259
255 return QVariant{};
260 return QVariant{};
256 }
261 }
257
262
258 void VariableModel::abortProgress(const QModelIndex &index)
263 void VariableModel::abortProgress(const QModelIndex &index)
259 {
264 {
260 if (auto variable = impl->m_Variables.at(index.row())) {
265 if (auto variable = impl->m_Variables.at(index.row())) {
261 emit abortProgessRequested(variable);
266 emit abortProgessRequested(variable);
262 }
267 }
263 }
268 }
264
269
265 void VariableModel::onVariableUpdated() noexcept
270 void VariableModel::onVariableUpdated() noexcept
266 {
271 {
267 // Finds variable that has been updated in the model
272 // Finds variable that has been updated in the model
268 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
273 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
269 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
274 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
270
275
271 if (updatedVariableIndex > -1) {
276 if (updatedVariableIndex > -1) {
272 emit dataChanged(createIndex(updatedVariableIndex, 0),
277 emit dataChanged(createIndex(updatedVariableIndex, 0),
273 createIndex(updatedVariableIndex, columnCount() - 1));
278 createIndex(updatedVariableIndex, columnCount() - 1));
274 }
279 }
275 }
280 }
276 }
281 }
277
282
278 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
283 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
279 {
284 {
280 auto begin = std::cbegin(m_Variables);
285 auto begin = std::cbegin(m_Variables);
281 auto end = std::cend(m_Variables);
286 auto end = std::cend(m_Variables);
282 auto it
287 auto it
283 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
288 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
284
289
285 if (it != end) {
290 if (it != end) {
286 // Gets the index of the variable in the model: we assume here that views have the same
291 // Gets the index of the variable in the model: we assume here that views have the same
287 // order as the model
292 // order as the model
288 return std::distance(begin, it);
293 return std::distance(begin, it);
289 }
294 }
290 else {
295 else {
291 return -1;
296 return -1;
292 }
297 }
293 }
298 }
General Comments 0
You need to be logged in to leave comments. Login now