##// END OF EJS Templates
Adds 'Duplicate' action on variable menu
Alexandre Leroux -
r650:35ef72ac0d81
parent child
Show More
@@ -1,122 +1,129
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
1 #ifndef SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
2 #define SCIQLOP_VARIABLECONTROLLER_H
3
3
4 #include "CoreGlobal.h"
4 #include "CoreGlobal.h"
5
5
6 #include <Data/AcquisitionDataPacket.h>
6 #include <Data/AcquisitionDataPacket.h>
7 #include <Data/SqpRange.h>
7 #include <Data/SqpRange.h>
8
8
9 #include <QLoggingCategory>
9 #include <QLoggingCategory>
10 #include <QObject>
10 #include <QObject>
11 #include <QUuid>
11 #include <QUuid>
12
12
13 #include <Common/spimpl.h>
13 #include <Common/spimpl.h>
14
14
15 class IDataProvider;
15 class IDataProvider;
16 class QItemSelectionModel;
16 class QItemSelectionModel;
17 class TimeController;
17 class TimeController;
18 class Variable;
18 class Variable;
19 class VariableModel;
19 class VariableModel;
20
20
21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
21 Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController)
22
22
23
23
24 /**
24 /**
25 * Possible types of zoom operation
25 * Possible types of zoom operation
26 */
26 */
27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
27 enum class AcquisitionZoomType { ZoomOut, ZoomIn, PanRight, PanLeft, Unknown };
28
28
29
29
30 /**
30 /**
31 * @brief The VariableController class aims to handle the variables in SciQlop.
31 * @brief The VariableController class aims to handle the variables in SciQlop.
32 */
32 */
33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
33 class SCIQLOP_CORE_EXPORT VariableController : public QObject {
34 Q_OBJECT
34 Q_OBJECT
35 public:
35 public:
36 explicit VariableController(QObject *parent = 0);
36 explicit VariableController(QObject *parent = 0);
37 virtual ~VariableController();
37 virtual ~VariableController();
38
38
39 VariableModel *variableModel() noexcept;
39 VariableModel *variableModel() noexcept;
40 QItemSelectionModel *variableSelectionModel() noexcept;
40 QItemSelectionModel *variableSelectionModel() noexcept;
41
41
42 void setTimeController(TimeController *timeController) noexcept;
42 void setTimeController(TimeController *timeController) noexcept;
43
43
44 /**
44 /**
45 * Clones the variable passed in parameter and adds the duplicate to the controller
46 * @param variable the variable to duplicate
47 * @return the duplicate created, nullptr if the variable couldn't be created
48 */
49 std::shared_ptr<Variable> cloneVariable(std::shared_ptr<Variable> variable) noexcept;
50
51 /**
45 * Deletes from the controller the variable passed in parameter.
52 * Deletes from the controller the variable passed in parameter.
46 *
53 *
47 * Delete a variable includes:
54 * Delete a variable includes:
48 * - the deletion of the various references to the variable in SciQlop
55 * - the deletion of the various references to the variable in SciQlop
49 * - the deletion of the model variable
56 * - the deletion of the model variable
50 * - the deletion of the provider associated with the variable
57 * - the deletion of the provider associated with the variable
51 * - removing the cache associated with the variable
58 * - removing the cache associated with the variable
52 *
59 *
53 * @param variable the variable to delete from the controller.
60 * @param variable the variable to delete from the controller.
54 */
61 */
55 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
62 void deleteVariable(std::shared_ptr<Variable> variable) noexcept;
56
63
57 /**
64 /**
58 * Deletes from the controller the variables passed in parameter.
65 * Deletes from the controller the variables passed in parameter.
59 * @param variables the variables to delete from the controller.
66 * @param variables the variables to delete from the controller.
60 * @sa deleteVariable()
67 * @sa deleteVariable()
61 */
68 */
62 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
69 void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept;
63
70
64 /**
71 /**
65 * @brief abort the variable retrieve data progression
72 * @brief abort the variable retrieve data progression
66 */
73 */
67 void abortProgress(std::shared_ptr<Variable> variable);
74 void abortProgress(std::shared_ptr<Variable> variable);
68
75
69 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
76 static AcquisitionZoomType getZoomType(const SqpRange &range, const SqpRange &oldRange);
70 signals:
77 signals:
71 /// Signal emitted when a variable is about to be deleted from the controller
78 /// Signal emitted when a variable is about to be deleted from the controller
72 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
79 void variableAboutToBeDeleted(std::shared_ptr<Variable> variable);
73
80
74 /// Signal emitted when a data acquisition is requested on a range for a variable
81 /// Signal emitted when a data acquisition is requested on a range for a variable
75 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
82 void rangeChanged(std::shared_ptr<Variable> variable, const SqpRange &range);
76
83
77 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
84 /// Signal emitted when a sub range of the cacheRange of the variable can be displayed
78 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
85 void updateVarDisplaying(std::shared_ptr<Variable> variable, const SqpRange &range);
79
86
80 public slots:
87 public slots:
81 /// Request the data loading of the variable whithin range
88 /// Request the data loading of the variable whithin range
82 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
89 void onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables, const SqpRange &range,
83 const SqpRange &oldRange, bool synchronise);
90 const SqpRange &oldRange, bool synchronise);
84 /**
91 /**
85 * Creates a new variable and adds it to the model
92 * Creates a new variable and adds it to the model
86 * @param name the name of the new variable
93 * @param name the name of the new variable
87 * @param metadata the metadata of the new variable
94 * @param metadata the metadata of the new variable
88 * @param provider the data provider for the new variable
95 * @param provider the data provider for the new variable
89 * @return the pointer to the new variable or nullptr if the creation failed
96 * @return the pointer to the new variable or nullptr if the creation failed
90 */
97 */
91 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
98 std::shared_ptr<Variable> createVariable(const QString &name, const QVariantHash &metadata,
92 std::shared_ptr<IDataProvider> provider) noexcept;
99 std::shared_ptr<IDataProvider> provider) noexcept;
93
100
94 /// Update the temporal parameters of every selected variable to dateTime
101 /// Update the temporal parameters of every selected variable to dateTime
95 void onDateTimeOnSelection(const SqpRange &dateTime);
102 void onDateTimeOnSelection(const SqpRange &dateTime);
96
103
97
104
98 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
105 void onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
99 const SqpRange &cacheRangeRequested,
106 const SqpRange &cacheRangeRequested,
100 QVector<AcquisitionDataPacket> dataAcquired);
107 QVector<AcquisitionDataPacket> dataAcquired);
101
108
102 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
109 void onVariableRetrieveDataInProgress(QUuid identifier, double progress);
103
110
104 /// Cancel the current request for the variable
111 /// Cancel the current request for the variable
105 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
112 void onAbortProgressRequested(std::shared_ptr<Variable> variable);
106
113
107 /// synchronization group methods
114 /// synchronization group methods
108 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
115 void onAddSynchronizationGroupId(QUuid synchronizationGroupId);
109 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
116 void onRemoveSynchronizationGroupId(QUuid synchronizationGroupId);
110 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
117 void onAddSynchronized(std::shared_ptr<Variable> variable, QUuid synchronizationGroupId);
111
118
112 void initialize();
119 void initialize();
113 void finalize();
120 void finalize();
114
121
115 private:
122 private:
116 void waitForFinish();
123 void waitForFinish();
117
124
118 class VariableControllerPrivate;
125 class VariableControllerPrivate;
119 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
126 spimpl::unique_impl_ptr<VariableControllerPrivate> impl;
120 };
127 };
121
128
122 #endif // SCIQLOP_VARIABLECONTROLLER_H
129 #endif // SCIQLOP_VARIABLECONTROLLER_H
@@ -1,743 +1,748
1 #include <Variable/Variable.h>
1 #include <Variable/Variable.h>
2 #include <Variable/VariableAcquisitionWorker.h>
2 #include <Variable/VariableAcquisitionWorker.h>
3 #include <Variable/VariableCacheStrategy.h>
3 #include <Variable/VariableCacheStrategy.h>
4 #include <Variable/VariableController.h>
4 #include <Variable/VariableController.h>
5 #include <Variable/VariableModel.h>
5 #include <Variable/VariableModel.h>
6 #include <Variable/VariableSynchronizationGroup.h>
6 #include <Variable/VariableSynchronizationGroup.h>
7
7
8 #include <Data/DataProviderParameters.h>
8 #include <Data/DataProviderParameters.h>
9 #include <Data/IDataProvider.h>
9 #include <Data/IDataProvider.h>
10 #include <Data/IDataSeries.h>
10 #include <Data/IDataSeries.h>
11 #include <Data/VariableRequest.h>
11 #include <Data/VariableRequest.h>
12 #include <Time/TimeController.h>
12 #include <Time/TimeController.h>
13
13
14 #include <QMutex>
14 #include <QMutex>
15 #include <QThread>
15 #include <QThread>
16 #include <QUuid>
16 #include <QUuid>
17 #include <QtCore/QItemSelectionModel>
17 #include <QtCore/QItemSelectionModel>
18
18
19 #include <deque>
19 #include <deque>
20 #include <set>
20 #include <set>
21 #include <unordered_map>
21 #include <unordered_map>
22
22
23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
23 Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController")
24
24
25 namespace {
25 namespace {
26
26
27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
27 SqpRange computeSynchroRangeRequested(const SqpRange &varRange, const SqpRange &graphRange,
28 const SqpRange &oldGraphRange)
28 const SqpRange &oldGraphRange)
29 {
29 {
30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
30 auto zoomType = VariableController::getZoomType(graphRange, oldGraphRange);
31
31
32 auto varRangeRequested = varRange;
32 auto varRangeRequested = varRange;
33 switch (zoomType) {
33 switch (zoomType) {
34 case AcquisitionZoomType::ZoomIn: {
34 case AcquisitionZoomType::ZoomIn: {
35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
35 auto deltaLeft = graphRange.m_TStart - oldGraphRange.m_TStart;
36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
36 auto deltaRight = oldGraphRange.m_TEnd - graphRange.m_TEnd;
37 varRangeRequested.m_TStart += deltaLeft;
37 varRangeRequested.m_TStart += deltaLeft;
38 varRangeRequested.m_TEnd -= deltaRight;
38 varRangeRequested.m_TEnd -= deltaRight;
39 break;
39 break;
40 }
40 }
41
41
42 case AcquisitionZoomType::ZoomOut: {
42 case AcquisitionZoomType::ZoomOut: {
43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
43 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
44 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
45 varRangeRequested.m_TStart -= deltaLeft;
45 varRangeRequested.m_TStart -= deltaLeft;
46 varRangeRequested.m_TEnd += deltaRight;
46 varRangeRequested.m_TEnd += deltaRight;
47 break;
47 break;
48 }
48 }
49 case AcquisitionZoomType::PanRight: {
49 case AcquisitionZoomType::PanRight: {
50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
50 auto deltaRight = graphRange.m_TEnd - oldGraphRange.m_TEnd;
51 varRangeRequested.m_TStart += deltaRight;
51 varRangeRequested.m_TStart += deltaRight;
52 varRangeRequested.m_TEnd += deltaRight;
52 varRangeRequested.m_TEnd += deltaRight;
53 break;
53 break;
54 }
54 }
55 case AcquisitionZoomType::PanLeft: {
55 case AcquisitionZoomType::PanLeft: {
56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
56 auto deltaLeft = oldGraphRange.m_TStart - graphRange.m_TStart;
57 varRangeRequested.m_TStart -= deltaLeft;
57 varRangeRequested.m_TStart -= deltaLeft;
58 varRangeRequested.m_TEnd -= deltaLeft;
58 varRangeRequested.m_TEnd -= deltaLeft;
59 break;
59 break;
60 }
60 }
61 case AcquisitionZoomType::Unknown: {
61 case AcquisitionZoomType::Unknown: {
62 qCCritical(LOG_VariableController())
62 qCCritical(LOG_VariableController())
63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
63 << VariableController::tr("Impossible to synchronize: zoom type unknown");
64 break;
64 break;
65 }
65 }
66 default:
66 default:
67 qCCritical(LOG_VariableController()) << VariableController::tr(
67 qCCritical(LOG_VariableController()) << VariableController::tr(
68 "Impossible to synchronize: zoom type not take into account");
68 "Impossible to synchronize: zoom type not take into account");
69 // No action
69 // No action
70 break;
70 break;
71 }
71 }
72
72
73 return varRangeRequested;
73 return varRangeRequested;
74 }
74 }
75 }
75 }
76
76
77 struct VariableController::VariableControllerPrivate {
77 struct VariableController::VariableControllerPrivate {
78 explicit VariableControllerPrivate(VariableController *parent)
78 explicit VariableControllerPrivate(VariableController *parent)
79 : m_WorkingMutex{},
79 : m_WorkingMutex{},
80 m_VariableModel{new VariableModel{parent}},
80 m_VariableModel{new VariableModel{parent}},
81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
81 m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}},
82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
82 m_VariableCacheStrategy{std::make_unique<VariableCacheStrategy>()},
83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
83 m_VariableAcquisitionWorker{std::make_unique<VariableAcquisitionWorker>()},
84 q{parent}
84 q{parent}
85 {
85 {
86
86
87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
87 m_VariableAcquisitionWorker->moveToThread(&m_VariableAcquisitionWorkerThread);
88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
88 m_VariableAcquisitionWorkerThread.setObjectName("VariableAcquisitionWorkerThread");
89 }
89 }
90
90
91
91
92 virtual ~VariableControllerPrivate()
92 virtual ~VariableControllerPrivate()
93 {
93 {
94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
94 qCDebug(LOG_VariableController()) << tr("VariableControllerPrivate destruction");
95 m_VariableAcquisitionWorkerThread.quit();
95 m_VariableAcquisitionWorkerThread.quit();
96 m_VariableAcquisitionWorkerThread.wait();
96 m_VariableAcquisitionWorkerThread.wait();
97 }
97 }
98
98
99
99
100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
100 void processRequest(std::shared_ptr<Variable> var, const SqpRange &rangeRequested,
101 QUuid varRequestId);
101 QUuid varRequestId);
102
102
103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
103 QVector<SqpRange> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable,
104 const SqpRange &dateTime);
104 const SqpRange &dateTime);
105
105
106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
106 std::shared_ptr<Variable> findVariable(QUuid vIdentifier);
107 std::shared_ptr<IDataSeries>
107 std::shared_ptr<IDataSeries>
108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
108 retrieveDataSeries(const QVector<AcquisitionDataPacket> acqDataPacketVector);
109
109
110 void registerProvider(std::shared_ptr<IDataProvider> provider);
110 void registerProvider(std::shared_ptr<IDataProvider> provider);
111
111
112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
112 void storeVariableRequest(QUuid varId, QUuid varRequestId, const VariableRequest &varRequest);
113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
113 QUuid acceptVariableRequest(QUuid varId, std::shared_ptr<IDataSeries> dataSeries);
114 void updateVariableRequest(QUuid varRequestId);
114 void updateVariableRequest(QUuid varRequestId);
115 void cancelVariableRequest(QUuid varRequestId);
115 void cancelVariableRequest(QUuid varRequestId);
116
116
117 QMutex m_WorkingMutex;
117 QMutex m_WorkingMutex;
118 /// Variable model. The VariableController has the ownership
118 /// Variable model. The VariableController has the ownership
119 VariableModel *m_VariableModel;
119 VariableModel *m_VariableModel;
120 QItemSelectionModel *m_VariableSelectionModel;
120 QItemSelectionModel *m_VariableSelectionModel;
121
121
122
122
123 TimeController *m_TimeController{nullptr};
123 TimeController *m_TimeController{nullptr};
124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
124 std::unique_ptr<VariableCacheStrategy> m_VariableCacheStrategy;
125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
125 std::unique_ptr<VariableAcquisitionWorker> m_VariableAcquisitionWorker;
126 QThread m_VariableAcquisitionWorkerThread;
126 QThread m_VariableAcquisitionWorkerThread;
127
127
128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
128 std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> >
129 m_VariableToProviderMap;
129 m_VariableToProviderMap;
130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
130 std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap;
131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
131 std::map<QUuid, std::shared_ptr<VariableSynchronizationGroup> >
132 m_GroupIdToVariableSynchronizationGroupMap;
132 m_GroupIdToVariableSynchronizationGroupMap;
133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
133 std::map<QUuid, QUuid> m_VariableIdGroupIdMap;
134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
134 std::set<std::shared_ptr<IDataProvider> > m_ProviderSet;
135
135
136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
136 std::map<QUuid, std::map<QUuid, VariableRequest> > m_VarRequestIdToVarIdVarRequestMap;
137
137
138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
138 std::map<QUuid, std::deque<QUuid> > m_VarIdToVarRequestIdQueueMap;
139
139
140
140
141 VariableController *q;
141 VariableController *q;
142 };
142 };
143
143
144
144
145 VariableController::VariableController(QObject *parent)
145 VariableController::VariableController(QObject *parent)
146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
146 : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)}
147 {
147 {
148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
148 qCDebug(LOG_VariableController()) << tr("VariableController construction")
149 << QThread::currentThread();
149 << QThread::currentThread();
150
150
151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
151 connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this,
152 &VariableController::onAbortProgressRequested);
152 &VariableController::onAbortProgressRequested);
153
153
154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
154 connect(impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::dataProvided, this,
155 &VariableController::onDataProvided);
155 &VariableController::onDataProvided);
156 connect(impl->m_VariableAcquisitionWorker.get(),
156 connect(impl->m_VariableAcquisitionWorker.get(),
157 &VariableAcquisitionWorker::variableRequestInProgress, this,
157 &VariableAcquisitionWorker::variableRequestInProgress, this,
158 &VariableController::onVariableRetrieveDataInProgress);
158 &VariableController::onVariableRetrieveDataInProgress);
159
159
160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
160 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::started,
161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
161 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::initialize);
162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
162 connect(&impl->m_VariableAcquisitionWorkerThread, &QThread::finished,
163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
163 impl->m_VariableAcquisitionWorker.get(), &VariableAcquisitionWorker::finalize);
164
164
165
165
166 impl->m_VariableAcquisitionWorkerThread.start();
166 impl->m_VariableAcquisitionWorkerThread.start();
167 }
167 }
168
168
169 VariableController::~VariableController()
169 VariableController::~VariableController()
170 {
170 {
171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
171 qCDebug(LOG_VariableController()) << tr("VariableController destruction")
172 << QThread::currentThread();
172 << QThread::currentThread();
173 this->waitForFinish();
173 this->waitForFinish();
174 }
174 }
175
175
176 VariableModel *VariableController::variableModel() noexcept
176 VariableModel *VariableController::variableModel() noexcept
177 {
177 {
178 return impl->m_VariableModel;
178 return impl->m_VariableModel;
179 }
179 }
180
180
181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
181 QItemSelectionModel *VariableController::variableSelectionModel() noexcept
182 {
182 {
183 return impl->m_VariableSelectionModel;
183 return impl->m_VariableSelectionModel;
184 }
184 }
185
185
186 void VariableController::setTimeController(TimeController *timeController) noexcept
186 void VariableController::setTimeController(TimeController *timeController) noexcept
187 {
187 {
188 impl->m_TimeController = timeController;
188 impl->m_TimeController = timeController;
189 }
189 }
190
190
191 std::shared_ptr<Variable>
192 VariableController::cloneVariable(std::shared_ptr<Variable> variable) noexcept
193 {
194 }
195
191 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
196 void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept
192 {
197 {
193 if (!variable) {
198 if (!variable) {
194 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
199 qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null";
195 return;
200 return;
196 }
201 }
197
202
198 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
203 // Spreads in SciQlop that the variable will be deleted, so that potential receivers can
199 // make some treatments before the deletion
204 // make some treatments before the deletion
200 emit variableAboutToBeDeleted(variable);
205 emit variableAboutToBeDeleted(variable);
201
206
202 // Deletes identifier
207 // Deletes identifier
203 impl->m_VariableToIdentifierMap.erase(variable);
208 impl->m_VariableToIdentifierMap.erase(variable);
204
209
205 // Deletes provider
210 // Deletes provider
206 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
211 auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable);
207 qCDebug(LOG_VariableController())
212 qCDebug(LOG_VariableController())
208 << tr("Number of providers deleted for variable %1: %2")
213 << tr("Number of providers deleted for variable %1: %2")
209 .arg(variable->name(), QString::number(nbProvidersDeleted));
214 .arg(variable->name(), QString::number(nbProvidersDeleted));
210
215
211
216
212 // Deletes from model
217 // Deletes from model
213 impl->m_VariableModel->deleteVariable(variable);
218 impl->m_VariableModel->deleteVariable(variable);
214 }
219 }
215
220
216 void VariableController::deleteVariables(
221 void VariableController::deleteVariables(
217 const QVector<std::shared_ptr<Variable> > &variables) noexcept
222 const QVector<std::shared_ptr<Variable> > &variables) noexcept
218 {
223 {
219 for (auto variable : qAsConst(variables)) {
224 for (auto variable : qAsConst(variables)) {
220 deleteVariable(variable);
225 deleteVariable(variable);
221 }
226 }
222 }
227 }
223
228
224 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
229 void VariableController::abortProgress(std::shared_ptr<Variable> variable)
225 {
230 {
226 }
231 }
227
232
228 std::shared_ptr<Variable>
233 std::shared_ptr<Variable>
229 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
234 VariableController::createVariable(const QString &name, const QVariantHash &metadata,
230 std::shared_ptr<IDataProvider> provider) noexcept
235 std::shared_ptr<IDataProvider> provider) noexcept
231 {
236 {
232 if (!impl->m_TimeController) {
237 if (!impl->m_TimeController) {
233 qCCritical(LOG_VariableController())
238 qCCritical(LOG_VariableController())
234 << tr("Impossible to create variable: The time controller is null");
239 << tr("Impossible to create variable: The time controller is null");
235 return nullptr;
240 return nullptr;
236 }
241 }
237
242
238 auto range = impl->m_TimeController->dateTime();
243 auto range = impl->m_TimeController->dateTime();
239
244
240 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
245 if (auto newVariable = impl->m_VariableModel->createVariable(name, range, metadata)) {
241 auto identifier = QUuid::createUuid();
246 auto identifier = QUuid::createUuid();
242
247
243 // store the provider
248 // store the provider
244 impl->registerProvider(provider);
249 impl->registerProvider(provider);
245
250
246 // Associate the provider
251 // Associate the provider
247 impl->m_VariableToProviderMap[newVariable] = provider;
252 impl->m_VariableToProviderMap[newVariable] = provider;
248 impl->m_VariableToIdentifierMap[newVariable] = identifier;
253 impl->m_VariableToIdentifierMap[newVariable] = identifier;
249
254
250
255
251 auto varRequestId = QUuid::createUuid();
256 auto varRequestId = QUuid::createUuid();
252 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
257 qCInfo(LOG_VariableController()) << "processRequest for" << name << varRequestId;
253 impl->processRequest(newVariable, range, varRequestId);
258 impl->processRequest(newVariable, range, varRequestId);
254 impl->updateVariableRequest(varRequestId);
259 impl->updateVariableRequest(varRequestId);
255
260
256 return newVariable;
261 return newVariable;
257 }
262 }
258 }
263 }
259
264
260 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
265 void VariableController::onDateTimeOnSelection(const SqpRange &dateTime)
261 {
266 {
262 // TODO check synchronisation and Rescale
267 // TODO check synchronisation and Rescale
263 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
268 qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection"
264 << QThread::currentThread()->objectName();
269 << QThread::currentThread()->objectName();
265 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
270 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
266 auto varRequestId = QUuid::createUuid();
271 auto varRequestId = QUuid::createUuid();
267
272
268 for (const auto &selectedRow : qAsConst(selectedRows)) {
273 for (const auto &selectedRow : qAsConst(selectedRows)) {
269 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
274 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
270 selectedVariable->setRange(dateTime);
275 selectedVariable->setRange(dateTime);
271 impl->processRequest(selectedVariable, dateTime, varRequestId);
276 impl->processRequest(selectedVariable, dateTime, varRequestId);
272
277
273 // notify that rescale operation has to be done
278 // notify that rescale operation has to be done
274 emit rangeChanged(selectedVariable, dateTime);
279 emit rangeChanged(selectedVariable, dateTime);
275 }
280 }
276 }
281 }
277 impl->updateVariableRequest(varRequestId);
282 impl->updateVariableRequest(varRequestId);
278 }
283 }
279
284
280 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
285 void VariableController::onDataProvided(QUuid vIdentifier, const SqpRange &rangeRequested,
281 const SqpRange &cacheRangeRequested,
286 const SqpRange &cacheRangeRequested,
282 QVector<AcquisitionDataPacket> dataAcquired)
287 QVector<AcquisitionDataPacket> dataAcquired)
283 {
288 {
284 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
289 auto retrievedDataSeries = impl->retrieveDataSeries(dataAcquired);
285 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
290 auto varRequestId = impl->acceptVariableRequest(vIdentifier, retrievedDataSeries);
286 if (!varRequestId.isNull()) {
291 if (!varRequestId.isNull()) {
287 impl->updateVariableRequest(varRequestId);
292 impl->updateVariableRequest(varRequestId);
288 }
293 }
289 }
294 }
290
295
291 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
296 void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress)
292 {
297 {
293 if (auto var = impl->findVariable(identifier)) {
298 if (auto var = impl->findVariable(identifier)) {
294 impl->m_VariableModel->setDataProgress(var, progress);
299 impl->m_VariableModel->setDataProgress(var, progress);
295 }
300 }
296 else {
301 else {
297 qCCritical(LOG_VariableController())
302 qCCritical(LOG_VariableController())
298 << tr("Impossible to notify progression of a null variable");
303 << tr("Impossible to notify progression of a null variable");
299 }
304 }
300 }
305 }
301
306
302 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
307 void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable)
303 {
308 {
304 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
309 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested"
305 << QThread::currentThread()->objectName();
310 << QThread::currentThread()->objectName();
306
311
307 auto it = impl->m_VariableToIdentifierMap.find(variable);
312 auto it = impl->m_VariableToIdentifierMap.find(variable);
308 if (it != impl->m_VariableToIdentifierMap.cend()) {
313 if (it != impl->m_VariableToIdentifierMap.cend()) {
309 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
314 impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second);
310 }
315 }
311 else {
316 else {
312 qCWarning(LOG_VariableController())
317 qCWarning(LOG_VariableController())
313 << tr("Aborting progression of inexistant variable detected !!!")
318 << tr("Aborting progression of inexistant variable detected !!!")
314 << QThread::currentThread()->objectName();
319 << QThread::currentThread()->objectName();
315 }
320 }
316 }
321 }
317
322
318 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
323 void VariableController::onAddSynchronizationGroupId(QUuid synchronizationGroupId)
319 {
324 {
320 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
325 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronizationGroupId"
321 << QThread::currentThread()->objectName()
326 << QThread::currentThread()->objectName()
322 << synchronizationGroupId;
327 << synchronizationGroupId;
323 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
328 auto vSynchroGroup = std::make_shared<VariableSynchronizationGroup>();
324 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
329 impl->m_GroupIdToVariableSynchronizationGroupMap.insert(
325 std::make_pair(synchronizationGroupId, vSynchroGroup));
330 std::make_pair(synchronizationGroupId, vSynchroGroup));
326 }
331 }
327
332
328 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
333 void VariableController::onRemoveSynchronizationGroupId(QUuid synchronizationGroupId)
329 {
334 {
330 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
335 impl->m_GroupIdToVariableSynchronizationGroupMap.erase(synchronizationGroupId);
331 }
336 }
332
337
333 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
338 void VariableController::onAddSynchronized(std::shared_ptr<Variable> variable,
334 QUuid synchronizationGroupId)
339 QUuid synchronizationGroupId)
335
340
336 {
341 {
337 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
342 qCDebug(LOG_VariableController()) << "TORM: VariableController::onAddSynchronized"
338 << synchronizationGroupId;
343 << synchronizationGroupId;
339 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
344 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(variable);
340 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
345 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
341 auto groupIdToVSGIt
346 auto groupIdToVSGIt
342 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
347 = impl->m_GroupIdToVariableSynchronizationGroupMap.find(synchronizationGroupId);
343 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
348 if (groupIdToVSGIt != impl->m_GroupIdToVariableSynchronizationGroupMap.cend()) {
344 impl->m_VariableIdGroupIdMap.insert(
349 impl->m_VariableIdGroupIdMap.insert(
345 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
350 std::make_pair(varToVarIdIt->second, synchronizationGroupId));
346 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
351 groupIdToVSGIt->second->addVariableId(varToVarIdIt->second);
347 }
352 }
348 else {
353 else {
349 qCCritical(LOG_VariableController())
354 qCCritical(LOG_VariableController())
350 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
355 << tr("Impossible to synchronize a variable with an unknown sycnhronization group")
351 << variable->name();
356 << variable->name();
352 }
357 }
353 }
358 }
354 else {
359 else {
355 qCCritical(LOG_VariableController())
360 qCCritical(LOG_VariableController())
356 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
361 << tr("Impossible to synchronize a variable with no identifier") << variable->name();
357 }
362 }
358 }
363 }
359
364
360
365
361 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
366 void VariableController::onRequestDataLoading(QVector<std::shared_ptr<Variable> > variables,
362 const SqpRange &range, const SqpRange &oldRange,
367 const SqpRange &range, const SqpRange &oldRange,
363 bool synchronise)
368 bool synchronise)
364 {
369 {
365 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
370 // NOTE: oldRange isn't really necessary since oldRange == variable->range().
366
371
367 // we want to load data of the variable for the dateTime.
372 // we want to load data of the variable for the dateTime.
368 // First we check if the cache contains some of them.
373 // First we check if the cache contains some of them.
369 // For the other, we ask the provider to give them.
374 // For the other, we ask the provider to give them.
370
375
371 auto varRequestId = QUuid::createUuid();
376 auto varRequestId = QUuid::createUuid();
372 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
377 qCInfo(LOG_VariableController()) << "VariableController::onRequestDataLoading"
373 << QThread::currentThread()->objectName() << varRequestId;
378 << QThread::currentThread()->objectName() << varRequestId;
374
379
375 for (const auto &var : variables) {
380 for (const auto &var : variables) {
376 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
381 qCDebug(LOG_VariableController()) << "processRequest for" << var->name() << varRequestId;
377 impl->processRequest(var, range, varRequestId);
382 impl->processRequest(var, range, varRequestId);
378 }
383 }
379
384
380 if (synchronise) {
385 if (synchronise) {
381 // Get the group ids
386 // Get the group ids
382 qCDebug(LOG_VariableController())
387 qCDebug(LOG_VariableController())
383 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
388 << "TORM VariableController::onRequestDataLoading for synchro var ENABLE";
384 auto groupIds = std::set<QUuid>{};
389 auto groupIds = std::set<QUuid>{};
385 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
390 auto groupIdToOldRangeMap = std::map<QUuid, SqpRange>{};
386 for (const auto &var : variables) {
391 for (const auto &var : variables) {
387 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
392 auto varToVarIdIt = impl->m_VariableToIdentifierMap.find(var);
388 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
393 if (varToVarIdIt != impl->m_VariableToIdentifierMap.cend()) {
389 auto vId = varToVarIdIt->second;
394 auto vId = varToVarIdIt->second;
390 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
395 auto varIdToGroupIdIt = impl->m_VariableIdGroupIdMap.find(vId);
391 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
396 if (varIdToGroupIdIt != impl->m_VariableIdGroupIdMap.cend()) {
392 auto gId = varIdToGroupIdIt->second;
397 auto gId = varIdToGroupIdIt->second;
393 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
398 groupIdToOldRangeMap.insert(std::make_pair(gId, var->range()));
394 if (groupIds.find(gId) == groupIds.cend()) {
399 if (groupIds.find(gId) == groupIds.cend()) {
395 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
400 qCDebug(LOG_VariableController()) << "Synchro detect group " << gId;
396 groupIds.insert(gId);
401 groupIds.insert(gId);
397 }
402 }
398 }
403 }
399 }
404 }
400 }
405 }
401
406
402 // We assume here all group ids exist
407 // We assume here all group ids exist
403 for (const auto &gId : groupIds) {
408 for (const auto &gId : groupIds) {
404 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
409 auto vSynchronizationGroup = impl->m_GroupIdToVariableSynchronizationGroupMap.at(gId);
405 auto vSyncIds = vSynchronizationGroup->getIds();
410 auto vSyncIds = vSynchronizationGroup->getIds();
406 qCDebug(LOG_VariableController()) << "Var in synchro group ";
411 qCDebug(LOG_VariableController()) << "Var in synchro group ";
407 for (auto vId : vSyncIds) {
412 for (auto vId : vSyncIds) {
408 auto var = impl->findVariable(vId);
413 auto var = impl->findVariable(vId);
409
414
410 // Don't process already processed var
415 // Don't process already processed var
411 if (!variables.contains(var)) {
416 if (!variables.contains(var)) {
412 if (var != nullptr) {
417 if (var != nullptr) {
413 qCDebug(LOG_VariableController()) << "processRequest synchro for"
418 qCDebug(LOG_VariableController()) << "processRequest synchro for"
414 << var->name();
419 << var->name();
415 auto vSyncRangeRequested = computeSynchroRangeRequested(
420 auto vSyncRangeRequested = computeSynchroRangeRequested(
416 var->range(), range, groupIdToOldRangeMap.at(gId));
421 var->range(), range, groupIdToOldRangeMap.at(gId));
417 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
422 qCDebug(LOG_VariableController()) << "synchro RR" << vSyncRangeRequested;
418 impl->processRequest(var, vSyncRangeRequested, varRequestId);
423 impl->processRequest(var, vSyncRangeRequested, varRequestId);
419 }
424 }
420 else {
425 else {
421 qCCritical(LOG_VariableController())
426 qCCritical(LOG_VariableController())
422
427
423 << tr("Impossible to synchronize a null variable");
428 << tr("Impossible to synchronize a null variable");
424 }
429 }
425 }
430 }
426 }
431 }
427 }
432 }
428 }
433 }
429
434
430 impl->updateVariableRequest(varRequestId);
435 impl->updateVariableRequest(varRequestId);
431 }
436 }
432
437
433
438
434 void VariableController::initialize()
439 void VariableController::initialize()
435 {
440 {
436 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
441 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
437 impl->m_WorkingMutex.lock();
442 impl->m_WorkingMutex.lock();
438 qCDebug(LOG_VariableController()) << tr("VariableController init END");
443 qCDebug(LOG_VariableController()) << tr("VariableController init END");
439 }
444 }
440
445
441 void VariableController::finalize()
446 void VariableController::finalize()
442 {
447 {
443 impl->m_WorkingMutex.unlock();
448 impl->m_WorkingMutex.unlock();
444 }
449 }
445
450
446 void VariableController::waitForFinish()
451 void VariableController::waitForFinish()
447 {
452 {
448 QMutexLocker locker{&impl->m_WorkingMutex};
453 QMutexLocker locker{&impl->m_WorkingMutex};
449 }
454 }
450
455
451 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
456 AcquisitionZoomType VariableController::getZoomType(const SqpRange &range, const SqpRange &oldRange)
452 {
457 {
453 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
458 // t1.m_TStart <= t2.m_TStart && t2.m_TEnd <= t1.m_TEnd
454 auto zoomType = AcquisitionZoomType::Unknown;
459 auto zoomType = AcquisitionZoomType::Unknown;
455 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
460 if (range.m_TStart <= oldRange.m_TStart && oldRange.m_TEnd <= range.m_TEnd) {
456 zoomType = AcquisitionZoomType::ZoomOut;
461 zoomType = AcquisitionZoomType::ZoomOut;
457 }
462 }
458 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
463 else if (range.m_TStart > oldRange.m_TStart && range.m_TEnd > oldRange.m_TEnd) {
459 zoomType = AcquisitionZoomType::PanRight;
464 zoomType = AcquisitionZoomType::PanRight;
460 }
465 }
461 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
466 else if (range.m_TStart < oldRange.m_TStart && range.m_TEnd < oldRange.m_TEnd) {
462 zoomType = AcquisitionZoomType::PanLeft;
467 zoomType = AcquisitionZoomType::PanLeft;
463 }
468 }
464 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
469 else if (range.m_TStart > oldRange.m_TStart && oldRange.m_TEnd > range.m_TEnd) {
465 zoomType = AcquisitionZoomType::ZoomIn;
470 zoomType = AcquisitionZoomType::ZoomIn;
466 }
471 }
467 else {
472 else {
468 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
473 qCCritical(LOG_VariableController()) << "getZoomType: Unknown type detected";
469 }
474 }
470 return zoomType;
475 return zoomType;
471 }
476 }
472
477
473 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
478 void VariableController::VariableControllerPrivate::processRequest(std::shared_ptr<Variable> var,
474 const SqpRange &rangeRequested,
479 const SqpRange &rangeRequested,
475 QUuid varRequestId)
480 QUuid varRequestId)
476 {
481 {
477
482
478 // TODO: protect at
483 // TODO: protect at
479 auto varRequest = VariableRequest{};
484 auto varRequest = VariableRequest{};
480 auto varId = m_VariableToIdentifierMap.at(var);
485 auto varId = m_VariableToIdentifierMap.at(var);
481
486
482 auto varStrategyRangesRequested
487 auto varStrategyRangesRequested
483 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
488 = m_VariableCacheStrategy->computeStrategyRanges(var->range(), rangeRequested);
484 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
489 auto notInCacheRangeList = var->provideNotInCacheRangeList(varStrategyRangesRequested.second);
485 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
490 auto inCacheRangeList = var->provideInCacheRangeList(varStrategyRangesRequested.second);
486
491
487 if (!notInCacheRangeList.empty()) {
492 if (!notInCacheRangeList.empty()) {
488 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
493 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
489 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
494 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
490 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
495 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest RR ") << rangeRequested;
491 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
496 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest R ")
492 << varStrategyRangesRequested.first;
497 << varStrategyRangesRequested.first;
493 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
498 qCDebug(LOG_VariableAcquisitionWorker()) << tr("TORM processRequest CR ")
494 << varStrategyRangesRequested.second;
499 << varStrategyRangesRequested.second;
495 // store VarRequest
500 // store VarRequest
496 storeVariableRequest(varId, varRequestId, varRequest);
501 storeVariableRequest(varId, varRequestId, varRequest);
497
502
498 auto varProvider = m_VariableToProviderMap.at(var);
503 auto varProvider = m_VariableToProviderMap.at(var);
499 if (varProvider != nullptr) {
504 if (varProvider != nullptr) {
500 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
505 auto varRequestIdCanceled = m_VariableAcquisitionWorker->pushVariableRequest(
501 varRequestId, varId, varStrategyRangesRequested.first,
506 varRequestId, varId, varStrategyRangesRequested.first,
502 varStrategyRangesRequested.second,
507 varStrategyRangesRequested.second,
503 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
508 DataProviderParameters{std::move(notInCacheRangeList), var->metadata()},
504 varProvider);
509 varProvider);
505
510
506 if (!varRequestIdCanceled.isNull()) {
511 if (!varRequestIdCanceled.isNull()) {
507 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
512 qCInfo(LOG_VariableAcquisitionWorker()) << tr("varRequestIdCanceled: ")
508 << varRequestIdCanceled;
513 << varRequestIdCanceled;
509 cancelVariableRequest(varRequestIdCanceled);
514 cancelVariableRequest(varRequestIdCanceled);
510 }
515 }
511 }
516 }
512 else {
517 else {
513 qCCritical(LOG_VariableController())
518 qCCritical(LOG_VariableController())
514 << "Impossible to provide data with a null provider";
519 << "Impossible to provide data with a null provider";
515 }
520 }
516
521
517 if (!inCacheRangeList.empty()) {
522 if (!inCacheRangeList.empty()) {
518 emit q->updateVarDisplaying(var, inCacheRangeList.first());
523 emit q->updateVarDisplaying(var, inCacheRangeList.first());
519 }
524 }
520 }
525 }
521 else {
526 else {
522
527
523 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
528 varRequest.m_RangeRequested = varStrategyRangesRequested.first;
524 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
529 varRequest.m_CacheRangeRequested = varStrategyRangesRequested.second;
525 // store VarRequest
530 // store VarRequest
526 storeVariableRequest(varId, varRequestId, varRequest);
531 storeVariableRequest(varId, varRequestId, varRequest);
527 acceptVariableRequest(varId,
532 acceptVariableRequest(varId,
528 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
533 var->dataSeries()->subDataSeries(varStrategyRangesRequested.second));
529 }
534 }
530 }
535 }
531
536
532 std::shared_ptr<Variable>
537 std::shared_ptr<Variable>
533 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
538 VariableController::VariableControllerPrivate::findVariable(QUuid vIdentifier)
534 {
539 {
535 std::shared_ptr<Variable> var;
540 std::shared_ptr<Variable> var;
536 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
541 auto findReply = [vIdentifier](const auto &entry) { return vIdentifier == entry.second; };
537
542
538 auto end = m_VariableToIdentifierMap.cend();
543 auto end = m_VariableToIdentifierMap.cend();
539 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
544 auto it = std::find_if(m_VariableToIdentifierMap.cbegin(), end, findReply);
540 if (it != end) {
545 if (it != end) {
541 var = it->first;
546 var = it->first;
542 }
547 }
543 else {
548 else {
544 qCCritical(LOG_VariableController())
549 qCCritical(LOG_VariableController())
545 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
550 << tr("Impossible to find the variable with the identifier: ") << vIdentifier;
546 }
551 }
547
552
548 return var;
553 return var;
549 }
554 }
550
555
551 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
556 std::shared_ptr<IDataSeries> VariableController::VariableControllerPrivate::retrieveDataSeries(
552 const QVector<AcquisitionDataPacket> acqDataPacketVector)
557 const QVector<AcquisitionDataPacket> acqDataPacketVector)
553 {
558 {
554 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
559 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size")
555 << acqDataPacketVector.size();
560 << acqDataPacketVector.size();
556 std::shared_ptr<IDataSeries> dataSeries;
561 std::shared_ptr<IDataSeries> dataSeries;
557 if (!acqDataPacketVector.isEmpty()) {
562 if (!acqDataPacketVector.isEmpty()) {
558 dataSeries = acqDataPacketVector[0].m_DateSeries;
563 dataSeries = acqDataPacketVector[0].m_DateSeries;
559 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
564 for (int i = 1; i < acqDataPacketVector.size(); ++i) {
560 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
565 dataSeries->merge(acqDataPacketVector[i].m_DateSeries.get());
561 }
566 }
562 }
567 }
563 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
568 qCDebug(LOG_VariableController()) << tr("TORM: retrieveDataSeries acqDataPacketVector size END")
564 << acqDataPacketVector.size();
569 << acqDataPacketVector.size();
565 return dataSeries;
570 return dataSeries;
566 }
571 }
567
572
568 void VariableController::VariableControllerPrivate::registerProvider(
573 void VariableController::VariableControllerPrivate::registerProvider(
569 std::shared_ptr<IDataProvider> provider)
574 std::shared_ptr<IDataProvider> provider)
570 {
575 {
571 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
576 if (m_ProviderSet.find(provider) == m_ProviderSet.end()) {
572 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
577 qCDebug(LOG_VariableController()) << tr("Registering of a new provider")
573 << provider->objectName();
578 << provider->objectName();
574 m_ProviderSet.insert(provider);
579 m_ProviderSet.insert(provider);
575 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
580 connect(provider.get(), &IDataProvider::dataProvided, m_VariableAcquisitionWorker.get(),
576 &VariableAcquisitionWorker::onVariableDataAcquired);
581 &VariableAcquisitionWorker::onVariableDataAcquired);
577 connect(provider.get(), &IDataProvider::dataProvidedProgress,
582 connect(provider.get(), &IDataProvider::dataProvidedProgress,
578 m_VariableAcquisitionWorker.get(),
583 m_VariableAcquisitionWorker.get(),
579 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
584 &VariableAcquisitionWorker::onVariableRetrieveDataInProgress);
580 }
585 }
581 else {
586 else {
582 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
587 qCDebug(LOG_VariableController()) << tr("Cannot register provider, it already exists ");
583 }
588 }
584 }
589 }
585
590
586 void VariableController::VariableControllerPrivate::storeVariableRequest(
591 void VariableController::VariableControllerPrivate::storeVariableRequest(
587 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
592 QUuid varId, QUuid varRequestId, const VariableRequest &varRequest)
588 {
593 {
589 // First request for the variable. we can create an entry for it
594 // First request for the variable. we can create an entry for it
590 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
595 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
591 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
596 if (varIdToVarRequestIdQueueMapIt == m_VarIdToVarRequestIdQueueMap.cend()) {
592 auto varRequestIdQueue = std::deque<QUuid>{};
597 auto varRequestIdQueue = std::deque<QUuid>{};
593 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
598 qCDebug(LOG_VariableController()) << tr("Store REQUEST in QUEUE");
594 varRequestIdQueue.push_back(varRequestId);
599 varRequestIdQueue.push_back(varRequestId);
595 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
600 m_VarIdToVarRequestIdQueueMap.insert(std::make_pair(varId, std::move(varRequestIdQueue)));
596 }
601 }
597 else {
602 else {
598 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
603 qCDebug(LOG_VariableController()) << tr("Store REQUEST in EXISTING QUEUE");
599 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
604 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
600 varRequestIdQueue.push_back(varRequestId);
605 varRequestIdQueue.push_back(varRequestId);
601 }
606 }
602
607
603 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
608 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
604 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
609 if (varRequestIdToVarIdVarRequestMapIt == m_VarRequestIdToVarIdVarRequestMap.cend()) {
605 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
610 auto varIdToVarRequestMap = std::map<QUuid, VariableRequest>{};
606 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
611 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
607 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
612 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in MAP");
608 m_VarRequestIdToVarIdVarRequestMap.insert(
613 m_VarRequestIdToVarIdVarRequestMap.insert(
609 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
614 std::make_pair(varRequestId, std::move(varIdToVarRequestMap)));
610 }
615 }
611 else {
616 else {
612 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
617 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
613 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
618 qCDebug(LOG_VariableController()) << tr("Store REQUESTID in EXISTING MAP");
614 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
619 varIdToVarRequestMap.insert(std::make_pair(varId, varRequest));
615 }
620 }
616 }
621 }
617
622
618 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
623 QUuid VariableController::VariableControllerPrivate::acceptVariableRequest(
619 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
624 QUuid varId, std::shared_ptr<IDataSeries> dataSeries)
620 {
625 {
621 QUuid varRequestId;
626 QUuid varRequestId;
622 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
627 auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.find(varId);
623 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
628 if (varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.cend()) {
624 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
629 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
625 varRequestId = varRequestIdQueue.front();
630 varRequestId = varRequestIdQueue.front();
626 auto varRequestIdToVarIdVarRequestMapIt
631 auto varRequestIdToVarIdVarRequestMapIt
627 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
632 = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
628 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
633 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
629 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
634 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
630 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
635 auto varIdToVarRequestMapIt = varIdToVarRequestMap.find(varId);
631 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
636 if (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) {
632 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
637 qCDebug(LOG_VariableController()) << tr("acceptVariableRequest");
633 auto &varRequest = varIdToVarRequestMapIt->second;
638 auto &varRequest = varIdToVarRequestMapIt->second;
634 varRequest.m_DataSeries = dataSeries;
639 varRequest.m_DataSeries = dataSeries;
635 varRequest.m_CanUpdate = true;
640 varRequest.m_CanUpdate = true;
636 }
641 }
637 else {
642 else {
638 qCDebug(LOG_VariableController())
643 qCDebug(LOG_VariableController())
639 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
644 << tr("Impossible to acceptVariableRequest of a unknown variable id attached "
640 "to a variableRequestId")
645 "to a variableRequestId")
641 << varRequestId << varId;
646 << varRequestId << varId;
642 }
647 }
643 }
648 }
644 else {
649 else {
645 qCCritical(LOG_VariableController())
650 qCCritical(LOG_VariableController())
646 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
651 << tr("Impossible to acceptVariableRequest of a unknown variableRequestId")
647 << varRequestId;
652 << varRequestId;
648 }
653 }
649
654
650 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
655 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in QUEUE ?")
651 << varRequestIdQueue.size();
656 << varRequestIdQueue.size();
652 varRequestIdQueue.pop_front();
657 varRequestIdQueue.pop_front();
653 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
658 qCDebug(LOG_VariableController()) << tr("2: erase REQUEST in QUEUE ?")
654 << varRequestIdQueue.size();
659 << varRequestIdQueue.size();
655 if (varRequestIdQueue.empty()) {
660 if (varRequestIdQueue.empty()) {
656 m_VarIdToVarRequestIdQueueMap.erase(varId);
661 m_VarIdToVarRequestIdQueueMap.erase(varId);
657 }
662 }
658 }
663 }
659 else {
664 else {
660 qCCritical(LOG_VariableController())
665 qCCritical(LOG_VariableController())
661 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
666 << tr("Impossible to acceptVariableRequest of a unknown variable id") << varId;
662 }
667 }
663
668
664 return varRequestId;
669 return varRequestId;
665 }
670 }
666
671
667 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
672 void VariableController::VariableControllerPrivate::updateVariableRequest(QUuid varRequestId)
668 {
673 {
669
674
670 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
675 auto varRequestIdToVarIdVarRequestMapIt = m_VarRequestIdToVarIdVarRequestMap.find(varRequestId);
671 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
676 if (varRequestIdToVarIdVarRequestMapIt != m_VarRequestIdToVarIdVarRequestMap.cend()) {
672 bool processVariableUpdate = true;
677 bool processVariableUpdate = true;
673 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
678 auto &varIdToVarRequestMap = varRequestIdToVarIdVarRequestMapIt->second;
674 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
679 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
675 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
680 (varIdToVarRequestMapIt != varIdToVarRequestMap.cend()) && processVariableUpdate;
676 ++varIdToVarRequestMapIt) {
681 ++varIdToVarRequestMapIt) {
677 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
682 processVariableUpdate &= varIdToVarRequestMapIt->second.m_CanUpdate;
678 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
683 qCDebug(LOG_VariableController()) << tr("updateVariableRequest")
679 << processVariableUpdate;
684 << processVariableUpdate;
680 }
685 }
681
686
682 if (processVariableUpdate) {
687 if (processVariableUpdate) {
683 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
688 for (auto varIdToVarRequestMapIt = varIdToVarRequestMap.cbegin();
684 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
689 varIdToVarRequestMapIt != varIdToVarRequestMap.cend(); ++varIdToVarRequestMapIt) {
685 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
690 if (auto var = findVariable(varIdToVarRequestMapIt->first)) {
686 auto &varRequest = varIdToVarRequestMapIt->second;
691 auto &varRequest = varIdToVarRequestMapIt->second;
687 var->setRange(varRequest.m_RangeRequested);
692 var->setRange(varRequest.m_RangeRequested);
688 var->setCacheRange(varRequest.m_CacheRangeRequested);
693 var->setCacheRange(varRequest.m_CacheRangeRequested);
689 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
694 qCDebug(LOG_VariableController()) << tr("1: onDataProvided")
690 << varRequest.m_RangeRequested;
695 << varRequest.m_RangeRequested;
691 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
696 qCDebug(LOG_VariableController()) << tr("2: onDataProvided")
692 << varRequest.m_CacheRangeRequested;
697 << varRequest.m_CacheRangeRequested;
693 var->mergeDataSeries(varRequest.m_DataSeries);
698 var->mergeDataSeries(varRequest.m_DataSeries);
694 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
699 qCDebug(LOG_VariableController()) << tr("3: onDataProvided")
695 << varRequest.m_DataSeries->range();
700 << varRequest.m_DataSeries->range();
696 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
701 qCDebug(LOG_VariableController()) << tr("4: onDataProvided");
697
702
698 /// @todo MPL: confirm
703 /// @todo MPL: confirm
699 // Variable update is notified only if there is no pending request for it
704 // Variable update is notified only if there is no pending request for it
700 if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) {
705 if (m_VarIdToVarRequestIdQueueMap.count(varIdToVarRequestMapIt->first) == 0) {
701 emit var->updated();
706 emit var->updated();
702 }
707 }
703 }
708 }
704 else {
709 else {
705 qCCritical(LOG_VariableController())
710 qCCritical(LOG_VariableController())
706 << tr("Impossible to update data to a null variable");
711 << tr("Impossible to update data to a null variable");
707 }
712 }
708 }
713 }
709
714
710 // cleaning varRequestId
715 // cleaning varRequestId
711 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
716 qCDebug(LOG_VariableController()) << tr("0: erase REQUEST in MAP ?")
712 << m_VarRequestIdToVarIdVarRequestMap.size();
717 << m_VarRequestIdToVarIdVarRequestMap.size();
713 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
718 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
714 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
719 qCDebug(LOG_VariableController()) << tr("1: erase REQUEST in MAP ?")
715 << m_VarRequestIdToVarIdVarRequestMap.size();
720 << m_VarRequestIdToVarIdVarRequestMap.size();
716 }
721 }
717 }
722 }
718 else {
723 else {
719 qCCritical(LOG_VariableController())
724 qCCritical(LOG_VariableController())
720 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
725 << tr("Cannot updateVariableRequest for a unknow varRequestId") << varRequestId;
721 }
726 }
722 }
727 }
723
728
724 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
729 void VariableController::VariableControllerPrivate::cancelVariableRequest(QUuid varRequestId)
725 {
730 {
726 // cleaning varRequestId
731 // cleaning varRequestId
727 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
732 m_VarRequestIdToVarIdVarRequestMap.erase(varRequestId);
728
733
729 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
734 for (auto varIdToVarRequestIdQueueMapIt = m_VarIdToVarRequestIdQueueMap.begin();
730 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
735 varIdToVarRequestIdQueueMapIt != m_VarIdToVarRequestIdQueueMap.end();) {
731 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
736 auto &varRequestIdQueue = varIdToVarRequestIdQueueMapIt->second;
732 varRequestIdQueue.erase(
737 varRequestIdQueue.erase(
733 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
738 std::remove(varRequestIdQueue.begin(), varRequestIdQueue.end(), varRequestId),
734 varRequestIdQueue.end());
739 varRequestIdQueue.end());
735 if (varRequestIdQueue.empty()) {
740 if (varRequestIdQueue.empty()) {
736 varIdToVarRequestIdQueueMapIt
741 varIdToVarRequestIdQueueMapIt
737 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
742 = m_VarIdToVarRequestIdQueueMap.erase(varIdToVarRequestIdQueueMapIt);
738 }
743 }
739 else {
744 else {
740 ++varIdToVarRequestIdQueueMapIt;
745 ++varIdToVarRequestIdQueueMapIt;
741 }
746 }
742 }
747 }
743 }
748 }
@@ -1,222 +1,228
1 #include <Variable/RenameVariableDialog.h>
1 #include <Variable/RenameVariableDialog.h>
2 #include <Variable/Variable.h>
2 #include <Variable/Variable.h>
3 #include <Variable/VariableController.h>
3 #include <Variable/VariableController.h>
4 #include <Variable/VariableInspectorWidget.h>
4 #include <Variable/VariableInspectorWidget.h>
5 #include <Variable/VariableMenuHeaderWidget.h>
5 #include <Variable/VariableMenuHeaderWidget.h>
6 #include <Variable/VariableModel.h>
6 #include <Variable/VariableModel.h>
7
7
8 #include <ui_VariableInspectorWidget.h>
8 #include <ui_VariableInspectorWidget.h>
9
9
10 #include <QMouseEvent>
10 #include <QMouseEvent>
11 #include <QSortFilterProxyModel>
11 #include <QSortFilterProxyModel>
12 #include <QStyledItemDelegate>
12 #include <QStyledItemDelegate>
13 #include <QWidgetAction>
13 #include <QWidgetAction>
14
14
15 #include <SqpApplication.h>
15 #include <SqpApplication.h>
16
16
17 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
17 Q_LOGGING_CATEGORY(LOG_VariableInspectorWidget, "VariableInspectorWidget")
18
18
19
19
20 class QProgressBarItemDelegate : public QStyledItemDelegate {
20 class QProgressBarItemDelegate : public QStyledItemDelegate {
21
21
22 public:
22 public:
23 QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {}
23 QProgressBarItemDelegate(QObject *parent) : QStyledItemDelegate{parent} {}
24
24
25 void paint(QPainter *painter, const QStyleOptionViewItem &option,
25 void paint(QPainter *painter, const QStyleOptionViewItem &option,
26 const QModelIndex &index) const
26 const QModelIndex &index) const
27 {
27 {
28 auto data = index.data(Qt::DisplayRole);
28 auto data = index.data(Qt::DisplayRole);
29 auto progressData = index.data(VariableRoles::ProgressRole);
29 auto progressData = index.data(VariableRoles::ProgressRole);
30 if (data.isValid() && progressData.isValid()) {
30 if (data.isValid() && progressData.isValid()) {
31 auto name = data.value<QString>();
31 auto name = data.value<QString>();
32 auto progress = progressData.value<double>();
32 auto progress = progressData.value<double>();
33 if (progress > 0) {
33 if (progress > 0) {
34 auto cancelButtonWidth = 20;
34 auto cancelButtonWidth = 20;
35 auto progressBarOption = QStyleOptionProgressBar{};
35 auto progressBarOption = QStyleOptionProgressBar{};
36 auto progressRect = option.rect;
36 auto progressRect = option.rect;
37 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
37 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
38 progressBarOption.rect = progressRect;
38 progressBarOption.rect = progressRect;
39 progressBarOption.minimum = 0;
39 progressBarOption.minimum = 0;
40 progressBarOption.maximum = 100;
40 progressBarOption.maximum = 100;
41 progressBarOption.progress = progress;
41 progressBarOption.progress = progress;
42 progressBarOption.text
42 progressBarOption.text
43 = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%");
43 = QString("%1 %2").arg(name).arg(QString::number(progress, 'f', 2) + "%");
44 progressBarOption.textVisible = true;
44 progressBarOption.textVisible = true;
45 progressBarOption.textAlignment = Qt::AlignCenter;
45 progressBarOption.textAlignment = Qt::AlignCenter;
46
46
47
47
48 QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption,
48 QApplication::style()->drawControl(QStyle::CE_ProgressBar, &progressBarOption,
49 painter);
49 painter);
50
50
51 // Cancel button
51 // Cancel button
52 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
52 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
53 option.rect.height());
53 option.rect.height());
54 auto buttonOption = QStyleOptionButton{};
54 auto buttonOption = QStyleOptionButton{};
55 buttonOption.rect = buttonRect;
55 buttonOption.rect = buttonRect;
56 buttonOption.text = "X";
56 buttonOption.text = "X";
57
57
58 QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
58 QApplication::style()->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
59 }
59 }
60 else {
60 else {
61 QStyledItemDelegate::paint(painter, option, index);
61 QStyledItemDelegate::paint(painter, option, index);
62 }
62 }
63 }
63 }
64 else {
64 else {
65 QStyledItemDelegate::paint(painter, option, index);
65 QStyledItemDelegate::paint(painter, option, index);
66 }
66 }
67 }
67 }
68
68
69 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
69 bool editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option,
70 const QModelIndex &index)
70 const QModelIndex &index)
71 {
71 {
72 if (event->type() == QEvent::MouseButtonRelease) {
72 if (event->type() == QEvent::MouseButtonRelease) {
73 auto data = index.data(Qt::DisplayRole);
73 auto data = index.data(Qt::DisplayRole);
74 auto progressData = index.data(VariableRoles::ProgressRole);
74 auto progressData = index.data(VariableRoles::ProgressRole);
75 if (data.isValid() && progressData.isValid()) {
75 if (data.isValid() && progressData.isValid()) {
76 auto cancelButtonWidth = 20;
76 auto cancelButtonWidth = 20;
77 auto progressRect = option.rect;
77 auto progressRect = option.rect;
78 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
78 progressRect.setWidth(progressRect.width() - cancelButtonWidth);
79 // Cancel button
79 // Cancel button
80 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
80 auto buttonRect = QRect(progressRect.right(), option.rect.top(), cancelButtonWidth,
81 option.rect.height());
81 option.rect.height());
82
82
83 auto e = (QMouseEvent *)event;
83 auto e = (QMouseEvent *)event;
84 auto clickX = e->x();
84 auto clickX = e->x();
85 auto clickY = e->y();
85 auto clickY = e->y();
86
86
87 auto x = buttonRect.left(); // the X coordinate
87 auto x = buttonRect.left(); // the X coordinate
88 auto y = buttonRect.top(); // the Y coordinate
88 auto y = buttonRect.top(); // the Y coordinate
89 auto w = buttonRect.width(); // button width
89 auto w = buttonRect.width(); // button width
90 auto h = buttonRect.height(); // button height
90 auto h = buttonRect.height(); // button height
91
91
92 if (clickX > x && clickX < x + w) {
92 if (clickX > x && clickX < x + w) {
93 if (clickY > y && clickY < y + h) {
93 if (clickY > y && clickY < y + h) {
94 auto variableModel = sqpApp->variableController().variableModel();
94 auto variableModel = sqpApp->variableController().variableModel();
95 variableModel->abortProgress(index);
95 variableModel->abortProgress(index);
96 }
96 }
97 }
97 }
98 else {
98 else {
99 QStyledItemDelegate::editorEvent(event, model, option, index);
99 QStyledItemDelegate::editorEvent(event, model, option, index);
100 }
100 }
101 }
101 }
102 else {
102 else {
103 QStyledItemDelegate::editorEvent(event, model, option, index);
103 QStyledItemDelegate::editorEvent(event, model, option, index);
104 }
104 }
105 }
105 }
106 else {
106 else {
107 QStyledItemDelegate::editorEvent(event, model, option, index);
107 QStyledItemDelegate::editorEvent(event, model, option, index);
108 }
108 }
109 }
109 }
110 };
110 };
111
111
112 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
112 VariableInspectorWidget::VariableInspectorWidget(QWidget *parent)
113 : QWidget{parent},
113 : QWidget{parent},
114 ui{new Ui::VariableInspectorWidget},
114 ui{new Ui::VariableInspectorWidget},
115 m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}}
115 m_ProgressBarItemDelegate{new QProgressBarItemDelegate{this}}
116 {
116 {
117 ui->setupUi(this);
117 ui->setupUi(this);
118
118
119 // Sets model for table
119 // Sets model for table
120 // auto sortFilterModel = new QSortFilterProxyModel{this};
120 // auto sortFilterModel = new QSortFilterProxyModel{this};
121 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
121 // sortFilterModel->setSourceModel(sqpApp->variableController().variableModel());
122
122
123 auto variableModel = sqpApp->variableController().variableModel();
123 auto variableModel = sqpApp->variableController().variableModel();
124 ui->tableView->setModel(variableModel);
124 ui->tableView->setModel(variableModel);
125
125
126 // Adds extra signal/slot between view and model, so the view can be updated instantly when
126 // Adds extra signal/slot between view and model, so the view can be updated instantly when
127 // there is a change of data in the model
127 // there is a change of data in the model
128 connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
128 connect(variableModel, SIGNAL(dataChanged(const QModelIndex &, const QModelIndex &)), this,
129 SLOT(refresh()));
129 SLOT(refresh()));
130
130
131 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
131 ui->tableView->setSelectionModel(sqpApp->variableController().variableSelectionModel());
132 ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate);
132 ui->tableView->setItemDelegateForColumn(0, m_ProgressBarItemDelegate);
133
133
134 // Fixes column sizes
134 // Fixes column sizes
135 auto model = ui->tableView->model();
135 auto model = ui->tableView->model();
136 const auto count = model->columnCount();
136 const auto count = model->columnCount();
137 for (auto i = 0; i < count; ++i) {
137 for (auto i = 0; i < count; ++i) {
138 ui->tableView->setColumnWidth(
138 ui->tableView->setColumnWidth(
139 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
139 i, model->headerData(i, Qt::Horizontal, Qt::SizeHintRole).toSize().width());
140 }
140 }
141
141
142 // Sets selection options
142 // Sets selection options
143 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
143 ui->tableView->setSelectionBehavior(QTableView::SelectRows);
144 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
144 ui->tableView->setSelectionMode(QTableView::ExtendedSelection);
145
145
146 // Connection to show a menu when right clicking on the tree
146 // Connection to show a menu when right clicking on the tree
147 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
147 ui->tableView->setContextMenuPolicy(Qt::CustomContextMenu);
148 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
148 connect(ui->tableView, &QTableView::customContextMenuRequested, this,
149 &VariableInspectorWidget::onTableMenuRequested);
149 &VariableInspectorWidget::onTableMenuRequested);
150 }
150 }
151
151
152 VariableInspectorWidget::~VariableInspectorWidget()
152 VariableInspectorWidget::~VariableInspectorWidget()
153 {
153 {
154 delete ui;
154 delete ui;
155 }
155 }
156
156
157 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
157 void VariableInspectorWidget::onTableMenuRequested(const QPoint &pos) noexcept
158 {
158 {
159 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
159 auto selectedRows = ui->tableView->selectionModel()->selectedRows();
160
160
161 // Gets the model to retrieve the underlying selected variables
161 // Gets the model to retrieve the underlying selected variables
162 auto model = sqpApp->variableController().variableModel();
162 auto model = sqpApp->variableController().variableModel();
163 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
163 auto selectedVariables = QVector<std::shared_ptr<Variable> >{};
164 for (const auto &selectedRow : qAsConst(selectedRows)) {
164 for (const auto &selectedRow : qAsConst(selectedRows)) {
165 if (auto selectedVariable = model->variable(selectedRow.row())) {
165 if (auto selectedVariable = model->variable(selectedRow.row())) {
166 selectedVariables.push_back(selectedVariable);
166 selectedVariables.push_back(selectedVariable);
167 }
167 }
168 }
168 }
169
169
170 QMenu tableMenu{};
170 QMenu tableMenu{};
171
171
172 // Emits a signal so that potential receivers can populate the menu before displaying it
172 // Emits a signal so that potential receivers can populate the menu before displaying it
173 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
173 emit tableMenuAboutToBeDisplayed(&tableMenu, selectedVariables);
174
174
175 // Adds menu-specific actions
175 // Adds menu-specific actions
176 if (!selectedVariables.isEmpty()) {
176 if (!selectedVariables.isEmpty()) {
177 tableMenu.addSeparator();
177 tableMenu.addSeparator();
178
178
179 // 'Rename' action (only if one variable selected)
179 // 'Rename' and 'Duplicate' actions (only if one variable selected)
180 if (selectedVariables.size() == 1) {
180 if (selectedVariables.size() == 1) {
181 auto selectedVariable = selectedVariables.front();
181 auto selectedVariable = selectedVariables.front();
182
182
183 auto duplicateFun = [&selectedVariable]() {
184 sqpApp->variableController().cloneVariable(selectedVariable);
185 };
186
187 tableMenu.addAction(tr("Duplicate"), duplicateFun);
188
183 auto renameFun = [&selectedVariable, &model, this]() {
189 auto renameFun = [&selectedVariable, &model, this]() {
184 // Generates forbidden names (names associated to existing variables)
190 // Generates forbidden names (names associated to existing variables)
185 auto allVariables = model->variables();
191 auto allVariables = model->variables();
186 auto forbiddenNames = QVector<QString>(allVariables.size());
192 auto forbiddenNames = QVector<QString>(allVariables.size());
187 std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(),
193 std::transform(allVariables.cbegin(), allVariables.cend(), forbiddenNames.begin(),
188 [](const auto &variable) { return variable->name(); });
194 [](const auto &variable) { return variable->name(); });
189
195
190 RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this};
196 RenameVariableDialog dialog{selectedVariable->name(), forbiddenNames, this};
191 if (dialog.exec() == QDialog::Accepted) {
197 if (dialog.exec() == QDialog::Accepted) {
192 selectedVariable->setName(dialog.name());
198 selectedVariable->setName(dialog.name());
193 }
199 }
194 };
200 };
195
201
196 tableMenu.addAction(tr("Rename..."), renameFun);
202 tableMenu.addAction(tr("Rename..."), renameFun);
197 }
203 }
198
204
199 // 'Delete' action
205 // 'Delete' action
200 auto deleteFun = [&selectedVariables]() {
206 auto deleteFun = [&selectedVariables]() {
201 sqpApp->variableController().deleteVariables(selectedVariables);
207 sqpApp->variableController().deleteVariables(selectedVariables);
202 };
208 };
203
209
204 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
210 tableMenu.addAction(QIcon{":/icones/delete.png"}, tr("Delete"), deleteFun);
205 }
211 }
206
212
207 if (!tableMenu.isEmpty()) {
213 if (!tableMenu.isEmpty()) {
208 // Generates menu header (inserted before first action)
214 // Generates menu header (inserted before first action)
209 auto firstAction = tableMenu.actions().first();
215 auto firstAction = tableMenu.actions().first();
210 auto headerAction = new QWidgetAction{&tableMenu};
216 auto headerAction = new QWidgetAction{&tableMenu};
211 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
217 headerAction->setDefaultWidget(new VariableMenuHeaderWidget{selectedVariables, &tableMenu});
212 tableMenu.insertAction(firstAction, headerAction);
218 tableMenu.insertAction(firstAction, headerAction);
213
219
214 // Displays menu
220 // Displays menu
215 tableMenu.exec(QCursor::pos());
221 tableMenu.exec(QCursor::pos());
216 }
222 }
217 }
223 }
218
224
219 void VariableInspectorWidget::refresh() noexcept
225 void VariableInspectorWidget::refresh() noexcept
220 {
226 {
221 ui->tableView->viewport()->update();
227 ui->tableView->viewport()->update();
222 }
228 }
General Comments 0
You need to be logged in to leave comments. Login now