@@ -1,386 +1,368 | |||
|
1 | 1 | #include "Variable/VariableAcquisitionWorker.h" |
|
2 | 2 | |
|
3 | 3 | #include "Variable/Variable.h" |
|
4 | 4 | |
|
5 | 5 | #include <Data/AcquisitionRequest.h> |
|
6 | 6 | #include <Data/SqpRange.h> |
|
7 | 7 | |
|
8 | 8 | #include <unordered_map> |
|
9 | 9 | #include <utility> |
|
10 | 10 | |
|
11 | 11 | #include <QMutex> |
|
12 | 12 | #include <QReadWriteLock> |
|
13 | 13 | #include <QThread> |
|
14 | 14 | |
|
15 | 15 | #include <cmath> |
|
16 | 16 | |
|
17 | 17 | Q_LOGGING_CATEGORY(LOG_VariableAcquisitionWorker, "VariableAcquisitionWorker") |
|
18 | 18 | |
|
19 | 19 | struct VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate { |
|
20 | 20 | |
|
21 | 21 | explicit VariableAcquisitionWorkerPrivate(VariableAcquisitionWorker *parent) |
|
22 | 22 | : m_Lock{QReadWriteLock::Recursive}, q{parent} |
|
23 | 23 | { |
|
24 | 24 | } |
|
25 | 25 | |
|
26 | 26 | void lockRead() { m_Lock.lockForRead(); } |
|
27 | 27 | void lockWrite() { m_Lock.lockForWrite(); } |
|
28 | 28 | void unlock() { m_Lock.unlock(); } |
|
29 | 29 | |
|
30 | 30 | void removeVariableRequest(QUuid vIdentifier); |
|
31 | 31 | |
|
32 | 32 | /// Remove and/or abort all AcqRequest in link with varRequestId |
|
33 | 33 | void cancelVarRequest(QUuid varRequestId); |
|
34 | 34 | void removeAcqRequest(QUuid acqRequestId); |
|
35 | 35 | |
|
36 | 36 | QMutex m_WorkingMutex; |
|
37 | 37 | QReadWriteLock m_Lock; |
|
38 | 38 | |
|
39 | 39 | std::map<QUuid, QVector<AcquisitionDataPacket> > m_AcqIdentifierToAcqDataPacketVectorMap; |
|
40 | 40 | std::map<QUuid, AcquisitionRequest> m_AcqIdentifierToAcqRequestMap; |
|
41 | 41 | std::map<QUuid, QUuid> m_VIdentifierToCurrrentAcqIdMap; |
|
42 | 42 | VariableAcquisitionWorker *q; |
|
43 | 43 | }; |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | VariableAcquisitionWorker::VariableAcquisitionWorker(QObject *parent) |
|
47 | 47 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableAcquisitionWorkerPrivate>(this)} |
|
48 | 48 | { |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | VariableAcquisitionWorker::~VariableAcquisitionWorker() |
|
52 | 52 | { |
|
53 | 53 | qCInfo(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker destruction") |
|
54 | 54 | << QThread::currentThread(); |
|
55 | 55 | this->waitForFinish(); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | |
|
59 | 59 | QUuid VariableAcquisitionWorker::pushVariableRequest(QUuid varRequestId, QUuid vIdentifier, |
|
60 | 60 | DataProviderParameters parameters, |
|
61 | 61 | std::shared_ptr<IDataProvider> provider) |
|
62 | 62 | { |
|
63 | 63 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
64 | 64 | << tr("TORM VariableAcquisitionWorker::pushVariableRequest varRequestId: ") << varRequestId |
|
65 | 65 | << "vId: " << vIdentifier; |
|
66 | 66 | auto varRequestIdCanceled = QUuid(); |
|
67 | 67 | |
|
68 | 68 | // Request creation |
|
69 | 69 | auto acqRequest = AcquisitionRequest{}; |
|
70 | 70 | acqRequest.m_VarRequestId = varRequestId; |
|
71 | 71 | acqRequest.m_vIdentifier = vIdentifier; |
|
72 | 72 | acqRequest.m_DataProviderParameters = parameters; |
|
73 | 73 | acqRequest.m_Size = parameters.m_Times.size(); |
|
74 | 74 | acqRequest.m_Provider = provider; |
|
75 | 75 | qCInfo(LOG_VariableAcquisitionWorker()) << tr("Add acqRequest ") << acqRequest.m_AcqIdentifier |
|
76 | 76 | << acqRequest.m_Size; |
|
77 | 77 | |
|
78 | 78 | |
|
79 | 79 | // Register request |
|
80 | 80 | impl->lockWrite(); |
|
81 | 81 | impl->m_AcqIdentifierToAcqRequestMap.insert( |
|
82 | 82 | std::make_pair(acqRequest.m_AcqIdentifier, acqRequest)); |
|
83 | 83 | |
|
84 | 84 | auto it = impl->m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); |
|
85 | 85 | if (it != impl->m_VIdentifierToCurrrentAcqIdMap.cend()) { |
|
86 | 86 | // A current request already exists, we can cancel it |
|
87 | 87 | // remove old acqIdentifier from the worker |
|
88 | 88 | auto oldAcqId = it->second; |
|
89 | 89 | auto acqIdentifierToAcqRequestMapIt = impl->m_AcqIdentifierToAcqRequestMap.find(oldAcqId); |
|
90 | 90 | if (acqIdentifierToAcqRequestMapIt != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
91 | 91 | auto oldAcqRequest = acqIdentifierToAcqRequestMapIt->second; |
|
92 | 92 | varRequestIdCanceled = oldAcqRequest.m_VarRequestId; |
|
93 | 93 | } |
|
94 | 94 | impl->unlock(); |
|
95 | 95 | impl->cancelVarRequest(varRequestIdCanceled); |
|
96 | 96 | } |
|
97 | 97 | else { |
|
98 | 98 | impl->unlock(); |
|
99 | 99 | } |
|
100 | 100 | |
|
101 | 101 | // Request for the variable, it must be stored and executed |
|
102 | 102 | impl->lockWrite(); |
|
103 | 103 | impl->m_VIdentifierToCurrrentAcqIdMap.insert( |
|
104 | 104 | std::make_pair(vIdentifier, acqRequest.m_AcqIdentifier)); |
|
105 | 105 | impl->unlock(); |
|
106 | 106 | |
|
107 | 107 | QMetaObject::invokeMethod(this, "onExecuteRequest", Qt::QueuedConnection, |
|
108 | 108 | Q_ARG(QUuid, acqRequest.m_AcqIdentifier)); |
|
109 | 109 | |
|
110 | 110 | return varRequestIdCanceled; |
|
111 | 111 | } |
|
112 | 112 | |
|
113 | 113 | void VariableAcquisitionWorker::abortProgressRequested(QUuid vIdentifier) |
|
114 | 114 | { |
|
115 | 115 | impl->lockRead(); |
|
116 | 116 | |
|
117 | 117 | auto it = impl->m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); |
|
118 | 118 | if (it != impl->m_VIdentifierToCurrrentAcqIdMap.cend()) { |
|
119 | 119 | auto currentAcqId = it->second; |
|
120 | 120 | |
|
121 | 121 | auto it = impl->m_AcqIdentifierToAcqRequestMap.find(currentAcqId); |
|
122 | 122 | if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
123 | 123 | auto request = it->second; |
|
124 | 124 | impl->unlock(); |
|
125 | 125 | |
|
126 | 126 | // notify the request aborting to the provider |
|
127 | 127 | request.m_Provider->requestDataAborting(currentAcqId); |
|
128 | 128 | } |
|
129 | 129 | else { |
|
130 | 130 | impl->unlock(); |
|
131 | 131 | qCWarning(LOG_VariableAcquisitionWorker()) |
|
132 | 132 | << tr("Impossible to abort an unknown acquisition request") << currentAcqId; |
|
133 | 133 | } |
|
134 | 134 | } |
|
135 | 135 | else { |
|
136 | 136 | impl->unlock(); |
|
137 | 137 | } |
|
138 | 138 | } |
|
139 | 139 | |
|
140 | 140 | void VariableAcquisitionWorker::onVariableRetrieveDataInProgress(QUuid acqIdentifier, |
|
141 | 141 | double progress) |
|
142 | 142 | { |
|
143 |
qC |
|
|
144 | << QThread::currentThread()->objectName() | |
|
145 | << acqIdentifier << progress; | |
|
143 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableRetrieveDataInProgress ") | |
|
144 | << QThread::currentThread()->objectName() | |
|
145 | << acqIdentifier << progress; | |
|
146 | 146 | impl->lockRead(); |
|
147 | 147 | auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); |
|
148 | 148 | if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
149 | 149 | auto progressPartSize |
|
150 | 150 | = (aIdToARit->second.m_Size != 0) ? 100 / aIdToARit->second.m_Size : 0; |
|
151 | 151 | |
|
152 | 152 | auto currentPartProgress |
|
153 | 153 | = std::isnan(progress) ? 0.0 : (progress * progressPartSize) / 100.0; |
|
154 | 154 | |
|
155 | 155 | // We can only give an approximation of the currentProgression since its upgrade is async. |
|
156 | qCInfo(LOG_VariableAcquisitionWorker()) | |
|
157 | << tr("Progression: ") << aIdToARit->second.m_Progression << aIdToARit->second.m_Size; | |
|
158 | 156 | auto currentProgression = aIdToARit->second.m_Progression; |
|
159 | 157 | if (currentProgression == aIdToARit->second.m_Size) { |
|
160 | 158 | currentProgression = aIdToARit->second.m_Size - 1; |
|
161 | 159 | } |
|
162 | 160 | |
|
163 | 161 | auto currentAlreadyProgress = progressPartSize * currentProgression; |
|
164 | 162 | |
|
165 | 163 | |
|
166 | 164 | auto finalProgression = currentAlreadyProgress + currentPartProgress; |
|
167 | 165 | emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, finalProgression); |
|
168 | qCInfo(LOG_VariableAcquisitionWorker()) | |
|
169 | << tr("TORM: onVariableRetrieveDataInProgress ") | |
|
170 | << QThread::currentThread()->objectName() << aIdToARit->second.m_vIdentifier | |
|
171 | << progressPartSize << currentAlreadyProgress << currentPartProgress | |
|
172 | << finalProgression; | |
|
173 | 166 | |
|
174 | 167 | if (finalProgression == 100.0) { |
|
175 | qCInfo(LOG_VariableAcquisitionWorker()) | |
|
176 | << tr("TORM: onVariableRetrieveDataInProgress : finished : 0.0 "); | |
|
177 | 168 | emit variableRequestInProgress(aIdToARit->second.m_vIdentifier, 0.0); |
|
178 | 169 | } |
|
179 | 170 | } |
|
180 | 171 | impl->unlock(); |
|
181 | 172 | } |
|
182 | 173 | |
|
183 | 174 | void VariableAcquisitionWorker::onVariableAcquisitionFailed(QUuid acqIdentifier) |
|
184 | 175 | { |
|
185 | 176 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed") |
|
186 | 177 | << QThread::currentThread(); |
|
187 | 178 | impl->lockRead(); |
|
188 | 179 | auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); |
|
189 | 180 | if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
190 | 181 | auto request = it->second; |
|
191 | 182 | impl->unlock(); |
|
192 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("onVariableAcquisitionFailed") | |
|
193 | << acqIdentifier << request.m_vIdentifier | |
|
194 | << QThread::currentThread(); | |
|
195 | 183 | emit variableCanceledRequested(request.m_vIdentifier); |
|
196 | 184 | } |
|
197 | 185 | else { |
|
198 | 186 | impl->unlock(); |
|
199 | // TODO log no acqIdentifier recognized | |
|
200 | 187 | } |
|
201 | 188 | } |
|
202 | 189 | |
|
203 | 190 | void VariableAcquisitionWorker::onVariableDataAcquired(QUuid acqIdentifier, |
|
204 | 191 | std::shared_ptr<IDataSeries> dataSeries, |
|
205 | 192 | SqpRange dataRangeAcquired) |
|
206 | 193 | { |
|
207 | 194 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM: onVariableDataAcquired on range ") |
|
208 | 195 | << acqIdentifier << dataRangeAcquired; |
|
209 | 196 | impl->lockWrite(); |
|
210 | 197 | auto aIdToARit = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); |
|
211 | 198 | if (aIdToARit != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
212 | 199 | // Store the result |
|
213 | 200 | auto dataPacket = AcquisitionDataPacket{}; |
|
214 | 201 | dataPacket.m_Range = dataRangeAcquired; |
|
215 | 202 | dataPacket.m_DateSeries = dataSeries; |
|
216 | 203 | |
|
217 | 204 | auto aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier); |
|
218 | 205 | if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) { |
|
219 | 206 | // A current request result already exists, we can update it |
|
220 | 207 | aIdToADPVit->second.push_back(dataPacket); |
|
221 | 208 | } |
|
222 | 209 | else { |
|
223 | 210 | // First request result for the variable, it must be stored |
|
224 | 211 | impl->m_AcqIdentifierToAcqDataPacketVectorMap.insert( |
|
225 | 212 | std::make_pair(acqIdentifier, QVector<AcquisitionDataPacket>() << dataPacket)); |
|
226 | 213 | } |
|
227 | 214 | |
|
228 | 215 | |
|
229 | 216 | // Decrement the counter of the request |
|
230 | 217 | auto &acqRequest = aIdToARit->second; |
|
231 | qCInfo(LOG_VariableAcquisitionWorker()) << tr("TORM: +1 update progresson ") | |
|
232 | << acqIdentifier; | |
|
233 | 218 | acqRequest.m_Progression = acqRequest.m_Progression + 1; |
|
234 | 219 | |
|
235 | 220 | // if the counter is 0, we can return data then run the next request if it exists and |
|
236 | 221 | // removed the finished request |
|
237 | 222 | if (acqRequest.m_Size == acqRequest.m_Progression) { |
|
238 | 223 | auto varId = acqRequest.m_vIdentifier; |
|
239 | 224 | // Return the data |
|
240 | 225 | aIdToADPVit = impl->m_AcqIdentifierToAcqDataPacketVectorMap.find(acqIdentifier); |
|
241 | 226 | if (aIdToADPVit != impl->m_AcqIdentifierToAcqDataPacketVectorMap.cend()) { |
|
242 | 227 | emit dataProvided(varId, aIdToADPVit->second); |
|
243 | 228 | } |
|
244 | 229 | impl->unlock(); |
|
245 | 230 | } |
|
246 | 231 | else { |
|
247 | 232 | impl->unlock(); |
|
248 | 233 | } |
|
249 | 234 | } |
|
250 | 235 | else { |
|
251 | 236 | impl->unlock(); |
|
252 | 237 | qCWarning(LOG_VariableAcquisitionWorker()) |
|
253 | 238 | << tr("Impossible to retrieve AcquisitionRequest for the incoming data."); |
|
254 | 239 | } |
|
255 | 240 | } |
|
256 | 241 | |
|
257 | 242 | void VariableAcquisitionWorker::onExecuteRequest(QUuid acqIdentifier) |
|
258 | 243 | { |
|
259 | 244 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("onExecuteRequest") << QThread::currentThread(); |
|
260 | 245 | impl->lockRead(); |
|
261 | 246 | auto it = impl->m_AcqIdentifierToAcqRequestMap.find(acqIdentifier); |
|
262 | 247 | if (it != impl->m_AcqIdentifierToAcqRequestMap.cend()) { |
|
263 | 248 | auto request = it->second; |
|
264 | 249 | impl->unlock(); |
|
265 | 250 | emit variableRequestInProgress(request.m_vIdentifier, 0.1); |
|
266 | 251 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("Start request 10%") << acqIdentifier |
|
267 | 252 | << QThread::currentThread(); |
|
268 | 253 | request.m_Provider->requestDataLoading(acqIdentifier, request.m_DataProviderParameters); |
|
269 | 254 | } |
|
270 | 255 | else { |
|
271 | 256 | impl->unlock(); |
|
272 | 257 | // TODO log no acqIdentifier recognized |
|
273 | 258 | } |
|
274 | 259 | } |
|
275 | 260 | |
|
276 | 261 | void VariableAcquisitionWorker::initialize() |
|
277 | 262 | { |
|
278 | 263 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init") |
|
279 | 264 | << QThread::currentThread(); |
|
280 | 265 | impl->m_WorkingMutex.lock(); |
|
281 | 266 | qCDebug(LOG_VariableAcquisitionWorker()) << tr("VariableAcquisitionWorker init END"); |
|
282 | 267 | } |
|
283 | 268 | |
|
284 | 269 | void VariableAcquisitionWorker::finalize() |
|
285 | 270 | { |
|
286 | 271 | impl->m_WorkingMutex.unlock(); |
|
287 | 272 | } |
|
288 | 273 | |
|
289 | 274 | void VariableAcquisitionWorker::waitForFinish() |
|
290 | 275 | { |
|
291 | 276 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
292 | 277 | } |
|
293 | 278 | |
|
294 | 279 | void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeVariableRequest( |
|
295 | 280 | QUuid vIdentifier) |
|
296 | 281 | { |
|
297 | 282 | lockWrite(); |
|
298 | 283 | auto it = m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); |
|
299 | 284 | |
|
300 | 285 | if (it != m_VIdentifierToCurrrentAcqIdMap.cend()) { |
|
301 | 286 | // A current request already exists, we can replace the next one |
|
302 | 287 | |
|
303 | 288 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
304 | 289 | << "VariableAcquisitionWorkerPrivate::removeVariableRequest " |
|
305 | 290 | << QThread::currentThread()->objectName() << it->second; |
|
306 | 291 | m_AcqIdentifierToAcqRequestMap.erase(it->second); |
|
307 | 292 | m_AcqIdentifierToAcqDataPacketVectorMap.erase(it->second); |
|
308 | 293 | } |
|
309 | 294 | |
|
310 | 295 | // stop any progression |
|
311 | 296 | emit q->variableRequestInProgress(vIdentifier, 0.0); |
|
312 | 297 | |
|
313 | 298 | m_VIdentifierToCurrrentAcqIdMap.erase(vIdentifier); |
|
314 | 299 | unlock(); |
|
315 | 300 | } |
|
316 | 301 | |
|
317 | 302 | void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::cancelVarRequest( |
|
318 | 303 | QUuid varRequestId) |
|
319 | 304 | { |
|
320 | 305 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
321 |
<< "VariableAcquisitionWorkerPrivate::cancelVarRequest |
|
|
306 | << "VariableAcquisitionWorkerPrivate::cancelVarRequest start"; | |
|
322 | 307 | lockRead(); |
|
323 | 308 | // get all AcqIdentifier in link with varRequestId |
|
324 | 309 | QVector<QUuid> acqIdsToRm; |
|
325 | 310 | auto cend = m_AcqIdentifierToAcqRequestMap.cend(); |
|
326 | 311 | for (auto it = m_AcqIdentifierToAcqRequestMap.cbegin(); it != cend; ++it) { |
|
327 | 312 | if (it->second.m_VarRequestId == varRequestId) { |
|
328 | 313 | acqIdsToRm << it->first; |
|
329 | 314 | } |
|
330 | 315 | } |
|
331 | 316 | unlock(); |
|
332 | 317 | // run aborting or removing of acqIdsToRm |
|
333 | 318 | |
|
334 | 319 | for (auto acqId : acqIdsToRm) { |
|
335 | 320 | removeAcqRequest(acqId); |
|
336 | 321 | } |
|
337 | 322 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
338 | 323 | << "VariableAcquisitionWorkerPrivate::cancelVarRequest end"; |
|
339 | 324 | } |
|
340 | 325 | |
|
341 | 326 | void VariableAcquisitionWorker::VariableAcquisitionWorkerPrivate::removeAcqRequest( |
|
342 | 327 | QUuid acqRequestId) |
|
343 | 328 | { |
|
344 | 329 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
345 | 330 | << "VariableAcquisitionWorkerPrivate::removeAcqRequest"; |
|
346 | 331 | QUuid vIdentifier; |
|
347 | 332 | std::shared_ptr<IDataProvider> provider; |
|
348 | 333 | lockRead(); |
|
349 | 334 | auto acqIt = m_AcqIdentifierToAcqRequestMap.find(acqRequestId); |
|
350 | 335 | if (acqIt != m_AcqIdentifierToAcqRequestMap.cend()) { |
|
351 | 336 | vIdentifier = acqIt->second.m_vIdentifier; |
|
352 | 337 | provider = acqIt->second.m_Provider; |
|
353 | 338 | |
|
354 | 339 | auto it = m_VIdentifierToCurrrentAcqIdMap.find(vIdentifier); |
|
355 | 340 | if (it != m_VIdentifierToCurrrentAcqIdMap.cend()) { |
|
356 | 341 | if (it->second == acqRequestId) { |
|
357 | 342 | // acqRequest is currently running -> let's aborting it |
|
358 | 343 | unlock(); |
|
359 | 344 | |
|
360 | 345 | // notify the request aborting to the provider |
|
361 | 346 | provider->requestDataAborting(acqRequestId); |
|
362 | 347 | } |
|
363 | 348 | else { |
|
364 | 349 | unlock(); |
|
365 | 350 | } |
|
366 | 351 | } |
|
367 | 352 | else { |
|
368 | 353 | unlock(); |
|
369 | 354 | } |
|
370 | 355 | } |
|
371 | 356 | else { |
|
372 | 357 | unlock(); |
|
373 | 358 | } |
|
374 | 359 | |
|
375 | 360 | lockWrite(); |
|
376 | 361 | |
|
377 | qCDebug(LOG_VariableAcquisitionWorker()) | |
|
378 | << "VariableAcquisitionWorkerPrivate::updateToNextRequest removeAcqRequest: " | |
|
379 | << acqRequestId; | |
|
380 | 362 | m_AcqIdentifierToAcqDataPacketVectorMap.erase(acqRequestId); |
|
381 | 363 | m_AcqIdentifierToAcqRequestMap.erase(acqRequestId); |
|
382 | 364 | |
|
383 | 365 | unlock(); |
|
384 | 366 | qCDebug(LOG_VariableAcquisitionWorker()) |
|
385 | 367 | << "VariableAcquisitionWorkerPrivate::removeAcqRequest END"; |
|
386 | 368 | } |
@@ -1,1085 +1,1092 | |||
|
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 | template <typename VariableIterator> |
|
140 | 140 | void desynchronize(VariableIterator variableIt, const QUuid &syncGroupId); |
|
141 | 141 | |
|
142 | 142 | QMutex m_WorkingMutex; |
|
143 | 143 | /// Variable model. The VariableController has the ownership |
|
144 | 144 | VariableModel *m_VariableModel; |
|
145 | 145 | QItemSelectionModel *m_VariableSelectionModel; |
|
146 | 146 | |
|
147 | 147 | |
|
148 | 148 | TimeController *m_TimeController{nullptr}; |
|
149 | 149 | std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy; |
|
150 | 150 | std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker; |
|
151 | 151 | QThread m_VariableAcquisitionWorkerThread; |
|
152 | 152 | |
|
153 | 153 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
154 | 154 | m_VariableToProviderMap; |
|
155 | 155 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; |
|
156 | 156 | std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> > |
|
157 | 157 | m_GroupIdToVariableSynchronizationGroupMap; |
|
158 | 158 | std::map<QUuid, QUuid> m_VariableIdGroupIdMap; |
|
159 | 159 | std::set<std::shared_ptr<IDataProvider> > m_ProviderSet; |
|
160 | 160 | |
|
161 | 161 | std::map<QUuid, std::list<QUuid> > m_VarGroupIdToVarIds; |
|
162 | 162 | std::map<QUuid, std::unique_ptr<VariableRequestHandler> > m_VarIdToVarRequestHandler; |
|
163 | 163 | |
|
164 | 164 | VariableController *q; |
|
165 | 165 | }; |
|
166 | 166 | |
|
167 | 167 | |
|
168 | 168 | VariableController::VariableController(QObject *parent) |
|
169 | 169 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
170 | 170 | { |
|
171 | 171 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
172 | 172 | << QThread::currentThread(); |
|
173 | 173 | |
|
174 | 174 | connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, |
|
175 | 175 | &VariableController::onAbortProgressRequested); |
|
176 | 176 | |
|
177 | 177 | connect(impl->m_VariableAcquisitionWorker.get(), |
|
178 | 178 | &VariableAcquisitionWorker::variableCanceledRequested, this, |
|
179 | 179 | &VariableController::onAbortAcquisitionRequested); |
|
180 | 180 | |
|
181 | 181 | connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this, |
|
182 | 182 | &VariableController::onDataProvided); |
|
183 | 183 | connect(impl->m_VariableAcquisitionWorker.get(), |
|
184 | 184 | &VariableAcquisitionWorker::variableRequestInProgress, this, |
|
185 | 185 | &VariableController::onVariableRetrieveDataInProgress); |
|
186 | 186 | |
|
187 | 187 | |
|
188 | 188 | connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started, |
|
189 | 189 | impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize); |
|
190 | 190 | connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished, |
|
191 | 191 | impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize); |
|
192 | 192 | |
|
193 | 193 | connect(impl->m_VariableModel, &VariableModel::requestVariableRangeUpdate, this, |
|
194 | 194 | &VariableController::onUpdateDateTime); |
|
195 | 195 | |
|
196 | 196 | impl->m_VariableAcquisitionWorkerThread.start(); |
|
197 | 197 | } |
|
198 | 198 | |
|
199 | 199 | VariableController::~VariableController() |
|
200 | 200 | { |
|
201 | 201 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
202 | 202 | << QThread::currentThread(); |
|
203 | 203 | this->waitForFinish(); |
|
204 | 204 | } |
|
205 | 205 | |
|
206 | 206 | VariableModel *VariableController::variableModel() noexcept |
|
207 | 207 | { |
|
208 | 208 | return impl->m_VariableModel; |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept |
|
212 | 212 | { |
|
213 | 213 | return impl->m_VariableSelectionModel; |
|
214 | 214 | } |
|
215 | 215 | |
|
216 | 216 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
217 | 217 | { |
|
218 | 218 | impl->m_TimeController = timeController; |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | 221 | std::shared_ptr<Variable> |
|
222 | 222 | VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept |
|
223 | 223 | { |
|
224 | 224 | if (impl->m_VariableModel->containsVariable(variable)) { |
|
225 | 225 | // Clones variable |
|
226 | 226 | auto duplicate = variable->clone(); |
|
227 | 227 | |
|
228 | 228 | // Adds clone to model |
|
229 | 229 | impl->m_VariableModel->addVariable(duplicate); |
|
230 | 230 | |
|
231 | 231 | // Generates clone identifier |
|
232 | 232 | impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid(); |
|
233 | 233 | |
|
234 | 234 | // Registers provider |
|
235 | 235 | auto variableProvider = impl->m_VariableToProviderMap.at(variable); |
|
236 | 236 | auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr; |
|
237 | 237 | |
|
238 | 238 | impl->m_VariableToProviderMap[duplicate] = duplicateProvider; |
|
239 | 239 | if (duplicateProvider) { |
|
240 | 240 | impl->registerProvider(duplicateProvider); |
|
241 | 241 | } |
|
242 | 242 | |
|
243 | 243 | return duplicate; |
|
244 | 244 | } |
|
245 | 245 | else { |
|
246 | 246 | qCCritical(LOG_VariableController()) |
|
247 | 247 | << tr("Can't create duplicate of variable %1: variable not registered in the model") |
|
248 | 248 | .arg(variable->name()); |
|
249 | 249 | return nullptr; |
|
250 | 250 | } |
|
251 | 251 | } |
|
252 | 252 | |
|
253 | 253 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
254 | 254 | { |
|
255 | 255 | if (!variable) { |
|
256 | 256 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; |
|
257 | 257 | return; |
|
258 | 258 | } |
|
259 | 259 | |
|
260 | 260 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can |
|
261 | 261 | // make some treatments before the deletion |
|
262 | 262 | emit variableAboutToBeDeleted(variable); |
|
263 | 263 | |
|
264 | 264 | auto variableIt = impl->m_VariableToIdentifierMap.find(variable); |
|
265 | 265 | Q_ASSERT(variableIt != impl->m_VariableToIdentifierMap.cend()); |
|
266 | 266 | |
|
267 | 267 | auto variableId = variableIt->second; |
|
268 | 268 | |
|
269 | 269 | // Removes variable's handler |
|
270 | 270 | impl->m_VarIdToVarRequestHandler.erase(variableId); |
|
271 | 271 | |
|
272 | 272 | // Desynchronizes variable (if the variable is in a sync group) |
|
273 | 273 | auto syncGroupIt = impl->m_VariableIdGroupIdMap.find(variableId); |
|
274 | 274 | if (syncGroupIt != impl->m_VariableIdGroupIdMap.cend()) { |
|
275 | 275 | impl->desynchronize(variableIt, syncGroupIt->second); |
|
276 | 276 | } |
|
277 | 277 | |
|
278 | 278 | // Deletes identifier |
|
279 | 279 | impl->m_VariableToIdentifierMap.erase(variableIt); |
|
280 | 280 | |
|
281 | 281 | // Deletes provider |
|
282 | 282 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); |
|
283 | 283 | qCDebug(LOG_VariableController()) |
|
284 | 284 | << tr("Number of providers deleted for variable %1: %2") |
|
285 | 285 | .arg(variable->name(), QString::number(nbProvidersDeleted)); |
|
286 | 286 | |
|
287 | 287 | |
|
288 | 288 | // Deletes from model |
|
289 | 289 | impl->m_VariableModel->deleteVariable(variable); |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | void VariableController::deleteVariables( |
|
293 | 293 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
294 | 294 | { |
|
295 | 295 | for (auto variable : qAsConst(variables)) { |
|
296 | 296 | deleteVariable(variable); |
|
297 | 297 | } |
|
298 | 298 | } |
|
299 | 299 | |
|
300 | 300 | QByteArray |
|
301 | 301 | VariableController::mimeDataForVariables(const QList<std::shared_ptr<Variable> > &variables) const |
|
302 | 302 | { |
|
303 | 303 | auto encodedData = QByteArray{}; |
|
304 | 304 | |
|
305 | 305 | QVariantList ids; |
|
306 | 306 | for (auto &var : variables) { |
|
307 | 307 | auto itVar = impl->m_VariableToIdentifierMap.find(var); |
|
308 | 308 | if (itVar == impl->m_VariableToIdentifierMap.cend()) { |
|
309 | 309 | qCCritical(LOG_VariableController()) |
|
310 | 310 | << tr("Impossible to find the data for an unknown variable."); |
|
311 | 311 | } |
|
312 | 312 | |
|
313 | 313 | ids << itVar->second.toByteArray(); |
|
314 | 314 | } |
|
315 | 315 | |
|
316 | 316 | QDataStream stream{&encodedData, QIODevice::WriteOnly}; |
|
317 | 317 | stream << ids; |
|
318 | 318 | |
|
319 | 319 | return encodedData; |
|
320 | 320 | } |
|
321 | 321 | |
|
322 | 322 | QList<std::shared_ptr<Variable> > |
|
323 | 323 | VariableController::variablesForMimeData(const QByteArray &mimeData) const |
|
324 | 324 | { |
|
325 | 325 | auto variables = QList<std::shared_ptr<Variable> >{}; |
|
326 | 326 | QDataStream stream{mimeData}; |
|
327 | 327 | |
|
328 | 328 | QVariantList ids; |
|
329 | 329 | stream >> ids; |
|
330 | 330 | |
|
331 | 331 | for (auto id : ids) { |
|
332 | 332 | auto uuid = QUuid{id.toByteArray()}; |
|
333 | 333 | auto var = impl->findVariable(uuid); |
|
334 | 334 | variables << var; |
|
335 | 335 | } |
|
336 | 336 | |
|
337 | 337 | return variables; |
|
338 | 338 | } |
|
339 | 339 | |
|
340 | 340 | std::shared_ptr<Variable> |
|
341 | 341 | VariableController::createVariable(const QString &name, const QVariantHash &metadata, |
|
342 | 342 | std::shared_ptr<IDataProvider> provider) noexcept |
|
343 | 343 | { |
|
344 | 344 | if (!impl->m_TimeController) { |
|
345 | 345 | qCCritical(LOG_VariableController()) |
|
346 | 346 | << tr("Impossible to create variable: The time controller is null"); |
|
347 | 347 | return nullptr; |
|
348 | 348 | } |
|
349 | 349 | |
|
350 | 350 | auto range = impl->m_TimeController->dateTime(); |
|
351 | 351 | |
|
352 | 352 | if (auto newVariable = impl->m_VariableModel->createVariable(name, metadata)) { |
|
353 | 353 | auto varId = QUuid::createUuid(); |
|
354 | 354 | |
|
355 | 355 | // Create the handler |
|
356 | 356 | auto varRequestHandler = std::make_unique<VariableRequestHandler>(); |
|
357 | 357 | varRequestHandler->m_VarId = varId; |
|
358 | 358 | |
|
359 | 359 | impl->m_VarIdToVarRequestHandler.insert( |
|
360 | 360 | std::make_pair(varId, std::move(varRequestHandler))); |
|
361 | 361 | |
|
362 | 362 | // store the provider |
|
363 | 363 | impl->registerProvider(provider); |
|
364 | 364 | |
|
365 | 365 | // Associate the provider |
|
366 | 366 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
367 | 367 | impl->m_VariableToIdentifierMap[newVariable] = varId; |
|
368 | 368 | |
|
369 | 369 | this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{newVariable}, range, false); |
|
370 | 370 | |
|
371 | 371 | emit variableAdded(newVariable); |
|
372 | 372 | |
|
373 | 373 | return newVariable; |
|
374 | 374 | } |
|
375 | 375 | |
|
376 | 376 | qCCritical(LOG_VariableController()) << tr("Impossible to create variable"); |
|
377 | 377 | return nullptr; |
|
378 | 378 | } |
|
379 | 379 | |
|
380 | 380 | void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) |
|
381 | 381 | { |
|
382 | 382 | // NOTE: Even if acquisition request is aborting, the graphe range will be changed |
|
383 | 383 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" |
|
384 | 384 | << QThread::currentThread()->objectName(); |
|
385 | 385 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
386 | 386 | |
|
387 | 387 | // NOTE we only permit the time modification for one variable |
|
388 | 388 | if (selectedRows.size() == 1) { |
|
389 | 389 | |
|
390 | 390 | if (auto selectedVariable |
|
391 | 391 | = impl->m_VariableModel->variable(qAsConst(selectedRows).first().row())) { |
|
392 | 392 | |
|
393 | 393 | onUpdateDateTime(selectedVariable, dateTime); |
|
394 | 394 | } |
|
395 | 395 | } |
|
396 | 396 | else if (selectedRows.size() > 1) { |
|
397 | 397 | qCCritical(LOG_VariableController()) |
|
398 | 398 | << tr("Impossible to set time for more than 1 variable in the same time"); |
|
399 | 399 | } |
|
400 | 400 | else { |
|
401 | 401 | qCWarning(LOG_VariableController()) |
|
402 | 402 | << tr("There is no variable selected to set the time one"); |
|
403 | 403 | } |
|
404 | 404 | } |
|
405 | 405 | |
|
406 | 406 | void VariableController::onUpdateDateTime(std::shared_ptr<Variable> variable, |
|
407 | 407 | const SqpRange &dateTime) |
|
408 | 408 | { |
|
409 | 409 | auto itVar = impl->m_VariableToIdentifierMap.find(variable); |
|
410 | 410 | if (itVar == impl->m_VariableToIdentifierMap.cend()) { |
|
411 | 411 | qCCritical(LOG_VariableController()) |
|
412 | 412 | << tr("Impossible to onDateTimeOnSelection request for unknown variable"); |
|
413 | 413 | return; |
|
414 | 414 | } |
|
415 | 415 | |
|
416 | 416 | // notify that rescale operation has to be done |
|
417 | 417 | emit rangeChanged(variable, dateTime); |
|
418 | 418 | |
|
419 | 419 | auto synchro |
|
420 | 420 | = impl->m_VariableIdGroupIdMap.find(itVar->second) != impl->m_VariableIdGroupIdMap.cend(); |
|
421 | 421 | |
|
422 | 422 | this->onRequestDataLoading(QVector<std::shared_ptr<Variable> >{variable}, dateTime, synchro); |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | void VariableController::onDataProvided(QUuid vIdentifier, |
|
426 | 426 | QVector<AcquisitionDataPacket> dataAcquired) |
|
427 | 427 | { |
|
428 | 428 | qCDebug(LOG_VariableController()) << tr("onDataProvided") << QThread::currentThread(); |
|
429 | 429 | auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired); |
|
430 | 430 | auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries); |
|
431 | 431 | if (!varRequestId.isNull()) { |
|
432 | 432 | impl->updateVariables(varRequestId); |
|
433 | 433 | } |
|
434 | 434 | } |
|
435 | 435 | |
|
436 | 436 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) |
|
437 | 437 | { |
|
438 | 438 | qCDebug(LOG_VariableController()) |
|
439 | 439 | << "TORM: variableController::onVariableRetrieveDataInProgress" |
|
440 | 440 | << QThread::currentThread()->objectName() << progress; |
|
441 | 441 | if (auto var = impl->findVariable(identifier)) { |
|
442 | 442 | qCDebug(LOG_VariableController()) |
|
443 | 443 | << "TORM: variableController::onVariableRetrieveDataInProgress FOUND"; |
|
444 | 444 | impl->m_VariableModel->setDataProgress(var, progress); |
|
445 | 445 | } |
|
446 | 446 | else { |
|
447 | 447 | qCCritical(LOG_VariableController()) |
|
448 | 448 | << tr("Impossible to notify progression of a null variable"); |
|
449 | 449 | } |
|
450 | 450 | } |
|
451 | 451 | |
|
452 | 452 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) |
|
453 | 453 | { |
|
454 | 454 | qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortProgressRequested" |
|
455 | 455 | << QThread::currentThread()->objectName() << variable->name(); |
|
456 | 456 | |
|
457 | 457 | auto itVar = impl->m_VariableToIdentifierMap.find(variable); |
|
458 | 458 | if (itVar == impl->m_VariableToIdentifierMap.cend()) { |
|
459 | 459 | qCCritical(LOG_VariableController()) |
|
460 | 460 | << tr("Impossible to onAbortProgressRequested request for unknown variable"); |
|
461 | 461 | return; |
|
462 | 462 | } |
|
463 | 463 | |
|
464 | 464 | auto varId = itVar->second; |
|
465 | 465 | |
|
466 | 466 | auto itVarHandler = impl->m_VarIdToVarRequestHandler.find(varId); |
|
467 | 467 | if (itVarHandler == impl->m_VarIdToVarRequestHandler.cend()) { |
|
468 | 468 | qCCritical(LOG_VariableController()) |
|
469 | 469 | << tr("Impossible to onAbortProgressRequested for variable with unknown handler"); |
|
470 | 470 | return; |
|
471 | 471 | } |
|
472 | 472 | |
|
473 | 473 | auto varHandler = itVarHandler->second.get(); |
|
474 | 474 | |
|
475 | 475 | // case where a variable has a running request |
|
476 | 476 | if (varHandler->m_State != VariableRequestHandlerState::OFF) { |
|
477 | 477 | impl->cancelVariableRequest(varHandler->m_RunningVarRequest.m_VariableGroupId); |
|
478 | 478 | } |
|
479 | 479 | } |
|
480 | 480 | |
|
481 | 481 | void VariableController::onAbortAcquisitionRequested(QUuid vIdentifier) |
|
482 | 482 | { |
|
483 | 483 | qCDebug(LOG_VariableController()) << "TORM: variableController::onAbortAcquisitionRequested" |
|
484 | 484 | << QThread::currentThread()->objectName() << vIdentifier; |
|
485 | 485 | |
|
486 | 486 | if (auto var = impl->findVariable(vIdentifier)) { |
|
487 | 487 | this->onAbortProgressRequested(var); |
|
488 | 488 | } |
|
489 | 489 | else { |
|
490 | 490 | qCCritical(LOG_VariableController()) |
|
491 | 491 | << tr("Impossible to abort Acquisition Requestof a null variable"); |
|
492 | 492 | } |
|
493 | 493 | } |
|
494 | 494 | |
|
495 | 495 | void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId) |
|
496 | 496 | { |
|
497 | 497 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId" |
|
498 | 498 | << QThread::currentThread()->objectName() |
|
499 | 499 | << synchronizationGroupId; |
|
500 | 500 | auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>(); |
|
501 | 501 | impl->m_GroupIdToVariableSynchronizationGroupMap.insert( |
|
502 | 502 | std::make_pair(synchronizationGroupId, vSynchroGroup)); |
|
503 | 503 | } |
|
504 | 504 | |
|
505 | 505 | void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId) |
|
506 | 506 | { |
|
507 | 507 | impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId); |
|
508 | 508 | } |
|
509 | 509 | |
|
510 | 510 | void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable, |
|
511 | 511 | QUuid synchronizationGroupId) |
|
512 | 512 | |
|
513 | 513 | { |
|
514 | 514 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized" |
|
515 | 515 | << synchronizationGroupId; |
|
516 | 516 | auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable); |
|
517 | 517 | if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) { |
|
518 | 518 | auto groupIdToVSGIt |
|
519 | 519 | = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId); |
|
520 | 520 | if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) { |
|
521 | 521 | impl->m_VariableIdGroupIdMap.insert( |
|
522 | 522 | std::make_pair(varToVarIdIt->second, synchronizationGroupId)); |
|
523 | 523 | groupIdToVSGIt->second->addVariableId(varToVarIdIt->second); |
|
524 | 524 | } |
|
525 | 525 | else { |
|
526 | 526 | qCCritical(LOG_VariableController()) |
|
527 | 527 | << tr("Impossible to synchronize a variable with an unknown sycnhronization group") |
|
528 | 528 | << variable->name(); |
|
529 | 529 | } |
|
530 | 530 | } |
|
531 | 531 | else { |
|
532 | 532 | qCCritical(LOG_VariableController()) |
|
533 | 533 | << tr("Impossible to synchronize a variable with no identifier") << variable->name(); |
|
534 | 534 | } |
|
535 | 535 | } |
|
536 | 536 | |
|
537 | 537 | void VariableController::desynchronize(std::shared_ptr<Variable> variable, |
|
538 | 538 | QUuid synchronizationGroupId) |
|
539 | 539 | { |
|
540 | 540 | // Gets variable id |
|
541 | 541 | auto variableIt = impl->m_VariableToIdentifierMap.find(variable); |
|
542 | 542 | if (variableIt == impl->m_VariableToIdentifierMap.cend()) { |
|
543 | 543 | qCCritical(LOG_VariableController()) |
|
544 | 544 | << tr("Can't desynchronize variable %1: variable identifier not found") |
|
545 | 545 | .arg(variable->name()); |
|
546 | 546 | return; |
|
547 | 547 | } |
|
548 | 548 | |
|
549 | 549 | impl->desynchronize(variableIt, synchronizationGroupId); |
|
550 | 550 | } |
|
551 | 551 | |
|
552 | 552 | void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, |
|
553 | 553 | const SqpRange &range, bool synchronise) |
|
554 | 554 | { |
|
555 | 555 | // variables is assumed synchronized |
|
556 | 556 | // TODO: Asser variables synchronization |
|
557 | 557 | // we want to load data of the variable for the dateTime. |
|
558 | 558 | if (variables.isEmpty()) { |
|
559 | 559 | return; |
|
560 | 560 | } |
|
561 | 561 | |
|
562 | 562 | auto varRequestId = QUuid::createUuid(); |
|
563 | 563 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" |
|
564 | 564 | << QThread::currentThread()->objectName() << varRequestId |
|
565 | 565 | << range << synchronise; |
|
566 | 566 | |
|
567 | 567 | if (!synchronise) { |
|
568 | 568 | auto varIds = std::list<QUuid>{}; |
|
569 | 569 | for (const auto &var : variables) { |
|
570 | 570 | auto vId = impl->m_VariableToIdentifierMap.at(var); |
|
571 | 571 | varIds.push_back(vId); |
|
572 | 572 | } |
|
573 | 573 | impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds)); |
|
574 | 574 | for (const auto &var : variables) { |
|
575 |
qCDebug(LOG_VariableController()) << " |
|
|
575 | qCDebug(LOG_VariableController()) << "onRequestDataLoading: for" << varRequestId | |
|
576 | 576 | << varIds.size(); |
|
577 | 577 | impl->processRequest(var, range, varRequestId); |
|
578 | 578 | } |
|
579 | 579 | } |
|
580 | 580 | else { |
|
581 | 581 | auto vId = impl->m_VariableToIdentifierMap.at(variables.first()); |
|
582 | 582 | auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId); |
|
583 | 583 | if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) { |
|
584 | 584 | auto groupId = varIdToGroupIdIt->second; |
|
585 | 585 | |
|
586 | 586 | auto vSynchronizationGroup |
|
587 | 587 | = impl->m_GroupIdToVariableSynchronizationGroupMap.at(groupId); |
|
588 | 588 | auto vSyncIds = vSynchronizationGroup->getIds(); |
|
589 | 589 | |
|
590 | 590 | auto varIds = std::list<QUuid>{}; |
|
591 | 591 | for (auto vId : vSyncIds) { |
|
592 | 592 | varIds.push_back(vId); |
|
593 | 593 | } |
|
594 | qCDebug(LOG_VariableController()) << "onRequestDataLoading sync: for" << varRequestId | |
|
595 | << varIds.size(); | |
|
594 | 596 | impl->m_VarGroupIdToVarIds.insert(std::make_pair(varRequestId, varIds)); |
|
595 | 597 | |
|
596 | 598 | for (auto vId : vSyncIds) { |
|
597 | 599 | auto var = impl->findVariable(vId); |
|
598 | 600 | |
|
599 | 601 | // Don't process already processed var |
|
600 | 602 | if (var != nullptr) { |
|
601 | 603 | qCDebug(LOG_VariableController()) << "processRequest synchro for" << var->name() |
|
602 | 604 | << varRequestId; |
|
603 | 605 | auto vSyncRangeRequested |
|
604 | 606 | = variables.contains(var) |
|
605 | 607 | ? range |
|
606 | 608 | : computeSynchroRangeRequested(var->range(), range, |
|
607 | 609 | variables.first()->range()); |
|
608 | 610 | qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested; |
|
609 | 611 | impl->processRequest(var, vSyncRangeRequested, varRequestId); |
|
610 | 612 | } |
|
611 | 613 | else { |
|
612 | 614 | qCCritical(LOG_VariableController()) |
|
613 | 615 | |
|
614 | 616 | << tr("Impossible to synchronize a null variable"); |
|
615 | 617 | } |
|
616 | 618 | } |
|
617 | 619 | } |
|
618 | 620 | } |
|
619 | 621 | |
|
620 | 622 | impl->updateVariables(varRequestId); |
|
621 | 623 | } |
|
622 | 624 | |
|
623 | 625 | |
|
624 | 626 | void VariableController::initialize() |
|
625 | 627 | { |
|
626 | 628 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
627 | 629 | impl->m_WorkingMutex.lock(); |
|
628 | 630 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
629 | 631 | } |
|
630 | 632 | |
|
631 | 633 | void VariableController::finalize() |
|
632 | 634 | { |
|
633 | 635 | impl->m_WorkingMutex.unlock(); |
|
634 | 636 | } |
|
635 | 637 | |
|
636 | 638 | void VariableController::waitForFinish() |
|
637 | 639 | { |
|
638 | 640 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
639 | 641 | } |
|
640 | 642 | |
|
641 | 643 | AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange) |
|
642 | 644 | { |
|
643 | 645 | // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd |
|
644 | 646 | auto zoomType = AcquisitionZoomType::Unknown; |
|
645 | 647 | if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) { |
|
646 | 648 | qCDebug(LOG_VariableController()) << "zoomtype: ZoomOut"; |
|
647 | 649 | zoomType = AcquisitionZoomType::ZoomOut; |
|
648 | 650 | } |
|
649 | 651 | else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) { |
|
650 | 652 | qCDebug(LOG_VariableController()) << "zoomtype: PanRight"; |
|
651 | 653 | zoomType = AcquisitionZoomType::PanRight; |
|
652 | 654 | } |
|
653 | 655 | else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) { |
|
654 | 656 | qCDebug(LOG_VariableController()) << "zoomtype: PanLeft"; |
|
655 | 657 | zoomType = AcquisitionZoomType::PanLeft; |
|
656 | 658 | } |
|
657 | 659 | else if (range.m_TStart >= oldRange.m_TStart && oldRange.m_TEnd >= range.m_TEnd) { |
|
658 | 660 | qCDebug(LOG_VariableController()) << "zoomtype: ZoomIn"; |
|
659 | 661 | zoomType = AcquisitionZoomType::ZoomIn; |
|
660 | 662 | } |
|
661 | 663 | else { |
|
662 | 664 | qCDebug(LOG_VariableController()) << "getZoomType: Unknown type detected"; |
|
663 | 665 | } |
|
664 | 666 | return zoomType; |
|
665 | 667 | } |
|
666 | 668 | |
|
667 | 669 | void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var, |
|
668 | 670 | const SqpRange &rangeRequested, |
|
669 | 671 | QUuid varRequestId) |
|
670 | 672 | { |
|
671 | 673 | auto itVar = m_VariableToIdentifierMap.find(var); |
|
672 | 674 | if (itVar == m_VariableToIdentifierMap.cend()) { |
|
673 | 675 | qCCritical(LOG_VariableController()) |
|
674 | 676 | << tr("Impossible to process request for unknown variable"); |
|
675 | 677 | return; |
|
676 | 678 | } |
|
677 | 679 | |
|
678 | 680 | auto varId = itVar->second; |
|
679 | 681 | |
|
680 | 682 | auto itVarHandler = m_VarIdToVarRequestHandler.find(varId); |
|
681 | 683 | if (itVarHandler == m_VarIdToVarRequestHandler.cend()) { |
|
682 | 684 | qCCritical(LOG_VariableController()) |
|
683 | 685 | << tr("Impossible to process request for variable with unknown handler"); |
|
684 | 686 | return; |
|
685 | 687 | } |
|
686 | 688 | |
|
687 | 689 | auto oldRange = var->range(); |
|
688 | 690 | |
|
689 | 691 | auto varHandler = itVarHandler->second.get(); |
|
690 | 692 | |
|
691 | 693 | if (varHandler->m_State != VariableRequestHandlerState::OFF) { |
|
692 | 694 | oldRange = varHandler->m_RunningVarRequest.m_RangeRequested; |
|
693 | 695 | } |
|
694 | 696 | |
|
695 | 697 | auto varRequest = VariableRequest{}; |
|
696 | 698 | varRequest.m_VariableGroupId = varRequestId; |
|
697 | 699 | auto varStrategyRangesRequested |
|
698 | 700 | = m_VariableCacheStrategy->computeRange(oldRange, rangeRequested); |
|
699 | 701 | varRequest.m_RangeRequested = varStrategyRangesRequested.first; |
|
700 | 702 | varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second; |
|
701 | 703 | |
|
702 | 704 | switch (varHandler->m_State) { |
|
703 | 705 | case VariableRequestHandlerState::OFF: { |
|
704 | 706 | qCDebug(LOG_VariableController()) << tr("Process Request OFF") |
|
705 | 707 | << varRequest.m_RangeRequested |
|
706 | 708 | << varRequest.m_CacheRangeRequested; |
|
707 | 709 | varHandler->m_RunningVarRequest = varRequest; |
|
708 | 710 | varHandler->m_State = VariableRequestHandlerState::RUNNING; |
|
709 | 711 | executeVarRequest(var, varRequest); |
|
710 | 712 | break; |
|
711 | 713 | } |
|
712 | 714 | case VariableRequestHandlerState::RUNNING: { |
|
713 | 715 | qCDebug(LOG_VariableController()) << tr("Process Request RUNNING") |
|
714 | 716 | << varRequest.m_RangeRequested |
|
715 | 717 | << varRequest.m_CacheRangeRequested; |
|
716 | 718 | varHandler->m_State = VariableRequestHandlerState::PENDING; |
|
717 | 719 | varHandler->m_PendingVarRequest = varRequest; |
|
718 | 720 | break; |
|
719 | 721 | } |
|
720 | 722 | case VariableRequestHandlerState::PENDING: { |
|
721 | 723 | qCDebug(LOG_VariableController()) << tr("Process Request PENDING") |
|
722 | 724 | << varRequest.m_RangeRequested |
|
723 | 725 | << varRequest.m_CacheRangeRequested; |
|
724 | 726 | auto variableGroupIdToCancel = varHandler->m_PendingVarRequest.m_VariableGroupId; |
|
725 | 727 | cancelVariableRequest(variableGroupIdToCancel); |
|
726 | 728 | // Cancel variable can make state downgrade |
|
727 | 729 | varHandler->m_State = VariableRequestHandlerState::PENDING; |
|
728 | 730 | varHandler->m_PendingVarRequest = varRequest; |
|
729 | 731 | |
|
730 | 732 | break; |
|
731 | 733 | } |
|
732 | 734 | default: |
|
733 | 735 | qCCritical(LOG_VariableController()) |
|
734 | 736 | << QObject::tr("Unknown VariableRequestHandlerState"); |
|
735 | 737 | } |
|
736 | 738 | } |
|
737 | 739 | |
|
738 | 740 | std::shared_ptr<Variable> |
|
739 | 741 | VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier) |
|
740 | 742 | { |
|
741 | 743 | std::shared_ptr<Variable> var; |
|
742 | 744 | auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; }; |
|
743 | 745 | |
|
744 | 746 | auto end = m_VariableToIdentifierMap.cend(); |
|
745 | 747 | auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply); |
|
746 | 748 | if (it != end) { |
|
747 | 749 | var = it->first; |
|
748 | 750 | } |
|
749 | 751 | else { |
|
750 | 752 | qCCritical(LOG_VariableController()) |
|
751 | 753 | << tr("Impossible to find the variable with the identifier: ") << vIdentifier; |
|
752 | 754 | } |
|
753 | 755 | |
|
754 | 756 | return var; |
|
755 | 757 | } |
|
756 | 758 | |
|
757 | 759 | std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries( |
|
758 | 760 | const QVector<AcquisitionDataPacket> acqDataPacketVector) |
|
759 | 761 | { |
|
760 | 762 | qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size") |
|
761 | 763 | << acqDataPacketVector.size(); |
|
762 | 764 | std::shared_ptr<IDataSeries> dataSeries; |
|
763 | 765 | if (!acqDataPacketVector.isEmpty()) { |
|
764 | 766 | dataSeries = acqDataPacketVector[0].m_DateSeries; |
|
765 | 767 | for (int i = 1; i < acqDataPacketVector.size(); ++i) { |
|
766 | 768 | dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get()); |
|
767 | 769 | } |
|
768 | 770 | } |
|
769 | 771 | qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END") |
|
770 | 772 | << acqDataPacketVector.size(); |
|
771 | 773 | return dataSeries; |
|
772 | 774 | } |
|
773 | 775 | |
|
774 | 776 | void VariableController::VariableControllerPrivate::registerProvider( |
|
775 | 777 | std::shared_ptr<IDataProvider> provider) |
|
776 | 778 | { |
|
777 | 779 | if (m_ProviderSet.find(provider) == m_ProviderSet.end()) { |
|
778 | 780 | qCDebug(LOG_VariableController()) << tr("Registering of a new provider") |
|
779 | 781 | << provider->objectName(); |
|
780 | 782 | m_ProviderSet.insert(provider); |
|
781 | 783 | connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(), |
|
782 | 784 | &VariableAcquisitionWorker::onVariableDataAcquired); |
|
783 | 785 | connect(provider.get(), &IDataProvider::dataProvidedProgress, |
|
784 | 786 | m_VariableAcquisitionWorker.get(), |
|
785 | 787 | &VariableAcquisitionWorker::onVariableRetrieveDataInProgress); |
|
786 | 788 | connect(provider.get(), &IDataProvider::dataProvidedFailed, |
|
787 | 789 | m_VariableAcquisitionWorker.get(), |
|
788 | 790 | &VariableAcquisitionWorker::onVariableAcquisitionFailed); |
|
789 | 791 | } |
|
790 | 792 | else { |
|
791 | 793 | qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists "); |
|
792 | 794 | } |
|
793 | 795 | } |
|
794 | 796 | |
|
795 | 797 | QUuid VariableController::VariableControllerPrivate::acceptVariableRequest( |
|
796 | 798 | QUuid varId, std::shared_ptr<IDataSeries> dataSeries) |
|
797 | 799 | { |
|
798 | 800 | auto itVarHandler = m_VarIdToVarRequestHandler.find(varId); |
|
799 | 801 | if (itVarHandler == m_VarIdToVarRequestHandler.cend()) { |
|
800 | 802 | return QUuid(); |
|
801 | 803 | } |
|
802 | 804 | |
|
803 | 805 | auto varHandler = itVarHandler->second.get(); |
|
804 | 806 | if (varHandler->m_State == VariableRequestHandlerState::OFF) { |
|
805 | 807 | qCCritical(LOG_VariableController()) |
|
806 | 808 | << tr("acceptVariableRequest impossible on a variable with OFF state"); |
|
807 | 809 | } |
|
808 | 810 | |
|
809 | 811 | varHandler->m_RunningVarRequest.m_DataSeries = dataSeries; |
|
810 | 812 | varHandler->m_CanUpdate = true; |
|
811 | 813 | |
|
812 | 814 | // Element traitΓ©, on a dΓ©jΓ toutes les donnΓ©es necessaires |
|
813 | 815 | auto varGroupId = varHandler->m_RunningVarRequest.m_VariableGroupId; |
|
814 | 816 | qCDebug(LOG_VariableController()) << "Variable::acceptVariableRequest" << varGroupId |
|
815 | 817 | << m_VarGroupIdToVarIds.size(); |
|
816 | 818 | |
|
817 | 819 | return varHandler->m_RunningVarRequest.m_VariableGroupId; |
|
818 | 820 | } |
|
819 | 821 | |
|
820 | 822 | void VariableController::VariableControllerPrivate::updateVariables(QUuid varRequestId) |
|
821 | 823 | { |
|
822 | 824 | qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables" |
|
823 | 825 | << QThread::currentThread()->objectName() << varRequestId; |
|
824 | 826 | |
|
825 | 827 | auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); |
|
826 | 828 | if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { |
|
827 | 829 | qCWarning(LOG_VariableController()) |
|
828 | 830 | << tr("Impossible to updateVariables of unknown variables") << varRequestId; |
|
829 | 831 | return; |
|
830 | 832 | } |
|
831 | 833 | |
|
832 | 834 | auto &varIds = varGroupIdToVarIdsIt->second; |
|
833 | 835 | auto varIdsEnd = varIds.end(); |
|
834 | 836 | bool processVariableUpdate = true; |
|
835 | qCDebug(LOG_VariableController()) << "VariableControllerPrivate::updateVariables" | |
|
836 | << varRequestId << varIds.size(); | |
|
837 | 837 | for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd) && processVariableUpdate; |
|
838 | 838 | ++varIdsIt) { |
|
839 | 839 | auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); |
|
840 | 840 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { |
|
841 | 841 | processVariableUpdate &= itVarHandler->second->m_CanUpdate; |
|
842 | 842 | } |
|
843 | 843 | } |
|
844 | 844 | |
|
845 | 845 | if (processVariableUpdate) { |
|
846 | qCDebug(LOG_VariableController()) << "Final update OK for the var request" << varIds.size(); | |
|
847 | 846 | for (auto varIdsIt = varIds.begin(); varIdsIt != varIdsEnd; ++varIdsIt) { |
|
848 | 847 | auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); |
|
849 | 848 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { |
|
850 | 849 | if (auto var = findVariable(*varIdsIt)) { |
|
851 | 850 | auto &varRequest = itVarHandler->second->m_RunningVarRequest; |
|
852 | 851 | var->setRange(varRequest.m_RangeRequested); |
|
853 | 852 | var->setCacheRange(varRequest.m_CacheRangeRequested); |
|
854 | 853 | qCDebug(LOG_VariableController()) << tr("1: onDataProvided") |
|
855 | 854 | << varRequest.m_RangeRequested |
|
856 | 855 | << varRequest.m_CacheRangeRequested; |
|
857 | 856 | qCDebug(LOG_VariableController()) << tr("2: onDataProvided var points before") |
|
858 | 857 | << var->nbPoints() |
|
859 | 858 | << varRequest.m_DataSeries->nbPoints(); |
|
860 | 859 | var->mergeDataSeries(varRequest.m_DataSeries); |
|
861 | 860 | qCDebug(LOG_VariableController()) << tr("3: onDataProvided var points after") |
|
862 | 861 | << var->nbPoints(); |
|
863 | 862 | |
|
864 | 863 | emit var->updated(); |
|
865 | qCDebug(LOG_VariableController()) << tr("Update OK"); | |
|
866 | 864 | } |
|
867 | 865 | else { |
|
868 | 866 | qCCritical(LOG_VariableController()) |
|
869 | 867 | << tr("Impossible to update data to a null variable"); |
|
870 | 868 | } |
|
871 | 869 | } |
|
872 | 870 | } |
|
873 | 871 | updateVariableRequest(varRequestId); |
|
874 | 872 | |
|
875 | 873 | // cleaning varRequestId |
|
876 | 874 | qCDebug(LOG_VariableController()) << tr("m_VarGroupIdToVarIds erase") << varRequestId; |
|
877 | 875 | m_VarGroupIdToVarIds.erase(varRequestId); |
|
878 | 876 | if (m_VarGroupIdToVarIds.empty()) { |
|
879 | 877 | emit q->acquisitionFinished(); |
|
880 | 878 | } |
|
881 | 879 | } |
|
882 | 880 | } |
|
883 | 881 | |
|
884 | 882 | |
|
885 | 883 | void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId) |
|
886 | 884 | { |
|
887 | 885 | auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); |
|
888 | 886 | if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { |
|
889 | 887 | qCCritical(LOG_VariableController()) << QObject::tr( |
|
890 | 888 | "Impossible to updateVariableRequest since varGroupdId isn't here anymore"); |
|
891 | 889 | |
|
892 | 890 | return; |
|
893 | 891 | } |
|
894 | 892 | |
|
895 | 893 | auto &varIds = varGroupIdToVarIdsIt->second; |
|
896 | 894 | auto varIdsEnd = varIds.end(); |
|
895 | ||
|
896 | // First pass all canUpdate of handler to false | |
|
897 | 897 | for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) { |
|
898 | 898 | auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); |
|
899 | 899 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { |
|
900 | 900 | |
|
901 | 901 | auto varHandler = itVarHandler->second.get(); |
|
902 | 902 | varHandler->m_CanUpdate = false; |
|
903 | } | |
|
904 | } | |
|
905 | // Second update requests of handler | |
|
906 | for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) { | |
|
907 | auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); | |
|
908 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { | |
|
903 | 909 | |
|
910 | auto varHandler = itVarHandler->second.get(); | |
|
904 | 911 | |
|
905 | 912 | switch (varHandler->m_State) { |
|
906 | 913 | case VariableRequestHandlerState::OFF: { |
|
907 | 914 | qCCritical(LOG_VariableController()) |
|
908 | 915 | << QObject::tr("Impossible to update a variable with handler in OFF state"); |
|
909 | 916 | } break; |
|
910 | 917 | case VariableRequestHandlerState::RUNNING: { |
|
911 | 918 | varHandler->m_State = VariableRequestHandlerState::OFF; |
|
912 | 919 | varHandler->m_RunningVarRequest = VariableRequest{}; |
|
913 | 920 | break; |
|
914 | 921 | } |
|
915 | 922 | case VariableRequestHandlerState::PENDING: { |
|
916 | 923 | varHandler->m_State = VariableRequestHandlerState::RUNNING; |
|
917 | 924 | varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest; |
|
918 | 925 | varHandler->m_PendingVarRequest = VariableRequest{}; |
|
919 | 926 | auto var = findVariable(itVarHandler->first); |
|
920 | 927 | executeVarRequest(var, varHandler->m_RunningVarRequest); |
|
921 | 928 | updateVariables(varHandler->m_RunningVarRequest.m_VariableGroupId); |
|
922 | 929 | break; |
|
923 | 930 | } |
|
924 | 931 | default: |
|
925 | 932 | qCCritical(LOG_VariableController()) |
|
926 | 933 | << QObject::tr("Unknown VariableRequestHandlerState"); |
|
927 | 934 | } |
|
928 | 935 | } |
|
929 | 936 | } |
|
930 | 937 | } |
|
931 | 938 | |
|
932 | 939 | |
|
933 | 940 | void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId) |
|
934 | 941 | { |
|
935 | 942 | qCDebug(LOG_VariableController()) << tr("cancelVariableRequest") << varRequestId; |
|
936 | 943 | |
|
937 | 944 | auto varGroupIdToVarIdsIt = m_VarGroupIdToVarIds.find(varRequestId); |
|
938 | 945 | if (varGroupIdToVarIdsIt == m_VarGroupIdToVarIds.end()) { |
|
939 | 946 | qCCritical(LOG_VariableController()) |
|
940 | 947 | << tr("Impossible to cancelVariableRequest for unknown varGroupdId") << varRequestId; |
|
941 | 948 | return; |
|
942 | 949 | } |
|
943 | 950 | |
|
944 | 951 | auto &varIds = varGroupIdToVarIdsIt->second; |
|
945 | 952 | auto varIdsEnd = varIds.end(); |
|
946 | 953 | for (auto varIdsIt = varIds.begin(); (varIdsIt != varIdsEnd); ++varIdsIt) { |
|
947 | 954 | auto itVarHandler = m_VarIdToVarRequestHandler.find(*varIdsIt); |
|
948 | 955 | if (itVarHandler != m_VarIdToVarRequestHandler.cend()) { |
|
949 | 956 | |
|
950 | 957 | auto varHandler = itVarHandler->second.get(); |
|
951 | 958 | varHandler->m_VarId = QUuid{}; |
|
952 | 959 | switch (varHandler->m_State) { |
|
953 | 960 | case VariableRequestHandlerState::OFF: { |
|
954 | 961 | qCWarning(LOG_VariableController()) |
|
955 | 962 | << QObject::tr("Impossible to cancel a variable with no running request"); |
|
956 | 963 | break; |
|
957 | 964 | } |
|
958 | 965 | case VariableRequestHandlerState::RUNNING: { |
|
959 | 966 | |
|
960 | 967 | if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) { |
|
961 | 968 | auto var = findVariable(itVarHandler->first); |
|
962 | 969 | auto varProvider = m_VariableToProviderMap.at(var); |
|
963 | 970 | if (varProvider != nullptr) { |
|
964 | 971 | m_VariableAcquisitionWorker->abortProgressRequested( |
|
965 | 972 | itVarHandler->first); |
|
966 | 973 | } |
|
967 | 974 | m_VariableModel->setDataProgress(var, 0.0); |
|
968 | 975 | varHandler->m_CanUpdate = false; |
|
969 | 976 | varHandler->m_State = VariableRequestHandlerState::OFF; |
|
970 | 977 | varHandler->m_RunningVarRequest = VariableRequest{}; |
|
971 | 978 | } |
|
972 | 979 | else { |
|
973 | 980 | // TODO: log Impossible to cancel the running variable request beacause its |
|
974 | 981 | // varRequestId isn't not the canceled one |
|
975 | 982 | } |
|
976 | 983 | break; |
|
977 | 984 | } |
|
978 | 985 | case VariableRequestHandlerState::PENDING: { |
|
979 | 986 | if (varHandler->m_RunningVarRequest.m_VariableGroupId == varRequestId) { |
|
980 | 987 | auto var = findVariable(itVarHandler->first); |
|
981 | 988 | auto varProvider = m_VariableToProviderMap.at(var); |
|
982 | 989 | if (varProvider != nullptr) { |
|
983 | 990 | m_VariableAcquisitionWorker->abortProgressRequested( |
|
984 | 991 | itVarHandler->first); |
|
985 | 992 | } |
|
986 | 993 | m_VariableModel->setDataProgress(var, 0.0); |
|
987 | 994 | varHandler->m_CanUpdate = false; |
|
988 | 995 | varHandler->m_State = VariableRequestHandlerState::RUNNING; |
|
989 | 996 | varHandler->m_RunningVarRequest = varHandler->m_PendingVarRequest; |
|
990 | 997 | varHandler->m_PendingVarRequest = VariableRequest{}; |
|
991 | 998 | executeVarRequest(var, varHandler->m_RunningVarRequest); |
|
992 | 999 | } |
|
993 | 1000 | else if (varHandler->m_PendingVarRequest.m_VariableGroupId == varRequestId) { |
|
994 | 1001 | varHandler->m_State = VariableRequestHandlerState::RUNNING; |
|
995 | 1002 | varHandler->m_PendingVarRequest = VariableRequest{}; |
|
996 | 1003 | } |
|
997 | 1004 | else { |
|
998 | 1005 | // TODO: log Impossible to cancel the variable request beacause its |
|
999 | 1006 | // varRequestId isn't not the canceled one |
|
1000 | 1007 | } |
|
1001 | 1008 | break; |
|
1002 | 1009 | } |
|
1003 | 1010 | default: |
|
1004 | 1011 | qCCritical(LOG_VariableController()) |
|
1005 | 1012 | << QObject::tr("Unknown VariableRequestHandlerState"); |
|
1006 | 1013 | } |
|
1007 | 1014 | } |
|
1008 | 1015 | } |
|
1009 | 1016 | qCDebug(LOG_VariableController()) << tr("cancelVariableRequest: erase") << varRequestId; |
|
1010 | 1017 | m_VarGroupIdToVarIds.erase(varRequestId); |
|
1011 | 1018 | if (m_VarGroupIdToVarIds.empty()) { |
|
1012 | 1019 | emit q->acquisitionFinished(); |
|
1013 | 1020 | } |
|
1014 | 1021 | } |
|
1015 | 1022 | |
|
1016 | 1023 | void VariableController::VariableControllerPrivate::executeVarRequest(std::shared_ptr<Variable> var, |
|
1017 | 1024 | VariableRequest &varRequest) |
|
1018 | 1025 | { |
|
1019 | qCDebug(LOG_VariableController()) << tr("TORM: executeVarRequest"); | |
|
1020 | ||
|
1021 | 1026 | auto varIdIt = m_VariableToIdentifierMap.find(var); |
|
1022 | 1027 | if (varIdIt == m_VariableToIdentifierMap.cend()) { |
|
1023 | 1028 | qCWarning(LOG_VariableController()) << tr( |
|
1024 | 1029 | "Can't execute request of a variable that is not registered (may has been deleted)"); |
|
1025 | 1030 | return; |
|
1026 | 1031 | } |
|
1027 | 1032 | |
|
1028 | 1033 | auto varId = varIdIt->second; |
|
1029 | 1034 | |
|
1030 | 1035 | auto varCacheRange = var->cacheRange(); |
|
1031 | 1036 | auto varCacheRangeRequested = varRequest.m_CacheRangeRequested; |
|
1032 | 1037 | auto notInCacheRangeList |
|
1033 | 1038 | = Variable::provideNotInCacheRangeList(varCacheRange, varCacheRangeRequested); |
|
1034 | 1039 | auto inCacheRangeList |
|
1035 | 1040 | = Variable::provideInCacheRangeList(varCacheRange, varCacheRangeRequested); |
|
1036 | 1041 | |
|
1037 | 1042 | if (!notInCacheRangeList.empty()) { |
|
1038 | 1043 | |
|
1039 | 1044 | auto varProvider = m_VariableToProviderMap.at(var); |
|
1040 | 1045 | if (varProvider != nullptr) { |
|
1041 | 1046 | qCDebug(LOG_VariableController()) << "executeVarRequest " << varRequest.m_RangeRequested |
|
1042 | 1047 | << varRequest.m_CacheRangeRequested; |
|
1043 | 1048 | m_VariableAcquisitionWorker->pushVariableRequest( |
|
1044 | 1049 | varRequest.m_VariableGroupId, varId, |
|
1045 | 1050 | DataProviderParameters{std::move(notInCacheRangeList), var->metadata()}, |
|
1046 | 1051 | varProvider); |
|
1047 | 1052 | } |
|
1048 | 1053 | else { |
|
1049 | 1054 | qCCritical(LOG_VariableController()) |
|
1050 | 1055 | << "Impossible to provide data with a null provider"; |
|
1051 | 1056 | } |
|
1052 | 1057 | |
|
1053 | 1058 | if (!inCacheRangeList.empty()) { |
|
1054 | 1059 | emit q->updateVarDisplaying(var, inCacheRangeList.first()); |
|
1055 | 1060 | } |
|
1056 | 1061 | } |
|
1057 | 1062 | else { |
|
1063 | qCDebug(LOG_VariableController()) << "All already in the cache " | |
|
1064 | << varRequest.m_RangeRequested; | |
|
1058 | 1065 | acceptVariableRequest(varId, |
|
1059 | 1066 | var->dataSeries()->subDataSeries(varRequest.m_CacheRangeRequested)); |
|
1060 | 1067 | } |
|
1061 | 1068 | } |
|
1062 | 1069 | |
|
1063 | 1070 | template <typename VariableIterator> |
|
1064 | 1071 | void VariableController::VariableControllerPrivate::desynchronize(VariableIterator variableIt, |
|
1065 | 1072 | const QUuid &syncGroupId) |
|
1066 | 1073 | { |
|
1067 | 1074 | const auto &variable = variableIt->first; |
|
1068 | 1075 | const auto &variableId = variableIt->second; |
|
1069 | 1076 | |
|
1070 | 1077 | // Gets synchronization group |
|
1071 | 1078 | auto groupIt = m_GroupIdToVariableSynchronizationGroupMap.find(syncGroupId); |
|
1072 | 1079 | if (groupIt == m_GroupIdToVariableSynchronizationGroupMap.cend()) { |
|
1073 | 1080 | qCCritical(LOG_VariableController()) |
|
1074 | 1081 | << tr("Can't desynchronize variable %1: unknown synchronization group") |
|
1075 | 1082 | .arg(variable->name()); |
|
1076 | 1083 | return; |
|
1077 | 1084 | } |
|
1078 | 1085 | |
|
1079 | 1086 | // Removes variable from synchronization group |
|
1080 | 1087 | auto synchronizationGroup = groupIt->second; |
|
1081 | 1088 | synchronizationGroup->removeVariableId(variableId); |
|
1082 | 1089 | |
|
1083 | 1090 | // Removes link between variable and synchronization group |
|
1084 | 1091 | m_VariableIdGroupIdMap.erase(variableId); |
|
1085 | 1092 | } |
@@ -1,1077 +1,1077 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphWidget.h" |
|
2 | 2 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
3 | 3 | #include "Visualization/VisualizationCursorItem.h" |
|
4 | 4 | #include "Visualization/VisualizationDefs.h" |
|
5 | 5 | #include "Visualization/VisualizationGraphHelper.h" |
|
6 | 6 | #include "Visualization/VisualizationGraphRenderingDelegate.h" |
|
7 | 7 | #include "Visualization/VisualizationMultiZoneSelectionDialog.h" |
|
8 | 8 | #include "Visualization/VisualizationSelectionZoneItem.h" |
|
9 | 9 | #include "Visualization/VisualizationSelectionZoneManager.h" |
|
10 | 10 | #include "Visualization/VisualizationWidget.h" |
|
11 | 11 | #include "Visualization/VisualizationZoneWidget.h" |
|
12 | 12 | #include "ui_VisualizationGraphWidget.h" |
|
13 | 13 | |
|
14 | 14 | #include <Actions/ActionsGuiController.h> |
|
15 | 15 | #include <Actions/FilteringAction.h> |
|
16 | 16 | #include <Common/MimeTypesDef.h> |
|
17 | 17 | #include <Data/ArrayData.h> |
|
18 | 18 | #include <Data/IDataSeries.h> |
|
19 | 19 | #include <Data/SpectrogramSeries.h> |
|
20 | 20 | #include <DragAndDrop/DragDropGuiController.h> |
|
21 | 21 | #include <Settings/SqpSettingsDefs.h> |
|
22 | 22 | #include <SqpApplication.h> |
|
23 | 23 | #include <Time/TimeController.h> |
|
24 | 24 | #include <Variable/Variable.h> |
|
25 | 25 | #include <Variable/VariableController.h> |
|
26 | 26 | |
|
27 | 27 | #include <unordered_map> |
|
28 | 28 | |
|
29 | 29 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") |
|
30 | 30 | |
|
31 | 31 | namespace { |
|
32 | 32 | |
|
33 | 33 | /// Key pressed to enable drag&drop in all modes |
|
34 | 34 | const auto DRAG_DROP_MODIFIER = Qt::AltModifier; |
|
35 | 35 | |
|
36 | 36 | /// Key pressed to enable zoom on horizontal axis |
|
37 | 37 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::ControlModifier; |
|
38 | 38 | |
|
39 | 39 | /// Key pressed to enable zoom on vertical axis |
|
40 | 40 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ShiftModifier; |
|
41 | 41 | |
|
42 | 42 | /// Speed of a step of a wheel event for a pan, in percentage of the axis range |
|
43 | 43 | const auto PAN_SPEED = 5; |
|
44 | 44 | |
|
45 | 45 | /// Key pressed to enable a calibration pan |
|
46 | 46 | const auto VERTICAL_PAN_MODIFIER = Qt::AltModifier; |
|
47 | 47 | |
|
48 | 48 | /// Key pressed to enable multi selection of selection zones |
|
49 | 49 | const auto MULTI_ZONE_SELECTION_MODIFIER = Qt::ControlModifier; |
|
50 | 50 | |
|
51 | 51 | /// Minimum size for the zoom box, in percentage of the axis range |
|
52 | 52 | const auto ZOOM_BOX_MIN_SIZE = 0.8; |
|
53 | 53 | |
|
54 | 54 | /// Format of the dates appearing in the label of a cursor |
|
55 | 55 | const auto CURSOR_LABELS_DATETIME_FORMAT = QStringLiteral("yyyy/MM/dd\nhh:mm:ss:zzz"); |
|
56 | 56 | |
|
57 | 57 | } // namespace |
|
58 | 58 | |
|
59 | 59 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { |
|
60 | 60 | |
|
61 | 61 | explicit VisualizationGraphWidgetPrivate(const QString &name) |
|
62 | 62 | : m_Name{name}, |
|
63 | 63 | m_Flags{GraphFlag::EnableAll}, |
|
64 | 64 | m_IsCalibration{false}, |
|
65 | 65 | m_RenderingDelegate{nullptr} |
|
66 | 66 | { |
|
67 | 67 | } |
|
68 | 68 | |
|
69 | 69 | void updateData(PlottablesMap &plottables, std::shared_ptr<Variable> variable, |
|
70 | 70 | const SqpRange &range) |
|
71 | 71 | { |
|
72 | 72 | VisualizationGraphHelper::updateData(plottables, variable, range); |
|
73 | 73 | |
|
74 | 74 | // Prevents that data has changed to update rendering |
|
75 | 75 | m_RenderingDelegate->onPlotUpdated(); |
|
76 | 76 | } |
|
77 | 77 | |
|
78 | 78 | QString m_Name; |
|
79 | 79 | // 1 variable -> n qcpplot |
|
80 | 80 | std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap; |
|
81 | 81 | GraphFlags m_Flags; |
|
82 | 82 | bool m_IsCalibration; |
|
83 | 83 | /// Delegate used to attach rendering features to the plot |
|
84 | 84 | std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; |
|
85 | 85 | |
|
86 | 86 | QCPItemRect *m_DrawingZoomRect = nullptr; |
|
87 | 87 | QStack<QPair<QCPRange, QCPRange> > m_ZoomStack; |
|
88 | 88 | |
|
89 | 89 | std::unique_ptr<VisualizationCursorItem> m_HorizontalCursor = nullptr; |
|
90 | 90 | std::unique_ptr<VisualizationCursorItem> m_VerticalCursor = nullptr; |
|
91 | 91 | |
|
92 | 92 | VisualizationSelectionZoneItem *m_DrawingZone = nullptr; |
|
93 | 93 | VisualizationSelectionZoneItem *m_HoveredZone = nullptr; |
|
94 | 94 | QVector<VisualizationSelectionZoneItem *> m_SelectionZones; |
|
95 | 95 | |
|
96 | 96 | bool m_HasMovedMouse = false; // Indicates if the mouse moved in a releaseMouse even |
|
97 | 97 | |
|
98 | 98 | bool m_VariableAutoRangeOnInit = true; |
|
99 | 99 | |
|
100 | 100 | void startDrawingRect(const QPoint &pos, QCustomPlot &plot) |
|
101 | 101 | { |
|
102 | 102 | removeDrawingRect(plot); |
|
103 | 103 | |
|
104 | 104 | auto axisPos = posToAxisPos(pos, plot); |
|
105 | 105 | |
|
106 | 106 | m_DrawingZoomRect = new QCPItemRect{&plot}; |
|
107 | 107 | QPen p; |
|
108 | 108 | p.setWidth(2); |
|
109 | 109 | m_DrawingZoomRect->setPen(p); |
|
110 | 110 | |
|
111 | 111 | m_DrawingZoomRect->topLeft->setCoords(axisPos); |
|
112 | 112 | m_DrawingZoomRect->bottomRight->setCoords(axisPos); |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | 115 | void removeDrawingRect(QCustomPlot &plot) |
|
116 | 116 | { |
|
117 | 117 | if (m_DrawingZoomRect) { |
|
118 | 118 | plot.removeItem(m_DrawingZoomRect); // the item is deleted by QCustomPlot |
|
119 | 119 | m_DrawingZoomRect = nullptr; |
|
120 | 120 | plot.replot(QCustomPlot::rpQueuedReplot); |
|
121 | 121 | } |
|
122 | 122 | } |
|
123 | 123 | |
|
124 | 124 | void startDrawingZone(const QPoint &pos, VisualizationGraphWidget *graph) |
|
125 | 125 | { |
|
126 | 126 | endDrawingZone(graph); |
|
127 | 127 | |
|
128 | 128 | auto axisPos = posToAxisPos(pos, graph->plot()); |
|
129 | 129 | |
|
130 | 130 | m_DrawingZone = new VisualizationSelectionZoneItem{&graph->plot()}; |
|
131 | 131 | m_DrawingZone->setRange(axisPos.x(), axisPos.x()); |
|
132 | 132 | m_DrawingZone->setEditionEnabled(false); |
|
133 | 133 | } |
|
134 | 134 | |
|
135 | 135 | void endDrawingZone(VisualizationGraphWidget *graph) |
|
136 | 136 | { |
|
137 | 137 | if (m_DrawingZone) { |
|
138 | 138 | auto drawingZoneRange = m_DrawingZone->range(); |
|
139 | 139 | if (qAbs(drawingZoneRange.m_TEnd - drawingZoneRange.m_TStart) > 0) { |
|
140 | 140 | m_DrawingZone->setEditionEnabled(true); |
|
141 | 141 | addSelectionZone(m_DrawingZone); |
|
142 | 142 | } |
|
143 | 143 | else { |
|
144 | 144 | graph->plot().removeItem(m_DrawingZone); // the item is deleted by QCustomPlot |
|
145 | 145 | } |
|
146 | 146 | |
|
147 | 147 | graph->plot().replot(QCustomPlot::rpQueuedReplot); |
|
148 | 148 | m_DrawingZone = nullptr; |
|
149 | 149 | } |
|
150 | 150 | } |
|
151 | 151 | |
|
152 | 152 | void setSelectionZonesEditionEnabled(bool value) |
|
153 | 153 | { |
|
154 | 154 | for (auto s : m_SelectionZones) { |
|
155 | 155 | s->setEditionEnabled(value); |
|
156 | 156 | } |
|
157 | 157 | } |
|
158 | 158 | |
|
159 | 159 | void addSelectionZone(VisualizationSelectionZoneItem *zone) { m_SelectionZones << zone; } |
|
160 | 160 | |
|
161 | 161 | VisualizationSelectionZoneItem *selectionZoneAt(const QPoint &pos, |
|
162 | 162 | const QCustomPlot &plot) const |
|
163 | 163 | { |
|
164 | 164 | VisualizationSelectionZoneItem *selectionZoneItemUnderCursor = nullptr; |
|
165 | 165 | auto minDistanceToZone = -1; |
|
166 | 166 | for (auto zone : m_SelectionZones) { |
|
167 | 167 | auto distanceToZone = zone->selectTest(pos, false); |
|
168 | 168 | if ((minDistanceToZone < 0 || distanceToZone <= minDistanceToZone) |
|
169 | 169 | && distanceToZone >= 0 && distanceToZone < plot.selectionTolerance()) { |
|
170 | 170 | selectionZoneItemUnderCursor = zone; |
|
171 | 171 | } |
|
172 | 172 | } |
|
173 | 173 | |
|
174 | 174 | return selectionZoneItemUnderCursor; |
|
175 | 175 | } |
|
176 | 176 | |
|
177 | 177 | QVector<VisualizationSelectionZoneItem *> selectionZonesAt(const QPoint &pos, |
|
178 | 178 | const QCustomPlot &plot) const |
|
179 | 179 | { |
|
180 | 180 | QVector<VisualizationSelectionZoneItem *> zones; |
|
181 | 181 | for (auto zone : m_SelectionZones) { |
|
182 | 182 | auto distanceToZone = zone->selectTest(pos, false); |
|
183 | 183 | if (distanceToZone >= 0 && distanceToZone < plot.selectionTolerance()) { |
|
184 | 184 | zones << zone; |
|
185 | 185 | } |
|
186 | 186 | } |
|
187 | 187 | |
|
188 | 188 | return zones; |
|
189 | 189 | } |
|
190 | 190 | |
|
191 | 191 | void moveSelectionZoneOnTop(VisualizationSelectionZoneItem *zone, QCustomPlot &plot) |
|
192 | 192 | { |
|
193 | 193 | if (!m_SelectionZones.isEmpty() && m_SelectionZones.last() != zone) { |
|
194 | 194 | zone->moveToTop(); |
|
195 | 195 | m_SelectionZones.removeAll(zone); |
|
196 | 196 | m_SelectionZones.append(zone); |
|
197 | 197 | } |
|
198 | 198 | } |
|
199 | 199 | |
|
200 | 200 | QPointF posToAxisPos(const QPoint &pos, QCustomPlot &plot) const |
|
201 | 201 | { |
|
202 | 202 | auto axisX = plot.axisRect()->axis(QCPAxis::atBottom); |
|
203 | 203 | auto axisY = plot.axisRect()->axis(QCPAxis::atLeft); |
|
204 | 204 | return QPointF{axisX->pixelToCoord(pos.x()), axisY->pixelToCoord(pos.y())}; |
|
205 | 205 | } |
|
206 | 206 | |
|
207 | 207 | bool pointIsInAxisRect(const QPointF &axisPoint, QCustomPlot &plot) const |
|
208 | 208 | { |
|
209 | 209 | auto axisX = plot.axisRect()->axis(QCPAxis::atBottom); |
|
210 | 210 | auto axisY = plot.axisRect()->axis(QCPAxis::atLeft); |
|
211 | 211 | return axisX->range().contains(axisPoint.x()) && axisY->range().contains(axisPoint.y()); |
|
212 | 212 | } |
|
213 | 213 | }; |
|
214 | 214 | |
|
215 | 215 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) |
|
216 | 216 | : VisualizationDragWidget{parent}, |
|
217 | 217 | ui{new Ui::VisualizationGraphWidget}, |
|
218 | 218 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>(name)} |
|
219 | 219 | { |
|
220 | 220 | ui->setupUi(this); |
|
221 | 221 | |
|
222 | 222 | // 'Close' options : widget is deleted when closed |
|
223 | 223 | setAttribute(Qt::WA_DeleteOnClose); |
|
224 | 224 | |
|
225 | 225 | // Set qcpplot properties : |
|
226 | 226 | // - zoom is enabled |
|
227 | 227 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation |
|
228 | 228 | ui->widget->setInteractions(QCP::iRangeZoom); |
|
229 | 229 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal | Qt::Vertical); |
|
230 | 230 | |
|
231 | 231 | // The delegate must be initialized after the ui as it uses the plot |
|
232 | 232 | impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*this); |
|
233 | 233 | |
|
234 | 234 | // Init the cursors |
|
235 | 235 | impl->m_HorizontalCursor = std::make_unique<VisualizationCursorItem>(&plot()); |
|
236 | 236 | impl->m_HorizontalCursor->setOrientation(Qt::Horizontal); |
|
237 | 237 | impl->m_VerticalCursor = std::make_unique<VisualizationCursorItem>(&plot()); |
|
238 | 238 | impl->m_VerticalCursor->setOrientation(Qt::Vertical); |
|
239 | 239 | |
|
240 | 240 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); |
|
241 | 241 | connect(ui->widget, &QCustomPlot::mouseRelease, this, |
|
242 | 242 | &VisualizationGraphWidget::onMouseRelease); |
|
243 | 243 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); |
|
244 | 244 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); |
|
245 | 245 | connect(ui->widget, &QCustomPlot::mouseDoubleClick, this, |
|
246 | 246 | &VisualizationGraphWidget::onMouseDoubleClick); |
|
247 | 247 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( |
|
248 | 248 | &QCPAxis::rangeChanged), |
|
249 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | |
|
249 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | |
|
250 | 250 | |
|
251 | 251 | // Activates menu when right clicking on the graph |
|
252 | 252 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
253 | 253 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, |
|
254 | 254 | &VisualizationGraphWidget::onGraphMenuRequested); |
|
255 | 255 | |
|
256 | 256 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), |
|
257 | 257 | &VariableController::onRequestDataLoading); |
|
258 | 258 | |
|
259 | 259 | connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this, |
|
260 | 260 | &VisualizationGraphWidget::onUpdateVarDisplaying); |
|
261 | 261 | |
|
262 | 262 | // Necessary for all platform since Qt::AA_EnableHighDpiScaling is enable. |
|
263 | 263 | plot().setPlottingHint(QCP::phFastPolylines, true); |
|
264 | 264 | } |
|
265 | 265 | |
|
266 | 266 | |
|
267 | 267 | VisualizationGraphWidget::~VisualizationGraphWidget() |
|
268 | 268 | { |
|
269 | 269 | delete ui; |
|
270 | 270 | } |
|
271 | 271 | |
|
272 | 272 | VisualizationZoneWidget *VisualizationGraphWidget::parentZoneWidget() const noexcept |
|
273 | 273 | { |
|
274 | 274 | auto parent = parentWidget(); |
|
275 | 275 | while (parent != nullptr && !qobject_cast<VisualizationZoneWidget *>(parent)) { |
|
276 | 276 | parent = parent->parentWidget(); |
|
277 | 277 | } |
|
278 | 278 | |
|
279 | 279 | return qobject_cast<VisualizationZoneWidget *>(parent); |
|
280 | 280 | } |
|
281 | 281 | |
|
282 | 282 | VisualizationWidget *VisualizationGraphWidget::parentVisualizationWidget() const |
|
283 | 283 | { |
|
284 | 284 | auto parent = parentWidget(); |
|
285 | 285 | while (parent != nullptr && !qobject_cast<VisualizationWidget *>(parent)) { |
|
286 | 286 | parent = parent->parentWidget(); |
|
287 | 287 | } |
|
288 | 288 | |
|
289 | 289 | return qobject_cast<VisualizationWidget *>(parent); |
|
290 | 290 | } |
|
291 | 291 | |
|
292 | 292 | void VisualizationGraphWidget::setFlags(GraphFlags flags) |
|
293 | 293 | { |
|
294 | 294 | impl->m_Flags = std::move(flags); |
|
295 | 295 | } |
|
296 | 296 | |
|
297 | 297 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range) |
|
298 | 298 | { |
|
299 | 299 | /// Lambda used to set graph's units and range according to the variable passed in parameter |
|
300 | 300 | auto loadRange = [this](std::shared_ptr<Variable> variable, const SqpRange &range) { |
|
301 | 301 | impl->m_RenderingDelegate->setAxesUnits(*variable); |
|
302 | 302 | |
|
303 | 303 | this->setFlags(GraphFlag::DisableAll); |
|
304 | 304 | setGraphRange(range); |
|
305 | 305 | this->setFlags(GraphFlag::EnableAll); |
|
306 | 306 | emit requestDataLoading({variable}, range, false); |
|
307 | 307 | }; |
|
308 | 308 | |
|
309 | 309 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
310 | 310 | |
|
311 | 311 | // Calls update of graph's range and units when the data of the variable have been initialized. |
|
312 | 312 | // Note: we use QueuedConnection here as the update event must be called in the UI thread |
|
313 | 313 | connect(variable.get(), &Variable::dataInitialized, this, |
|
314 | 314 | [ varW = std::weak_ptr<Variable>{variable}, range, loadRange, this ]() { |
|
315 | 315 | if (auto var = varW.lock()) { |
|
316 | 316 | // If the variable is the first added in the graph, we load its range |
|
317 | 317 | auto firstVariableInGraph = range == INVALID_RANGE; |
|
318 | 318 | auto loadedRange = graphRange(); |
|
319 | 319 | if (impl->m_VariableAutoRangeOnInit) { |
|
320 | 320 | loadedRange = firstVariableInGraph ? var->range() : range; |
|
321 | 321 | } |
|
322 | 322 | loadRange(var, loadedRange); |
|
323 | 323 | setYRange(var); |
|
324 | 324 | } |
|
325 | 325 | }, |
|
326 | 326 | Qt::QueuedConnection); |
|
327 | 327 | |
|
328 | 328 | // Uses delegate to create the qcpplot components according to the variable |
|
329 | 329 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); |
|
330 | 330 | |
|
331 | 331 | // Sets graph properties |
|
332 | 332 | impl->m_RenderingDelegate->setGraphProperties(*variable, createdPlottables); |
|
333 | 333 | |
|
334 | 334 | impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); |
|
335 | 335 | |
|
336 | 336 | // If the variable already has its data loaded, load its units and its range in the graph |
|
337 | 337 | if (variable->dataSeries() != nullptr) { |
|
338 | 338 | loadRange(variable, range); |
|
339 | 339 | } |
|
340 | 340 | |
|
341 | 341 | emit variableAdded(variable); |
|
342 | 342 | } |
|
343 | 343 | |
|
344 | 344 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept |
|
345 | 345 | { |
|
346 | 346 | // Each component associated to the variable : |
|
347 | 347 | // - is removed from qcpplot (which deletes it) |
|
348 | 348 | // - is no longer referenced in the map |
|
349 | 349 | auto variableIt = impl->m_VariableToPlotMultiMap.find(variable); |
|
350 | 350 | if (variableIt != impl->m_VariableToPlotMultiMap.cend()) { |
|
351 | 351 | emit variableAboutToBeRemoved(variable); |
|
352 | 352 | |
|
353 | 353 | auto &plottablesMap = variableIt->second; |
|
354 | 354 | |
|
355 | 355 | for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend(); |
|
356 | 356 | plottableIt != plottableEnd;) { |
|
357 | 357 | ui->widget->removePlottable(plottableIt->second); |
|
358 | 358 | plottableIt = plottablesMap.erase(plottableIt); |
|
359 | 359 | } |
|
360 | 360 | |
|
361 | 361 | impl->m_VariableToPlotMultiMap.erase(variableIt); |
|
362 | 362 | } |
|
363 | 363 | |
|
364 | 364 | // Updates graph |
|
365 | 365 | ui->widget->replot(); |
|
366 | 366 | } |
|
367 | 367 | |
|
368 | 368 | QList<std::shared_ptr<Variable> > VisualizationGraphWidget::variables() const |
|
369 | 369 | { |
|
370 | 370 | auto variables = QList<std::shared_ptr<Variable> >{}; |
|
371 | 371 | for (auto it = std::cbegin(impl->m_VariableToPlotMultiMap); |
|
372 | 372 | it != std::cend(impl->m_VariableToPlotMultiMap); ++it) { |
|
373 | 373 | variables << it->first; |
|
374 | 374 | } |
|
375 | 375 | |
|
376 | 376 | return variables; |
|
377 | 377 | } |
|
378 | 378 | |
|
379 | 379 | void VisualizationGraphWidget::setYRange(std::shared_ptr<Variable> variable) |
|
380 | 380 | { |
|
381 | 381 | if (!variable) { |
|
382 | 382 | qCCritical(LOG_VisualizationGraphWidget()) << "Can't set y-axis range: variable is null"; |
|
383 | 383 | return; |
|
384 | 384 | } |
|
385 | 385 | |
|
386 | 386 | VisualizationGraphHelper::setYAxisRange(variable, *ui->widget); |
|
387 | 387 | } |
|
388 | 388 | |
|
389 | 389 | SqpRange VisualizationGraphWidget::graphRange() const noexcept |
|
390 | 390 | { |
|
391 | 391 | auto graphRange = ui->widget->xAxis->range(); |
|
392 | 392 | return SqpRange{graphRange.lower, graphRange.upper}; |
|
393 | 393 | } |
|
394 | 394 | |
|
395 | 395 | void VisualizationGraphWidget::setGraphRange(const SqpRange &range, bool calibration) |
|
396 | 396 | { |
|
397 | 397 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START"); |
|
398 | 398 | |
|
399 | 399 | if (calibration) { |
|
400 | 400 | impl->m_IsCalibration = true; |
|
401 | 401 | } |
|
402 | 402 | |
|
403 | 403 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); |
|
404 | 404 | ui->widget->replot(); |
|
405 | 405 | |
|
406 | 406 | if (calibration) { |
|
407 | 407 | impl->m_IsCalibration = false; |
|
408 | 408 | } |
|
409 | 409 | |
|
410 | 410 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); |
|
411 | 411 | } |
|
412 | 412 | |
|
413 | 413 | void VisualizationGraphWidget::setAutoRangeOnVariableInitialization(bool value) |
|
414 | 414 | { |
|
415 | 415 | impl->m_VariableAutoRangeOnInit = value; |
|
416 | 416 | } |
|
417 | 417 | |
|
418 | 418 | QVector<SqpRange> VisualizationGraphWidget::selectionZoneRanges() const |
|
419 | 419 | { |
|
420 | 420 | QVector<SqpRange> ranges; |
|
421 | 421 | for (auto zone : impl->m_SelectionZones) { |
|
422 | 422 | ranges << zone->range(); |
|
423 | 423 | } |
|
424 | 424 | |
|
425 | 425 | return ranges; |
|
426 | 426 | } |
|
427 | 427 | |
|
428 | 428 | void VisualizationGraphWidget::addSelectionZones(const QVector<SqpRange> &ranges) |
|
429 | 429 | { |
|
430 | 430 | for (const auto &range : ranges) { |
|
431 | 431 | // note: ownership is transfered to QCustomPlot |
|
432 | 432 | auto zone = new VisualizationSelectionZoneItem(&plot()); |
|
433 | 433 | zone->setRange(range.m_TStart, range.m_TEnd); |
|
434 | 434 | impl->addSelectionZone(zone); |
|
435 | 435 | } |
|
436 | 436 | |
|
437 | 437 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
438 | 438 | } |
|
439 | 439 | |
|
440 | 440 | VisualizationSelectionZoneItem *VisualizationGraphWidget::addSelectionZone(const QString &name, |
|
441 | 441 | const SqpRange &range) |
|
442 | 442 | { |
|
443 | 443 | // note: ownership is transfered to QCustomPlot |
|
444 | 444 | auto zone = new VisualizationSelectionZoneItem(&plot()); |
|
445 | 445 | zone->setName(name); |
|
446 | 446 | zone->setRange(range.m_TStart, range.m_TEnd); |
|
447 | 447 | impl->addSelectionZone(zone); |
|
448 | 448 | |
|
449 | 449 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
450 | 450 | |
|
451 | 451 | return zone; |
|
452 | 452 | } |
|
453 | 453 | |
|
454 | 454 | void VisualizationGraphWidget::removeSelectionZone(VisualizationSelectionZoneItem *selectionZone) |
|
455 | 455 | { |
|
456 | 456 | parentVisualizationWidget()->selectionZoneManager().setSelected(selectionZone, false); |
|
457 | 457 | |
|
458 | 458 | if (impl->m_HoveredZone == selectionZone) { |
|
459 | 459 | impl->m_HoveredZone = nullptr; |
|
460 | 460 | setCursor(Qt::ArrowCursor); |
|
461 | 461 | } |
|
462 | 462 | |
|
463 | 463 | impl->m_SelectionZones.removeAll(selectionZone); |
|
464 | 464 | plot().removeItem(selectionZone); |
|
465 | 465 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
466 | 466 | } |
|
467 | 467 | |
|
468 | 468 | void VisualizationGraphWidget::undoZoom() |
|
469 | 469 | { |
|
470 | 470 | auto zoom = impl->m_ZoomStack.pop(); |
|
471 | 471 | auto axisX = plot().axisRect()->axis(QCPAxis::atBottom); |
|
472 | 472 | auto axisY = plot().axisRect()->axis(QCPAxis::atLeft); |
|
473 | 473 | |
|
474 | 474 | axisX->setRange(zoom.first); |
|
475 | 475 | axisY->setRange(zoom.second); |
|
476 | 476 | |
|
477 | 477 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
478 | 478 | } |
|
479 | 479 | |
|
480 | 480 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
481 | 481 | { |
|
482 | 482 | if (visitor) { |
|
483 | 483 | visitor->visit(this); |
|
484 | 484 | } |
|
485 | 485 | else { |
|
486 | 486 | qCCritical(LOG_VisualizationGraphWidget()) |
|
487 | 487 | << tr("Can't visit widget : the visitor is null"); |
|
488 | 488 | } |
|
489 | 489 | } |
|
490 | 490 | |
|
491 | 491 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
492 | 492 | { |
|
493 | 493 | auto isSpectrogram = [](const auto &variable) { |
|
494 | 494 | return std::dynamic_pointer_cast<SpectrogramSeries>(variable.dataSeries()) != nullptr; |
|
495 | 495 | }; |
|
496 | 496 | |
|
497 | 497 | // - A spectrogram series can't be dropped on graph with existing plottables |
|
498 | 498 | // - No data series can be dropped on graph with existing spectrogram series |
|
499 | 499 | return isSpectrogram(variable) |
|
500 | 500 | ? impl->m_VariableToPlotMultiMap.empty() |
|
501 | 501 | : std::none_of( |
|
502 | 502 | impl->m_VariableToPlotMultiMap.cbegin(), impl->m_VariableToPlotMultiMap.cend(), |
|
503 | 503 | [isSpectrogram](const auto &entry) { return isSpectrogram(*entry.first); }); |
|
504 | 504 | } |
|
505 | 505 | |
|
506 | 506 | bool VisualizationGraphWidget::contains(const Variable &variable) const |
|
507 | 507 | { |
|
508 | 508 | // Finds the variable among the keys of the map |
|
509 | 509 | auto variablePtr = &variable; |
|
510 | 510 | auto findVariable |
|
511 | 511 | = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); }; |
|
512 | 512 | |
|
513 | 513 | auto end = impl->m_VariableToPlotMultiMap.cend(); |
|
514 | 514 | auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable); |
|
515 | 515 | return it != end; |
|
516 | 516 | } |
|
517 | 517 | |
|
518 | 518 | QString VisualizationGraphWidget::name() const |
|
519 | 519 | { |
|
520 | 520 | return impl->m_Name; |
|
521 | 521 | } |
|
522 | 522 | |
|
523 | 523 | QMimeData *VisualizationGraphWidget::mimeData(const QPoint &position) const |
|
524 | 524 | { |
|
525 | 525 | auto mimeData = new QMimeData; |
|
526 | 526 | |
|
527 | 527 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(position, plot()); |
|
528 | 528 | if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones |
|
529 | 529 | && selectionZoneItemUnderCursor) { |
|
530 | 530 | mimeData->setData(MIME_TYPE_TIME_RANGE, TimeController::mimeDataForTimeRange( |
|
531 | 531 | selectionZoneItemUnderCursor->range())); |
|
532 | 532 | mimeData->setData(MIME_TYPE_SELECTION_ZONE, TimeController::mimeDataForTimeRange( |
|
533 | 533 | selectionZoneItemUnderCursor->range())); |
|
534 | 534 | } |
|
535 | 535 | else { |
|
536 | 536 | mimeData->setData(MIME_TYPE_GRAPH, QByteArray{}); |
|
537 | 537 | |
|
538 | 538 | auto timeRangeData = TimeController::mimeDataForTimeRange(graphRange()); |
|
539 | 539 | mimeData->setData(MIME_TYPE_TIME_RANGE, timeRangeData); |
|
540 | 540 | } |
|
541 | 541 | |
|
542 | 542 | return mimeData; |
|
543 | 543 | } |
|
544 | 544 | |
|
545 | 545 | QPixmap VisualizationGraphWidget::customDragPixmap(const QPoint &dragPosition) |
|
546 | 546 | { |
|
547 | 547 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(dragPosition, plot()); |
|
548 | 548 | if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones |
|
549 | 549 | && selectionZoneItemUnderCursor) { |
|
550 | 550 | |
|
551 | 551 | auto zoneTopLeft = selectionZoneItemUnderCursor->topLeft->pixelPosition(); |
|
552 | 552 | auto zoneBottomRight = selectionZoneItemUnderCursor->bottomRight->pixelPosition(); |
|
553 | 553 | |
|
554 | 554 | auto zoneSize = QSizeF{qAbs(zoneBottomRight.x() - zoneTopLeft.x()), |
|
555 | 555 | qAbs(zoneBottomRight.y() - zoneTopLeft.y())} |
|
556 | 556 | .toSize(); |
|
557 | 557 | |
|
558 | 558 | auto pixmap = QPixmap(zoneSize); |
|
559 | 559 | render(&pixmap, QPoint(), QRegion{QRect{zoneTopLeft.toPoint(), zoneSize}}); |
|
560 | 560 | |
|
561 | 561 | return pixmap; |
|
562 | 562 | } |
|
563 | 563 | |
|
564 | 564 | return QPixmap(); |
|
565 | 565 | } |
|
566 | 566 | |
|
567 | 567 | bool VisualizationGraphWidget::isDragAllowed() const |
|
568 | 568 | { |
|
569 | 569 | return true; |
|
570 | 570 | } |
|
571 | 571 | |
|
572 | 572 | void VisualizationGraphWidget::highlightForMerge(bool highlighted) |
|
573 | 573 | { |
|
574 | 574 | if (highlighted) { |
|
575 | 575 | plot().setBackground(QBrush(QColor("#BBD5EE"))); |
|
576 | 576 | } |
|
577 | 577 | else { |
|
578 | 578 | plot().setBackground(QBrush(Qt::white)); |
|
579 | 579 | } |
|
580 | 580 | |
|
581 | 581 | plot().update(); |
|
582 | 582 | } |
|
583 | 583 | |
|
584 | 584 | void VisualizationGraphWidget::addVerticalCursor(double time) |
|
585 | 585 | { |
|
586 | 586 | impl->m_VerticalCursor->setPosition(time); |
|
587 | 587 | impl->m_VerticalCursor->setVisible(true); |
|
588 | 588 | |
|
589 | 589 | auto text |
|
590 | 590 | = DateUtils::dateTime(time).toString(CURSOR_LABELS_DATETIME_FORMAT).replace(' ', '\n'); |
|
591 | 591 | impl->m_VerticalCursor->setLabelText(text); |
|
592 | 592 | } |
|
593 | 593 | |
|
594 | 594 | void VisualizationGraphWidget::addVerticalCursorAtViewportPosition(double position) |
|
595 | 595 | { |
|
596 | 596 | impl->m_VerticalCursor->setAbsolutePosition(position); |
|
597 | 597 | impl->m_VerticalCursor->setVisible(true); |
|
598 | 598 | |
|
599 | 599 | auto axis = plot().axisRect()->axis(QCPAxis::atBottom); |
|
600 | 600 | auto text |
|
601 | 601 | = DateUtils::dateTime(axis->pixelToCoord(position)).toString(CURSOR_LABELS_DATETIME_FORMAT); |
|
602 | 602 | impl->m_VerticalCursor->setLabelText(text); |
|
603 | 603 | } |
|
604 | 604 | |
|
605 | 605 | void VisualizationGraphWidget::removeVerticalCursor() |
|
606 | 606 | { |
|
607 | 607 | impl->m_VerticalCursor->setVisible(false); |
|
608 | 608 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
609 | 609 | } |
|
610 | 610 | |
|
611 | 611 | void VisualizationGraphWidget::addHorizontalCursor(double value) |
|
612 | 612 | { |
|
613 | 613 | impl->m_HorizontalCursor->setPosition(value); |
|
614 | 614 | impl->m_HorizontalCursor->setVisible(true); |
|
615 | 615 | impl->m_HorizontalCursor->setLabelText(QString::number(value)); |
|
616 | 616 | } |
|
617 | 617 | |
|
618 | 618 | void VisualizationGraphWidget::addHorizontalCursorAtViewportPosition(double position) |
|
619 | 619 | { |
|
620 | 620 | impl->m_HorizontalCursor->setAbsolutePosition(position); |
|
621 | 621 | impl->m_HorizontalCursor->setVisible(true); |
|
622 | 622 | |
|
623 | 623 | auto axis = plot().axisRect()->axis(QCPAxis::atLeft); |
|
624 | 624 | impl->m_HorizontalCursor->setLabelText(QString::number(axis->pixelToCoord(position))); |
|
625 | 625 | } |
|
626 | 626 | |
|
627 | 627 | void VisualizationGraphWidget::removeHorizontalCursor() |
|
628 | 628 | { |
|
629 | 629 | impl->m_HorizontalCursor->setVisible(false); |
|
630 | 630 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
631 | 631 | } |
|
632 | 632 | |
|
633 | 633 | void VisualizationGraphWidget::closeEvent(QCloseEvent *event) |
|
634 | 634 | { |
|
635 | 635 | Q_UNUSED(event); |
|
636 | 636 | |
|
637 | 637 | for (auto i : impl->m_SelectionZones) { |
|
638 | 638 | parentVisualizationWidget()->selectionZoneManager().setSelected(i, false); |
|
639 | 639 | } |
|
640 | 640 | |
|
641 | 641 | // Prevents that all variables will be removed from graph when it will be closed |
|
642 | 642 | for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { |
|
643 | 643 | emit variableAboutToBeRemoved(variableEntry.first); |
|
644 | 644 | } |
|
645 | 645 | } |
|
646 | 646 | |
|
647 | 647 | void VisualizationGraphWidget::enterEvent(QEvent *event) |
|
648 | 648 | { |
|
649 | 649 | Q_UNUSED(event); |
|
650 | 650 | impl->m_RenderingDelegate->showGraphOverlay(true); |
|
651 | 651 | } |
|
652 | 652 | |
|
653 | 653 | void VisualizationGraphWidget::leaveEvent(QEvent *event) |
|
654 | 654 | { |
|
655 | 655 | Q_UNUSED(event); |
|
656 | 656 | impl->m_RenderingDelegate->showGraphOverlay(false); |
|
657 | 657 | |
|
658 | 658 | if (auto parentZone = parentZoneWidget()) { |
|
659 | 659 | parentZone->notifyMouseLeaveGraph(this); |
|
660 | 660 | } |
|
661 | 661 | else { |
|
662 | 662 | qCWarning(LOG_VisualizationGraphWidget()) << "leaveEvent: No parent zone widget"; |
|
663 | 663 | } |
|
664 | 664 | |
|
665 | 665 | if (impl->m_HoveredZone) { |
|
666 | 666 | impl->m_HoveredZone->setHovered(false); |
|
667 | 667 | impl->m_HoveredZone = nullptr; |
|
668 | 668 | } |
|
669 | 669 | } |
|
670 | 670 | |
|
671 | 671 | QCustomPlot &VisualizationGraphWidget::plot() const noexcept |
|
672 | 672 | { |
|
673 | 673 | return *ui->widget; |
|
674 | 674 | } |
|
675 | 675 | |
|
676 | 676 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept |
|
677 | 677 | { |
|
678 | 678 | QMenu graphMenu{}; |
|
679 | 679 | |
|
680 | 680 | // Iterates on variables (unique keys) |
|
681 | 681 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), |
|
682 | 682 | end = impl->m_VariableToPlotMultiMap.cend(); |
|
683 | 683 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
684 | 684 | // 'Remove variable' action |
|
685 | 685 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), |
|
686 | 686 | [ this, var = it->first ]() { removeVariable(var); }); |
|
687 | 687 | } |
|
688 | 688 | |
|
689 | 689 | if (!impl->m_ZoomStack.isEmpty()) { |
|
690 | 690 | if (!graphMenu.isEmpty()) { |
|
691 | 691 | graphMenu.addSeparator(); |
|
692 | 692 | } |
|
693 | 693 | |
|
694 | 694 | graphMenu.addAction(tr("Undo Zoom"), [this]() { undoZoom(); }); |
|
695 | 695 | } |
|
696 | 696 | |
|
697 | 697 | // Selection Zone Actions |
|
698 | 698 | auto selectionZoneItem = impl->selectionZoneAt(pos, plot()); |
|
699 | 699 | if (selectionZoneItem) { |
|
700 | 700 | auto selectedItems = parentVisualizationWidget()->selectionZoneManager().selectedItems(); |
|
701 | 701 | selectedItems.removeAll(selectionZoneItem); |
|
702 | 702 | selectedItems.prepend(selectionZoneItem); // Put the current selection zone first |
|
703 | 703 | |
|
704 | 704 | auto zoneActions = sqpApp->actionsGuiController().selectionZoneActions(); |
|
705 | 705 | if (!zoneActions.isEmpty() && !graphMenu.isEmpty()) { |
|
706 | 706 | graphMenu.addSeparator(); |
|
707 | 707 | } |
|
708 | 708 | |
|
709 | 709 | QHash<QString, QMenu *> subMenus; |
|
710 | 710 | QHash<QString, bool> subMenusEnabled; |
|
711 | 711 | QHash<QString, FilteringAction *> filteredMenu; |
|
712 | 712 | |
|
713 | 713 | for (auto zoneAction : zoneActions) { |
|
714 | 714 | |
|
715 | 715 | auto isEnabled = zoneAction->isEnabled(selectedItems); |
|
716 | 716 | |
|
717 | 717 | auto menu = &graphMenu; |
|
718 | 718 | QString menuPath; |
|
719 | 719 | for (auto subMenuName : zoneAction->subMenuList()) { |
|
720 | 720 | menuPath += '/'; |
|
721 | 721 | menuPath += subMenuName; |
|
722 | 722 | |
|
723 | 723 | if (!subMenus.contains(menuPath)) { |
|
724 | 724 | menu = menu->addMenu(subMenuName); |
|
725 | 725 | subMenus[menuPath] = menu; |
|
726 | 726 | subMenusEnabled[menuPath] = isEnabled; |
|
727 | 727 | } |
|
728 | 728 | else { |
|
729 | 729 | menu = subMenus.value(menuPath); |
|
730 | 730 | if (isEnabled) { |
|
731 | 731 | // The sub menu is enabled if at least one of its actions is enabled |
|
732 | 732 | subMenusEnabled[menuPath] = true; |
|
733 | 733 | } |
|
734 | 734 | } |
|
735 | 735 | } |
|
736 | 736 | |
|
737 | 737 | FilteringAction *filterAction = nullptr; |
|
738 | 738 | if (sqpApp->actionsGuiController().isMenuFiltered(zoneAction->subMenuList())) { |
|
739 | 739 | filterAction = filteredMenu.value(menuPath); |
|
740 | 740 | if (!filterAction) { |
|
741 | 741 | filterAction = new FilteringAction{this}; |
|
742 | 742 | filteredMenu[menuPath] = filterAction; |
|
743 | 743 | menu->addAction(filterAction); |
|
744 | 744 | } |
|
745 | 745 | } |
|
746 | 746 | |
|
747 | 747 | auto action = menu->addAction(zoneAction->name()); |
|
748 | 748 | action->setEnabled(isEnabled); |
|
749 | 749 | action->setShortcut(zoneAction->displayedShortcut()); |
|
750 | 750 | QObject::connect(action, &QAction::triggered, |
|
751 | 751 | [zoneAction, selectedItems]() { zoneAction->execute(selectedItems); }); |
|
752 | 752 | |
|
753 | 753 | if (filterAction && zoneAction->isFilteringAllowed()) { |
|
754 | 754 | filterAction->addActionToFilter(action); |
|
755 | 755 | } |
|
756 | 756 | } |
|
757 | 757 | |
|
758 | 758 | for (auto it = subMenus.cbegin(); it != subMenus.cend(); ++it) { |
|
759 | 759 | it.value()->setEnabled(subMenusEnabled[it.key()]); |
|
760 | 760 | } |
|
761 | 761 | } |
|
762 | 762 | |
|
763 | 763 | if (!graphMenu.isEmpty()) { |
|
764 | 764 | graphMenu.exec(QCursor::pos()); |
|
765 | 765 | } |
|
766 | 766 | } |
|
767 | 767 | |
|
768 | 768 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) |
|
769 | 769 | { |
|
770 | 770 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") |
|
771 | 771 | << QThread::currentThread()->objectName() << "DoAcqui" |
|
772 | 772 | << impl->m_Flags.testFlag(GraphFlag::EnableAcquisition); |
|
773 | 773 | |
|
774 | 774 | auto graphRange = SqpRange{t1.lower, t1.upper}; |
|
775 | 775 | auto oldGraphRange = SqpRange{t2.lower, t2.upper}; |
|
776 | 776 | |
|
777 | 777 | if (impl->m_Flags.testFlag(GraphFlag::EnableAcquisition)) { |
|
778 | 778 | QVector<std::shared_ptr<Variable> > variableUnderGraphVector; |
|
779 | 779 | |
|
780 | 780 | for (auto it = impl->m_VariableToPlotMultiMap.begin(), |
|
781 | 781 | end = impl->m_VariableToPlotMultiMap.end(); |
|
782 | 782 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
783 | 783 | variableUnderGraphVector.push_back(it->first); |
|
784 | 784 | } |
|
785 | 785 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, |
|
786 | 786 | !impl->m_IsCalibration); |
|
787 | 787 | } |
|
788 | 788 | |
|
789 | 789 | if (impl->m_Flags.testFlag(GraphFlag::EnableSynchronization) && !impl->m_IsCalibration) { |
|
790 | 790 | qCDebug(LOG_VisualizationGraphWidget()) |
|
791 | 791 | << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") |
|
792 | 792 | << QThread::currentThread()->objectName() << graphRange << oldGraphRange; |
|
793 | 793 | emit synchronize(graphRange, oldGraphRange); |
|
794 | 794 | } |
|
795 | 795 | |
|
796 | 796 | auto pos = mapFromGlobal(QCursor::pos()); |
|
797 | 797 | auto axisPos = impl->posToAxisPos(pos, plot()); |
|
798 | 798 | if (auto parentZone = parentZoneWidget()) { |
|
799 | 799 | if (impl->pointIsInAxisRect(axisPos, plot())) { |
|
800 | 800 | parentZone->notifyMouseMoveInGraph(pos, axisPos, this); |
|
801 | 801 | } |
|
802 | 802 | else { |
|
803 | 803 | parentZone->notifyMouseLeaveGraph(this); |
|
804 | 804 | } |
|
805 | 805 | } |
|
806 | 806 | else { |
|
807 | 807 | qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget"; |
|
808 | 808 | } |
|
809 | 809 | |
|
810 | 810 | // Quits calibration |
|
811 | 811 | impl->m_IsCalibration = false; |
|
812 | 812 | } |
|
813 | 813 | |
|
814 | 814 | void VisualizationGraphWidget::onMouseDoubleClick(QMouseEvent *event) noexcept |
|
815 | 815 | { |
|
816 | 816 | impl->m_RenderingDelegate->onMouseDoubleClick(event); |
|
817 | 817 | } |
|
818 | 818 | |
|
819 | 819 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept |
|
820 | 820 | { |
|
821 | 821 | // Handles plot rendering when mouse is moving |
|
822 | 822 | impl->m_RenderingDelegate->onMouseMove(event); |
|
823 | 823 | |
|
824 | 824 | auto axisPos = impl->posToAxisPos(event->pos(), plot()); |
|
825 | 825 | |
|
826 | 826 | // Zoom box and zone drawing |
|
827 | 827 | if (impl->m_DrawingZoomRect) { |
|
828 | 828 | impl->m_DrawingZoomRect->bottomRight->setCoords(axisPos); |
|
829 | 829 | } |
|
830 | 830 | else if (impl->m_DrawingZone) { |
|
831 | 831 | impl->m_DrawingZone->setEnd(axisPos.x()); |
|
832 | 832 | } |
|
833 | 833 | |
|
834 | 834 | // Cursor |
|
835 | 835 | if (auto parentZone = parentZoneWidget()) { |
|
836 | 836 | if (impl->pointIsInAxisRect(axisPos, plot())) { |
|
837 | 837 | parentZone->notifyMouseMoveInGraph(event->pos(), axisPos, this); |
|
838 | 838 | } |
|
839 | 839 | else { |
|
840 | 840 | parentZone->notifyMouseLeaveGraph(this); |
|
841 | 841 | } |
|
842 | 842 | } |
|
843 | 843 | else { |
|
844 | 844 | qCWarning(LOG_VisualizationGraphWidget()) << "onMouseMove: No parent zone widget"; |
|
845 | 845 | } |
|
846 | 846 | |
|
847 | 847 | // Search for the selection zone under the mouse |
|
848 | 848 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot()); |
|
849 | 849 | if (selectionZoneItemUnderCursor && !impl->m_DrawingZone |
|
850 | 850 | && sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones) { |
|
851 | 851 | |
|
852 | 852 | // Sets the appropriate cursor shape |
|
853 | 853 | auto cursorShape = selectionZoneItemUnderCursor->curshorShapeForPosition(event->pos()); |
|
854 | 854 | setCursor(cursorShape); |
|
855 | 855 | |
|
856 | 856 | // Manages the hovered zone |
|
857 | 857 | if (selectionZoneItemUnderCursor != impl->m_HoveredZone) { |
|
858 | 858 | if (impl->m_HoveredZone) { |
|
859 | 859 | impl->m_HoveredZone->setHovered(false); |
|
860 | 860 | } |
|
861 | 861 | selectionZoneItemUnderCursor->setHovered(true); |
|
862 | 862 | impl->m_HoveredZone = selectionZoneItemUnderCursor; |
|
863 | 863 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
864 | 864 | } |
|
865 | 865 | } |
|
866 | 866 | else { |
|
867 | 867 | // There is no zone under the mouse or the interaction mode is not "selection zones" |
|
868 | 868 | if (impl->m_HoveredZone) { |
|
869 | 869 | impl->m_HoveredZone->setHovered(false); |
|
870 | 870 | impl->m_HoveredZone = nullptr; |
|
871 | 871 | } |
|
872 | 872 | |
|
873 | 873 | setCursor(Qt::ArrowCursor); |
|
874 | 874 | } |
|
875 | 875 | |
|
876 | 876 | impl->m_HasMovedMouse = true; |
|
877 | 877 | VisualizationDragWidget::mouseMoveEvent(event); |
|
878 | 878 | } |
|
879 | 879 | |
|
880 | 880 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
881 | 881 | { |
|
882 | 882 | auto value = event->angleDelta().x() + event->angleDelta().y(); |
|
883 | 883 | if (value != 0) { |
|
884 | 884 | |
|
885 | 885 | auto direction = value > 0 ? 1.0 : -1.0; |
|
886 | 886 | auto isZoomX = event->modifiers().testFlag(HORIZONTAL_ZOOM_MODIFIER); |
|
887 | 887 | auto isZoomY = event->modifiers().testFlag(VERTICAL_ZOOM_MODIFIER); |
|
888 | 888 | impl->m_IsCalibration = event->modifiers().testFlag(VERTICAL_PAN_MODIFIER); |
|
889 | 889 | |
|
890 | 890 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
891 | 891 | zoomOrientations.setFlag(Qt::Horizontal, isZoomX); |
|
892 | 892 | zoomOrientations.setFlag(Qt::Vertical, isZoomY); |
|
893 | 893 | |
|
894 | 894 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
895 | 895 | |
|
896 | 896 | if (!isZoomX && !isZoomY) { |
|
897 | 897 | auto axis = plot().axisRect()->axis(QCPAxis::atBottom); |
|
898 | 898 | auto diff = direction * (axis->range().size() * (PAN_SPEED / 100.0)); |
|
899 | 899 | |
|
900 | 900 | axis->setRange(axis->range() + diff); |
|
901 | 901 | |
|
902 | 902 | if (plot().noAntialiasingOnDrag()) { |
|
903 | 903 | plot().setNotAntialiasedElements(QCP::aeAll); |
|
904 | 904 | } |
|
905 | 905 | |
|
906 | 906 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
907 | 907 | } |
|
908 | 908 | } |
|
909 | 909 | } |
|
910 | 910 | |
|
911 | 911 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept |
|
912 | 912 | { |
|
913 | 913 | auto isDragDropClick = event->modifiers().testFlag(DRAG_DROP_MODIFIER); |
|
914 | 914 | auto isSelectionZoneMode |
|
915 | 915 | = sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones; |
|
916 | 916 | auto isLeftClick = event->buttons().testFlag(Qt::LeftButton); |
|
917 | 917 | |
|
918 | 918 | if (!isDragDropClick && isLeftClick) { |
|
919 | 919 | if (sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::ZoomBox) { |
|
920 | 920 | // Starts a zoom box |
|
921 | 921 | impl->startDrawingRect(event->pos(), plot()); |
|
922 | 922 | } |
|
923 | 923 | else if (isSelectionZoneMode && impl->m_DrawingZone == nullptr) { |
|
924 | 924 | // Starts a new selection zone |
|
925 | 925 | auto zoneAtPos = impl->selectionZoneAt(event->pos(), plot()); |
|
926 | 926 | if (!zoneAtPos) { |
|
927 | 927 | impl->startDrawingZone(event->pos(), this); |
|
928 | 928 | } |
|
929 | 929 | } |
|
930 | 930 | } |
|
931 | 931 | |
|
932 | 932 | // Allows mouse panning only in default mode |
|
933 | 933 | plot().setInteraction(QCP::iRangeDrag, sqpApp->plotsInteractionMode() |
|
934 | 934 | == SqpApplication::PlotsInteractionMode::None |
|
935 | 935 | && !isDragDropClick); |
|
936 | 936 | |
|
937 | 937 | // Allows zone edition only in selection zone mode without drag&drop |
|
938 | 938 | impl->setSelectionZonesEditionEnabled(isSelectionZoneMode && !isDragDropClick); |
|
939 | 939 | |
|
940 | 940 | // Selection / Deselection |
|
941 | 941 | if (isSelectionZoneMode) { |
|
942 | 942 | auto isMultiSelectionClick = event->modifiers().testFlag(MULTI_ZONE_SELECTION_MODIFIER); |
|
943 | 943 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot()); |
|
944 | 944 | |
|
945 | 945 | |
|
946 | 946 | if (selectionZoneItemUnderCursor && !selectionZoneItemUnderCursor->selected() |
|
947 | 947 | && !isMultiSelectionClick) { |
|
948 | 948 | parentVisualizationWidget()->selectionZoneManager().select( |
|
949 | 949 | {selectionZoneItemUnderCursor}); |
|
950 | 950 | } |
|
951 | 951 | else if (!selectionZoneItemUnderCursor && !isMultiSelectionClick && isLeftClick) { |
|
952 | 952 | parentVisualizationWidget()->selectionZoneManager().clearSelection(); |
|
953 | 953 | } |
|
954 | 954 | else { |
|
955 | 955 | // No selection change |
|
956 | 956 | } |
|
957 | 957 | |
|
958 | 958 | if (selectionZoneItemUnderCursor && isLeftClick) { |
|
959 | 959 | selectionZoneItemUnderCursor->setAssociatedEditedZones( |
|
960 | 960 | parentVisualizationWidget()->selectionZoneManager().selectedItems()); |
|
961 | 961 | } |
|
962 | 962 | } |
|
963 | 963 | |
|
964 | 964 | |
|
965 | 965 | impl->m_HasMovedMouse = false; |
|
966 | 966 | VisualizationDragWidget::mousePressEvent(event); |
|
967 | 967 | } |
|
968 | 968 | |
|
969 | 969 | void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept |
|
970 | 970 | { |
|
971 | 971 | if (impl->m_DrawingZoomRect) { |
|
972 | 972 | |
|
973 | 973 | auto axisX = plot().axisRect()->axis(QCPAxis::atBottom); |
|
974 | 974 | auto axisY = plot().axisRect()->axis(QCPAxis::atLeft); |
|
975 | 975 | |
|
976 | 976 | auto newAxisXRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().x(), |
|
977 | 977 | impl->m_DrawingZoomRect->bottomRight->coords().x()}; |
|
978 | 978 | |
|
979 | 979 | auto newAxisYRange = QCPRange{impl->m_DrawingZoomRect->topLeft->coords().y(), |
|
980 | 980 | impl->m_DrawingZoomRect->bottomRight->coords().y()}; |
|
981 | 981 | |
|
982 | 982 | impl->removeDrawingRect(plot()); |
|
983 | 983 | |
|
984 | 984 | if (newAxisXRange.size() > axisX->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0) |
|
985 | 985 | && newAxisYRange.size() > axisY->range().size() * (ZOOM_BOX_MIN_SIZE / 100.0)) { |
|
986 | 986 | impl->m_ZoomStack.push(qMakePair(axisX->range(), axisY->range())); |
|
987 | 987 | axisX->setRange(newAxisXRange); |
|
988 | 988 | axisY->setRange(newAxisYRange); |
|
989 | 989 | |
|
990 | 990 | plot().replot(QCustomPlot::rpQueuedReplot); |
|
991 | 991 | } |
|
992 | 992 | } |
|
993 | 993 | |
|
994 | 994 | impl->endDrawingZone(this); |
|
995 | 995 | |
|
996 | 996 | // Selection / Deselection |
|
997 | 997 | auto isSelectionZoneMode |
|
998 | 998 | = sqpApp->plotsInteractionMode() == SqpApplication::PlotsInteractionMode::SelectionZones; |
|
999 | 999 | if (isSelectionZoneMode) { |
|
1000 | 1000 | auto isMultiSelectionClick = event->modifiers().testFlag(MULTI_ZONE_SELECTION_MODIFIER); |
|
1001 | 1001 | auto selectionZoneItemUnderCursor = impl->selectionZoneAt(event->pos(), plot()); |
|
1002 | 1002 | if (selectionZoneItemUnderCursor && event->button() == Qt::LeftButton |
|
1003 | 1003 | && !impl->m_HasMovedMouse) { |
|
1004 | 1004 | |
|
1005 | 1005 | auto zonesUnderCursor = impl->selectionZonesAt(event->pos(), plot()); |
|
1006 | 1006 | if (zonesUnderCursor.count() > 1) { |
|
1007 | 1007 | // There are multiple zones under the mouse. |
|
1008 | 1008 | // Performs the selection with a selection dialog. |
|
1009 | 1009 | VisualizationMultiZoneSelectionDialog dialog{this}; |
|
1010 | 1010 | dialog.setZones(zonesUnderCursor); |
|
1011 | 1011 | dialog.move(mapToGlobal(event->pos() - QPoint(dialog.width() / 2, 20))); |
|
1012 | 1012 | dialog.activateWindow(); |
|
1013 | 1013 | dialog.raise(); |
|
1014 | 1014 | if (dialog.exec() == QDialog::Accepted) { |
|
1015 | 1015 | auto selection = dialog.selectedZones(); |
|
1016 | 1016 | |
|
1017 | 1017 | if (!isMultiSelectionClick) { |
|
1018 | 1018 | parentVisualizationWidget()->selectionZoneManager().clearSelection(); |
|
1019 | 1019 | } |
|
1020 | 1020 | |
|
1021 | 1021 | for (auto it = selection.cbegin(); it != selection.cend(); ++it) { |
|
1022 | 1022 | auto zone = it.key(); |
|
1023 | 1023 | auto isSelected = it.value(); |
|
1024 | 1024 | parentVisualizationWidget()->selectionZoneManager().setSelected(zone, |
|
1025 | 1025 | isSelected); |
|
1026 | 1026 | |
|
1027 | 1027 | if (isSelected) { |
|
1028 | 1028 | // Puts the zone on top of the stack so it can be moved or resized |
|
1029 | 1029 | impl->moveSelectionZoneOnTop(zone, plot()); |
|
1030 | 1030 | } |
|
1031 | 1031 | } |
|
1032 | 1032 | } |
|
1033 | 1033 | } |
|
1034 | 1034 | else { |
|
1035 | 1035 | if (!isMultiSelectionClick) { |
|
1036 | 1036 | parentVisualizationWidget()->selectionZoneManager().select( |
|
1037 | 1037 | {selectionZoneItemUnderCursor}); |
|
1038 | 1038 | impl->moveSelectionZoneOnTop(selectionZoneItemUnderCursor, plot()); |
|
1039 | 1039 | } |
|
1040 | 1040 | else { |
|
1041 | 1041 | parentVisualizationWidget()->selectionZoneManager().setSelected( |
|
1042 | 1042 | selectionZoneItemUnderCursor, !selectionZoneItemUnderCursor->selected() |
|
1043 | 1043 | || event->button() == Qt::RightButton); |
|
1044 | 1044 | } |
|
1045 | 1045 | } |
|
1046 | 1046 | } |
|
1047 | 1047 | else { |
|
1048 | 1048 | // No selection change |
|
1049 | 1049 | } |
|
1050 | 1050 | } |
|
1051 | 1051 | } |
|
1052 | 1052 | |
|
1053 | 1053 | void VisualizationGraphWidget::onDataCacheVariableUpdated() |
|
1054 | 1054 | { |
|
1055 | 1055 | auto graphRange = ui->widget->xAxis->range(); |
|
1056 | 1056 | auto dateTime = SqpRange{graphRange.lower, graphRange.upper}; |
|
1057 | 1057 | |
|
1058 | 1058 | for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { |
|
1059 | 1059 | auto variable = variableEntry.first; |
|
1060 | 1060 | qCDebug(LOG_VisualizationGraphWidget()) |
|
1061 | 1061 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); |
|
1062 | 1062 | qCDebug(LOG_VisualizationGraphWidget()) |
|
1063 | 1063 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; |
|
1064 | 1064 | if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { |
|
1065 | 1065 | impl->updateData(variableEntry.second, variable, variable->range()); |
|
1066 | 1066 | } |
|
1067 | 1067 | } |
|
1068 | 1068 | } |
|
1069 | 1069 | |
|
1070 | 1070 | void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable, |
|
1071 | 1071 | const SqpRange &range) |
|
1072 | 1072 | { |
|
1073 | 1073 | auto it = impl->m_VariableToPlotMultiMap.find(variable); |
|
1074 | 1074 | if (it != impl->m_VariableToPlotMultiMap.end()) { |
|
1075 | 1075 | impl->updateData(it->second, variable, range); |
|
1076 | 1076 | } |
|
1077 | 1077 | } |
@@ -1,274 +1,275 | |||
|
1 | 1 | #include "AmdaProvider.h" |
|
2 | 2 | #include "AmdaDefs.h" |
|
3 | 3 | #include "AmdaResultParser.h" |
|
4 | 4 | #include "AmdaServer.h" |
|
5 | 5 | |
|
6 | 6 | #include <Common/DateUtils.h> |
|
7 | 7 | #include <Data/DataProviderParameters.h> |
|
8 | 8 | #include <Network/NetworkController.h> |
|
9 | 9 | #include <SqpApplication.h> |
|
10 | 10 | #include <Variable/Variable.h> |
|
11 | 11 | |
|
12 | 12 | #include <QNetworkAccessManager> |
|
13 | 13 | #include <QNetworkReply> |
|
14 | 14 | #include <QTemporaryFile> |
|
15 | 15 | #include <QThread> |
|
16 | 16 | |
|
17 | 17 | Q_LOGGING_CATEGORY(LOG_AmdaProvider, "AmdaProvider") |
|
18 | 18 | |
|
19 | 19 | namespace { |
|
20 | 20 | |
|
21 | 21 | /// URL format for a request on AMDA server. The parameters are as follows: |
|
22 | 22 | /// - %1: server URL |
|
23 | 23 | /// - %2: start date |
|
24 | 24 | /// - %3: end date |
|
25 | 25 | /// - %4: parameter id |
|
26 | 26 | /// AMDA V2: http://amdatest.irap.omp.eu/php/rest/ |
|
27 | 27 | const auto AMDA_URL_FORMAT = QStringLiteral( |
|
28 | 28 | "http://%1/php/rest/" |
|
29 | 29 | "getParameter.php?startTime=%2&stopTime=%3¶meterID=%4&outputFormat=ASCII&" |
|
30 | 30 | "timeFormat=ISO8601&gzip=0"); |
|
31 | 31 | |
|
32 | 32 | /// Dates format passed in the URL (e.g 2013-09-23T09:00) |
|
33 | 33 | const auto AMDA_TIME_FORMAT = QStringLiteral("yyyy-MM-ddThh:mm:ss"); |
|
34 | 34 | |
|
35 | 35 | /// Formats a time to a date that can be passed in URL |
|
36 | 36 | QString dateFormat(double sqpRange) noexcept |
|
37 | 37 | { |
|
38 | 38 | auto dateTime = DateUtils::dateTime(sqpRange); |
|
39 | 39 | return dateTime.toString(AMDA_TIME_FORMAT); |
|
40 | 40 | } |
|
41 | 41 | |
|
42 | 42 | |
|
43 | 43 | } // namespace |
|
44 | 44 | |
|
45 | 45 | AmdaProvider::AmdaProvider() |
|
46 | 46 | { |
|
47 | 47 | qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::AmdaProvider") << QThread::currentThread(); |
|
48 | 48 | if (auto app = sqpApp) { |
|
49 | 49 | auto &networkController = app->networkController(); |
|
50 | 50 | connect(this, SIGNAL(requestConstructed(std::shared_ptr<QNetworkRequest>, QUuid, |
|
51 | 51 | std::function<void(QNetworkReply *, QUuid)>)), |
|
52 | 52 | &networkController, |
|
53 | 53 | SLOT(onProcessRequested(std::shared_ptr<QNetworkRequest>, QUuid, |
|
54 | 54 | std::function<void(QNetworkReply *, QUuid)>))); |
|
55 | 55 | |
|
56 | 56 | |
|
57 | 57 | connect(&sqpApp->networkController(), |
|
58 | 58 | SIGNAL(replyDownloadProgress(QUuid, std::shared_ptr<QNetworkRequest>, double)), |
|
59 | 59 | this, |
|
60 | 60 | SLOT(onReplyDownloadProgress(QUuid, std::shared_ptr<QNetworkRequest>, double))); |
|
61 | 61 | } |
|
62 | 62 | } |
|
63 | 63 | |
|
64 | 64 | std::shared_ptr<IDataProvider> AmdaProvider::clone() const |
|
65 | 65 | { |
|
66 | 66 | // No copy is made in the clone |
|
67 | 67 | return std::make_shared<AmdaProvider>(); |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | void AmdaProvider::requestDataLoading(QUuid acqIdentifier, const DataProviderParameters ¶meters) |
|
71 | 71 | { |
|
72 | 72 | // NOTE: Try to use multithread if possible |
|
73 | 73 | const auto times = parameters.m_Times; |
|
74 | 74 | const auto data = parameters.m_Data; |
|
75 | 75 | for (const auto &dateTime : qAsConst(times)) { |
|
76 | 76 | qCDebug(LOG_AmdaProvider()) << tr("TORM AmdaProvider::requestDataLoading ") << acqIdentifier |
|
77 | 77 | << dateTime; |
|
78 | 78 | this->retrieveData(acqIdentifier, dateTime, data); |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | // TORM when AMDA will support quick asynchrone request |
|
82 | 82 | QThread::msleep(1000); |
|
83 | 83 | } |
|
84 | 84 | } |
|
85 | 85 | |
|
86 | 86 | void AmdaProvider::requestDataAborting(QUuid acqIdentifier) |
|
87 | 87 | { |
|
88 | 88 | if (auto app = sqpApp) { |
|
89 | 89 | auto &networkController = app->networkController(); |
|
90 | 90 | networkController.onReplyCanceled(acqIdentifier); |
|
91 | 91 | } |
|
92 | 92 | } |
|
93 | 93 | |
|
94 | 94 | void AmdaProvider::onReplyDownloadProgress(QUuid acqIdentifier, |
|
95 | 95 | std::shared_ptr<QNetworkRequest> networkRequest, |
|
96 | 96 | double progress) |
|
97 | 97 | { |
|
98 | 98 | qCDebug(LOG_AmdaProvider()) << tr("onReplyDownloadProgress") << acqIdentifier |
|
99 | 99 | << networkRequest.get() << progress; |
|
100 | 100 | auto acqIdToRequestProgressMapIt = m_AcqIdToRequestProgressMap.find(acqIdentifier); |
|
101 | 101 | if (acqIdToRequestProgressMapIt != m_AcqIdToRequestProgressMap.end()) { |
|
102 | 102 | |
|
103 | 103 | // Update the progression for the current request |
|
104 | 104 | auto requestPtr = networkRequest; |
|
105 | 105 | auto findRequest = [requestPtr](const auto &entry) { return requestPtr == entry.first; }; |
|
106 | 106 | |
|
107 | 107 | auto &requestProgressMap = acqIdToRequestProgressMapIt->second; |
|
108 | 108 | auto requestProgressMapEnd = requestProgressMap.end(); |
|
109 | 109 | auto requestProgressMapIt |
|
110 | 110 | = std::find_if(requestProgressMap.begin(), requestProgressMapEnd, findRequest); |
|
111 | 111 | |
|
112 | 112 | if (requestProgressMapIt != requestProgressMapEnd) { |
|
113 | 113 | requestProgressMapIt->second = progress; |
|
114 | 114 | } |
|
115 | 115 | else { |
|
116 | 116 | // This case can happened when a progression is send after the request has been |
|
117 | 117 | // finished. |
|
118 | 118 | // Generaly the case when aborting a request |
|
119 | 119 | qCDebug(LOG_AmdaProvider()) << tr("Can't retrieve Request in progress") << acqIdentifier |
|
120 | 120 | << networkRequest.get() << progress; |
|
121 | 121 | } |
|
122 | 122 | |
|
123 | 123 | // Compute the current final progress and notify it |
|
124 | 124 | double finalProgress = 0.0; |
|
125 | 125 | |
|
126 | 126 | auto fraq = requestProgressMap.size(); |
|
127 | 127 | |
|
128 | 128 | for (auto requestProgress : requestProgressMap) { |
|
129 | 129 | finalProgress += requestProgress.second; |
|
130 | 130 | qCDebug(LOG_AmdaProvider()) << tr("Current final progress without fraq:") |
|
131 | 131 | << finalProgress << requestProgress.second; |
|
132 | 132 | } |
|
133 | 133 | |
|
134 | 134 | if (fraq > 0) { |
|
135 | 135 | finalProgress = finalProgress / fraq; |
|
136 | 136 | } |
|
137 | 137 | |
|
138 |
qCDebug(LOG_AmdaProvider()) << tr(" |
|
|
138 | qCDebug(LOG_AmdaProvider()) << tr("final progress: ") | |
|
139 | << QThread::currentThread()->objectName() << acqIdentifier | |
|
140 | << fraq << finalProgress; | |
|
139 | 141 | emit dataProvidedProgress(acqIdentifier, finalProgress); |
|
140 | 142 | } |
|
141 | 143 | else { |
|
142 | 144 | // This case can happened when a progression is send after the request has been finished. |
|
143 | 145 | // Generaly the case when aborting a request |
|
146 | qCDebug(LOG_AmdaProvider()) << tr("Acquisition request not found: final progress: 100 : ") | |
|
147 | << QThread::currentThread()->objectName() << acqIdentifier; | |
|
144 | 148 | emit dataProvidedProgress(acqIdentifier, 100.0); |
|
145 | 149 | } |
|
146 | 150 | } |
|
147 | 151 | |
|
148 | 152 | void AmdaProvider::retrieveData(QUuid token, const SqpRange &dateTime, const QVariantHash &data) |
|
149 | 153 | { |
|
150 | 154 | // Retrieves product ID from data: if the value is invalid, no request is made |
|
151 | 155 | auto productId = data.value(AMDA_XML_ID_KEY).toString(); |
|
152 | 156 | if (productId.isNull()) { |
|
153 | 157 | qCCritical(LOG_AmdaProvider()) << tr("Can't retrieve data: unknown product id"); |
|
154 | 158 | return; |
|
155 | 159 | } |
|
156 | 160 | |
|
157 | 161 | // Retrieves the data type that determines whether the expected format for the result file is |
|
158 | 162 | // scalar, vector... |
|
159 | 163 | auto productValueType |
|
160 | 164 | = DataSeriesTypeUtils::fromString(data.value(AMDA_DATA_TYPE_KEY).toString()); |
|
161 | 165 | |
|
162 | 166 | // /////////// // |
|
163 | 167 | // Creates URL // |
|
164 | 168 | // /////////// // |
|
165 | 169 | |
|
166 | 170 | auto startDate = dateFormat(dateTime.m_TStart); |
|
167 | 171 | auto endDate = dateFormat(dateTime.m_TEnd); |
|
168 | 172 | |
|
169 | 173 | QVariantHash urlProperties{{AMDA_SERVER_KEY, data.value(AMDA_SERVER_KEY)}}; |
|
170 | 174 | auto url = QUrl{QString{AMDA_URL_FORMAT}.arg(AmdaServer::instance().url(urlProperties), |
|
171 | 175 | startDate, endDate, productId)}; |
|
172 | qCInfo(LOG_AmdaProvider()) << tr("TORM AmdaProvider::retrieveData url:") << url; | |
|
173 | 176 | auto tempFile = std::make_shared<QTemporaryFile>(); |
|
174 | 177 | |
|
175 | 178 | // LAMBDA |
|
176 | 179 | auto httpDownloadFinished = [this, dateTime, tempFile, |
|
177 | 180 | productValueType](QNetworkReply *reply, QUuid dataId) noexcept { |
|
178 | 181 | |
|
179 | 182 | // Don't do anything if the reply was abort |
|
180 | 183 | if (reply->error() == QNetworkReply::NoError) { |
|
181 | 184 | |
|
182 | 185 | if (tempFile) { |
|
183 | 186 | auto replyReadAll = reply->readAll(); |
|
184 | 187 | if (!replyReadAll.isEmpty()) { |
|
185 | 188 | tempFile->write(replyReadAll); |
|
186 | 189 | } |
|
187 | 190 | tempFile->close(); |
|
188 | 191 | |
|
189 | 192 | // Parse results file |
|
190 | 193 | if (auto dataSeries |
|
191 | 194 | = AmdaResultParser::readTxt(tempFile->fileName(), productValueType)) { |
|
192 | 195 | emit dataProvided(dataId, dataSeries, dateTime); |
|
193 | 196 | } |
|
194 | 197 | else { |
|
195 | 198 | /// @todo ALX : debug |
|
196 | 199 | emit dataProvidedFailed(dataId); |
|
197 | 200 | } |
|
198 | 201 | } |
|
199 | 202 | m_AcqIdToRequestProgressMap.erase(dataId); |
|
200 | 203 | } |
|
201 | 204 | else { |
|
202 | 205 | qCCritical(LOG_AmdaProvider()) << tr("httpDownloadFinished ERROR"); |
|
203 | 206 | emit dataProvidedFailed(dataId); |
|
204 | 207 | } |
|
205 | 208 | |
|
206 | 209 | }; |
|
207 | 210 | auto httpFinishedLambda |
|
208 | 211 | = [this, httpDownloadFinished, tempFile](QNetworkReply *reply, QUuid dataId) noexcept { |
|
209 | 212 | |
|
210 | 213 | // Don't do anything if the reply was abort |
|
211 | 214 | if (reply->error() == QNetworkReply::NoError) { |
|
212 | 215 | auto downloadFileUrl = QUrl{QString{reply->readAll()}.trimmed()}; |
|
213 | 216 | |
|
214 | qCInfo(LOG_AmdaProvider()) | |
|
215 | << tr("TORM AmdaProvider::retrieveData downloadFileUrl:") << downloadFileUrl; | |
|
217 | qCDebug(LOG_AmdaProvider()) << tr("AmdaProvider::retrieveData downloadFileUrl:") | |
|
218 | << downloadFileUrl; | |
|
216 | 219 | // Executes request for downloading file // |
|
217 | 220 | |
|
218 | 221 | // Creates destination file |
|
219 | 222 | if (tempFile->open()) { |
|
220 | 223 | // Executes request and store the request for progression |
|
221 | 224 | auto request = std::make_shared<QNetworkRequest>(downloadFileUrl); |
|
222 | 225 | updateRequestProgress(dataId, request, 0.0); |
|
223 | 226 | emit requestConstructed(request, dataId, httpDownloadFinished); |
|
224 | 227 | } |
|
225 | 228 | else { |
|
226 | 229 | emit dataProvidedFailed(dataId); |
|
227 | 230 | } |
|
228 | 231 | } |
|
229 | 232 | else { |
|
230 | 233 | qCCritical(LOG_AmdaProvider()) << tr("httpFinishedLambda ERROR"); |
|
231 | 234 | m_AcqIdToRequestProgressMap.erase(dataId); |
|
232 | 235 | emit dataProvidedFailed(dataId); |
|
233 | 236 | } |
|
234 | 237 | }; |
|
235 | 238 | |
|
236 | 239 | // //////////////// // |
|
237 | 240 | // Executes request // |
|
238 | 241 | // //////////////// // |
|
239 | 242 | |
|
240 | 243 | auto request = std::make_shared<QNetworkRequest>(url); |
|
241 | qCDebug(LOG_AmdaProvider()) << tr("First Request creation") << request.get(); | |
|
242 | 244 | updateRequestProgress(token, request, 0.0); |
|
243 | 245 | |
|
244 | 246 | emit requestConstructed(request, token, httpFinishedLambda); |
|
245 | 247 | } |
|
246 | 248 | |
|
247 | 249 | void AmdaProvider::updateRequestProgress(QUuid acqIdentifier, |
|
248 | 250 | std::shared_ptr<QNetworkRequest> request, double progress) |
|
249 | 251 | { |
|
250 | qCDebug(LOG_AmdaProvider()) << tr("updateRequestProgress request") << request.get(); | |
|
251 | 252 | auto acqIdToRequestProgressMapIt = m_AcqIdToRequestProgressMap.find(acqIdentifier); |
|
252 | 253 | if (acqIdToRequestProgressMapIt != m_AcqIdToRequestProgressMap.end()) { |
|
253 | 254 | auto &requestProgressMap = acqIdToRequestProgressMapIt->second; |
|
254 | 255 | auto requestProgressMapIt = requestProgressMap.find(request); |
|
255 | 256 | if (requestProgressMapIt != requestProgressMap.end()) { |
|
256 | 257 | requestProgressMapIt->second = progress; |
|
257 | 258 | qCDebug(LOG_AmdaProvider()) << tr("updateRequestProgress new progress for request") |
|
258 | 259 | << acqIdentifier << request.get() << progress; |
|
259 | 260 | } |
|
260 | 261 | else { |
|
261 | 262 | qCDebug(LOG_AmdaProvider()) << tr("updateRequestProgress new request") << acqIdentifier |
|
262 | 263 | << request.get() << progress; |
|
263 | 264 | acqIdToRequestProgressMapIt->second.insert(std::make_pair(request, progress)); |
|
264 | 265 | } |
|
265 | 266 | } |
|
266 | 267 | else { |
|
267 | 268 | qCDebug(LOG_AmdaProvider()) << tr("updateRequestProgress new acqIdentifier") |
|
268 | 269 | << acqIdentifier << request.get() << progress; |
|
269 | 270 | auto requestProgressMap = std::map<std::shared_ptr<QNetworkRequest>, double>{}; |
|
270 | 271 | requestProgressMap.insert(std::make_pair(request, progress)); |
|
271 | 272 | m_AcqIdToRequestProgressMap.insert( |
|
272 | 273 | std::make_pair(acqIdentifier, std::move(requestProgressMap))); |
|
273 | 274 | } |
|
274 | 275 | } |
@@ -1,268 +1,268 | |||
|
1 | 1 | #include "FuzzingOperations.h" |
|
2 | 2 | #include "FuzzingUtils.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | |
|
6 | 6 | #include <Variable/Variable.h> |
|
7 | 7 | #include <Variable/VariableController.h> |
|
8 | 8 | |
|
9 | 9 | #include <QUuid> |
|
10 | 10 | |
|
11 | 11 | #include <functional> |
|
12 | 12 | |
|
13 | 13 | Q_LOGGING_CATEGORY(LOG_FuzzingOperations, "FuzzingOperations") |
|
14 | 14 | |
|
15 | 15 | namespace { |
|
16 | 16 | |
|
17 | 17 | struct CreateOperation : public IFuzzingOperation { |
|
18 | 18 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
19 | 19 | { |
|
20 | 20 | // A variable can be created only if it doesn't exist yet |
|
21 | 21 | return fuzzingState.variableState(variableId).m_Variable == nullptr; |
|
22 | 22 | } |
|
23 | 23 | |
|
24 | 24 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
25 | 25 | VariableController &variableController, |
|
26 | 26 | const Properties &properties) const override |
|
27 | 27 | { |
|
28 | 28 | // Retrieves metadata pool from properties, and choose one of the metadata entries to |
|
29 | 29 | // associate it with the variable |
|
30 | 30 | auto metaDataPool = properties.value(METADATA_POOL_PROPERTY).value<MetadataPool>(); |
|
31 | 31 | auto variableMetadata = RandomGenerator::instance().randomChoice(metaDataPool); |
|
32 | 32 | |
|
33 | 33 | // Retrieves provider |
|
34 | 34 | auto variableProvider |
|
35 | 35 | = properties.value(PROVIDER_PROPERTY).value<std::shared_ptr<IDataProvider> >(); |
|
36 | 36 | |
|
37 | 37 | auto variableName = QString{"Var_%1"}.arg(QUuid::createUuid().toString()); |
|
38 | 38 | qCInfo(LOG_FuzzingOperations()).noquote() << "Creating variable" << variableName |
|
39 | 39 | << "(metadata:" << variableMetadata << ")..."; |
|
40 | 40 | |
|
41 | 41 | auto newVariable |
|
42 | 42 | = variableController.createVariable(variableName, variableMetadata, variableProvider); |
|
43 | 43 | |
|
44 | 44 | // Updates variable's state |
|
45 | 45 | auto &variableState = fuzzingState.variableState(variableId); |
|
46 | 46 | variableState.m_Range = properties.value(INITIAL_RANGE_PROPERTY).value<SqpRange>(); |
|
47 | 47 | std::swap(variableState.m_Variable, newVariable); |
|
48 | 48 | } |
|
49 | 49 | }; |
|
50 | 50 | |
|
51 | 51 | struct DeleteOperation : public IFuzzingOperation { |
|
52 | 52 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
53 | 53 | { |
|
54 | 54 | // A variable can be delete only if it exists |
|
55 | 55 | return fuzzingState.variableState(variableId).m_Variable != nullptr; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
59 | 59 | VariableController &variableController, const Properties &) const override |
|
60 | 60 | { |
|
61 | 61 | auto &variableState = fuzzingState.variableState(variableId); |
|
62 | 62 | |
|
63 | 63 | qCInfo(LOG_FuzzingOperations()).noquote() << "Deleting variable" |
|
64 | 64 | << variableState.m_Variable->name() << "..."; |
|
65 | 65 | variableController.deleteVariable(variableState.m_Variable); |
|
66 | 66 | |
|
67 | 67 | // Updates variable's state |
|
68 | 68 | variableState.m_Range = INVALID_RANGE; |
|
69 | 69 | variableState.m_Variable = nullptr; |
|
70 | 70 | |
|
71 | 71 | // Desynchronizes the variable if it was in a sync group |
|
72 | 72 | auto syncGroupId = fuzzingState.syncGroupId(variableId); |
|
73 | 73 | fuzzingState.desynchronizeVariable(variableId, syncGroupId); |
|
74 | 74 | } |
|
75 | 75 | }; |
|
76 | 76 | |
|
77 | 77 | /** |
|
78 | 78 | * Defines a move operation through a range. |
|
79 | 79 | * |
|
80 | 80 | * A move operation is determined by three functions: |
|
81 | 81 | * - Two 'move' functions, used to indicate in which direction the beginning and the end of a range |
|
82 | 82 | * are going during the operation. These functions will be: |
|
83 | 83 | * -- {<- / <-} for pan left |
|
84 | 84 | * -- {-> / ->} for pan right |
|
85 | 85 | * -- {-> / <-} for zoom in |
|
86 | 86 | * -- {<- / ->} for zoom out |
|
87 | 87 | * - One 'max move' functions, used to compute the max delta at which the operation can move a |
|
88 | 88 | * range, according to a max range. For exemple, for a range of {1, 5} and a max range of {0, 10}, |
|
89 | 89 | * max deltas will be: |
|
90 | 90 | * -- {0, 4} for pan left |
|
91 | 91 | * -- {6, 10} for pan right |
|
92 | 92 | * -- {3, 3} for zoom in |
|
93 | 93 | * -- {0, 6} for zoom out (same spacing left and right) |
|
94 | 94 | */ |
|
95 | 95 | struct MoveOperation : public IFuzzingOperation { |
|
96 | 96 | using MoveFunction = std::function<double(double currentValue, double maxValue)>; |
|
97 | 97 | using MaxMoveFunction = std::function<double(const SqpRange &range, const SqpRange &maxRange)>; |
|
98 | 98 | |
|
99 | 99 | explicit MoveOperation(MoveFunction rangeStartMoveFun, MoveFunction rangeEndMoveFun, |
|
100 | 100 | MaxMoveFunction maxMoveFun, |
|
101 | 101 | const QString &label = QStringLiteral("Move operation")) |
|
102 | 102 | : m_RangeStartMoveFun{std::move(rangeStartMoveFun)}, |
|
103 | 103 | m_RangeEndMoveFun{std::move(rangeEndMoveFun)}, |
|
104 | 104 | m_MaxMoveFun{std::move(maxMoveFun)}, |
|
105 | 105 | m_Label{label} |
|
106 | 106 | { |
|
107 | 107 | } |
|
108 | 108 | |
|
109 | 109 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
110 | 110 | { |
|
111 | 111 | return fuzzingState.variableState(variableId).m_Variable != nullptr; |
|
112 | 112 | } |
|
113 | 113 | |
|
114 | 114 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
115 | 115 | VariableController &variableController, |
|
116 | 116 | const Properties &properties) const override |
|
117 | 117 | { |
|
118 | 118 | auto &variableState = fuzzingState.variableState(variableId); |
|
119 | 119 | auto variable = variableState.m_Variable; |
|
120 | 120 | |
|
121 | 121 | // Gets the max range defined |
|
122 | 122 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
123 | 123 | .value<SqpRange>(); |
|
124 | 124 | auto variableRange = variableState.m_Range; |
|
125 | 125 | |
|
126 | 126 | if (maxRange == INVALID_RANGE || variableRange.m_TStart < maxRange.m_TStart |
|
127 | 127 | || variableRange.m_TEnd > maxRange.m_TEnd) { |
|
128 | 128 | qCWarning(LOG_FuzzingOperations()) << "Can't execute operation: invalid max range"; |
|
129 | 129 | return; |
|
130 | 130 | } |
|
131 | 131 | |
|
132 | 132 | // Computes the max delta at which the variable can move, up to the limits of the max range |
|
133 | 133 | auto deltaMax = m_MaxMoveFun(variableRange, maxRange); |
|
134 | 134 | |
|
135 | 135 | // Generates random delta that will be used to move variable |
|
136 | 136 | auto delta = RandomGenerator::instance().generateDouble(0, deltaMax); |
|
137 | 137 | |
|
138 | 138 | // Moves variable to its new range |
|
139 | 139 | auto isSynchronized = !fuzzingState.syncGroupId(variableId).isNull(); |
|
140 | 140 | auto newVariableRange = SqpRange{m_RangeStartMoveFun(variableRange.m_TStart, delta), |
|
141 | 141 | m_RangeEndMoveFun(variableRange.m_TEnd, delta)}; |
|
142 | 142 | qCInfo(LOG_FuzzingOperations()).noquote() << "Performing" << m_Label << "on" |
|
143 | 143 | << variable->name() << "(from" << variableRange |
|
144 | 144 | << "to" << newVariableRange << ")..."; |
|
145 | 145 | variableController.onRequestDataLoading({variable}, newVariableRange, isSynchronized); |
|
146 | 146 | |
|
147 | 147 | // Updates state |
|
148 | 148 | fuzzingState.updateRanges(variableId, newVariableRange); |
|
149 | 149 | } |
|
150 | 150 | |
|
151 | 151 | MoveFunction m_RangeStartMoveFun; |
|
152 | 152 | MoveFunction m_RangeEndMoveFun; |
|
153 | 153 | MaxMoveFunction m_MaxMoveFun; |
|
154 | 154 | QString m_Label; |
|
155 | 155 | }; |
|
156 | 156 | |
|
157 | 157 | struct SynchronizeOperation : public IFuzzingOperation { |
|
158 | 158 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
159 | 159 | { |
|
160 | 160 | auto variable = fuzzingState.variableState(variableId).m_Variable; |
|
161 | 161 | return variable != nullptr && !fuzzingState.m_SyncGroupsPool.empty() |
|
162 | 162 | && fuzzingState.syncGroupId(variableId).isNull(); |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
166 | 166 | VariableController &variableController, const Properties &) const override |
|
167 | 167 | { |
|
168 | 168 | auto &variableState = fuzzingState.variableState(variableId); |
|
169 | 169 | |
|
170 | 170 | // Chooses a random synchronization group and adds the variable into sync group |
|
171 | 171 | auto syncGroupId = RandomGenerator::instance().randomChoice(fuzzingState.syncGroupsIds()); |
|
172 | 172 | qCInfo(LOG_FuzzingOperations()).noquote() << "Adding" << variableState.m_Variable->name() |
|
173 | 173 | << "into synchronization group" << syncGroupId |
|
174 | 174 | << "..."; |
|
175 | 175 | variableController.onAddSynchronized(variableState.m_Variable, syncGroupId); |
|
176 | 176 | |
|
177 | 177 | // Updates state |
|
178 | 178 | fuzzingState.synchronizeVariable(variableId, syncGroupId); |
|
179 | 179 | |
|
180 | 180 | variableController.onRequestDataLoading({variableState.m_Variable}, variableState.m_Range, |
|
181 |
|
|
|
181 | true); | |
|
182 | 182 | } |
|
183 | 183 | }; |
|
184 | 184 | |
|
185 | 185 | struct DesynchronizeOperation : public IFuzzingOperation { |
|
186 | 186 | bool canExecute(VariableId variableId, const FuzzingState &fuzzingState) const override |
|
187 | 187 | { |
|
188 | 188 | auto variable = fuzzingState.variableState(variableId).m_Variable; |
|
189 | 189 | return variable != nullptr && !fuzzingState.syncGroupId(variableId).isNull(); |
|
190 | 190 | } |
|
191 | 191 | |
|
192 | 192 | void execute(VariableId variableId, FuzzingState &fuzzingState, |
|
193 | 193 | VariableController &variableController, const Properties &) const override |
|
194 | 194 | { |
|
195 | 195 | auto &variableState = fuzzingState.variableState(variableId); |
|
196 | 196 | |
|
197 | 197 | // Gets the sync group of the variable |
|
198 | 198 | auto syncGroupId = fuzzingState.syncGroupId(variableId); |
|
199 | 199 | |
|
200 | 200 | qCInfo(LOG_FuzzingOperations()).noquote() << "Removing" << variableState.m_Variable->name() |
|
201 | 201 | << "from synchronization group" << syncGroupId |
|
202 | 202 | << "..."; |
|
203 | 203 | variableController.desynchronize(variableState.m_Variable, syncGroupId); |
|
204 | 204 | |
|
205 | 205 | // Updates state |
|
206 | 206 | fuzzingState.desynchronizeVariable(variableId, syncGroupId); |
|
207 | 207 | } |
|
208 | 208 | }; |
|
209 | 209 | |
|
210 | 210 | struct UnknownOperation : public IFuzzingOperation { |
|
211 | 211 | bool canExecute(VariableId, const FuzzingState &) const override { return false; } |
|
212 | 212 | |
|
213 | 213 | void execute(VariableId, FuzzingState &, VariableController &, |
|
214 | 214 | const Properties &) const override |
|
215 | 215 | { |
|
216 | 216 | } |
|
217 | 217 | }; |
|
218 | 218 | |
|
219 | 219 | } // namespace |
|
220 | 220 | |
|
221 | 221 | std::unique_ptr<IFuzzingOperation> FuzzingOperationFactory::create(FuzzingOperationType type) |
|
222 | 222 | { |
|
223 | 223 | switch (type) { |
|
224 | 224 | case FuzzingOperationType::CREATE: |
|
225 | 225 | return std::make_unique<CreateOperation>(); |
|
226 | 226 | case FuzzingOperationType::DELETE: |
|
227 | 227 | return std::make_unique<DeleteOperation>(); |
|
228 | 228 | case FuzzingOperationType::PAN_LEFT: |
|
229 | 229 | return std::make_unique<MoveOperation>( |
|
230 | 230 | std::minus<double>(), std::minus<double>(), |
|
231 | 231 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
232 | 232 | return range.m_TStart - maxRange.m_TStart; |
|
233 | 233 | }, |
|
234 | 234 | QStringLiteral("Pan left operation")); |
|
235 | 235 | case FuzzingOperationType::PAN_RIGHT: |
|
236 | 236 | return std::make_unique<MoveOperation>( |
|
237 | 237 | std::plus<double>(), std::plus<double>(), |
|
238 | 238 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
239 | 239 | return maxRange.m_TEnd - range.m_TEnd; |
|
240 | 240 | }, |
|
241 | 241 | QStringLiteral("Pan right operation")); |
|
242 | 242 | case FuzzingOperationType::ZOOM_IN: |
|
243 | 243 | return std::make_unique<MoveOperation>( |
|
244 | 244 | std::plus<double>(), std::minus<double>(), |
|
245 | 245 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
246 | 246 | Q_UNUSED(maxRange) |
|
247 | 247 | return range.m_TEnd - (range.m_TStart + range.m_TEnd) / 2.; |
|
248 | 248 | }, |
|
249 | 249 | QStringLiteral("Zoom in operation")); |
|
250 | 250 | case FuzzingOperationType::ZOOM_OUT: |
|
251 | 251 | return std::make_unique<MoveOperation>( |
|
252 | 252 | std::minus<double>(), std::plus<double>(), |
|
253 | 253 | [](const SqpRange &range, const SqpRange &maxRange) { |
|
254 | 254 | return std::min(range.m_TStart - maxRange.m_TStart, |
|
255 | 255 | maxRange.m_TEnd - range.m_TEnd); |
|
256 | 256 | }, |
|
257 | 257 | QStringLiteral("Zoom out operation")); |
|
258 | 258 | case FuzzingOperationType::SYNCHRONIZE: |
|
259 | 259 | return std::make_unique<SynchronizeOperation>(); |
|
260 | 260 | case FuzzingOperationType::DESYNCHRONIZE: |
|
261 | 261 | return std::make_unique<DesynchronizeOperation>(); |
|
262 | 262 | default: |
|
263 | 263 | // Default case returns unknown operation |
|
264 | 264 | break; |
|
265 | 265 | } |
|
266 | 266 | |
|
267 | 267 | return std::make_unique<UnknownOperation>(); |
|
268 | 268 | } |
@@ -1,558 +1,560 | |||
|
1 | 1 | #include "FuzzingDefs.h" |
|
2 | 2 | #include "FuzzingOperations.h" |
|
3 | 3 | #include "FuzzingUtils.h" |
|
4 | 4 | #include "FuzzingValidators.h" |
|
5 | 5 | |
|
6 | 6 | #include "AmdaProvider.h" |
|
7 | 7 | |
|
8 | 8 | #include <Common/SignalWaiter.h> |
|
9 | 9 | #include <Network/NetworkController.h> |
|
10 | 10 | #include <Settings/SqpSettingsDefs.h> |
|
11 | 11 | #include <SqpApplication.h> |
|
12 | 12 | #include <Time/TimeController.h> |
|
13 | 13 | #include <Variable/Variable.h> |
|
14 | 14 | #include <Variable/VariableController.h> |
|
15 | 15 | |
|
16 | 16 | #include <QLoggingCategory> |
|
17 | 17 | #include <QObject> |
|
18 | 18 | #include <QtTest> |
|
19 | 19 | |
|
20 | 20 | #include <memory> |
|
21 | 21 | |
|
22 | 22 | Q_LOGGING_CATEGORY(LOG_TestAmdaFuzzing, "TestAmdaFuzzing") |
|
23 | 23 | |
|
24 | 24 | /** |
|
25 | 25 | * Macro used to generate a getter for a property in @sa FuzzingTest. The macro generates a static |
|
26 | 26 | * attribute that is initialized by searching in properties the property and use a default value if |
|
27 | 27 | * it's not present. Macro arguments are: |
|
28 | 28 | * - GETTER_NAME : name of the getter |
|
29 | 29 | * - PROPERTY_NAME: used to generate constants for property's name ({PROPERTY_NAME}_PROPERTY) and |
|
30 | 30 | * default value ({PROPERTY_NAME}_DEFAULT_VALUE) |
|
31 | 31 | * - TYPE : return type of the getter |
|
32 | 32 | */ |
|
33 | 33 | // clang-format off |
|
34 | 34 | #define DECLARE_PROPERTY_GETTER(GETTER_NAME, PROPERTY_NAME, TYPE) \ |
|
35 | 35 | TYPE GETTER_NAME() const \ |
|
36 | 36 | { \ |
|
37 | 37 | static auto result = m_Properties.value(PROPERTY_NAME##_PROPERTY, PROPERTY_NAME##_DEFAULT_VALUE).value<TYPE>(); \ |
|
38 | 38 | return result; \ |
|
39 | 39 | } \ |
|
40 | 40 | // clang-format on |
|
41 | 41 | |
|
42 | 42 | namespace { |
|
43 | 43 | |
|
44 | 44 | // /////// // |
|
45 | 45 | // Aliases // |
|
46 | 46 | // /////// // |
|
47 | 47 | |
|
48 | 48 | using IntPair = std::pair<int, int>; |
|
49 | 49 | using Weight = double; |
|
50 | 50 | using Weights = std::vector<Weight>; |
|
51 | 51 | |
|
52 | 52 | struct OperationProperty { |
|
53 | 53 | Weight m_Weight{1.}; |
|
54 | 54 | bool m_WaitAcquisition{false}; |
|
55 | 55 | }; |
|
56 | 56 | |
|
57 | 57 | using VariableOperation = std::pair<VariableId, std::shared_ptr<IFuzzingOperation> >; |
|
58 | 58 | using VariablesOperations = std::vector<VariableOperation>; |
|
59 | 59 | |
|
60 | 60 | struct CustomVariableOperation { |
|
61 | 61 | VariableOperation m_VariableOperation{}; |
|
62 | 62 | bool m_WaitAcquisition{false}; |
|
63 | 63 | }; |
|
64 | 64 | using CustomVariablesOperations = std::vector<CustomVariableOperation>; |
|
65 | 65 | |
|
66 | 66 | using OperationsTypes = std::map<FuzzingOperationType, OperationProperty>; |
|
67 | 67 | using OperationsPool = std::map<std::shared_ptr<IFuzzingOperation>, OperationProperty>; |
|
68 | 68 | |
|
69 | 69 | using Validators = std::vector<std::shared_ptr<IFuzzingValidator> >; |
|
70 | 70 | |
|
71 | 71 | // ///////// // |
|
72 | 72 | // Constants // |
|
73 | 73 | // ///////// // |
|
74 | 74 | |
|
75 | 75 | // Defaults values used when the associated properties have not been set for the test |
|
76 |
const auto ACQUISITION_TIMEOUT_DEFAULT_VALUE = |
|
|
77 |
const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 1 |
|
|
78 |
const auto NB_MAX_SYNC_GROUPS_DEFAULT_VALUE = |
|
|
79 |
const auto NB_MAX_VARIABLES_DEFAULT_VALUE = |
|
|
76 | const auto ACQUISITION_TIMEOUT_DEFAULT_VALUE = 100000; | |
|
77 | const auto NB_MAX_OPERATIONS_DEFAULT_VALUE = 160; | |
|
78 | const auto NB_MAX_SYNC_GROUPS_DEFAULT_VALUE = 8; | |
|
79 | const auto NB_MAX_VARIABLES_DEFAULT_VALUE = 30; | |
|
80 | 80 | const auto AVAILABLE_OPERATIONS_DEFAULT_VALUE = QVariant::fromValue( |
|
81 | OperationsTypes{{FuzzingOperationType::CREATE, {400., true}}, | |
|
81 | OperationsTypes{{FuzzingOperationType::CREATE, {40000., true}}, | |
|
82 | 82 | {FuzzingOperationType::DELETE, {0.0}}, // Delete operation is less frequent |
|
83 | 83 | {FuzzingOperationType::PAN_LEFT, {1.}}, |
|
84 | 84 | {FuzzingOperationType::PAN_RIGHT, {1.}}, |
|
85 |
{FuzzingOperationType::ZOOM_IN, { |
|
|
86 |
{FuzzingOperationType::ZOOM_OUT, { |
|
|
87 |
{FuzzingOperationType::SYNCHRONIZE, { |
|
|
88 |
{FuzzingOperationType::DESYNCHRONIZE, {0. |
|
|
85 | {FuzzingOperationType::ZOOM_IN, {1.}}, | |
|
86 | {FuzzingOperationType::ZOOM_OUT, {1.}}, | |
|
87 | {FuzzingOperationType::SYNCHRONIZE, {500.0}}, | |
|
88 | {FuzzingOperationType::DESYNCHRONIZE, {0.0}}}); | |
|
89 | 89 | const auto CACHE_TOLERANCE_DEFAULT_VALUE = 0.2; |
|
90 | 90 | |
|
91 | 91 | /// Min/max delays between each operation (in ms) |
|
92 |
const auto OPERATION_DELAY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(20, |
|
|
92 | const auto OPERATION_DELAY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(20, 3000)); | |
|
93 | 93 | |
|
94 | 94 | /// Validators for the tests (executed in the order in which they're defined) |
|
95 | 95 | const auto VALIDATORS_DEFAULT_VALUE = QVariant::fromValue( |
|
96 | 96 | ValidatorsTypes{{FuzzingValidatorType::RANGE, FuzzingValidatorType::DATA}}); |
|
97 | 97 | |
|
98 | 98 | /// Min/max number of operations to execute before calling validation |
|
99 |
const auto VALIDATION_FREQUENCY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(2, |
|
|
99 | const auto VALIDATION_FREQUENCY_BOUNDS_DEFAULT_VALUE = QVariant::fromValue(std::make_pair(2, 10)); | |
|
100 | 100 | |
|
101 | 101 | |
|
102 | 102 | // /////// ////// |
|
103 | 103 | // CUSTOM CASE // |
|
104 | 104 | // /////// ////// |
|
105 | 105 | |
|
106 | 106 | auto op = [](auto type){ |
|
107 | 107 | return FuzzingOperationFactory::create(type); |
|
108 | 108 | }; |
|
109 | 109 | |
|
110 | 110 | |
|
111 | 111 | const QVariant CUSTOM_CASE_ONE =QVariant::fromValue(CustomVariablesOperations{{{0, op(FuzzingOperationType::CREATE)}, true}, |
|
112 | 112 | {{0, op(FuzzingOperationType::SYNCHRONIZE)}}, |
|
113 | 113 | {{1, op(FuzzingOperationType::CREATE)}, true}, |
|
114 | 114 | {{1, op(FuzzingOperationType::SYNCHRONIZE)}}, |
|
115 | 115 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
116 | 116 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
117 | 117 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
118 | 118 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
119 | 119 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
120 | 120 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
121 | 121 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
122 | 122 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
123 | 123 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
124 | 124 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
125 | 125 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
126 | 126 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
127 | 127 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
128 | 128 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
129 | 129 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
130 | 130 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
131 | 131 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
132 | 132 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
133 | 133 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
134 | 134 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
135 | 135 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
136 | 136 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
137 | 137 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
138 | 138 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
139 | 139 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
140 | 140 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
141 | 141 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
142 | 142 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
143 | 143 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
144 | 144 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
145 | 145 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
146 | 146 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
147 | 147 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
148 | 148 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
149 | 149 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
150 | 150 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
151 | 151 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
152 | 152 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
153 | 153 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
154 | 154 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
155 | 155 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
156 | 156 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
157 | 157 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
158 | 158 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
159 | 159 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
160 | 160 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
161 | 161 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
162 | 162 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
163 | 163 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
164 | 164 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
165 | 165 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
166 | 166 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
167 | 167 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
168 | 168 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
169 | 169 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
170 | 170 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
171 | 171 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
172 | 172 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
173 | 173 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
174 | 174 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
175 | 175 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
176 | 176 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
177 | 177 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
178 | 178 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
179 | 179 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
180 | 180 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
181 | 181 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
182 | 182 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
183 | 183 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
184 | 184 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
185 | 185 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
186 | 186 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
187 | 187 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
188 | 188 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
189 | 189 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
190 | 190 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
191 | 191 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
192 | 192 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
193 | 193 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
194 | 194 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
195 | 195 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
196 | 196 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
197 | 197 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
198 | 198 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
199 | 199 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
200 | 200 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
201 | 201 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
202 | 202 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
203 | 203 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
204 | 204 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
205 | 205 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
206 | 206 | {{1, op(FuzzingOperationType::PAN_LEFT)}}, |
|
207 | 207 | {{0, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
208 | 208 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
209 | 209 | {{0, op(FuzzingOperationType::PAN_LEFT)}}, |
|
210 | 210 | {{1, op(FuzzingOperationType::PAN_RIGHT)}}, |
|
211 | 211 | }); |
|
212 | 212 | |
|
213 | 213 | const QVariant CUSTOM_CASE_TWO =QVariant::fromValue(CustomVariablesOperations{{{0, op(FuzzingOperationType::CREATE)}, true}, |
|
214 | 214 | {{0, op(FuzzingOperationType::SYNCHRONIZE)}}, |
|
215 | 215 | {{1, op(FuzzingOperationType::CREATE)}, true}, |
|
216 | 216 | {{1, op(FuzzingOperationType::SYNCHRONIZE)}}, |
|
217 | 217 | {{1, op(FuzzingOperationType::ZOOM_OUT)}}, |
|
218 | 218 | {{1, op(FuzzingOperationType::ZOOM_IN)}}, |
|
219 | 219 | }); |
|
220 | 220 | |
|
221 | 221 | // /////// // |
|
222 | 222 | // Methods // |
|
223 | 223 | // /////// // |
|
224 | 224 | |
|
225 | 225 | /// Goes through the variables pool and operations pool to determine the set of {variable/operation} |
|
226 | 226 | /// pairs that are valid (i.e. operation that can be executed on variable) |
|
227 | 227 | std::pair<VariablesOperations, Weights> availableOperations(const FuzzingState &fuzzingState, |
|
228 | 228 | const OperationsPool &operationsPool) |
|
229 | 229 | { |
|
230 | 230 | VariablesOperations result{}; |
|
231 | 231 | Weights weights{}; |
|
232 | 232 | |
|
233 | 233 | for (const auto &variablesPoolEntry : fuzzingState.m_VariablesPool) { |
|
234 | 234 | auto variableId = variablesPoolEntry.first; |
|
235 | 235 | |
|
236 | 236 | for (const auto &operationsPoolEntry : operationsPool) { |
|
237 | 237 | auto operation = operationsPoolEntry.first; |
|
238 | 238 | auto operationProperty = operationsPoolEntry.second; |
|
239 | 239 | |
|
240 | 240 | // A pair is valid if the current operation can be executed on the current variable |
|
241 | 241 | if (operation->canExecute(variableId, fuzzingState)) { |
|
242 | 242 | result.push_back({variableId, operation}); |
|
243 | 243 | weights.push_back(operationProperty.m_Weight); |
|
244 | 244 | } |
|
245 | 245 | } |
|
246 | 246 | } |
|
247 | 247 | |
|
248 | 248 | return {result, weights}; |
|
249 | 249 | } |
|
250 | 250 | |
|
251 | 251 | OperationsPool createOperationsPool(const OperationsTypes &types) |
|
252 | 252 | { |
|
253 | 253 | OperationsPool result{}; |
|
254 | 254 | |
|
255 | 255 | std::transform( |
|
256 | 256 | types.cbegin(), types.cend(), std::inserter(result, result.end()), [](const auto &type) { |
|
257 | 257 | return std::make_pair(FuzzingOperationFactory::create(type.first), type.second); |
|
258 | 258 | }); |
|
259 | 259 | |
|
260 | 260 | return result; |
|
261 | 261 | } |
|
262 | 262 | |
|
263 | 263 | Validators createValidators(const ValidatorsTypes &types) |
|
264 | 264 | { |
|
265 | 265 | Validators result{}; |
|
266 | 266 | |
|
267 | 267 | std::transform(types.cbegin(), types.cend(), std::inserter(result, result.end()), |
|
268 | 268 | [](const auto &type) { return FuzzingValidatorFactory::create(type); }); |
|
269 | 269 | |
|
270 | 270 | return result; |
|
271 | 271 | } |
|
272 | 272 | |
|
273 | 273 | /** |
|
274 | 274 | * Validates all the variables' states passed in parameter, according to a set of validators |
|
275 | 275 | * @param variablesPool the variables' states |
|
276 | 276 | * @param validators the validators used for validation |
|
277 | 277 | */ |
|
278 | 278 | void validate(const VariablesPool &variablesPool, const Validators &validators) |
|
279 | 279 | { |
|
280 | 280 | for (const auto &variablesPoolEntry : variablesPool) { |
|
281 | 281 | auto variableId = variablesPoolEntry.first; |
|
282 | 282 | const auto &variableState = variablesPoolEntry.second; |
|
283 | 283 | |
|
284 | 284 | auto variableMessage = variableState.m_Variable ? variableState.m_Variable->name() |
|
285 | 285 | : QStringLiteral("null variable"); |
|
286 | 286 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validating state of variable at index" |
|
287 | 287 | << variableId << "(" << variableMessage << ")..."; |
|
288 | 288 | |
|
289 | 289 | for (const auto &validator : validators) { |
|
290 | 290 | validator->validate(VariableState{variableState}); |
|
291 | 291 | } |
|
292 | 292 | |
|
293 | 293 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Validation completed."; |
|
294 | 294 | } |
|
295 | 295 | } |
|
296 | 296 | |
|
297 | 297 | /** |
|
298 | 298 | * Class to run random tests |
|
299 | 299 | */ |
|
300 | 300 | class FuzzingTest { |
|
301 | 301 | public: |
|
302 | 302 | explicit FuzzingTest(VariableController &variableController, Properties properties) |
|
303 | 303 | : m_VariableController{variableController}, |
|
304 | 304 | m_Properties{std::move(properties)}, |
|
305 | 305 | m_FuzzingState{} |
|
306 | 306 | { |
|
307 | 307 | // Inits variables pool: at init, all variables are null |
|
308 | 308 | for (auto variableId = 0; variableId < nbMaxVariables(); ++variableId) { |
|
309 | 309 | m_FuzzingState.m_VariablesPool[variableId] = VariableState{}; |
|
310 | 310 | } |
|
311 | 311 | |
|
312 | 312 | // Inits sync groups and registers them into the variable controller |
|
313 | 313 | for (auto i = 0; i < nbMaxSyncGroups(); ++i) { |
|
314 | 314 | auto syncGroupId = SyncGroupId::createUuid(); |
|
315 | 315 | variableController.onAddSynchronizationGroupId(syncGroupId); |
|
316 | 316 | m_FuzzingState.m_SyncGroupsPool[syncGroupId] = SyncGroup{}; |
|
317 | 317 | } |
|
318 | 318 | } |
|
319 | 319 | |
|
320 | 320 | void execute() |
|
321 | 321 | { |
|
322 | 322 | |
|
323 | 323 | // Inits the count of the number of operations before the next validation |
|
324 | 324 | int nextValidationCounter = 0; |
|
325 | 325 | auto updateValidationCounter = [this, &nextValidationCounter]() { |
|
326 | 326 | nextValidationCounter = RandomGenerator::instance().generateInt( |
|
327 | 327 | validationFrequencies().first, validationFrequencies().second); |
|
328 | 328 | qCInfo(LOG_TestAmdaFuzzing()).noquote() |
|
329 | 329 | << "Next validation in " << nextValidationCounter << "operation(s)..."; |
|
330 | 330 | }; |
|
331 | 331 | updateValidationCounter(); |
|
332 | 332 | |
|
333 | 333 | // Get custom operations |
|
334 | 334 | auto customOperations = m_Properties.value(CUSTOM_OPERATIONS_PROPERTY).value<CustomVariablesOperations>(); |
|
335 | 335 | auto isCustomTest = !customOperations.empty(); |
|
336 | 336 | |
|
337 | 337 | auto nbOperations = isCustomTest ? customOperations.size() : nbMaxOperations(); |
|
338 | 338 | |
|
339 | 339 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Running" << nbOperations << "operations on" |
|
340 | 340 | << nbMaxVariables() << "variable(s)..."; |
|
341 | 341 | |
|
342 | 342 | auto canExecute = true; |
|
343 | 343 | for (auto i = 0; i < nbOperations && canExecute; ++i) { |
|
344 | 344 | VariableOperation variableOperation{}; |
|
345 | 345 | auto forceWait = false; |
|
346 | 346 | |
|
347 | 347 | if(isCustomTest){ |
|
348 | 348 | auto customOperation = customOperations.front(); |
|
349 | 349 | variableOperation = customOperation.m_VariableOperation; |
|
350 | 350 | customOperations.erase(customOperations.begin()); |
|
351 | 351 | |
|
352 | 352 | canExecute = variableOperation.second->canExecute(variableOperation.first, m_FuzzingState); |
|
353 | 353 | forceWait = customOperation.m_WaitAcquisition; |
|
354 | 354 | } else { |
|
355 | 355 | // Retrieves all operations that can be executed in the current context |
|
356 | 356 | VariablesOperations variableOperations{}; |
|
357 | 357 | Weights weights{}; |
|
358 | 358 | std::tie(variableOperations, weights) |
|
359 | 359 | = availableOperations(m_FuzzingState, operationsPool()); |
|
360 | 360 | |
|
361 | 361 | // Of the operations available, chooses a random operation and executes it |
|
362 | 362 | variableOperation |
|
363 | 363 | = RandomGenerator::instance().randomChoice(variableOperations, weights); |
|
364 | 364 | canExecute = !variableOperations.empty(); |
|
365 | 365 | forceWait = operationsPool().at(variableOperation.second).m_WaitAcquisition; |
|
366 | 366 | } |
|
367 | 367 | |
|
368 | 368 | if (canExecute) { |
|
369 | 369 | --nextValidationCounter; |
|
370 | 370 | |
|
371 | 371 | auto variableId = variableOperation.first; |
|
372 | 372 | auto fuzzingOperation = variableOperation.second; |
|
373 | 373 | |
|
374 | 374 | auto waitAcquisition = nextValidationCounter == 0 |
|
375 | 375 | || forceWait; |
|
376 | 376 | |
|
377 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Operation :" << i +1 << " on" | |
|
378 | << nbOperations; | |
|
377 | 379 | fuzzingOperation->execute(variableId, m_FuzzingState, m_VariableController, |
|
378 | 380 | m_Properties); |
|
379 | 381 | |
|
380 | 382 | if (waitAcquisition) { |
|
381 | 383 | qCDebug(LOG_TestAmdaFuzzing()) << "Waiting for acquisition to finish..."; |
|
382 | 384 | SignalWaiter{m_VariableController, SIGNAL(acquisitionFinished())}.wait( |
|
383 | 385 | acquisitionTimeout()); |
|
384 | 386 | |
|
385 | 387 | // Validates variables |
|
386 | 388 | if (nextValidationCounter == 0) { |
|
387 | 389 | validate(m_FuzzingState.m_VariablesPool, validators()); |
|
388 | 390 | updateValidationCounter(); |
|
389 | 391 | } |
|
390 | 392 | } |
|
391 | 393 | else { |
|
392 | 394 | // Delays the next operation with a randomly generated time |
|
393 | 395 | auto delay = RandomGenerator::instance().generateInt(operationDelays().first, |
|
394 | 396 | operationDelays().second); |
|
395 | 397 | qCDebug(LOG_TestAmdaFuzzing()) |
|
396 | 398 | << "Waiting " << delay << "ms before the next operation..."; |
|
397 | 399 | QTest::qWait(delay); |
|
398 | 400 | } |
|
399 | 401 | } |
|
400 | 402 | else { |
|
401 | 403 | qCInfo(LOG_TestAmdaFuzzing()).noquote() |
|
402 | 404 | << "No more operations are available, the execution of the test will stop..."; |
|
403 | 405 | } |
|
404 | 406 | } |
|
405 | 407 | |
|
406 | 408 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Execution of the test completed."; |
|
407 | 409 | } |
|
408 | 410 | |
|
409 | 411 | private: |
|
410 | 412 | OperationsPool operationsPool() const |
|
411 | 413 | { |
|
412 | 414 | static auto result = createOperationsPool( |
|
413 | 415 | m_Properties.value(AVAILABLE_OPERATIONS_PROPERTY, AVAILABLE_OPERATIONS_DEFAULT_VALUE) |
|
414 | 416 | .value<OperationsTypes>()); |
|
415 | 417 | return result; |
|
416 | 418 | } |
|
417 | 419 | |
|
418 | 420 | Validators validators() const |
|
419 | 421 | { |
|
420 | 422 | static auto result |
|
421 | 423 | = createValidators(m_Properties.value(VALIDATORS_PROPERTY, VALIDATORS_DEFAULT_VALUE) |
|
422 | 424 | .value<ValidatorsTypes>()); |
|
423 | 425 | return result; |
|
424 | 426 | } |
|
425 | 427 | |
|
426 | 428 | DECLARE_PROPERTY_GETTER(nbMaxOperations, NB_MAX_OPERATIONS, int) |
|
427 | 429 | DECLARE_PROPERTY_GETTER(nbMaxSyncGroups, NB_MAX_SYNC_GROUPS, int) |
|
428 | 430 | DECLARE_PROPERTY_GETTER(nbMaxVariables, NB_MAX_VARIABLES, int) |
|
429 | 431 | DECLARE_PROPERTY_GETTER(operationDelays, OPERATION_DELAY_BOUNDS, IntPair) |
|
430 | 432 | DECLARE_PROPERTY_GETTER(validationFrequencies, VALIDATION_FREQUENCY_BOUNDS, IntPair) |
|
431 | 433 | DECLARE_PROPERTY_GETTER(acquisitionTimeout, ACQUISITION_TIMEOUT, int) |
|
432 | 434 | |
|
433 | 435 | VariableController &m_VariableController; |
|
434 | 436 | Properties m_Properties; |
|
435 | 437 | FuzzingState m_FuzzingState; |
|
436 | 438 | }; |
|
437 | 439 | |
|
438 | 440 | } // namespace |
|
439 | 441 | |
|
440 | 442 | Q_DECLARE_METATYPE(OperationsTypes) |
|
441 | 443 | Q_DECLARE_METATYPE(CustomVariablesOperations) |
|
442 | 444 | |
|
443 | 445 | class TestAmdaFuzzing : public QObject { |
|
444 | 446 | Q_OBJECT |
|
445 | 447 | |
|
446 | 448 | private slots: |
|
447 | 449 | /// Input data for @sa testFuzzing() |
|
448 | 450 | void testFuzzing_data(); |
|
449 | 451 | void testFuzzing(); |
|
450 | 452 | }; |
|
451 | 453 | |
|
452 | 454 | void TestAmdaFuzzing::testFuzzing_data() |
|
453 | 455 | { |
|
454 | 456 | // Note: Comment this line to run fuzzing tests |
|
455 | 457 | // QSKIP("Fuzzing tests are disabled by default"); |
|
456 | 458 | |
|
457 | 459 | // ////////////// // |
|
458 | 460 | // Test structure // |
|
459 | 461 | // ////////////// // |
|
460 | 462 | |
|
461 | 463 | QTest::addColumn<Properties>("properties"); // Properties for random test |
|
462 | 464 | |
|
463 | 465 | // ////////// // |
|
464 | 466 | // Test cases // |
|
465 | 467 | // ////////// // |
|
466 | 468 | |
|
467 |
auto maxRange = SqpRange::fromDateTime({2017, 1, |
|
|
469 | auto maxRange = SqpRange::fromDateTime({2017, 1, 5}, {8, 0}, {2017, 1, 5}, {16, 0}); | |
|
468 | 470 | MetadataPool metadataPool{{{"dataType", "vector"}, {"xml:id", "c1_b"}}}; |
|
469 | 471 | |
|
470 | 472 | // Note: we don't use auto here as we want to pass std::shared_ptr<IDataProvider> as is in the |
|
471 | 473 | // QVariant |
|
472 | 474 | std::shared_ptr<IDataProvider> provider = std::make_shared<AmdaProvider>(); |
|
473 | 475 | |
|
474 | 476 | |
|
475 | 477 | // Case Custom one : Only lot of pan on two variables synchronized |
|
476 | 478 | // QTest::newRow("fuzzingTestPan") << Properties{ |
|
477 | 479 | // {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, |
|
478 | 480 | // {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, |
|
479 | 481 | // {PROVIDER_PROPERTY, QVariant::fromValue(provider)}, |
|
480 | 482 | // {CUSTOM_OPERATIONS_PROPERTY, CUSTOM_CASE_ONE}}; |
|
481 | 483 | |
|
482 | QTest::newRow("fuzzingTestZoom") << Properties{ | |
|
483 | {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, | |
|
484 | {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, | |
|
485 | {PROVIDER_PROPERTY, QVariant::fromValue(provider)}, | |
|
486 | {CUSTOM_OPERATIONS_PROPERTY, CUSTOM_CASE_TWO}}; | |
|
484 | // QTest::newRow("fuzzingTestZoom") << Properties{ | |
|
485 | // {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, | |
|
486 | // {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, | |
|
487 | // {PROVIDER_PROPERTY, QVariant::fromValue(provider)}, | |
|
488 | // {CUSTOM_OPERATIONS_PROPERTY, CUSTOM_CASE_TWO}}; | |
|
487 | 489 | |
|
488 | 490 | |
|
489 | 491 | //// Fuzzing |
|
490 |
|
|
|
491 |
|
|
|
492 |
|
|
|
493 |
|
|
|
492 | QTest::newRow("fuzzingTest") << Properties{ | |
|
493 | {MAX_RANGE_PROPERTY, QVariant::fromValue(maxRange)}, | |
|
494 | {METADATA_POOL_PROPERTY, QVariant::fromValue(metadataPool)}, | |
|
495 | {PROVIDER_PROPERTY, QVariant::fromValue(provider)}}; | |
|
494 | 496 | |
|
495 | 497 | } |
|
496 | 498 | |
|
497 | 499 | void TestAmdaFuzzing::testFuzzing() |
|
498 | 500 | { |
|
499 | 501 | QFETCH(Properties, properties); |
|
500 | 502 | |
|
501 | 503 | // Sets cache property |
|
502 | 504 | QSettings settings{}; |
|
503 | 505 | auto cacheTolerance = properties.value(CACHE_TOLERANCE_PROPERTY, CACHE_TOLERANCE_DEFAULT_VALUE); |
|
504 | 506 | settings.setValue(GENERAL_TOLERANCE_AT_INIT_KEY, cacheTolerance); |
|
505 | 507 | settings.setValue(GENERAL_TOLERANCE_AT_UPDATE_KEY, cacheTolerance); |
|
506 | 508 | |
|
507 | 509 | auto &variableController = sqpApp->variableController(); |
|
508 | 510 | auto &timeController = sqpApp->timeController(); |
|
509 | 511 | |
|
510 | 512 | // Generates random initial range (bounded to max range) |
|
511 | 513 | auto maxRange = properties.value(MAX_RANGE_PROPERTY, QVariant::fromValue(INVALID_RANGE)) |
|
512 | 514 | .value<SqpRange>(); |
|
513 | 515 | |
|
514 | 516 | QVERIFY(maxRange != INVALID_RANGE); |
|
515 | 517 | |
|
516 | 518 | auto initialRangeStart |
|
517 | 519 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
518 | 520 | auto initialRangeEnd |
|
519 | 521 | = RandomGenerator::instance().generateDouble(maxRange.m_TStart, maxRange.m_TEnd); |
|
520 | 522 | if (initialRangeStart > initialRangeEnd) { |
|
521 | 523 | std::swap(initialRangeStart, initialRangeEnd); |
|
522 | 524 | } |
|
523 | 525 | |
|
524 | 526 | // Sets initial range on time controller |
|
525 | 527 | SqpRange initialRange{initialRangeStart, initialRangeEnd}; |
|
526 | 528 | qCInfo(LOG_TestAmdaFuzzing()).noquote() << "Setting initial range to" << initialRange << "..."; |
|
527 | 529 | timeController.onTimeToUpdate(initialRange); |
|
528 | 530 | properties.insert(INITIAL_RANGE_PROPERTY, QVariant::fromValue(initialRange)); |
|
529 | 531 | |
|
530 | 532 | FuzzingTest test{variableController, properties}; |
|
531 | 533 | test.execute(); |
|
532 | 534 | } |
|
533 | 535 | |
|
534 | 536 | int main(int argc, char *argv[]) |
|
535 | 537 | { |
|
536 | 538 | // Increases the test function timeout (which is 5 minutes by default) to 12 hours |
|
537 | 539 | // https://stackoverflow.com/questions/42655932/setting-timeout-to-qt-test |
|
538 | 540 | qputenv("QTEST_FUNCTION_TIMEOUT", QByteArray::number(12*60*60*1000)); |
|
539 | 541 | |
|
540 | 542 | QLoggingCategory::setFilterRules( |
|
541 | 543 | "*.warning=false\n" |
|
542 | 544 | "*.info=false\n" |
|
543 | 545 | "*.debug=false\n" |
|
544 | 546 | "FuzzingOperations.info=true\n" |
|
545 | 547 | "FuzzingValidators.info=true\n" |
|
546 | 548 | "TestAmdaFuzzing.info=true\n"); |
|
547 | 549 | |
|
548 | 550 | SqpApplication app{argc, argv}; |
|
549 | 551 | SqpApplication::setOrganizationName("LPP"); |
|
550 | 552 | SqpApplication::setOrganizationDomain("lpp.fr"); |
|
551 | 553 | SqpApplication::setApplicationName("SciQLop-TestFuzzing"); |
|
552 | 554 | app.setAttribute(Qt::AA_Use96Dpi, true); |
|
553 | 555 | TestAmdaFuzzing testObject{}; |
|
554 | 556 | QTEST_SET_MAIN_SOURCE_PATH |
|
555 | 557 | return QTest::qExec(&testObject, argc, argv); |
|
556 | 558 | } |
|
557 | 559 | |
|
558 | 560 | #include "TestAmdaFuzzing.moc" |
General Comments 0
You need to be logged in to leave comments.
Login now