##// END OF EJS Templates
Drop TIME mime data on a variable
trabillard -
r933:ba45dcc66c3a
parent child
Show More
@@ -1,135 +1,138
1 1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECONTROLLER_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/AcquisitionDataPacket.h>
7 7 #include <Data/SqpRange.h>
8 8
9 9 #include <QLoggingCategory>
10 10 #include <QObject>
11 11 #include <QUuid>
12 12
13 13 #include <Common/spimpl.h>
14 14
15 15 class IDataProvider;
16 16 class QItemSelectionModel;
17 17 class TimeController;
18 18 class Variable;
19 19 class VariableModel;
20 20
21 21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
22 22
23 23
24 24 /**
25 25 * Possible types of zoom operation
26 26 */
27 27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
28 28
29 29
30 30 /**
31 31 * @brief The VariableController class aims to handle the variables in SciQlop.
32 32 */
33 33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
34 34 Q_OBJECT
35 35 public:
36 36 explicit VariableController(QObject *parent = 0);
37 37 virtual ~VariableController();
38 38
39 39 VariableModel *variableModel() noexcept;
40 40 QItemSelectionModel *variableSelectionModel() noexcept;
41 41
42 42 void setTimeController(TimeController *timeController) noexcept;
43 43
44 44 /**
45 45 * Clones the variable passed in parameter and adds the duplicate to the controller
46 46 * @param variable the variable to duplicate
47 47 * @return the duplicate created, nullptr if the variable couldn't be created
48 48 */
49 49 std::shared_ptr<Variable> cloneVariable(std::shared_ptr<Variable> variable) noexcept;
50 50
51 51 /**
52 52 * Deletes from the controller the variable passed in parameter.
53 53 *
54 54 * Delete a variable includes:
55 55 * - the deletion of the various references to the variable in SciQlop
56 56 * - the deletion of the model variable
57 57 * - the deletion of the provider associated with the variable
58 58 * - removing the cache associated with the variable
59 59 *
60 60 * @param variable the variable to delete from the controller.
61 61 */
62 62 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
63 63
64 64 /**
65 65 * Deletes from the controller the variables passed in parameter.
66 66 * @param variables the variables to delete from the controller.
67 67 * @sa deleteVariable()
68 68 */
69 69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
70 70
71 71 /// Returns the MIME data associated to a list of variables
72 72 QByteArray mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const;
73 73
74 74 /// Returns the list of variables contained in a MIME data
75 75 QList<std::shared_ptr<Variable> > variablesForMimeData(const QByteArray &mimeData) const;
76 76
77 77 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
78 78 signals:
79 79 /// Signal emitted when a variable is about to be deleted from the controller
80 80 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
81 81
82 82 /// Signal emitted when a data acquisition is requested on a range for a variable
83 83 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
84 84
85 85 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
86 86 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
87 87
88 88 public slots:
89 89 /// Request the data loading of the variable whithin range
90 90 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
91 91 bool synchronise);
92 92 /**
93 93 * Creates a new variable and adds it to the model
94 94 * @param name the name of the new variable
95 95 * @param metadata the metadata of the new variable
96 96 * @param provider the data provider for the new variable
97 97 * @return the pointer to the new variable or nullptr if the creation failed
98 98 */
99 99 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
100 100 std::shared_ptr<IDataProvider> provider) noexcept;
101 101
102 102 /// Update the temporal parameters of every selected variable to dateTime
103 103 void onDateTimeOnSelection(const SqpRange &dateTime);
104 104
105 /// Update the temporal parameters of the specified variable
106 void onUpdateDateTime(std::shared_ptr<Variable> variable, const SqpRange &dateTime);
107
105 108
106 109 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
107 110 const SqpRange &cacheRangeRequested,
108 111 QVector<AcquisitionDataPacket> dataAcquired);
109 112
110 113 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
111 114
112 115 /// Cancel the current request for the variable
113 116 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
114 117 void onAbortAcquisitionRequested(QUuid vIdentifier);
115 118
116 119 // synchronization group methods
117 120 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
118 121 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
119 122 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
120 123
121 124 /// Desynchronizes the variable of the group whose identifier is passed in parameter
122 125 /// @remarks the method does nothing if the variable is not part of the group
123 126 void desynchronize(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
124 127
125 128 void initialize();
126 129 void finalize();
127 130
128 131 private:
129 132 void waitForFinish();
130 133
131 134 class VariableControllerPrivate;
132 135 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
133 136 };
134 137
135 138 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,110 +1,111
1 1 #ifndef SCIQLOP_VARIABLEMODEL_H
2 2 #define SCIQLOP_VARIABLEMODEL_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/SqpRange.h>
7 7
8 8 #include <QAbstractTableModel>
9 9 #include <QLoggingCategory>
10 10
11 11 #include <Common/MetaTypes.h>
12 12 #include <Common/spimpl.h>
13 13
14 14 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableModel)
15 15
16 16 enum VariableRoles { ProgressRole = Qt::UserRole };
17 17
18 18
19 19 class IDataSeries;
20 20 class Variable;
21 21 class VariableController;
22 22
23 23 /**
24 24 * @brief The VariableModel class aims to hold the variables that have been created in SciQlop
25 25 */
26 26 class SCIQLOP_CORE_EXPORT VariableModel : public QAbstractTableModel {
27 27 Q_OBJECT
28 28 public:
29 29 explicit VariableModel(VariableController *parent = nullptr);
30 30
31 31 /**
32 32 * Adds an existing variable in the model.
33 33 * @param variable the variable to add.
34 34 * @remarks the variable's name is modified to avoid name duplicates
35 35 * @remarks this method does nothing if the variable already exists in the model
36 36 */
37 37 void addVariable(std::shared_ptr<Variable> variable) noexcept;
38 38
39 39 /**
40 40 * Checks that a variable is contained in the model
41 41 * @param variable the variable to check
42 42 * @return true if the variable is in the model, false otherwise
43 43 */
44 44 bool containsVariable(std::shared_ptr<Variable> variable) const noexcept;
45 45
46 46 /**
47 47 * Creates a new variable in the model
48 48 * @param name the name of the new variable
49 49 * @param metadata the metadata associated to the new variable
50 50 * @return the pointer to the new variable
51 51 */
52 52 std::shared_ptr<Variable> createVariable(const QString &name,
53 53 const QVariantHash &metadata) noexcept;
54 54
55 55 /**
56 56 * Deletes a variable from the model, if it exists
57 57 * @param variable the variable to delete
58 58 */
59 59 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
60 60
61 61
62 62 std::shared_ptr<Variable> variable(int index) const;
63 63 std::vector<std::shared_ptr<Variable> > variables() const;
64 64
65 65 void setDataProgress(std::shared_ptr<Variable> variable, double progress);
66 66
67 67
68 68 // /////////////////////////// //
69 69 // QAbstractTableModel methods //
70 70 // /////////////////////////// //
71 71
72 72 virtual int columnCount(const QModelIndex &parent = QModelIndex{}) const override;
73 73 virtual int rowCount(const QModelIndex &parent = QModelIndex{}) const override;
74 74 virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
75 75 virtual QVariant headerData(int section, Qt::Orientation orientation,
76 76 int role = Qt::DisplayRole) const override;
77 77 virtual Qt::ItemFlags flags(const QModelIndex &index) const override;
78 78
79 79 // ///////////////// //
80 80 // Drag&Drop methods //
81 81 // ///////////////// //
82 82
83 83 virtual Qt::DropActions supportedDropActions() const override;
84 84 virtual Qt::DropActions supportedDragActions() const override;
85 85 virtual QStringList mimeTypes() const override;
86 86 virtual QMimeData *mimeData(const QModelIndexList &indexes) const override;
87 87 virtual bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
88 88 const QModelIndex &parent) const override;
89 89 virtual bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
90 90 const QModelIndex &parent) override;
91 91
92 92 void abortProgress(const QModelIndex &index);
93 93
94 94 signals:
95 95 void abortProgessRequested(std::shared_ptr<Variable> variable);
96 96 void requestVariable(const QVariantHash &productData);
97 void requestVariableRangeUpdate(std::shared_ptr<Variable> variable, const SqpRange &range);
97 98
98 99 private:
99 100 class VariableModelPrivate;
100 101 spimpl::unique_impl_ptr<VariableModelPrivate> impl;
101 102
102 103 private slots:
103 104 /// Slot called when data of a variable has been updated
104 105 void onVariableUpdated() noexcept;
105 106 };
106 107
107 108 // Registers QVector<int> metatype so it can be used in VariableModel::dataChanged() signal
108 109 SCIQLOP_REGISTER_META_TYPE(QVECTOR_INT_REGISTRY, QVector<int>)
109 110
110 111 #endif // SCIQLOP_VARIABLEMODEL_H
@@ -1,1053 +1,1060
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableAcquisitionWorker.h>
3 3 #include <Variable/VariableCacheStrategy.h>
4 4 #include <Variable/VariableCacheStrategyFactory.h>
5 5 #include <Variable/VariableController.h>
6 6 #include <Variable/VariableModel.h>
7 7 #include <Variable/VariableSynchronizationGroup.h>
8 8
9 9 #include <Data/DataProviderParameters.h>
10 10 #include <Data/IDataProvider.h>
11 11 #include <Data/IDataSeries.h>
12 12 #include <Data/VariableRequest.h>
13 13 #include <Time/TimeController.h>
14 14
15 15 #include <QDataStream>
16 16 #include <QMutex>
17 17 #include <QThread>
18 18 #include <QUuid>
19 19 #include <QtCore/QItemSelectionModel>
20 20
21 21 #include <deque>
22 22 #include <set>
23 23 #include <unordered_map>
24 24
25 25 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
26 26
27 27 namespace {
28 28
29 29 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
30 30 const SqpRange &oldGraphRange)
31 31 {
32 32 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
33 33
34 34 auto varRangeRequested = varRange;
35 35 switch (zoomType) {
36 36 case AcquisitionZoomType::ZoomIn: {
37 37 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
38 38 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
39 39 varRangeRequested.m_TStart += deltaLeft;
40 40 varRangeRequested.m_TEnd -= deltaRight;
41 41 break;
42 42 }
43 43
44 44 case AcquisitionZoomType::ZoomOut: {
45 45 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
46 46 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
47 47 varRangeRequested.m_TStart -= deltaLeft;
48 48 varRangeRequested.m_TEnd += deltaRight;
49 49 break;
50 50 }
51 51 case AcquisitionZoomType::PanRight: {
52 52 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
53 53 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
54 54 varRangeRequested.m_TStart += deltaLeft;
55 55 varRangeRequested.m_TEnd += deltaRight;
56 56 break;
57 57 }
58 58 case AcquisitionZoomType::PanLeft: {
59 59 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
60 60 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
61 61 varRangeRequested.m_TStart -= deltaLeft;
62 62 varRangeRequested.m_TEnd -= deltaRight;
63 63 break;
64 64 }
65 65 case AcquisitionZoomType::Unknown: {
66 66 qCCritical(LOG_VariableController())
67 67 << VariableController::tr("Impossible to synchronize: zoom type unknown");
68 68 break;
69 69 }
70 70 default:
71 71 qCCritical(LOG_VariableController()) << VariableController::tr(
72 72 "Impossible to synchronize: zoom type not take into account");
73 73 // No action
74 74 break;
75 75 }
76 76
77 77 return varRangeRequested;
78 78 }
79 79 }
80 80
81 81 enum class VariableRequestHandlerState { OFF, RUNNING, PENDING };
82 82
83 83 struct VariableRequestHandler {
84 84
85 85 VariableRequestHandler()
86 86 {
87 87 m_CanUpdate = false;
88 88 m_State = VariableRequestHandlerState::OFF;
89 89 }
90 90
91 91 QUuid m_VarId;
92 92 VariableRequest m_RunningVarRequest;
93 93 VariableRequest m_PendingVarRequest;
94 94 VariableRequestHandlerState m_State;
95 95 bool m_CanUpdate;
96 96 };
97 97
98 98 struct VariableController::VariableControllerPrivate {
99 99 explicit VariableControllerPrivate(VariableController *parent)
100 100 : m_WorkingMutex{},
101 101 m_VariableModel{new VariableModel{parent}},
102 102 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
103 103 // m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
104 104 m_VariableCacheStrategy{VariableCacheStrategyFactory::createCacheStrategy(
105 105 CacheStrategy::SingleThreshold)},
106 106 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
107 107 q{parent}
108 108 {
109 109
110 110 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
111 111 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
112 112 }
113 113
114 114
115 115 virtual ~VariableControllerPrivate()
116 116 {
117 117 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
118 118 m_VariableAcquisitionWorkerThread.quit();
119 119 m_VariableAcquisitionWorkerThread.wait();
120 120 }
121 121
122 122
123 123 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
124 124 QUuid varRequestId);
125 125
126 126 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
127 127 std::shared_ptr<IDataSeries>
128 128 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
129 129
130 130 void registerProvider(std::shared_ptr<IDataProvider> provider);
131 131
132 132 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
133 133 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
134 134 void updateVariables(QUuid varRequestId);
135 135 void updateVariableRequest(QUuid varRequestId);
136 136 void cancelVariableRequest(QUuid varRequestId);
137 137 void executeVarRequest(std::shared_ptr<Variable> var, VariableRequest &varRequest);
138 138
139 139 QMutex m_WorkingMutex;
140 140 /// Variable model. The VariableController has the ownership
141 141 VariableModel *m_VariableModel;
142 142 QItemSelectionModel *m_VariableSelectionModel;
143 143
144 144
145 145 TimeController *m_TimeController{nullptr};
146 146 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
147 147 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
148 148 QThread m_VariableAcquisitionWorkerThread;
149 149
150 150 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
151 151 m_VariableToProviderMap;
152 152 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
153 153 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
154 154 m_GroupIdToVariableSynchronizationGroupMap;
155 155 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
156 156 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
157 157
158 158 std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds;
159 159 std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler;
160 160
161 161 VariableController *q;
162 162 };
163 163
164 164
165 165 VariableController::VariableController(QObject *parent)
166 166 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
167 167 {
168 168 qCDebug(LOG_VariableController()) << tr("VariableController construction")
169 169 << QThread::currentThread();
170 170
171 171 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
172 172 &VariableController::onAbortProgressRequested);
173 173
174 174 connect(impl->m_VariableAcquisitionWorker.get(),
175 175 &VariableAcquisitionWorker::variableCanceledRequested, this,
176 176 &VariableController::onAbortAcquisitionRequested);
177 177
178 178 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
179 179 &VariableController::onDataProvided);
180 180 connect(impl->m_VariableAcquisitionWorker.get(),
181 181 &VariableAcquisitionWorker::variableRequestInProgress, this,
182 182 &VariableController::onVariableRetrieveDataInProgress);
183 183
184 184
185 185 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
186 186 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
187 187 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
188 188 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
189 189
190 connect(impl->m_VariableModel, &VariableModel::requestVariableRangeUpdate, this,
191 &VariableController::onUpdateDateTime);
190 192
191 193 impl->m_VariableAcquisitionWorkerThread.start();
192 194 }
193 195
194 196 VariableController::~VariableController()
195 197 {
196 198 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
197 199 << QThread::currentThread();
198 200 this->waitForFinish();
199 201 }
200 202
201 203 VariableModel *VariableController::variableModel() noexcept
202 204 {
203 205 return impl->m_VariableModel;
204 206 }
205 207
206 208 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
207 209 {
208 210 return impl->m_VariableSelectionModel;
209 211 }
210 212
211 213 void VariableController::setTimeController(TimeController *timeController) noexcept
212 214 {
213 215 impl->m_TimeController = timeController;
214 216 }
215 217
216 218 std::shared_ptr<Variable>
217 219 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
218 220 {
219 221 if (impl->m_VariableModel->containsVariable(variable)) {
220 222 // Clones variable
221 223 auto duplicate = variable->clone();
222 224
223 225 // Adds clone to model
224 226 impl->m_VariableModel->addVariable(duplicate);
225 227
226 228 // Generates clone identifier
227 229 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
228 230
229 231 // Registers provider
230 232 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
231 233 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
232 234
233 235 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
234 236 if (duplicateProvider) {
235 237 impl->registerProvider(duplicateProvider);
236 238 }
237 239
238 240 return duplicate;
239 241 }
240 242 else {
241 243 qCCritical(LOG_VariableController())
242 244 << tr("Can't create duplicate of variable %1: variable not registered in the model")
243 245 .arg(variable->name());
244 246 return nullptr;
245 247 }
246 248 }
247 249
248 250 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
249 251 {
250 252 if (!variable) {
251 253 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
252 254 return;
253 255 }
254 256
255 257 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
256 258 // make some treatments before the deletion
257 259 emit variableAboutToBeDeleted(variable);
258 260
259 261 // Deletes identifier
260 262 impl->m_VariableToIdentifierMap.erase(variable);
261 263
262 264 // Deletes provider
263 265 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
264 266 qCDebug(LOG_VariableController())
265 267 << tr("Number of providers deleted for variable %1: %2")
266 268 .arg(variable->name(), QString::number(nbProvidersDeleted));
267 269
268 270
269 271 // Deletes from model
270 272 impl->m_VariableModel->deleteVariable(variable);
271 273 }
272 274
273 275 void VariableController::deleteVariables(
274 276 const QVector<std::shared_ptr<Variable> > &variables) noexcept
275 277 {
276 278 for (auto variable : qAsConst(variables)) {
277 279 deleteVariable(variable);
278 280 }
279 281 }
280 282
281 283 QByteArray
282 284 VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const
283 285 {
284 286 auto encodedData = QByteArray{};
285 287
286 288 QVariantList ids;
287 289 for (auto &var : variables) {
288 290 auto itVar = impl->m_VariableToIdentifierMap.find(var);
289 291 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
290 292 qCCritical(LOG_VariableController())
291 293 << tr("Impossible to find the data for an unknown variable.");
292 294 }
293 295
294 296 ids << itVar->second.toByteArray();
295 297 }
296 298
297 299 QDataStream stream{&encodedData, QIODevice::WriteOnly};
298 300 stream << ids;
299 301
300 302 return encodedData;
301 303 }
302 304
303 305 QList<std::shared_ptr<Variable> >
304 306 VariableController::variablesForMimeData(const QByteArray &mimeData) const
305 307 {
306 308 auto variables = QList<std::shared_ptr<Variable> >{};
307 309 QDataStream stream{mimeData};
308 310
309 311 QVariantList ids;
310 312 stream >> ids;
311 313
312 314 for (auto id : ids) {
313 315 auto uuid = QUuid{id.toByteArray()};
314 316 auto var = impl->findVariable(uuid);
315 317 variables << var;
316 318 }
317 319
318 320 return variables;
319 321 }
320 322
321 323 std::shared_ptr<Variable>
322 324 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
323 325 std::shared_ptr<IDataProvider> provider) noexcept
324 326 {
325 327 if (!impl->m_TimeController) {
326 328 qCCritical(LOG_VariableController())
327 329 << tr("Impossible to create variable: The time controller is null");
328 330 return nullptr;
329 331 }
330 332
331 333 auto range = impl->m_TimeController->dateTime();
332 334
333 335 if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) {
334 336 auto varId = QUuid::createUuid();
335 337
336 338 // Create the handler
337 339 auto varRequestHandler = std::make_unique<VariableRequestHandler>();
338 340 varRequestHandler->m_VarId = varId;
339 341
340 342 impl->m_VarIdToVarRequestHandler.insert(
341 343 std::make_pair(varId, std::move(varRequestHandler)));
342 344
343 345 // store the provider
344 346 impl->registerProvider(provider);
345 347
346 348 // Associate the provider
347 349 impl->m_VariableToProviderMap[newVariable] = provider;
348 350 impl->m_VariableToIdentifierMap[newVariable] = varId;
349 351
350 352 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false);
351 353
352 354 // auto varRequestId = QUuid::createUuid();
353 355 // qCInfo(LOG_VariableController()) << "createVariable: " << varId << varRequestId;
354 356 // impl->processRequest(newVariable, range, varRequestId);
355 357 // impl->updateVariableRequest(varRequestId);
356 358
357 359 return newVariable;
358 360 }
359 361 }
360 362
361 363 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
362 364 {
363 365 // NOTE: Even if acquisition request is aborting, the graphe range will be changed
364 366 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
365 367 << QThread::currentThread()->objectName();
366 368 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
367 369
368 370 // NOTE we only permit the time modification for one variable
369 371 // DEPRECATED
370 372 // auto variables = QVector<std::shared_ptr<Variable> >{};
371 373 // for (const auto &selectedRow : qAsConst(selectedRows)) {
372 374 // if (auto selectedVariable =
373 375 // impl->m_VariableModel->variable(selectedRow.row())) {
374 376 // variables << selectedVariable;
375 377
376 378 // // notify that rescale operation has to be done
377 379 // emit rangeChanged(selectedVariable, dateTime);
378 380 // }
379 381 // }
380 382 // if (!variables.isEmpty()) {
381 383 // this->onRequestDataLoading(variables, dateTime, synchro);
382 384 // }
383 385 if (selectedRows.size() == 1) {
384 386
385 387 if (auto selectedVariable
386 388 = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) {
387 389
388 auto itVar = impl->m_VariableToIdentifierMap.find(selectedVariable);
389 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
390 qCCritical(LOG_VariableController())
391 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
392 return;
393 }
394
395 // notify that rescale operation has to be done
396 emit rangeChanged(selectedVariable, dateTime);
397
398 auto synchro = impl->m_VariableIdGroupIdMap.find(itVar->second)
399 != impl->m_VariableIdGroupIdMap.cend();
400
401 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{selectedVariable},
402 dateTime, synchro);
390 onUpdateDateTime(selectedVariable, dateTime);
403 391 }
404 392 }
405 393 else if (selectedRows.size() > 1) {
406 394 qCCritical(LOG_VariableController())
407 395 << tr("Impossible to set time for more than 1 variable in the same time");
408 396 }
409 397 else {
410 398 qCWarning(LOG_VariableController())
411 399 << tr("There is no variable selected to set the time one");
412 400 }
413 401 }
414 402
403 void VariableController::onUpdateDateTime(std::shared_ptr<Variable> variable,
404 const SqpRange &dateTime)
405 {
406 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
407 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
408 qCCritical(LOG_VariableController())
409 << tr("Impossible to onDateTimeOnSelection request for unknown variable");
410 return;
411 }
412
413 // notify that rescale operation has to be done
414 emit rangeChanged(variable, dateTime);
415
416 auto synchro
417 = impl->m_VariableIdGroupIdMap.find(itVar->second) != impl->m_VariableIdGroupIdMap.cend();
418
419 this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{variable}, dateTime, synchro);
420 }
421
415 422 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
416 423 const SqpRange &cacheRangeRequested,
417 424 QVector<AcquisitionDataPacket> dataAcquired)
418 425 {
419 426 qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread();
420 427 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
421 428 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
422 429 if (!varRequestId.isNull()) {
423 430 impl->updateVariables(varRequestId);
424 431 }
425 432 }
426 433
427 434 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
428 435 {
429 436 qCDebug(LOG_VariableController())
430 437 << "TORM: variableController::onVariableRetrieveDataInProgress"
431 438 << QThread::currentThread()->objectName() << progress;
432 439 if (auto var = impl->findVariable(identifier)) {
433 440 impl->m_VariableModel->setDataProgress(var, progress);
434 441 }
435 442 else {
436 443 qCCritical(LOG_VariableController())
437 444 << tr("Impossible to notify progression of a null variable");
438 445 }
439 446 }
440 447
441 448 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
442 449 {
443 450 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested"
444 451 << QThread::currentThread()->objectName() << variable->name();
445 452
446 453 auto itVar = impl->m_VariableToIdentifierMap.find(variable);
447 454 if (itVar == impl->m_VariableToIdentifierMap.cend()) {
448 455 qCCritical(LOG_VariableController())
449 456 << tr("Impossible to onAbortProgressRequested request for unknown variable");
450 457 return;
451 458 }
452 459
453 460 auto varId = itVar->second;
454 461
455 462 auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId);
456 463 if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) {
457 464 qCCritical(LOG_VariableController())
458 465 << tr("Impossible to onAbortProgressRequested for variable with unknown handler");
459 466 return;
460 467 }
461 468
462 469 auto varHandler = itVarHandler->second.get();
463 470
464 471 // case where a variable has a running request
465 472 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
466 473 impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId);
467 474 }
468 475 }
469 476
470 477 void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier)
471 478 {
472 479 qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested"
473 480 << QThread::currentThread()->objectName() << vIdentifier;
474 481
475 482 if (auto var = impl->findVariable(vIdentifier)) {
476 483 this->onAbortProgressRequested(var);
477 484 }
478 485 else {
479 486 qCCritical(LOG_VariableController())
480 487 << tr("Impossible to abort Acquisition Requestof a null variable");
481 488 }
482 489 }
483 490
484 491 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
485 492 {
486 493 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
487 494 << QThread::currentThread()->objectName()
488 495 << synchronizationGroupId;
489 496 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
490 497 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
491 498 std::make_pair(synchronizationGroupId, vSynchroGroup));
492 499 }
493 500
494 501 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
495 502 {
496 503 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
497 504 }
498 505
499 506 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
500 507 QUuid synchronizationGroupId)
501 508
502 509 {
503 510 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
504 511 << synchronizationGroupId;
505 512 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
506 513 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
507 514 auto groupIdToVSGIt
508 515 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
509 516 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
510 517 impl->m_VariableIdGroupIdMap.insert(
511 518 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
512 519 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
513 520 }
514 521 else {
515 522 qCCritical(LOG_VariableController())
516 523 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
517 524 << variable->name();
518 525 }
519 526 }
520 527 else {
521 528 qCCritical(LOG_VariableController())
522 529 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
523 530 }
524 531 }
525 532
526 533 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
527 534 QUuid synchronizationGroupId)
528 535 {
529 536 // Gets variable id
530 537 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
531 538 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
532 539 qCCritical(LOG_VariableController())
533 540 << tr("Can't desynchronize variable %1: variable identifier not found")
534 541 .arg(variable->name());
535 542 return;
536 543 }
537 544
538 545 // Gets synchronization group
539 546 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
540 547 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
541 548 qCCritical(LOG_VariableController())
542 549 << tr("Can't desynchronize variable %1: unknown synchronization group")
543 550 .arg(variable->name());
544 551 return;
545 552 }
546 553
547 554 auto variableId = variableIt->second;
548 555
549 556 // Removes variable from synchronization group
550 557 auto synchronizationGroup = groupIt->second;
551 558 synchronizationGroup->removeVariableId(variableId);
552 559
553 560 // Removes link between variable and synchronization group
554 561 impl->m_VariableIdGroupIdMap.erase(variableId);
555 562 }
556 563
557 564 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
558 565 const SqpRange &range, bool synchronise)
559 566 {
560 567 // variables is assumed synchronized
561 568 // TODO: Asser variables synchronization
562 569 // we want to load data of the variable for the dateTime.
563 570 if (variables.isEmpty()) {
564 571 return;
565 572 }
566 573
567 574 auto varRequestId = QUuid::createUuid();
568 575 qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading"
569 576 << QThread::currentThread()->objectName() << varRequestId
570 577 << range << synchronise;
571 578
572 579 if (!synchronise) {
573 580 auto varIds = std::list<QUuid>{};
574 581 for (const auto &var : variables) {
575 582 auto vId = impl->m_VariableToIdentifierMap.at(var);
576 583 varIds.push_back(vId);
577 584 }
578 585 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
579 586 for (const auto &var : variables) {
580 587 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId
581 588 << varIds.size();
582 589 impl->processRequest(var, range, varRequestId);
583 590 }
584 591 }
585 592 else {
586 593 auto vId = impl->m_VariableToIdentifierMap.at(variables.first());
587 594 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
588 595 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
589 596 auto groupId = varIdToGroupIdIt->second;
590 597
591 598 auto vSynchronizationGroup
592 599 = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId);
593 600 auto vSyncIds = vSynchronizationGroup->getIds();
594 601
595 602 auto varIds = std::list<QUuid>{};
596 603 for (auto vId : vSyncIds) {
597 604 varIds.push_back(vId);
598 605 }
599 606 impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds));
600 607
601 608 for (auto vId : vSyncIds) {
602 609 auto var = impl->findVariable(vId);
603 610
604 611 // Don't process already processed var
605 612 if (var != nullptr) {
606 613 qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name()
607 614 << varRequestId;
608 615 auto vSyncRangeRequested
609 616 = variables.contains(var)
610 617 ? range
611 618 : computeSynchroRangeRequested(var->range(), range,
612 619 variables.first()->range());
613 620 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
614 621 impl->processRequest(var, vSyncRangeRequested, varRequestId);
615 622 }
616 623 else {
617 624 qCCritical(LOG_VariableController())
618 625
619 626 << tr("Impossible to synchronize a null variable");
620 627 }
621 628 }
622 629 }
623 630 }
624 631
625 632 impl->updateVariables(varRequestId);
626 633 }
627 634
628 635
629 636 void VariableController::initialize()
630 637 {
631 638 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
632 639 impl->m_WorkingMutex.lock();
633 640 qCDebug(LOG_VariableController()) << tr("VariableController init END");
634 641 }
635 642
636 643 void VariableController::finalize()
637 644 {
638 645 impl->m_WorkingMutex.unlock();
639 646 }
640 647
641 648 void VariableController::waitForFinish()
642 649 {
643 650 QMutexLocker locker{&impl->m_WorkingMutex};
644 651 }
645 652
646 653 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
647 654 {
648 655 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
649 656 auto zoomType = AcquisitionZoomType::Unknown;
650 657 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
651 658 qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut";
652 659 zoomType = AcquisitionZoomType::ZoomOut;
653 660 }
654 661 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
655 662 qCDebug(LOG_VariableController()) << "zoomtype: PanRight";
656 663 zoomType = AcquisitionZoomType::PanRight;
657 664 }
658 665 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
659 666 qCDebug(LOG_VariableController()) << "zoomtype: PanLeft";
660 667 zoomType = AcquisitionZoomType::PanLeft;
661 668 }
662 669 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
663 670 qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn";
664 671 zoomType = AcquisitionZoomType::ZoomIn;
665 672 }
666 673 else {
667 674 qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected";
668 675 }
669 676 return zoomType;
670 677 }
671 678
672 679 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
673 680 const SqpRange &rangeRequested,
674 681 QUuid varRequestId)
675 682 {
676 683 auto itVar = m_VariableToIdentifierMap.find(var);
677 684 if (itVar == m_VariableToIdentifierMap.cend()) {
678 685 qCCritical(LOG_VariableController())
679 686 << tr("Impossible to process request for unknown variable");
680 687 return;
681 688 }
682 689
683 690 auto varId = itVar->second;
684 691
685 692 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
686 693 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
687 694 qCCritical(LOG_VariableController())
688 695 << tr("Impossible to process request for variable with unknown handler");
689 696 return;
690 697 }
691 698
692 699 auto oldRange = var->range();
693 700
694 701 auto varHandler = itVarHandler->second.get();
695 702
696 703 if (varHandler->m_State != VariableRequestHandlerState::OFF) {
697 704 oldRange = varHandler->m_RunningVarRequest.m_RangeRequested;
698 705 }
699 706
700 707 auto varRequest = VariableRequest{};
701 708 varRequest.m_VariableGroupId = varRequestId;
702 709 auto varStrategyRangesRequested
703 710 = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested);
704 711 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
705 712 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
706 713
707 714 switch (varHandler->m_State) {
708 715 case VariableRequestHandlerState::OFF: {
709 716 qCDebug(LOG_VariableController()) << tr("Process Request OFF")
710 717 << varRequest.m_RangeRequested
711 718 << varRequest.m_CacheRangeRequested;
712 719 varHandler->m_RunningVarRequest = varRequest;
713 720 varHandler->m_State = VariableRequestHandlerState::RUNNING;
714 721 executeVarRequest(var, varRequest);
715 722 break;
716 723 }
717 724 case VariableRequestHandlerState::RUNNING: {
718 725 qCDebug(LOG_VariableController()) << tr("Process Request RUNNING")
719 726 << varRequest.m_RangeRequested
720 727 << varRequest.m_CacheRangeRequested;
721 728 varHandler->m_State = VariableRequestHandlerState::PENDING;
722 729 varHandler->m_PendingVarRequest = varRequest;
723 730 break;
724 731 }
725 732 case VariableRequestHandlerState::PENDING: {
726 733 qCDebug(LOG_VariableController()) << tr("Process Request PENDING")
727 734 << varRequest.m_RangeRequested
728 735 << varRequest.m_CacheRangeRequested;
729 736 auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId;
730 737 cancelVariableRequest(variableGroupIdToCancel);
731 738 // Cancel variable can make state downgrade
732 739 varHandler->m_State = VariableRequestHandlerState::PENDING;
733 740 varHandler->m_PendingVarRequest = varRequest;
734 741
735 742 break;
736 743 }
737 744 default:
738 745 qCCritical(LOG_VariableController())
739 746 << QObject::tr("Unknown VariableRequestHandlerState");
740 747 }
741 748 }
742 749
743 750 std::shared_ptr<Variable>
744 751 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
745 752 {
746 753 std::shared_ptr<Variable> var;
747 754 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
748 755
749 756 auto end = m_VariableToIdentifierMap.cend();
750 757 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
751 758 if (it != end) {
752 759 var = it->first;
753 760 }
754 761 else {
755 762 qCCritical(LOG_VariableController())
756 763 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
757 764 }
758 765
759 766 return var;
760 767 }
761 768
762 769 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
763 770 const QVector<AcquisitionDataPacket> acqDataPacketVector)
764 771 {
765 772 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
766 773 << acqDataPacketVector.size();
767 774 std::shared_ptr<IDataSeries> dataSeries;
768 775 if (!acqDataPacketVector.isEmpty()) {
769 776 dataSeries = acqDataPacketVector[0].m_DateSeries;
770 777 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
771 778 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
772 779 }
773 780 }
774 781 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
775 782 << acqDataPacketVector.size();
776 783 return dataSeries;
777 784 }
778 785
779 786 void VariableController::VariableControllerPrivate::registerProvider(
780 787 std::shared_ptr<IDataProvider> provider)
781 788 {
782 789 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
783 790 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
784 791 << provider->objectName();
785 792 m_ProviderSet.insert(provider);
786 793 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
787 794 &VariableAcquisitionWorker::onVariableDataAcquired);
788 795 connect(provider.get(), &IDataProvider::dataProvidedProgress,
789 796 m_VariableAcquisitionWorker.get(),
790 797 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
791 798 connect(provider.get(), &IDataProvider::dataProvidedFailed,
792 799 m_VariableAcquisitionWorker.get(),
793 800 &VariableAcquisitionWorker::onVariableAcquisitionFailed);
794 801 }
795 802 else {
796 803 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
797 804 }
798 805 }
799 806
800 807 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
801 808 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
802 809 {
803 810 auto itVarHandler = m_VarIdToVarRequestHandler.find(varId);
804 811 if (itVarHandler == m_VarIdToVarRequestHandler.cend()) {
805 812 return QUuid();
806 813 }
807 814
808 815 auto varHandler = itVarHandler->second.get();
809 816 if (varHandler->m_State == VariableRequestHandlerState::OFF) {
810 817 qCCritical(LOG_VariableController())
811 818 << tr("acceptVariableRequest impossible on a variable with OFF state");
812 819 }
813 820
814 821 varHandler->m_RunningVarRequest.m_DataSeries = dataSeries;
815 822 varHandler->m_CanUpdate = true;
816 823
817 824 // Element traitΓ©, on a dΓ©jΓ  toutes les donnΓ©es necessaires
818 825 auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId;
819 826 qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId
820 827 << m_VarGroupIdToVarIds.size();
821 828
822 829 return varHandler->m_RunningVarRequest.m_VariableGroupId;
823 830 }
824 831
825 832 void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId)
826 833 {
827 834 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
828 835 << QThread::currentThread()->objectName() << varRequestId;
829 836
830 837 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
831 838 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
832 839 qCWarning(LOG_VariableController())
833 840 << tr("Impossible to updateVariables of unknown variables") << varRequestId;
834 841 return;
835 842 }
836 843
837 844 auto &varIds = varGroupIdToVarIdsIt->second;
838 845 auto varIdsEnd = varIds.end();
839 846 bool processVariableUpdate = true;
840 847 qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables"
841 848 << varRequestId << varIds.size();
842 849 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate;
843 850 ++varIdsIt) {
844 851 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
845 852 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
846 853 processVariableUpdate &= itVarHandler->second->m_CanUpdate;
847 854 }
848 855 }
849 856
850 857 if (processVariableUpdate) {
851 858 qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size();
852 859 for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) {
853 860 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
854 861 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
855 862 if (auto var = findVariable(*varIdsIt)) {
856 863 auto &varRequest = itVarHandler->second->m_RunningVarRequest;
857 864 var->setRange(varRequest.m_RangeRequested);
858 865 var->setCacheRange(varRequest.m_CacheRangeRequested);
859 866 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
860 867 << varRequest.m_RangeRequested
861 868 << varRequest.m_CacheRangeRequested;
862 869 qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before")
863 870 << var->nbPoints()
864 871 << varRequest.m_DataSeries->nbPoints();
865 872 var->mergeDataSeries(varRequest.m_DataSeries);
866 873 qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after")
867 874 << var->nbPoints();
868 875
869 876 emit var->updated();
870 877 qCDebug(LOG_VariableController()) << tr("Update OK");
871 878 }
872 879 else {
873 880 qCCritical(LOG_VariableController())
874 881 << tr("Impossible to update data to a null variable");
875 882 }
876 883 }
877 884 }
878 885 updateVariableRequest(varRequestId);
879 886
880 887 // cleaning varRequestId
881 888 qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId;
882 889 m_VarGroupIdToVarIds.erase(varRequestId);
883 890 }
884 891 }
885 892
886 893
887 894 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
888 895 {
889 896 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
890 897 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
891 898 qCCritical(LOG_VariableController()) << QObject::tr(
892 899 "Impossible to updateVariableRequest since varGroupdId isn't here anymore");
893 900
894 901 return;
895 902 }
896 903
897 904 auto &varIds = varGroupIdToVarIdsIt->second;
898 905 auto varIdsEnd = varIds.end();
899 906 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
900 907 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
901 908 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
902 909
903 910 auto varHandler = itVarHandler->second.get();
904 911 varHandler->m_CanUpdate = false;
905 912
906 913
907 914 switch (varHandler->m_State) {
908 915 case VariableRequestHandlerState::OFF: {
909 916 qCCritical(LOG_VariableController())
910 917 << QObject::tr("Impossible to update a variable with handler in OFF state");
911 918 } break;
912 919 case VariableRequestHandlerState::RUNNING: {
913 920 varHandler->m_State = VariableRequestHandlerState::OFF;
914 921 varHandler->m_RunningVarRequest = VariableRequest{};
915 922 break;
916 923 }
917 924 case VariableRequestHandlerState::PENDING: {
918 925 varHandler->m_State = VariableRequestHandlerState::RUNNING;
919 926 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
920 927 varHandler->m_PendingVarRequest = VariableRequest{};
921 928 auto var = findVariable(itVarHandler->first);
922 929 executeVarRequest(var, varHandler->m_RunningVarRequest);
923 930 break;
924 931 }
925 932 default:
926 933 qCCritical(LOG_VariableController())
927 934 << QObject::tr("Unknown VariableRequestHandlerState");
928 935 }
929 936 }
930 937 }
931 938 }
932 939
933 940
934 941 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
935 942 {
936 943 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId;
937 944
938 945 auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId);
939 946 if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) {
940 947 qCCritical(LOG_VariableController())
941 948 << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId;
942 949 return;
943 950 }
944 951
945 952 auto &varIds = varGroupIdToVarIdsIt->second;
946 953 auto varIdsEnd = varIds.end();
947 954 for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) {
948 955 auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt);
949 956 if (itVarHandler != m_VarIdToVarRequestHandler.cend()) {
950 957
951 958 auto varHandler = itVarHandler->second.get();
952 959 varHandler->m_VarId = QUuid{};
953 960 switch (varHandler->m_State) {
954 961 case VariableRequestHandlerState::OFF: {
955 962 qCWarning(LOG_VariableController())
956 963 << QObject::tr("Impossible to cancel a variable with no running request");
957 964 break;
958 965 }
959 966 case VariableRequestHandlerState::RUNNING: {
960 967
961 968 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
962 969 auto var = findVariable(itVarHandler->first);
963 970 auto varProvider = m_VariableToProviderMap.at(var);
964 971 if (varProvider != nullptr) {
965 972 m_VariableAcquisitionWorker->abortProgressRequested(
966 973 itVarHandler->first);
967 974 }
968 975 m_VariableModel->setDataProgress(var, 0.0);
969 976 varHandler->m_CanUpdate = false;
970 977 varHandler->m_State = VariableRequestHandlerState::OFF;
971 978 varHandler->m_RunningVarRequest = VariableRequest{};
972 979 }
973 980 else {
974 981 // TODO: log Impossible to cancel the running variable request beacause its
975 982 // varRequestId isn't not the canceled one
976 983 }
977 984 break;
978 985 }
979 986 case VariableRequestHandlerState::PENDING: {
980 987 if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) {
981 988 auto var = findVariable(itVarHandler->first);
982 989 auto varProvider = m_VariableToProviderMap.at(var);
983 990 if (varProvider != nullptr) {
984 991 m_VariableAcquisitionWorker->abortProgressRequested(
985 992 itVarHandler->first);
986 993 }
987 994 m_VariableModel->setDataProgress(var, 0.0);
988 995 varHandler->m_CanUpdate = false;
989 996 varHandler->m_State = VariableRequestHandlerState::RUNNING;
990 997 varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest;
991 998 varHandler->m_PendingVarRequest = VariableRequest{};
992 999 executeVarRequest(var, varHandler->m_RunningVarRequest);
993 1000 }
994 1001 else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) {
995 1002 varHandler->m_State = VariableRequestHandlerState::RUNNING;
996 1003 varHandler->m_PendingVarRequest = VariableRequest{};
997 1004 }
998 1005 else {
999 1006 // TODO: log Impossible to cancel the variable request beacause its
1000 1007 // varRequestId isn't not the canceled one
1001 1008 }
1002 1009 break;
1003 1010 }
1004 1011 default:
1005 1012 qCCritical(LOG_VariableController())
1006 1013 << QObject::tr("Unknown VariableRequestHandlerState");
1007 1014 }
1008 1015 }
1009 1016 }
1010 1017 qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId;
1011 1018 m_VarGroupIdToVarIds.erase(varRequestId);
1012 1019 }
1013 1020
1014 1021 void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var,
1015 1022 VariableRequest &varRequest)
1016 1023 {
1017 1024 qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest");
1018 1025
1019 1026 auto varId = m_VariableToIdentifierMap.at(var);
1020 1027
1021 1028 auto varCacheRange = var->cacheRange();
1022 1029 auto varCacheRangeRequested = varRequest.m_CacheRangeRequested;
1023 1030 auto notInCacheRangeList
1024 1031 = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested);
1025 1032 auto inCacheRangeList
1026 1033 = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested);
1027 1034
1028 1035 if (!notInCacheRangeList.empty()) {
1029 1036
1030 1037 auto varProvider = m_VariableToProviderMap.at(var);
1031 1038 if (varProvider != nullptr) {
1032 1039 qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested
1033 1040 << varRequest.m_CacheRangeRequested;
1034 1041 m_VariableAcquisitionWorker->pushVariableRequest(
1035 1042 varRequest.m_VariableGroupId, varId, varRequest.m_RangeRequested,
1036 1043 varRequest.m_CacheRangeRequested,
1037 1044 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
1038 1045 varProvider);
1039 1046 }
1040 1047 else {
1041 1048 qCCritical(LOG_VariableController())
1042 1049 << "Impossible to provide data with a null provider";
1043 1050 }
1044 1051
1045 1052 if (!inCacheRangeList.empty()) {
1046 1053 emit q->updateVarDisplaying(var, inCacheRangeList.first());
1047 1054 }
1048 1055 }
1049 1056 else {
1050 1057 acceptVariableRequest(varId,
1051 1058 var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested));
1052 1059 }
1053 1060 }
@@ -1,383 +1,394
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableController.h>
3 3 #include <Variable/VariableModel.h>
4 4
5 5 #include <Common/DateUtils.h>
6 6 #include <Common/MimeTypesDef.h>
7 7 #include <Common/StringUtils.h>
8 8
9 9 #include <Data/IDataSeries.h>
10 10
11 11 #include <DataSource/DataSourceController.h>
12 12 #include <Time/TimeController.h>
13 13
14 14 #include <QMimeData>
15 15 #include <QSize>
16 #include <QTimer>
16 17 #include <unordered_map>
17 18
18 19 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
19 20
20 21 namespace {
21 22
22 23 // Column indexes
23 24 const auto NAME_COLUMN = 0;
24 25 const auto TSTART_COLUMN = 1;
25 26 const auto TEND_COLUMN = 2;
26 27 const auto NBPOINTS_COLUMN = 3;
27 28 const auto UNIT_COLUMN = 4;
28 29 const auto MISSION_COLUMN = 5;
29 30 const auto PLUGIN_COLUMN = 6;
30 31 const auto NB_COLUMNS = 7;
31 32
32 33 // Column properties
33 34 const auto DEFAULT_HEIGHT = 25;
34 35 const auto DEFAULT_WIDTH = 100;
35 36
36 37 struct ColumnProperties {
37 38 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
38 39 int height = DEFAULT_HEIGHT)
39 40 : m_Name{name}, m_Width{width}, m_Height{height}
40 41 {
41 42 }
42 43
43 44 QString m_Name;
44 45 int m_Width;
45 46 int m_Height;
46 47 };
47 48
48 49 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
49 50 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
50 51 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
51 52 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
52 53 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
53 54
54 55 /// Format for datetimes
55 56 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
56 57
57 58 QString uniqueName(const QString &defaultName,
58 59 const std::vector<std::shared_ptr<Variable> > &variables)
59 60 {
60 61 auto forbiddenNames = std::vector<QString>(variables.size());
61 62 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
62 63 [](const auto &variable) { return variable->name(); });
63 64 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
64 65 Q_ASSERT(!uniqueName.isEmpty());
65 66
66 67 return uniqueName;
67 68 }
68 69
69 70 } // namespace
70 71
71 72 struct VariableModel::VariableModelPrivate {
72 73 /// Variables created in SciQlop
73 74 std::vector<std::shared_ptr<Variable> > m_Variables;
74 75 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
75 76 VariableController *m_VariableController;
76 77
77 78 /// Return the row index of the variable. -1 if it's not found
78 79 int indexOfVariable(Variable *variable) const noexcept;
79 80 };
80 81
81 82 VariableModel::VariableModel(VariableController *parent)
82 83 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
83 84 {
84 85 impl->m_VariableController = parent;
85 86 }
86 87
87 88 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
88 89 {
89 90 auto insertIndex = rowCount();
90 91 beginInsertRows({}, insertIndex, insertIndex);
91 92
92 93 // Generates unique name for the variable
93 94 variable->setName(uniqueName(variable->name(), impl->m_Variables));
94 95
95 96 impl->m_Variables.push_back(variable);
96 97 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
97 98
98 99 endInsertRows();
99 100 }
100 101
101 102 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
102 103 {
103 104 auto end = impl->m_Variables.cend();
104 105 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
105 106 }
106 107
107 108 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
108 109 const QVariantHash &metadata) noexcept
109 110 {
110 111 auto variable = std::make_shared<Variable>(name, metadata);
111 112 addVariable(variable);
112 113
113 114 return variable;
114 115 }
115 116
116 117 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
117 118 {
118 119 if (!variable) {
119 120 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
120 121 return;
121 122 }
122 123
123 124 // Finds variable in the model
124 125 auto begin = impl->m_Variables.cbegin();
125 126 auto end = impl->m_Variables.cend();
126 127 auto it = std::find(begin, end, variable);
127 128 if (it != end) {
128 129 auto removeIndex = std::distance(begin, it);
129 130
130 131 // Deletes variable
131 132 beginRemoveRows({}, removeIndex, removeIndex);
132 133 impl->m_Variables.erase(it);
133 134 endRemoveRows();
134 135 }
135 136 else {
136 137 qCritical(LOG_VariableModel())
137 138 << tr("Can't delete variable %1 from the model: the variable is not in the model")
138 139 .arg(variable->name());
139 140 }
140 141
141 142 // Removes variable from progress map
142 143 impl->m_VariableToProgress.erase(variable);
143 144 }
144 145
145 146
146 147 std::shared_ptr<Variable> VariableModel::variable(int index) const
147 148 {
148 149 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
149 150 }
150 151
151 152 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
152 153 {
153 154 return impl->m_Variables;
154 155 }
155 156
156 157 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
157 158 {
158 159 if (progress > 0.0) {
159 160 impl->m_VariableToProgress[variable] = progress;
160 161 }
161 162 else {
162 163 impl->m_VariableToProgress.erase(variable);
163 164 }
164 165 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
165 166
166 167 emit dataChanged(modelIndex, modelIndex);
167 168 }
168 169
169 170 int VariableModel::columnCount(const QModelIndex &parent) const
170 171 {
171 172 Q_UNUSED(parent);
172 173
173 174 return NB_COLUMNS;
174 175 }
175 176
176 177 int VariableModel::rowCount(const QModelIndex &parent) const
177 178 {
178 179 Q_UNUSED(parent);
179 180
180 181 return impl->m_Variables.size();
181 182 }
182 183
183 184 QVariant VariableModel::data(const QModelIndex &index, int role) const
184 185 {
185 186 if (!index.isValid()) {
186 187 return QVariant{};
187 188 }
188 189
189 190 if (index.row() < 0 || index.row() >= rowCount()) {
190 191 return QVariant{};
191 192 }
192 193
193 194 if (role == Qt::DisplayRole) {
194 195 if (auto variable = impl->m_Variables.at(index.row()).get()) {
195 196 switch (index.column()) {
196 197 case NAME_COLUMN:
197 198 return variable->name();
198 199 case TSTART_COLUMN: {
199 200 auto range = variable->realRange();
200 201 return range != INVALID_RANGE
201 202 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
202 203 : QVariant{};
203 204 }
204 205 case TEND_COLUMN: {
205 206 auto range = variable->realRange();
206 207 return range != INVALID_RANGE
207 208 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
208 209 : QVariant{};
209 210 }
210 211 case NBPOINTS_COLUMN:
211 212 return variable->nbPoints();
212 213 case UNIT_COLUMN:
213 214 return variable->metadata().value(QStringLiteral("units"));
214 215 case MISSION_COLUMN:
215 216 return variable->metadata().value(QStringLiteral("mission"));
216 217 case PLUGIN_COLUMN:
217 218 return variable->metadata().value(QStringLiteral("plugin"));
218 219 default:
219 220 // No action
220 221 break;
221 222 }
222 223
223 224 qWarning(LOG_VariableModel())
224 225 << tr("Can't get data (unknown column %1)").arg(index.column());
225 226 }
226 227 else {
227 228 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
228 229 }
229 230 }
230 231 else if (role == VariableRoles::ProgressRole) {
231 232 if (auto variable = impl->m_Variables.at(index.row())) {
232 233
233 234 auto it = impl->m_VariableToProgress.find(variable);
234 235 if (it != impl->m_VariableToProgress.cend()) {
235 236 return it->second;
236 237 }
237 238 }
238 239 }
239 240
240 241 return QVariant{};
241 242 }
242 243
243 244 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
244 245 {
245 246 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
246 247 return QVariant{};
247 248 }
248 249
249 250 if (orientation == Qt::Horizontal) {
250 251 auto propertiesIt = COLUMN_PROPERTIES.find(section);
251 252 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
252 253 // Role is either DisplayRole or SizeHintRole
253 254 return (role == Qt::DisplayRole)
254 255 ? QVariant{propertiesIt->m_Name}
255 256 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
256 257 }
257 258 else {
258 259 qWarning(LOG_VariableModel())
259 260 << tr("Can't get header data (unknown column %1)").arg(section);
260 261 }
261 262 }
262 263
263 264 return QVariant{};
264 265 }
265 266
266 267 Qt::ItemFlags VariableModel::flags(const QModelIndex &index) const
267 268 {
268 269 return QAbstractTableModel::flags(index) | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled;
269 270 }
270 271
271 272 Qt::DropActions VariableModel::supportedDropActions() const
272 273 {
273 274 return Qt::CopyAction | Qt::MoveAction;
274 275 }
275 276
276 277 Qt::DropActions VariableModel::supportedDragActions() const
277 278 {
278 279 return Qt::CopyAction | Qt::MoveAction;
279 280 }
280 281
281 282 QStringList VariableModel::mimeTypes() const
282 283 {
283 284 return {MIME_TYPE_VARIABLE_LIST, MIME_TYPE_TIME_RANGE};
284 285 }
285 286
286 287 QMimeData *VariableModel::mimeData(const QModelIndexList &indexes) const
287 288 {
288 289 auto mimeData = new QMimeData;
289 290
290 291 QList<std::shared_ptr<Variable> > variableList;
291 292
292 293
293 294 SqpRange firstTimeRange;
294 295 for (const auto &index : indexes) {
295 296 if (index.column() == 0) { // only the first column
296 297 auto variable = impl->m_Variables.at(index.row());
297 298 if (variable.get() && index.isValid()) {
298 299
299 300 if (variableList.isEmpty()) {
300 301 // Gets the range of the first variable
301 302 firstTimeRange = std::move(variable->range());
302 303 }
303 304
304 305 variableList << variable;
305 306 }
306 307 }
307 308 }
308 309
309 310 auto variablesEncodedData = impl->m_VariableController->mimeDataForVariables(variableList);
310 311 mimeData->setData(MIME_TYPE_VARIABLE_LIST, variablesEncodedData);
311 312
312 313 if (variableList.count() == 1) {
313 314 // No time range MIME data if multiple variables are dragged
314 315 auto timeEncodedData = TimeController::mimeDataForTimeRange(firstTimeRange);
315 316 mimeData->setData(MIME_TYPE_TIME_RANGE, timeEncodedData);
316 317 }
317 318
318 319 return mimeData;
319 320 }
320 321
321 322 bool VariableModel::canDropMimeData(const QMimeData *data, Qt::DropAction action, int row,
322 323 int column, const QModelIndex &parent) const
323 324 {
324 325 // drop of a product
325 return data->hasFormat(MIME_TYPE_PRODUCT_LIST);
326 return data->hasFormat(MIME_TYPE_PRODUCT_LIST)
327 || (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()
328 && !data->hasFormat(MIME_TYPE_VARIABLE_LIST));
326 329 }
327 330
328 331 bool VariableModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column,
329 332 const QModelIndex &parent)
330 333 {
331 334 auto dropDone = false;
332 335
333 336 if (data->hasFormat(MIME_TYPE_PRODUCT_LIST)) {
334 337
335 338 auto productList
336 339 = DataSourceController::productsDataForMimeData(data->data(MIME_TYPE_PRODUCT_LIST));
337 340
338 341 for (auto metaData : productList) {
339 342 emit requestVariable(metaData.toHash());
340 343 }
341 344
342 345 dropDone = true;
343 346 }
347 else if (data->hasFormat(MIME_TYPE_TIME_RANGE) && parent.isValid()) {
348 auto variable = this->variable(parent.row());
349 auto range = TimeController::timeRangeForMimeData(data->data(MIME_TYPE_TIME_RANGE));
350
351 emit requestVariableRangeUpdate(variable, range);
352
353 dropDone = true;
354 }
344 355
345 356 return dropDone;
346 357 }
347 358
348 359 void VariableModel::abortProgress(const QModelIndex &index)
349 360 {
350 361 if (auto variable = impl->m_Variables.at(index.row())) {
351 362 emit abortProgessRequested(variable);
352 363 }
353 364 }
354 365
355 366 void VariableModel::onVariableUpdated() noexcept
356 367 {
357 368 // Finds variable that has been updated in the model
358 369 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
359 370 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
360 371
361 372 if (updatedVariableIndex > -1) {
362 373 emit dataChanged(createIndex(updatedVariableIndex, 0),
363 374 createIndex(updatedVariableIndex, columnCount() - 1));
364 375 }
365 376 }
366 377 }
367 378
368 379 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
369 380 {
370 381 auto begin = std::cbegin(m_Variables);
371 382 auto end = std::cend(m_Variables);
372 383 auto it
373 384 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
374 385
375 386 if (it != end) {
376 387 // Gets the index of the variable in the model: we assume here that views have the same
377 388 // order as the model
378 389 return std::distance(begin, it);
379 390 }
380 391 else {
381 392 return -1;
382 393 }
383 394 }
General Comments 0
You need to be logged in to leave comments. Login now