##// END OF EJS Templates
Handles desynchronisation when removing variable from a graph (1)...
Alexandre Leroux -
r676:e5aabf0f62fb
parent child
Show More
@@ -1,129 +1,133
1 1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 2 #define SCIQLOP_VARIABLECONTROLLER_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/AcquisitionDataPacket.h>
7 7 #include <Data/SqpRange.h>
8 8
9 9 #include <QLoggingCategory>
10 10 #include <QObject>
11 11 #include <QUuid>
12 12
13 13 #include <Common/spimpl.h>
14 14
15 15 class IDataProvider;
16 16 class QItemSelectionModel;
17 17 class TimeController;
18 18 class Variable;
19 19 class VariableModel;
20 20
21 21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
22 22
23 23
24 24 /**
25 25 * Possible types of zoom operation
26 26 */
27 27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
28 28
29 29
30 30 /**
31 31 * @brief The VariableController class aims to handle the variables in SciQlop.
32 32 */
33 33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
34 34 Q_OBJECT
35 35 public:
36 36 explicit VariableController(QObject *parent = 0);
37 37 virtual ~VariableController();
38 38
39 39 VariableModel *variableModel() noexcept;
40 40 QItemSelectionModel *variableSelectionModel() noexcept;
41 41
42 42 void setTimeController(TimeController *timeController) noexcept;
43 43
44 44 /**
45 45 * Clones the variable passed in parameter and adds the duplicate to the controller
46 46 * @param variable the variable to duplicate
47 47 * @return the duplicate created, nullptr if the variable couldn't be created
48 48 */
49 49 std::shared_ptr<Variable> cloneVariable(std::shared_ptr<Variable> variable) noexcept;
50 50
51 51 /**
52 52 * Deletes from the controller the variable passed in parameter.
53 53 *
54 54 * Delete a variable includes:
55 55 * - the deletion of the various references to the variable in SciQlop
56 56 * - the deletion of the model variable
57 57 * - the deletion of the provider associated with the variable
58 58 * - removing the cache associated with the variable
59 59 *
60 60 * @param variable the variable to delete from the controller.
61 61 */
62 62 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
63 63
64 64 /**
65 65 * Deletes from the controller the variables passed in parameter.
66 66 * @param variables the variables to delete from the controller.
67 67 * @sa deleteVariable()
68 68 */
69 69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
70 70
71 71 /**
72 72 * @brief abort the variable retrieve data progression
73 73 */
74 74 void abortProgress(std::shared_ptr<Variable> variable);
75 75
76 76 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
77 77 signals:
78 78 /// Signal emitted when a variable is about to be deleted from the controller
79 79 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
80 80
81 81 /// Signal emitted when a data acquisition is requested on a range for a variable
82 82 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
83 83
84 84 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
85 85 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
86 86
87 87 public slots:
88 88 /// Request the data loading of the variable whithin range
89 89 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
90 90 const SqpRange &oldRange, bool synchronise);
91 91 /**
92 92 * Creates a new variable and adds it to the model
93 93 * @param name the name of the new variable
94 94 * @param metadata the metadata of the new variable
95 95 * @param provider the data provider for the new variable
96 96 * @return the pointer to the new variable or nullptr if the creation failed
97 97 */
98 98 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
99 99 std::shared_ptr<IDataProvider> provider) noexcept;
100 100
101 101 /// Update the temporal parameters of every selected variable to dateTime
102 102 void onDateTimeOnSelection(const SqpRange &dateTime);
103 103
104 104
105 105 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
106 106 const SqpRange &cacheRangeRequested,
107 107 QVector<AcquisitionDataPacket> dataAcquired);
108 108
109 109 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
110 110
111 111 /// Cancel the current request for the variable
112 112 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
113 113
114 /// synchronization group methods
114 // synchronization group methods
115 115 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
116 116 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
117 117 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
118 118
119 /// Desynchronizes the variable of the group whose identifier is passed in parameter
120 /// @remarks the method does nothing if the variable is not part of the group
121 void desynchronize(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
122
119 123 void initialize();
120 124 void finalize();
121 125
122 126 private:
123 127 void waitForFinish();
124 128
125 129 class VariableControllerPrivate;
126 130 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
127 131 };
128 132
129 133 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,775 +1,805
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableAcquisitionWorker.h>
3 3 #include <Variable/VariableCacheStrategy.h>
4 4 #include <Variable/VariableController.h>
5 5 #include <Variable/VariableModel.h>
6 6 #include <Variable/VariableSynchronizationGroup.h>
7 7
8 8 #include <Data/DataProviderParameters.h>
9 9 #include <Data/IDataProvider.h>
10 10 #include <Data/IDataSeries.h>
11 11 #include <Data/VariableRequest.h>
12 12 #include <Time/TimeController.h>
13 13
14 14 #include <QMutex>
15 15 #include <QThread>
16 16 #include <QUuid>
17 17 #include <QtCore/QItemSelectionModel>
18 18
19 19 #include <deque>
20 20 #include <set>
21 21 #include <unordered_map>
22 22
23 23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24 24
25 25 namespace {
26 26
27 27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 28 const SqpRange &oldGraphRange)
29 29 {
30 30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31 31
32 32 auto varRangeRequested = varRange;
33 33 switch (zoomType) {
34 34 case AcquisitionZoomType::ZoomIn: {
35 35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 37 varRangeRequested.m_TStart += deltaLeft;
38 38 varRangeRequested.m_TEnd -= deltaRight;
39 39 break;
40 40 }
41 41
42 42 case AcquisitionZoomType::ZoomOut: {
43 43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 45 varRangeRequested.m_TStart -= deltaLeft;
46 46 varRangeRequested.m_TEnd += deltaRight;
47 47 break;
48 48 }
49 49 case AcquisitionZoomType::PanRight: {
50 50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
51 51 varRangeRequested.m_TStart += deltaRight;
52 52 varRangeRequested.m_TEnd += deltaRight;
53 53 break;
54 54 }
55 55 case AcquisitionZoomType::PanLeft: {
56 56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
57 57 varRangeRequested.m_TStart -= deltaLeft;
58 58 varRangeRequested.m_TEnd -= deltaLeft;
59 59 break;
60 60 }
61 61 case AcquisitionZoomType::Unknown: {
62 62 qCCritical(LOG_VariableController())
63 63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
64 64 break;
65 65 }
66 66 default:
67 67 qCCritical(LOG_VariableController()) << VariableController::tr(
68 68 "Impossible to synchronize: zoom type not take into account");
69 69 // No action
70 70 break;
71 71 }
72 72
73 73 return varRangeRequested;
74 74 }
75 75 }
76 76
77 77 struct VariableController::VariableControllerPrivate {
78 78 explicit VariableControllerPrivate(VariableController *parent)
79 79 : m_WorkingMutex{},
80 80 m_VariableModel{new VariableModel{parent}},
81 81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
82 82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
83 83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
84 84 q{parent}
85 85 {
86 86
87 87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
88 88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
89 89 }
90 90
91 91
92 92 virtual ~VariableControllerPrivate()
93 93 {
94 94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
95 95 m_VariableAcquisitionWorkerThread.quit();
96 96 m_VariableAcquisitionWorkerThread.wait();
97 97 }
98 98
99 99
100 100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
101 101 QUuid varRequestId);
102 102
103 103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
104 104 const SqpRange &dateTime);
105 105
106 106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
107 107 std::shared_ptr<IDataSeries>
108 108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
109 109
110 110 void registerProvider(std::shared_ptr<IDataProvider> provider);
111 111
112 112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
113 113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
114 114 void updateVariableRequest(QUuid varRequestId);
115 115 void cancelVariableRequest(QUuid varRequestId);
116 116
117 117 QMutex m_WorkingMutex;
118 118 /// Variable model. The VariableController has the ownership
119 119 VariableModel *m_VariableModel;
120 120 QItemSelectionModel *m_VariableSelectionModel;
121 121
122 122
123 123 TimeController *m_TimeController{nullptr};
124 124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
125 125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
126 126 QThread m_VariableAcquisitionWorkerThread;
127 127
128 128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
129 129 m_VariableToProviderMap;
130 130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
131 131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
132 132 m_GroupIdToVariableSynchronizationGroupMap;
133 133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
134 134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
135 135
136 136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
137 137
138 138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
139 139
140 140
141 141 VariableController *q;
142 142 };
143 143
144 144
145 145 VariableController::VariableController(QObject *parent)
146 146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
147 147 {
148 148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
149 149 << QThread::currentThread();
150 150
151 151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
152 152 &VariableController::onAbortProgressRequested);
153 153
154 154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
155 155 &VariableController::onDataProvided);
156 156 connect(impl->m_VariableAcquisitionWorker.get(),
157 157 &VariableAcquisitionWorker::variableRequestInProgress, this,
158 158 &VariableController::onVariableRetrieveDataInProgress);
159 159
160 160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
161 161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
162 162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
163 163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
164 164
165 165
166 166 impl->m_VariableAcquisitionWorkerThread.start();
167 167 }
168 168
169 169 VariableController::~VariableController()
170 170 {
171 171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
172 172 << QThread::currentThread();
173 173 this->waitForFinish();
174 174 }
175 175
176 176 VariableModel *VariableController::variableModel() noexcept
177 177 {
178 178 return impl->m_VariableModel;
179 179 }
180 180
181 181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
182 182 {
183 183 return impl->m_VariableSelectionModel;
184 184 }
185 185
186 186 void VariableController::setTimeController(TimeController *timeController) noexcept
187 187 {
188 188 impl->m_TimeController = timeController;
189 189 }
190 190
191 191 std::shared_ptr<Variable>
192 192 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
193 193 {
194 194 if (impl->m_VariableModel->containsVariable(variable)) {
195 195 // Clones variable
196 196 auto duplicate = variable->clone();
197 197
198 198 // Adds clone to model
199 199 impl->m_VariableModel->addVariable(duplicate);
200 200
201 201 // Generates clone identifier
202 202 impl->m_VariableToIdentifierMap[duplicate] = QUuid::createUuid();
203 203
204 204 // Registers provider
205 205 auto variableProvider = impl->m_VariableToProviderMap.at(variable);
206 206 auto duplicateProvider = variableProvider != nullptr ? variableProvider->clone() : nullptr;
207 207
208 208 impl->m_VariableToProviderMap[duplicate] = duplicateProvider;
209 209 if (duplicateProvider) {
210 210 impl->registerProvider(duplicateProvider);
211 211 }
212 212
213 213 return duplicate;
214 214 }
215 215 else {
216 216 qCCritical(LOG_VariableController())
217 217 << tr("Can't create duplicate of variable %1: variable not registered in the model")
218 218 .arg(variable->name());
219 219 return nullptr;
220 220 }
221 221 }
222 222
223 223 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
224 224 {
225 225 if (!variable) {
226 226 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
227 227 return;
228 228 }
229 229
230 230 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
231 231 // make some treatments before the deletion
232 232 emit variableAboutToBeDeleted(variable);
233 233
234 234 // Deletes identifier
235 235 impl->m_VariableToIdentifierMap.erase(variable);
236 236
237 237 // Deletes provider
238 238 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
239 239 qCDebug(LOG_VariableController())
240 240 << tr("Number of providers deleted for variable %1: %2")
241 241 .arg(variable->name(), QString::number(nbProvidersDeleted));
242 242
243 243
244 244 // Deletes from model
245 245 impl->m_VariableModel->deleteVariable(variable);
246 246 }
247 247
248 248 void VariableController::deleteVariables(
249 249 const QVector<std::shared_ptr<Variable> > &variables) noexcept
250 250 {
251 251 for (auto variable : qAsConst(variables)) {
252 252 deleteVariable(variable);
253 253 }
254 254 }
255 255
256 256 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
257 257 {
258 258 }
259 259
260 260 std::shared_ptr<Variable>
261 261 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
262 262 std::shared_ptr<IDataProvider> provider) noexcept
263 263 {
264 264 if (!impl->m_TimeController) {
265 265 qCCritical(LOG_VariableController())
266 266 << tr("Impossible to create variable: The time controller is null");
267 267 return nullptr;
268 268 }
269 269
270 270 auto range = impl->m_TimeController->dateTime();
271 271
272 272 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
273 273 auto identifier = QUuid::createUuid();
274 274
275 275 // store the provider
276 276 impl->registerProvider(provider);
277 277
278 278 // Associate the provider
279 279 impl->m_VariableToProviderMap[newVariable] = provider;
280 280 impl->m_VariableToIdentifierMap[newVariable] = identifier;
281 281
282 282
283 283 auto varRequestId = QUuid::createUuid();
284 284 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
285 285 impl->processRequest(newVariable, range, varRequestId);
286 286 impl->updateVariableRequest(varRequestId);
287 287
288 288 return newVariable;
289 289 }
290 290 }
291 291
292 292 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
293 293 {
294 294 // TODO check synchronisation and Rescale
295 295 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
296 296 << QThread::currentThread()->objectName();
297 297 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
298 298 auto varRequestId = QUuid::createUuid();
299 299
300 300 for (const auto &selectedRow : qAsConst(selectedRows)) {
301 301 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
302 302 selectedVariable->setRange(dateTime);
303 303 impl->processRequest(selectedVariable, dateTime, varRequestId);
304 304
305 305 // notify that rescale operation has to be done
306 306 emit rangeChanged(selectedVariable, dateTime);
307 307 }
308 308 }
309 309 impl->updateVariableRequest(varRequestId);
310 310 }
311 311
312 312 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
313 313 const SqpRange &cacheRangeRequested,
314 314 QVector<AcquisitionDataPacket> dataAcquired)
315 315 {
316 316 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
317 317 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
318 318 if (!varRequestId.isNull()) {
319 319 impl->updateVariableRequest(varRequestId);
320 320 }
321 321 }
322 322
323 323 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
324 324 {
325 325 if (auto var = impl->findVariable(identifier)) {
326 326 impl->m_VariableModel->setDataProgress(var, progress);
327 327 }
328 328 else {
329 329 qCCritical(LOG_VariableController())
330 330 << tr("Impossible to notify progression of a null variable");
331 331 }
332 332 }
333 333
334 334 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
335 335 {
336 336 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
337 337 << QThread::currentThread()->objectName();
338 338
339 339 auto it = impl->m_VariableToIdentifierMap.find(variable);
340 340 if (it != impl->m_VariableToIdentifierMap.cend()) {
341 341 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
342 342 }
343 343 else {
344 344 qCWarning(LOG_VariableController())
345 345 << tr("Aborting progression of inexistant variable detected !!!")
346 346 << QThread::currentThread()->objectName();
347 347 }
348 348 }
349 349
350 350 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
351 351 {
352 352 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
353 353 << QThread::currentThread()->objectName()
354 354 << synchronizationGroupId;
355 355 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
356 356 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
357 357 std::make_pair(synchronizationGroupId, vSynchroGroup));
358 358 }
359 359
360 360 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
361 361 {
362 362 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
363 363 }
364 364
365 365 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
366 366 QUuid synchronizationGroupId)
367 367
368 368 {
369 369 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
370 370 << synchronizationGroupId;
371 371 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
372 372 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
373 373 auto groupIdToVSGIt
374 374 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
375 375 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
376 376 impl->m_VariableIdGroupIdMap.insert(
377 377 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
378 378 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
379 379 }
380 380 else {
381 381 qCCritical(LOG_VariableController())
382 382 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
383 383 << variable->name();
384 384 }
385 385 }
386 386 else {
387 387 qCCritical(LOG_VariableController())
388 388 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
389 389 }
390 390 }
391 391
392 void VariableController::desynchronize(std::shared_ptr<Variable> variable,
393 QUuid synchronizationGroupId)
394 {
395 // Gets variable id
396 auto variableIt = impl->m_VariableToIdentifierMap.find(variable);
397 if (variableIt == impl->m_VariableToIdentifierMap.cend()) {
398 qCCritical(LOG_VariableController())
399 << tr("Can't desynchronize variable %1: variable identifier not found")
400 .arg(variable->name());
401 return;
402 }
403
404 // Gets synchronization group
405 auto groupIt = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
406 if (groupIt == impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
407 qCCritical(LOG_VariableController())
408 << tr("Can't desynchronize variable %1: unknown synchronization group")
409 .arg(variable->name());
410 return;
411 }
412
413 auto variableId = variableIt->second;
414
415 // Removes variable from synchronization group
416 auto synchronizationGroup = groupIt->second;
417 synchronizationGroup->removeVariableId(variableId);
418
419 // Removes link between variable and synchronization group
420 impl->m_VariableIdGroupIdMap.erase(variableId);
421 }
392 422
393 423 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
394 424 const SqpRange &range, const SqpRange &oldRange,
395 425 bool synchronise)
396 426 {
397 427 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
398 428
399 429 // we want to load data of the variable for the dateTime.
400 430 // First we check if the cache contains some of them.
401 431 // For the other, we ask the provider to give them.
402 432
403 433 auto varRequestId = QUuid::createUuid();
404 434 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
405 435 << QThread::currentThread()->objectName() << varRequestId;
406 436
407 437 for (const auto &var : variables) {
408 438 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
409 439 impl->processRequest(var, range, varRequestId);
410 440 }
411 441
412 442 if (synchronise) {
413 443 // Get the group ids
414 444 qCDebug(LOG_VariableController())
415 445 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
416 446 auto groupIds = std::set<QUuid>{};
417 447 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
418 448 for (const auto &var : variables) {
419 449 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
420 450 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
421 451 auto vId = varToVarIdIt->second;
422 452 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
423 453 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
424 454 auto gId = varIdToGroupIdIt->second;
425 455 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
426 456 if (groupIds.find(gId) == groupIds.cend()) {
427 457 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
428 458 groupIds.insert(gId);
429 459 }
430 460 }
431 461 }
432 462 }
433 463
434 464 // We assume here all group ids exist
435 465 for (const auto &gId : groupIds) {
436 466 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
437 467 auto vSyncIds = vSynchronizationGroup->getIds();
438 468 qCDebug(LOG_VariableController()) << "Var in synchro group ";
439 469 for (auto vId : vSyncIds) {
440 470 auto var = impl->findVariable(vId);
441 471
442 472 // Don't process already processed var
443 473 if (!variables.contains(var)) {
444 474 if (var != nullptr) {
445 475 qCDebug(LOG_VariableController()) << "processRequest synchro for"
446 476 << var->name();
447 477 auto vSyncRangeRequested = computeSynchroRangeRequested(
448 478 var->range(), range, groupIdToOldRangeMap.at(gId));
449 479 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
450 480 impl->processRequest(var, vSyncRangeRequested, varRequestId);
451 481 }
452 482 else {
453 483 qCCritical(LOG_VariableController())
454 484
455 485 << tr("Impossible to synchronize a null variable");
456 486 }
457 487 }
458 488 }
459 489 }
460 490 }
461 491
462 492 impl->updateVariableRequest(varRequestId);
463 493 }
464 494
465 495
466 496 void VariableController::initialize()
467 497 {
468 498 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
469 499 impl->m_WorkingMutex.lock();
470 500 qCDebug(LOG_VariableController()) << tr("VariableController init END");
471 501 }
472 502
473 503 void VariableController::finalize()
474 504 {
475 505 impl->m_WorkingMutex.unlock();
476 506 }
477 507
478 508 void VariableController::waitForFinish()
479 509 {
480 510 QMutexLocker locker{&impl->m_WorkingMutex};
481 511 }
482 512
483 513 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
484 514 {
485 515 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
486 516 auto zoomType = AcquisitionZoomType::Unknown;
487 517 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
488 518 zoomType = AcquisitionZoomType::ZoomOut;
489 519 }
490 520 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
491 521 zoomType = AcquisitionZoomType::PanRight;
492 522 }
493 523 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
494 524 zoomType = AcquisitionZoomType::PanLeft;
495 525 }
496 526 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
497 527 zoomType = AcquisitionZoomType::ZoomIn;
498 528 }
499 529 else {
500 530 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
501 531 }
502 532 return zoomType;
503 533 }
504 534
505 535 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
506 536 const SqpRange &rangeRequested,
507 537 QUuid varRequestId)
508 538 {
509 539
510 540 // TODO: protect at
511 541 auto varRequest = VariableRequest{};
512 542 auto varId = m_VariableToIdentifierMap.at(var);
513 543
514 544 auto varStrategyRangesRequested
515 545 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
516 546 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
517 547 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
518 548
519 549 if (!notInCacheRangeList.empty()) {
520 550 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
521 551 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
522 552 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
523 553 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
524 554 << varStrategyRangesRequested.first;
525 555 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
526 556 << varStrategyRangesRequested.second;
527 557 // store VarRequest
528 558 storeVariableRequest(varId, varRequestId, varRequest);
529 559
530 560 auto varProvider = m_VariableToProviderMap.at(var);
531 561 if (varProvider != nullptr) {
532 562 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
533 563 varRequestId, varId, varStrategyRangesRequested.first,
534 564 varStrategyRangesRequested.second,
535 565 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
536 566 varProvider);
537 567
538 568 if (!varRequestIdCanceled.isNull()) {
539 569 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
540 570 << varRequestIdCanceled;
541 571 cancelVariableRequest(varRequestIdCanceled);
542 572 }
543 573 }
544 574 else {
545 575 qCCritical(LOG_VariableController())
546 576 << "Impossible to provide data with a null provider";
547 577 }
548 578
549 579 if (!inCacheRangeList.empty()) {
550 580 emit q->updateVarDisplaying(var, inCacheRangeList.first());
551 581 }
552 582 }
553 583 else {
554 584
555 585 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
556 586 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
557 587 // store VarRequest
558 588 storeVariableRequest(varId, varRequestId, varRequest);
559 589 acceptVariableRequest(varId,
560 590 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
561 591 }
562 592 }
563 593
564 594 std::shared_ptr<Variable>
565 595 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
566 596 {
567 597 std::shared_ptr<Variable> var;
568 598 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
569 599
570 600 auto end = m_VariableToIdentifierMap.cend();
571 601 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
572 602 if (it != end) {
573 603 var = it->first;
574 604 }
575 605 else {
576 606 qCCritical(LOG_VariableController())
577 607 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
578 608 }
579 609
580 610 return var;
581 611 }
582 612
583 613 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
584 614 const QVector<AcquisitionDataPacket> acqDataPacketVector)
585 615 {
586 616 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
587 617 << acqDataPacketVector.size();
588 618 std::shared_ptr<IDataSeries> dataSeries;
589 619 if (!acqDataPacketVector.isEmpty()) {
590 620 dataSeries = acqDataPacketVector[0].m_DateSeries;
591 621 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
592 622 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
593 623 }
594 624 }
595 625 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
596 626 << acqDataPacketVector.size();
597 627 return dataSeries;
598 628 }
599 629
600 630 void VariableController::VariableControllerPrivate::registerProvider(
601 631 std::shared_ptr<IDataProvider> provider)
602 632 {
603 633 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
604 634 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
605 635 << provider->objectName();
606 636 m_ProviderSet.insert(provider);
607 637 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
608 638 &VariableAcquisitionWorker::onVariableDataAcquired);
609 639 connect(provider.get(), &IDataProvider::dataProvidedProgress,
610 640 m_VariableAcquisitionWorker.get(),
611 641 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
612 642 }
613 643 else {
614 644 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
615 645 }
616 646 }
617 647
618 648 void VariableController::VariableControllerPrivate::storeVariableRequest(
619 649 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
620 650 {
621 651 // First request for the variable. we can create an entry for it
622 652 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
623 653 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
624 654 auto varRequestIdQueue = std::deque<QUuid>{};
625 655 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
626 656 varRequestIdQueue.push_back(varRequestId);
627 657 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
628 658 }
629 659 else {
630 660 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
631 661 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
632 662 varRequestIdQueue.push_back(varRequestId);
633 663 }
634 664
635 665 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
636 666 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
637 667 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
638 668 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
639 669 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
640 670 m_VarRequestIdToVarIdVarRequestMap.insert(
641 671 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
642 672 }
643 673 else {
644 674 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
645 675 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
646 676 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
647 677 }
648 678 }
649 679
650 680 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
651 681 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
652 682 {
653 683 QUuid varRequestId;
654 684 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
655 685 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
656 686 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
657 687 varRequestId = varRequestIdQueue.front();
658 688 auto varRequestIdToVarIdVarRequestMapIt
659 689 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
660 690 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
661 691 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
662 692 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
663 693 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
664 694 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
665 695 auto &varRequest = varIdToVarRequestMapIt->second;
666 696 varRequest.m_DataSeries = dataSeries;
667 697 varRequest.m_CanUpdate = true;
668 698 }
669 699 else {
670 700 qCDebug(LOG_VariableController())
671 701 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
672 702 "to a variableRequestId")
673 703 << varRequestId << varId;
674 704 }
675 705 }
676 706 else {
677 707 qCCritical(LOG_VariableController())
678 708 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
679 709 << varRequestId;
680 710 }
681 711
682 712 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
683 713 << varRequestIdQueue.size();
684 714 varRequestIdQueue.pop_front();
685 715 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
686 716 << varRequestIdQueue.size();
687 717 if (varRequestIdQueue.empty()) {
688 718 m_VarIdToVarRequestIdQueueMap.erase(varId);
689 719 }
690 720 }
691 721 else {
692 722 qCCritical(LOG_VariableController())
693 723 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
694 724 }
695 725
696 726 return varRequestId;
697 727 }
698 728
699 729 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
700 730 {
701 731
702 732 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
703 733 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
704 734 bool processVariableUpdate = true;
705 735 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
706 736 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
707 737 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
708 738 ++varIdToVarRequestMapIt) {
709 739 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
710 740 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
711 741 << processVariableUpdate;
712 742 }
713 743
714 744 if (processVariableUpdate) {
715 745 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
716 746 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
717 747 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
718 748 auto &varRequest = varIdToVarRequestMapIt->second;
719 749 var->setRange(varRequest.m_RangeRequested);
720 750 var->setCacheRange(varRequest.m_CacheRangeRequested);
721 751 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
722 752 << varRequest.m_RangeRequested;
723 753 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
724 754 << varRequest.m_CacheRangeRequested;
725 755 var->mergeDataSeries(varRequest.m_DataSeries);
726 756 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
727 757 << varRequest.m_DataSeries->range();
728 758 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
729 759
730 760 /// @todo MPL: confirm
731 761 // Variable update is notified only if there is no pending request for it
732 762 if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) {
733 763 emit var->updated();
734 764 }
735 765 }
736 766 else {
737 767 qCCritical(LOG_VariableController())
738 768 << tr("Impossible to update data to a null variable");
739 769 }
740 770 }
741 771
742 772 // cleaning varRequestId
743 773 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
744 774 << m_VarRequestIdToVarIdVarRequestMap.size();
745 775 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
746 776 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
747 777 << m_VarRequestIdToVarIdVarRequestMap.size();
748 778 }
749 779 }
750 780 else {
751 781 qCCritical(LOG_VariableController())
752 782 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
753 783 }
754 784 }
755 785
756 786 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
757 787 {
758 788 // cleaning varRequestId
759 789 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
760 790
761 791 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
762 792 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
763 793 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
764 794 varRequestIdQueue.erase(
765 795 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
766 796 varRequestIdQueue.end());
767 797 if (varRequestIdQueue.empty()) {
768 798 varIdToVarRequestIdQueueMapIt
769 799 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
770 800 }
771 801 else {
772 802 ++varIdToVarRequestIdQueueMapIt;
773 803 }
774 804 }
775 805 }
@@ -1,94 +1,97
1 1 #ifndef SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
2 2 #define SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
3 3
4 4 #include "Visualization/IVisualizationWidget.h"
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QWidget>
8 8
9 9 #include <memory>
10 10
11 11 #include <Common/spimpl.h>
12 12
13 13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphWidget)
14 14
15 15 class QCPRange;
16 16 class QCustomPlot;
17 17 class SqpRange;
18 18 class Variable;
19 19
20 20 namespace Ui {
21 21 class VisualizationGraphWidget;
22 22 } // namespace Ui
23 23
24 24 class VisualizationGraphWidget : public QWidget, public IVisualizationWidget {
25 25 Q_OBJECT
26 26
27 27 friend class QCustomPlotSynchronizer;
28 28 friend class VisualizationGraphRenderingDelegate;
29 29
30 30 public:
31 31 explicit VisualizationGraphWidget(const QString &name = {}, QWidget *parent = 0);
32 32 virtual ~VisualizationGraphWidget();
33 33
34 34 /// If acquisition isn't enable, requestDataLoading signal cannot be emit
35 35 void enableAcquisition(bool enable);
36 36
37 37 void addVariable(std::shared_ptr<Variable> variable, SqpRange range);
38 38
39 39 /// Removes a variable from the graph
40 40 void removeVariable(std::shared_ptr<Variable> variable) noexcept;
41 41
42 42 void setRange(std::shared_ptr<Variable> variable, const SqpRange &range);
43 43 void setYRange(const SqpRange &range);
44 44 SqpRange graphRange() const noexcept;
45 45 void setGraphRange(const SqpRange &range);
46 46
47 47 // IVisualizationWidget interface
48 48 void accept(IVisualizationWidgetVisitor *visitor) override;
49 49 bool canDrop(const Variable &variable) const override;
50 50 bool contains(const Variable &variable) const override;
51 51 QString name() const override;
52 52
53 53
54 54 signals:
55 55 void synchronize(const SqpRange &range, const SqpRange &oldRange);
56 56 void requestDataLoading(QVector<std::shared_ptr<Variable> > variable, const SqpRange &range,
57 57 const SqpRange &oldRange, bool synchronise);
58 58
59 /// Signal emitted when the variable is about to be removed from the graph
60 void variableAboutToBeRemoved(std::shared_ptr<Variable> var);
61 /// Signal emitted when the variable has been added to the graph
59 62 void variableAdded(std::shared_ptr<Variable> var);
60 63
61 64 protected:
62 65 void enterEvent(QEvent *event) override;
63 66 void leaveEvent(QEvent *event) override;
64 67
65 68 QCustomPlot &plot() noexcept;
66 69
67 70 private:
68 71 Ui::VisualizationGraphWidget *ui;
69 72
70 73 class VisualizationGraphWidgetPrivate;
71 74 spimpl::unique_impl_ptr<VisualizationGraphWidgetPrivate> impl;
72 75
73 76 private slots:
74 77 /// Slot called when right clicking on the graph (displays a menu)
75 78 void onGraphMenuRequested(const QPoint &pos) noexcept;
76 79
77 80 /// Rescale the X axe to range parameter
78 81 void onRangeChanged(const QCPRange &t1, const QCPRange &t2);
79 82
80 83 /// Slot called when a mouse move was made
81 84 void onMouseMove(QMouseEvent *event) noexcept;
82 85 /// Slot called when a mouse wheel was made, to perform some processing before the zoom is done
83 86 void onMouseWheel(QWheelEvent *event) noexcept;
84 87 /// Slot called when a mouse press was made, to activate the calibration of a graph
85 88 void onMousePress(QMouseEvent *event) noexcept;
86 89 /// Slot called when a mouse release was made, to deactivate the calibration of a graph
87 90 void onMouseRelease(QMouseEvent *event) noexcept;
88 91
89 92 void onDataCacheVariableUpdated();
90 93
91 94 void onUpdateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
92 95 };
93 96
94 97 #endif // SCIQLOP_VISUALIZATIONGRAPHWIDGET_H
@@ -1,55 +1,57
1 1 #ifndef SCIQLOP_VISUALIZATIONZONEWIDGET_H
2 2 #define SCIQLOP_VISUALIZATIONZONEWIDGET_H
3 3
4 4 #include "Visualization/IVisualizationWidget.h"
5 5
6 6 #include <QLoggingCategory>
7 7 #include <QWidget>
8 8
9 9 #include <memory>
10 10
11 11 #include <Common/spimpl.h>
12 12
13 13 Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationZoneWidget)
14 14
15 15 namespace Ui {
16 16 class VisualizationZoneWidget;
17 17 } // Ui
18 18
19 19 class Variable;
20 20 class VisualizationGraphWidget;
21 21
22 22 class VisualizationZoneWidget : public QWidget, public IVisualizationWidget {
23 23 Q_OBJECT
24 24
25 25 public:
26 26 explicit VisualizationZoneWidget(const QString &name = {}, QWidget *parent = 0);
27 27 virtual ~VisualizationZoneWidget();
28 28
29 29 /// Add a graph widget
30 30 void addGraph(VisualizationGraphWidget *graphWidget);
31 31
32 32 /**
33 33 * Creates a graph using a variable. The variable will be displayed in the new graph.
34 34 * @param variable the variable for which to create the graph
35 35 * @return the pointer to the created graph
36 36 */
37 37 VisualizationGraphWidget *createGraph(std::shared_ptr<Variable> variable);
38 38
39 39 // IVisualizationWidget interface
40 40 void accept(IVisualizationWidgetVisitor *visitor) override;
41 41 bool canDrop(const Variable &variable) const override;
42 42 bool contains(const Variable &variable) const override;
43 43 QString name() const override;
44 44
45 45 private:
46 46 Ui::VisualizationZoneWidget *ui;
47 47
48 48 class VisualizationZoneWidgetPrivate;
49 49 spimpl::unique_impl_ptr<VisualizationZoneWidgetPrivate> impl;
50 50
51 51 private slots:
52 52 void onVariableAdded(std::shared_ptr<Variable> variable);
53 /// Slot called when a variable is about to be removed from a graph contained in the zone
54 void onVariableAboutToBeRemoved(std::shared_ptr<Variable> variable);
53 55 };
54 56
55 57 #endif // SCIQLOP_VISUALIZATIONZONEWIDGET_H
@@ -1,344 +1,346
1 1 #include "Visualization/VisualizationGraphWidget.h"
2 2 #include "Visualization/IVisualizationWidgetVisitor.h"
3 3 #include "Visualization/VisualizationDefs.h"
4 4 #include "Visualization/VisualizationGraphHelper.h"
5 5 #include "Visualization/VisualizationGraphRenderingDelegate.h"
6 6 #include "ui_VisualizationGraphWidget.h"
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/IDataSeries.h>
10 10 #include <Settings/SqpSettingsDefs.h>
11 11 #include <SqpApplication.h>
12 12 #include <Variable/Variable.h>
13 13 #include <Variable/VariableController.h>
14 14
15 15 #include <unordered_map>
16 16
17 17 Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget")
18 18
19 19 namespace {
20 20
21 21 /// Key pressed to enable zoom on horizontal axis
22 22 const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier;
23 23
24 24 /// Key pressed to enable zoom on vertical axis
25 25 const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier;
26 26
27 27 } // namespace
28 28
29 29 struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate {
30 30
31 31 explicit VisualizationGraphWidgetPrivate(const QString &name)
32 32 : m_Name{name},
33 33 m_DoAcquisition{true},
34 34 m_IsCalibration{false},
35 35 m_RenderingDelegate{nullptr}
36 36 {
37 37 }
38 38
39 39 QString m_Name;
40 40 // 1 variable -> n qcpplot
41 41 std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap;
42 42 bool m_DoAcquisition;
43 43 bool m_IsCalibration;
44 44 QCPItemTracer *m_TextTracer;
45 45 /// Delegate used to attach rendering features to the plot
46 46 std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate;
47 47 };
48 48
49 49 VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent)
50 50 : QWidget{parent},
51 51 ui{new Ui::VisualizationGraphWidget},
52 52 impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>(name)}
53 53 {
54 54 ui->setupUi(this);
55 55
56 56 // 'Close' options : widget is deleted when closed
57 57 setAttribute(Qt::WA_DeleteOnClose);
58 58
59 59 // Set qcpplot properties :
60 60 // - Drag (on x-axis) and zoom are enabled
61 61 // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation
62 62 ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectItems);
63 63 ui->widget->axisRect()->setRangeDrag(Qt::Horizontal);
64 64
65 65 // The delegate must be initialized after the ui as it uses the plot
66 66 impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*this);
67 67
68 68 connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress);
69 69 connect(ui->widget, &QCustomPlot::mouseRelease, this,
70 70 &VisualizationGraphWidget::onMouseRelease);
71 71 connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove);
72 72 connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel);
73 73 connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>(
74 74 &QCPAxis::rangeChanged),
75 75 this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection);
76 76
77 77 // Activates menu when right clicking on the graph
78 78 ui->widget->setContextMenuPolicy(Qt::CustomContextMenu);
79 79 connect(ui->widget, &QCustomPlot::customContextMenuRequested, this,
80 80 &VisualizationGraphWidget::onGraphMenuRequested);
81 81
82 82 connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(),
83 83 &VariableController::onRequestDataLoading);
84 84
85 85 connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this,
86 86 &VisualizationGraphWidget::onUpdateVarDisplaying);
87 87 }
88 88
89 89
90 90 VisualizationGraphWidget::~VisualizationGraphWidget()
91 91 {
92 92 delete ui;
93 93 }
94 94
95 95 void VisualizationGraphWidget::enableAcquisition(bool enable)
96 96 {
97 97 impl->m_DoAcquisition = enable;
98 98 }
99 99
100 100 void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range)
101 101 {
102 102 // Uses delegate to create the qcpplot components according to the variable
103 103 auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget);
104 104 impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)});
105 105
106 106 // Set axes properties according to the units of the data series
107 107 /// @todo : for the moment, no control is performed on the axes: the units and the tickers
108 108 /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph
109 109 auto xAxisUnit = Unit{};
110 110 auto valuesUnit = Unit{};
111 111
112 112 if (auto dataSeries = variable->dataSeries()) {
113 113 dataSeries->lockRead();
114 114 xAxisUnit = dataSeries->xAxisUnit();
115 115 valuesUnit = dataSeries->valuesUnit();
116 116 dataSeries->unlock();
117 117 }
118 118 impl->m_RenderingDelegate->setAxesProperties(xAxisUnit, valuesUnit);
119 119
120 120 connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated()));
121 121
122 122 auto varRange = variable->range();
123 123
124 124 this->enableAcquisition(false);
125 125 this->setGraphRange(range);
126 126 this->enableAcquisition(true);
127 127
128 128 emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange,
129 129 false);
130 130
131 131 emit variableAdded(variable);
132 132 }
133 133
134 134 void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept
135 135 {
136 136 // Each component associated to the variable :
137 137 // - is removed from qcpplot (which deletes it)
138 138 // - is no longer referenced in the map
139 139 auto variableIt = impl->m_VariableToPlotMultiMap.find(variable);
140 140 if (variableIt != impl->m_VariableToPlotMultiMap.cend()) {
141 emit variableAboutToBeRemoved(variable);
142
141 143 auto &plottablesMap = variableIt->second;
142 144
143 145 for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend();
144 146 plottableIt != plottableEnd;) {
145 147 ui->widget->removePlottable(plottableIt->second);
146 148 plottableIt = plottablesMap.erase(plottableIt);
147 149 }
148 150
149 151 impl->m_VariableToPlotMultiMap.erase(variableIt);
150 152 }
151 153
152 154 // Updates graph
153 155 ui->widget->replot();
154 156 }
155 157
156 158 void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range)
157 159 {
158 160 // Note: in case of different axes that depends on variable, we could start with a code like
159 161 // that:
160 162 // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable);
161 163 // for (auto it = componentsIt.first; it != componentsIt.second;) {
162 164 // }
163 165 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
164 166 ui->widget->replot();
165 167 }
166 168
167 169 void VisualizationGraphWidget::setYRange(const SqpRange &range)
168 170 {
169 171 ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd);
170 172 }
171 173
172 174 SqpRange VisualizationGraphWidget::graphRange() const noexcept
173 175 {
174 176 auto graphRange = ui->widget->xAxis->range();
175 177 return SqpRange{graphRange.lower, graphRange.upper};
176 178 }
177 179
178 180 void VisualizationGraphWidget::setGraphRange(const SqpRange &range)
179 181 {
180 182 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START");
181 183 ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd);
182 184 ui->widget->replot();
183 185 qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END");
184 186 }
185 187
186 188 void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor)
187 189 {
188 190 if (visitor) {
189 191 visitor->visit(this);
190 192 }
191 193 else {
192 194 qCCritical(LOG_VisualizationGraphWidget())
193 195 << tr("Can't visit widget : the visitor is null");
194 196 }
195 197 }
196 198
197 199 bool VisualizationGraphWidget::canDrop(const Variable &variable) const
198 200 {
199 201 /// @todo : for the moment, a graph can always accomodate a variable
200 202 Q_UNUSED(variable);
201 203 return true;
202 204 }
203 205
204 206 bool VisualizationGraphWidget::contains(const Variable &variable) const
205 207 {
206 208 // Finds the variable among the keys of the map
207 209 auto variablePtr = &variable;
208 210 auto findVariable
209 211 = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); };
210 212
211 213 auto end = impl->m_VariableToPlotMultiMap.cend();
212 214 auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable);
213 215 return it != end;
214 216 }
215 217
216 218 QString VisualizationGraphWidget::name() const
217 219 {
218 220 return impl->m_Name;
219 221 }
220 222
221 223 void VisualizationGraphWidget::enterEvent(QEvent *event)
222 224 {
223 225 Q_UNUSED(event);
224 226 impl->m_RenderingDelegate->showGraphOverlay(true);
225 227 }
226 228
227 229 void VisualizationGraphWidget::leaveEvent(QEvent *event)
228 230 {
229 231 Q_UNUSED(event);
230 232 impl->m_RenderingDelegate->showGraphOverlay(false);
231 233 }
232 234
233 235 QCustomPlot &VisualizationGraphWidget::plot() noexcept
234 236 {
235 237 return *ui->widget;
236 238 }
237 239
238 240 void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept
239 241 {
240 242 QMenu graphMenu{};
241 243
242 244 // Iterates on variables (unique keys)
243 245 for (auto it = impl->m_VariableToPlotMultiMap.cbegin(),
244 246 end = impl->m_VariableToPlotMultiMap.cend();
245 247 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
246 248 // 'Remove variable' action
247 249 graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()),
248 250 [ this, var = it->first ]() { removeVariable(var); });
249 251 }
250 252
251 253 if (!graphMenu.isEmpty()) {
252 254 graphMenu.exec(QCursor::pos());
253 255 }
254 256 }
255 257
256 258 void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2)
257 259 {
258 260 qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged")
259 261 << QThread::currentThread()->objectName() << "DoAcqui"
260 262 << impl->m_DoAcquisition;
261 263
262 264 auto graphRange = SqpRange{t1.lower, t1.upper};
263 265 auto oldGraphRange = SqpRange{t2.lower, t2.upper};
264 266
265 267 if (impl->m_DoAcquisition) {
266 268 QVector<std::shared_ptr<Variable> > variableUnderGraphVector;
267 269
268 270 for (auto it = impl->m_VariableToPlotMultiMap.begin(),
269 271 end = impl->m_VariableToPlotMultiMap.end();
270 272 it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) {
271 273 variableUnderGraphVector.push_back(it->first);
272 274 }
273 275 emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange,
274 276 !impl->m_IsCalibration);
275 277
276 278 if (!impl->m_IsCalibration) {
277 279 qCDebug(LOG_VisualizationGraphWidget())
278 280 << tr("TORM: VisualizationGraphWidget::Synchronize notify !!")
279 281 << QThread::currentThread()->objectName() << graphRange << oldGraphRange;
280 282 emit synchronize(graphRange, oldGraphRange);
281 283 }
282 284 }
283 285 }
284 286
285 287 void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept
286 288 {
287 289 // Handles plot rendering when mouse is moving
288 290 impl->m_RenderingDelegate->onMouseMove(event);
289 291 }
290 292
291 293 void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept
292 294 {
293 295 auto zoomOrientations = QFlags<Qt::Orientation>{};
294 296
295 297 // Lambda that enables a zoom orientation if the key modifier related to this orientation
296 298 // has
297 299 // been pressed
298 300 auto enableOrientation
299 301 = [&zoomOrientations, event](const auto &orientation, const auto &modifier) {
300 302 auto orientationEnabled = event->modifiers().testFlag(modifier);
301 303 zoomOrientations.setFlag(orientation, orientationEnabled);
302 304 };
303 305 enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER);
304 306 enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER);
305 307
306 308 ui->widget->axisRect()->setRangeZoom(zoomOrientations);
307 309 }
308 310
309 311 void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept
310 312 {
311 313 impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier);
312 314 }
313 315
314 316 void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept
315 317 {
316 318 impl->m_IsCalibration = false;
317 319 }
318 320
319 321 void VisualizationGraphWidget::onDataCacheVariableUpdated()
320 322 {
321 323 auto graphRange = ui->widget->xAxis->range();
322 324 auto dateTime = SqpRange{graphRange.lower, graphRange.upper};
323 325
324 326 for (auto &variableEntry : impl->m_VariableToPlotMultiMap) {
325 327 auto variable = variableEntry.first;
326 328 qCDebug(LOG_VisualizationGraphWidget())
327 329 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range();
328 330 qCDebug(LOG_VisualizationGraphWidget())
329 331 << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime;
330 332 if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) {
331 333 VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(),
332 334 variable->range());
333 335 }
334 336 }
335 337 }
336 338
337 339 void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable,
338 340 const SqpRange &range)
339 341 {
340 342 auto it = impl->m_VariableToPlotMultiMap.find(variable);
341 343 if (it != impl->m_VariableToPlotMultiMap.end()) {
342 344 VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range);
343 345 }
344 346 }
@@ -1,268 +1,277
1 1 #include "Visualization/VisualizationZoneWidget.h"
2 2
3 3 #include "Visualization/IVisualizationWidgetVisitor.h"
4 4 #include "Visualization/QCustomPlotSynchronizer.h"
5 5 #include "Visualization/VisualizationGraphWidget.h"
6 6 #include "ui_VisualizationZoneWidget.h"
7 7
8 8 #include <Data/SqpRange.h>
9 9 #include <Variable/Variable.h>
10 10 #include <Variable/VariableController.h>
11 11
12 12 #include <QUuid>
13 13 #include <SqpApplication.h>
14 14 #include <cmath>
15 15
16 16 Q_LOGGING_CATEGORY(LOG_VisualizationZoneWidget, "VisualizationZoneWidget")
17 17
18 18 namespace {
19 19
20 20 /// Minimum height for graph added in zones (in pixels)
21 21 const auto GRAPH_MINIMUM_HEIGHT = 300;
22 22
23 23 /// Generates a default name for a new graph, according to the number of graphs already displayed in
24 24 /// the zone
25 25 QString defaultGraphName(const QLayout &layout)
26 26 {
27 27 auto count = 0;
28 28 for (auto i = 0; i < layout.count(); ++i) {
29 29 if (dynamic_cast<VisualizationGraphWidget *>(layout.itemAt(i)->widget())) {
30 30 count++;
31 31 }
32 32 }
33 33
34 34 return QObject::tr("Graph %1").arg(count + 1);
35 35 }
36 36
37 37 } // namespace
38 38
39 39 struct VisualizationZoneWidget::VisualizationZoneWidgetPrivate {
40 40
41 41 explicit VisualizationZoneWidgetPrivate()
42 42 : m_SynchronisationGroupId{QUuid::createUuid()},
43 43 m_Synchronizer{std::make_unique<QCustomPlotSynchronizer>()}
44 44 {
45 45 }
46 46 QUuid m_SynchronisationGroupId;
47 47 std::unique_ptr<IGraphSynchronizer> m_Synchronizer;
48 48 };
49 49
50 50 VisualizationZoneWidget::VisualizationZoneWidget(const QString &name, QWidget *parent)
51 51 : QWidget{parent},
52 52 ui{new Ui::VisualizationZoneWidget},
53 53 impl{spimpl::make_unique_impl<VisualizationZoneWidgetPrivate>()}
54 54 {
55 55 ui->setupUi(this);
56 56
57 57 ui->zoneNameLabel->setText(name);
58 58
59 59 // 'Close' options : widget is deleted when closed
60 60 setAttribute(Qt::WA_DeleteOnClose);
61 61 connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationZoneWidget::close);
62 62 ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton));
63 63
64 64 // Synchronisation id
65 65 QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronizationGroupId",
66 66 Qt::QueuedConnection, Q_ARG(QUuid, impl->m_SynchronisationGroupId));
67 67 }
68 68
69 69 VisualizationZoneWidget::~VisualizationZoneWidget()
70 70 {
71 71 delete ui;
72 72 }
73 73
74 74 void VisualizationZoneWidget::addGraph(VisualizationGraphWidget *graphWidget)
75 75 {
76 76 // Synchronize new graph with others in the zone
77 77 impl->m_Synchronizer->addGraph(*graphWidget);
78 78
79 79 ui->visualizationZoneFrame->layout()->addWidget(graphWidget);
80 80 }
81 81
82 82 VisualizationGraphWidget *VisualizationZoneWidget::createGraph(std::shared_ptr<Variable> variable)
83 83 {
84 84 auto graphWidget = new VisualizationGraphWidget{
85 85 defaultGraphName(*ui->visualizationZoneFrame->layout()), this};
86 86
87 87
88 88 // Set graph properties
89 89 graphWidget->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::MinimumExpanding);
90 90 graphWidget->setMinimumHeight(GRAPH_MINIMUM_HEIGHT);
91 91
92 92
93 93 // Lambda to synchronize zone widget
94 94 auto synchronizeZoneWidget = [this, graphWidget](const SqpRange &graphRange,
95 95 const SqpRange &oldGraphRange) {
96 96
97 97 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
98 98 auto frameLayout = ui->visualizationZoneFrame->layout();
99 99 for (auto i = 0; i < frameLayout->count(); ++i) {
100 100 auto graphChild
101 101 = dynamic_cast<VisualizationGraphWidget *>(frameLayout->itemAt(i)->widget());
102 102 if (graphChild && (graphChild != graphWidget)) {
103 103
104 104 auto graphChildRange = graphChild->graphRange();
105 105 switch (zoomType) {
106 106 case AcquisitionZoomType::ZoomIn: {
107 107 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
108 108 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
109 109 graphChildRange.m_TStart += deltaLeft;
110 110 graphChildRange.m_TEnd -= deltaRight;
111 111 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: ZoomIn");
112 112 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaLeft")
113 113 << deltaLeft;
114 114 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaRight")
115 115 << deltaRight;
116 116 qCDebug(LOG_VisualizationZoneWidget())
117 117 << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart;
118 118
119 119 break;
120 120 }
121 121
122 122 case AcquisitionZoomType::ZoomOut: {
123 123 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: ZoomOut");
124 124 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
125 125 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
126 126 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaLeft")
127 127 << deltaLeft;
128 128 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: deltaRight")
129 129 << deltaRight;
130 130 qCDebug(LOG_VisualizationZoneWidget())
131 131 << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart;
132 132 graphChildRange.m_TStart -= deltaLeft;
133 133 graphChildRange.m_TEnd += deltaRight;
134 134 break;
135 135 }
136 136 case AcquisitionZoomType::PanRight: {
137 137 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: PanRight");
138 138 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
139 139 graphChildRange.m_TStart += deltaRight;
140 140 graphChildRange.m_TEnd += deltaRight;
141 141 qCDebug(LOG_VisualizationZoneWidget())
142 142 << tr("TORM: dt") << graphRange.m_TEnd - graphRange.m_TStart;
143 143 break;
144 144 }
145 145 case AcquisitionZoomType::PanLeft: {
146 146 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: PanLeft");
147 147 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
148 148 graphChildRange.m_TStart -= deltaLeft;
149 149 graphChildRange.m_TEnd -= deltaLeft;
150 150 break;
151 151 }
152 152 case AcquisitionZoomType::Unknown: {
153 153 qCDebug(LOG_VisualizationZoneWidget())
154 154 << tr("Impossible to synchronize: zoom type unknown");
155 155 break;
156 156 }
157 157 default:
158 158 qCCritical(LOG_VisualizationZoneWidget())
159 159 << tr("Impossible to synchronize: zoom type not take into account");
160 160 // No action
161 161 break;
162 162 }
163 163 graphChild->enableAcquisition(false);
164 164 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: Range before: ")
165 165 << graphChild->graphRange();
166 166 qCDebug(LOG_VisualizationZoneWidget()) << tr("TORM: Range after : ")
167 167 << graphChildRange;
168 168 qCDebug(LOG_VisualizationZoneWidget())
169 169 << tr("TORM: child dt") << graphChildRange.m_TEnd - graphChildRange.m_TStart;
170 170 graphChild->setGraphRange(graphChildRange);
171 171 graphChild->enableAcquisition(true);
172 172 }
173 173 }
174 174 };
175 175
176 176 // connection for synchronization
177 177 connect(graphWidget, &VisualizationGraphWidget::synchronize, synchronizeZoneWidget);
178 178 connect(graphWidget, &VisualizationGraphWidget::variableAdded, this,
179 179 &VisualizationZoneWidget::onVariableAdded);
180 connect(graphWidget, &VisualizationGraphWidget::variableAboutToBeRemoved, this,
181 &VisualizationZoneWidget::onVariableAboutToBeRemoved);
180 182
181 183 auto range = SqpRange{};
182 184
183 185 // Apply visitor to graph children
184 186 auto layout = ui->visualizationZoneFrame->layout();
185 187 if (layout->count() > 0) {
186 188 // Case of a new graph in a existant zone
187 189 if (auto visualizationGraphWidget
188 190 = dynamic_cast<VisualizationGraphWidget *>(layout->itemAt(0)->widget())) {
189 191 range = visualizationGraphWidget->graphRange();
190 192 }
191 193 }
192 194 else {
193 195 // Case of a new graph as the first of the zone
194 196 range = variable->range();
195 197 }
196 198
197 199 this->addGraph(graphWidget);
198 200
199 201 graphWidget->addVariable(variable, range);
200 202
201 203 // get y using variable range
202 204 if (auto dataSeries = variable->dataSeries()) {
203 205 dataSeries->lockRead();
204 206 auto valuesBounds
205 207 = dataSeries->valuesBounds(variable->range().m_TStart, variable->range().m_TEnd);
206 208 auto end = dataSeries->cend();
207 209 if (valuesBounds.first != end && valuesBounds.second != end) {
208 210 auto rangeValue = [](const auto &value) { return std::isnan(value) ? 0. : value; };
209 211
210 212 auto minValue = rangeValue(valuesBounds.first->minValue());
211 213 auto maxValue = rangeValue(valuesBounds.second->maxValue());
212 214
213 215 graphWidget->setYRange(SqpRange{minValue, maxValue});
214 216 }
215 217 dataSeries->unlock();
216 218 }
217 219
218 220 return graphWidget;
219 221 }
220 222
221 223 void VisualizationZoneWidget::accept(IVisualizationWidgetVisitor *visitor)
222 224 {
223 225 if (visitor) {
224 226 visitor->visitEnter(this);
225 227
226 228 // Apply visitor to graph children
227 229 auto layout = ui->visualizationZoneFrame->layout();
228 230 for (auto i = 0; i < layout->count(); ++i) {
229 231 if (auto item = layout->itemAt(i)) {
230 232 // Widgets different from graphs are not visited (no action)
231 233 if (auto visualizationGraphWidget
232 234 = dynamic_cast<VisualizationGraphWidget *>(item->widget())) {
233 235 visualizationGraphWidget->accept(visitor);
234 236 }
235 237 }
236 238 }
237 239
238 240 visitor->visitLeave(this);
239 241 }
240 242 else {
241 243 qCCritical(LOG_VisualizationZoneWidget()) << tr("Can't visit widget : the visitor is null");
242 244 }
243 245 }
244 246
245 247 bool VisualizationZoneWidget::canDrop(const Variable &variable) const
246 248 {
247 249 // A tab can always accomodate a variable
248 250 Q_UNUSED(variable);
249 251 return true;
250 252 }
251 253
252 254 bool VisualizationZoneWidget::contains(const Variable &variable) const
253 255 {
254 256 Q_UNUSED(variable);
255 257 return false;
256 258 }
257 259
258 260 QString VisualizationZoneWidget::name() const
259 261 {
260 262 return ui->zoneNameLabel->text();
261 263 }
262 264
263 265 void VisualizationZoneWidget::onVariableAdded(std::shared_ptr<Variable> variable)
264 266 {
265 267 QMetaObject::invokeMethod(&sqpApp->variableController(), "onAddSynchronized",
266 268 Qt::QueuedConnection, Q_ARG(std::shared_ptr<Variable>, variable),
267 269 Q_ARG(QUuid, impl->m_SynchronisationGroupId));
268 270 }
271
272 void VisualizationZoneWidget::onVariableAboutToBeRemoved(std::shared_ptr<Variable> variable)
273 {
274 QMetaObject::invokeMethod(&sqpApp->variableController(), "desynchronize", Qt::QueuedConnection,
275 Q_ARG(std::shared_ptr<Variable>, variable),
276 Q_ARG(QUuid, impl->m_SynchronisationGroupId));
277 }
General Comments 0
You need to be logged in to leave comments. Login now