@@ -1,243 +1,276 | |||||
1 | #include <Variable/Variable.h> |
|
1 | #include <Variable/Variable.h> | |
2 | #include <Variable/VariableCacheController.h> |
|
2 | #include <Variable/VariableCacheController.h> | |
3 | #include <Variable/VariableController.h> |
|
3 | #include <Variable/VariableController.h> | |
4 | #include <Variable/VariableModel.h> |
|
4 | #include <Variable/VariableModel.h> | |
5 |
|
5 | |||
6 | #include <Data/DataProviderParameters.h> |
|
6 | #include <Data/DataProviderParameters.h> | |
7 | #include <Data/IDataProvider.h> |
|
7 | #include <Data/IDataProvider.h> | |
8 | #include <Data/IDataSeries.h> |
|
8 | #include <Data/IDataSeries.h> | |
9 | #include <Time/TimeController.h> |
|
9 | #include <Time/TimeController.h> | |
10 |
|
10 | |||
11 | #include <QMutex> |
|
11 | #include <QMutex> | |
12 | #include <QThread> |
|
12 | #include <QThread> | |
13 | #include <QUuid> |
|
13 | #include <QUuid> | |
14 | #include <QtCore/QItemSelectionModel> |
|
14 | #include <QtCore/QItemSelectionModel> | |
15 |
|
15 | |||
16 | #include <unordered_map> |
|
16 | #include <unordered_map> | |
17 |
|
17 | |||
18 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") |
|
18 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") | |
19 |
|
19 | |||
20 | struct VariableController::VariableControllerPrivate { |
|
20 | struct VariableController::VariableControllerPrivate { | |
21 | explicit VariableControllerPrivate(VariableController *parent) |
|
21 | explicit VariableControllerPrivate(VariableController *parent) | |
22 | : m_WorkingMutex{}, |
|
22 | : m_WorkingMutex{}, | |
23 | m_VariableModel{new VariableModel{parent}}, |
|
23 | m_VariableModel{new VariableModel{parent}}, | |
24 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, |
|
24 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, | |
25 | m_VariableCacheController{std::make_unique<VariableCacheController>()} |
|
25 | m_VariableCacheController{std::make_unique<VariableCacheController>()} | |
26 | { |
|
26 | { | |
27 | } |
|
27 | } | |
28 |
|
28 | |||
|
29 | QVector<SqpDateTime> provideNotInCacheDateTimeList(std::shared_ptr<Variable> variable, | |||
|
30 | const SqpDateTime &dateTime); | |||
|
31 | ||||
29 | QMutex m_WorkingMutex; |
|
32 | QMutex m_WorkingMutex; | |
30 | /// Variable model. The VariableController has the ownership |
|
33 | /// Variable model. The VariableController has the ownership | |
31 | VariableModel *m_VariableModel; |
|
34 | VariableModel *m_VariableModel; | |
32 | QItemSelectionModel *m_VariableSelectionModel; |
|
35 | QItemSelectionModel *m_VariableSelectionModel; | |
33 |
|
36 | |||
34 |
|
37 | |||
35 | TimeController *m_TimeController{nullptr}; |
|
38 | TimeController *m_TimeController{nullptr}; | |
36 | std::unique_ptr<VariableCacheController> m_VariableCacheController; |
|
39 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |
37 |
|
40 | |||
38 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
41 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | |
39 | m_VariableToProviderMap; |
|
42 | m_VariableToProviderMap; | |
40 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; |
|
43 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifierMap; | |
41 | }; |
|
44 | }; | |
42 |
|
45 | |||
43 | VariableController::VariableController(QObject *parent) |
|
46 | VariableController::VariableController(QObject *parent) | |
44 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
47 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | |
45 | { |
|
48 | { | |
46 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
49 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |
47 | << QThread::currentThread(); |
|
50 | << QThread::currentThread(); | |
48 |
|
51 | |||
49 | connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, |
|
52 | connect(impl->m_VariableModel, &VariableModel::abortProgessRequested, this, | |
50 | &VariableController::onAbortProgressRequested); |
|
53 | &VariableController::onAbortProgressRequested); | |
51 | } |
|
54 | } | |
52 |
|
55 | |||
53 | VariableController::~VariableController() |
|
56 | VariableController::~VariableController() | |
54 | { |
|
57 | { | |
55 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
58 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |
56 | << QThread::currentThread(); |
|
59 | << QThread::currentThread(); | |
57 | this->waitForFinish(); |
|
60 | this->waitForFinish(); | |
58 | } |
|
61 | } | |
59 |
|
62 | |||
60 | VariableModel *VariableController::variableModel() noexcept |
|
63 | VariableModel *VariableController::variableModel() noexcept | |
61 | { |
|
64 | { | |
62 | return impl->m_VariableModel; |
|
65 | return impl->m_VariableModel; | |
63 | } |
|
66 | } | |
64 |
|
67 | |||
65 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept |
|
68 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |
66 | { |
|
69 | { | |
67 | return impl->m_VariableSelectionModel; |
|
70 | return impl->m_VariableSelectionModel; | |
68 | } |
|
71 | } | |
69 |
|
72 | |||
70 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
73 | void VariableController::setTimeController(TimeController *timeController) noexcept | |
71 | { |
|
74 | { | |
72 | impl->m_TimeController = timeController; |
|
75 | impl->m_TimeController = timeController; | |
73 | } |
|
76 | } | |
74 |
|
77 | |||
75 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
78 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | |
76 | { |
|
79 | { | |
77 | if (!variable) { |
|
80 | if (!variable) { | |
78 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; |
|
81 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | |
79 | return; |
|
82 | return; | |
80 | } |
|
83 | } | |
81 |
|
84 | |||
82 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can |
|
85 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | |
83 | // make some treatments before the deletion |
|
86 | // make some treatments before the deletion | |
84 | emit variableAboutToBeDeleted(variable); |
|
87 | emit variableAboutToBeDeleted(variable); | |
85 |
|
88 | |||
86 | // Deletes identifier |
|
89 | // Deletes identifier | |
87 | impl->m_VariableToIdentifierMap.erase(variable); |
|
90 | impl->m_VariableToIdentifierMap.erase(variable); | |
88 |
|
91 | |||
89 | // Deletes provider |
|
92 | // Deletes provider | |
90 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); |
|
93 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); | |
91 | qCDebug(LOG_VariableController()) |
|
94 | qCDebug(LOG_VariableController()) | |
92 | << tr("Number of providers deleted for variable %1: %2") |
|
95 | << tr("Number of providers deleted for variable %1: %2") | |
93 | .arg(variable->name(), QString::number(nbProvidersDeleted)); |
|
96 | .arg(variable->name(), QString::number(nbProvidersDeleted)); | |
94 |
|
97 | |||
95 | // Clears cache |
|
98 | // Clears cache | |
96 | impl->m_VariableCacheController->clear(variable); |
|
99 | impl->m_VariableCacheController->clear(variable); | |
97 |
|
100 | |||
98 | // Deletes from model |
|
101 | // Deletes from model | |
99 | impl->m_VariableModel->deleteVariable(variable); |
|
102 | impl->m_VariableModel->deleteVariable(variable); | |
100 | } |
|
103 | } | |
101 |
|
104 | |||
102 | void VariableController::deleteVariables( |
|
105 | void VariableController::deleteVariables( | |
103 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
106 | const QVector<std::shared_ptr<Variable> > &variables) noexcept | |
104 | { |
|
107 | { | |
105 | for (auto variable : qAsConst(variables)) { |
|
108 | for (auto variable : qAsConst(variables)) { | |
106 | deleteVariable(variable); |
|
109 | deleteVariable(variable); | |
107 | } |
|
110 | } | |
108 | } |
|
111 | } | |
109 |
|
112 | |||
110 | void VariableController::abortProgress(std::shared_ptr<Variable> variable) |
|
113 | void VariableController::abortProgress(std::shared_ptr<Variable> variable) | |
111 | { |
|
114 | { | |
112 | } |
|
115 | } | |
113 |
|
116 | |||
114 | void VariableController::createVariable(const QString &name, const QVariantHash &metadata, |
|
117 | void VariableController::createVariable(const QString &name, const QVariantHash &metadata, | |
115 | std::shared_ptr<IDataProvider> provider) noexcept |
|
118 | std::shared_ptr<IDataProvider> provider) noexcept | |
116 | { |
|
119 | { | |
117 |
|
120 | |||
118 | if (!impl->m_TimeController) { |
|
121 | if (!impl->m_TimeController) { | |
119 | qCCritical(LOG_VariableController()) |
|
122 | qCCritical(LOG_VariableController()) | |
120 | << tr("Impossible to create variable: The time controller is null"); |
|
123 | << tr("Impossible to create variable: The time controller is null"); | |
121 | return; |
|
124 | return; | |
122 | } |
|
125 | } | |
123 |
|
126 | |||
124 | auto dateTime = impl->m_TimeController->dateTime(); |
|
127 | auto dateTime = impl->m_TimeController->dateTime(); | |
125 |
|
128 | |||
126 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { |
|
129 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { | |
127 | auto identifier = QUuid::createUuid(); |
|
130 | auto identifier = QUuid::createUuid(); | |
128 |
|
131 | |||
129 | // store the provider |
|
132 | // store the provider | |
130 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
133 | impl->m_VariableToProviderMap[newVariable] = provider; | |
131 | impl->m_VariableToIdentifierMap[newVariable] = identifier; |
|
134 | impl->m_VariableToIdentifierMap[newVariable] = identifier; | |
132 |
|
135 | |||
133 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( |
|
136 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( | |
134 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) |
|
137 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) | |
135 | { |
|
138 | { | |
136 | if (auto variable = varW.lock()) { |
|
139 | if (auto variable = varW.lock()) { | |
137 | auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable); |
|
140 | auto varIdentifier = impl->m_VariableToIdentifierMap.at(variable); | |
138 | if (varIdentifier == identifier) { |
|
141 | if (varIdentifier == identifier) { | |
139 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); |
|
142 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); | |
140 | variable->setDataSeries(dataSeriesAcquired); |
|
143 | variable->setDataSeries(dataSeriesAcquired); | |
141 | emit variable->updated(); |
|
144 | emit variable->updated(); | |
142 | } |
|
145 | } | |
143 | } |
|
146 | } | |
144 | }; |
|
147 | }; | |
145 |
|
148 | |||
146 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
149 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | |
147 | connect(provider.get(), &IDataProvider::dataProvidedProgress, this, |
|
150 | connect(provider.get(), &IDataProvider::dataProvidedProgress, this, | |
148 | &VariableController::onVariableRetrieveDataInProgress); |
|
151 | &VariableController::onVariableRetrieveDataInProgress); | |
149 | this->onRequestDataLoading(newVariable, dateTime); |
|
152 | this->onRequestDataLoading(newVariable, dateTime); | |
150 | } |
|
153 | } | |
151 | } |
|
154 | } | |
152 |
|
155 | |||
153 | void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) |
|
156 | void VariableController::onDateTimeOnSelection(const SqpRange &dateTime) | |
154 | { |
|
157 | { | |
155 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" |
|
158 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" | |
156 | << QThread::currentThread()->objectName(); |
|
159 | << QThread::currentThread()->objectName(); | |
157 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
160 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); | |
158 |
|
161 | |||
159 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
162 | for (const auto &selectedRow : qAsConst(selectedRows)) { | |
160 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
163 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { | |
161 | selectedVariable->setDateTime(dateTime); |
|
164 | selectedVariable->setDateTime(dateTime); | |
162 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
165 | this->onRequestDataLoading(selectedVariable, dateTime); | |
163 |
|
166 | |||
164 | // notify that rescale operation has to be done |
|
167 | // notify that rescale operation has to be done | |
165 | emit rangeChanged(selectedVariable, dateTime); |
|
168 | emit rangeChanged(selectedVariable, dateTime); | |
166 | } |
|
169 | } | |
167 | } |
|
170 | } | |
168 | } |
|
171 | } | |
169 |
|
172 | |||
170 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) |
|
173 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) | |
171 | { |
|
174 | { | |
172 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; |
|
175 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; | |
173 |
|
176 | |||
174 | auto end = impl->m_VariableToIdentifierMap.cend(); |
|
177 | auto end = impl->m_VariableToIdentifierMap.cend(); | |
175 | auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply); |
|
178 | auto it = std::find_if(impl->m_VariableToIdentifierMap.cbegin(), end, findReply); | |
176 | if (it != end) { |
|
179 | if (it != end) { | |
177 | impl->m_VariableModel->setDataProgress(it->first, progress); |
|
180 | impl->m_VariableModel->setDataProgress(it->first, progress); | |
178 | } |
|
181 | } | |
179 | } |
|
182 | } | |
180 |
|
183 | |||
181 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) |
|
184 | void VariableController::onAbortProgressRequested(std::shared_ptr<Variable> variable) | |
182 | { |
|
185 | { | |
183 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested" |
|
186 | qCDebug(LOG_VariableController()) << "TORM: VariableController::onAbortProgressRequested" | |
184 | << QThread::currentThread()->objectName(); |
|
187 | << QThread::currentThread()->objectName(); | |
185 |
|
188 | |||
186 | auto it = impl->m_VariableToIdentifierMap.find(variable); |
|
189 | auto it = impl->m_VariableToIdentifierMap.find(variable); | |
187 | if (it != impl->m_VariableToIdentifierMap.cend()) { |
|
190 | if (it != impl->m_VariableToIdentifierMap.cend()) { | |
188 | impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second); |
|
191 | impl->m_VariableToProviderMap.at(variable)->requestDataAborting(it->second); | |
189 | } |
|
192 | } | |
190 | else { |
|
193 | else { | |
191 | qCWarning(LOG_VariableController()) |
|
194 | qCWarning(LOG_VariableController()) | |
192 | << tr("Aborting progression of inexistant variable detected !!!") |
|
195 | << tr("Aborting progression of inexistant variable detected !!!") | |
193 | << QThread::currentThread()->objectName(); |
|
196 | << QThread::currentThread()->objectName(); | |
194 | } |
|
197 | } | |
195 | } |
|
198 | } | |
196 |
|
199 | |||
197 |
|
200 | |||
198 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
201 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |
199 | const SqpRange &dateTime) |
|
202 | const SqpRange &dateTime) | |
200 | { |
|
203 | { | |
201 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" |
|
204 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" | |
202 | << QThread::currentThread()->objectName(); |
|
205 | << QThread::currentThread()->objectName(); | |
203 | // we want to load data of the variable for the dateTime. |
|
206 | // we want to load data of the variable for the dateTime. | |
204 | // First we check if the cache contains some of them. |
|
207 | // First we check if the cache contains some of them. | |
205 | // For the other, we ask the provider to give them. |
|
208 | // For the other, we ask the provider to give them. | |
206 | if (variable) { |
|
209 | if (variable) { | |
207 |
|
210 | |||
208 | auto dateTimeListNotInCache |
|
211 | auto dateTimeListNotInCache | |
209 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
212 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); | |
210 |
|
213 | |||
211 | if (!dateTimeListNotInCache.empty()) { |
|
214 | if (!dateTimeListNotInCache.empty()) { | |
212 | // Ask the provider for each data on the dateTimeListNotInCache |
|
215 | // Ask the provider for each data on the dateTimeListNotInCache | |
213 | auto identifier = impl->m_VariableToIdentifierMap.at(variable); |
|
216 | auto identifier = impl->m_VariableToIdentifierMap.at(variable); | |
214 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
217 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | |
215 | identifier, |
|
218 | identifier, | |
216 | DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); |
|
219 | DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); | |
217 | } |
|
220 | } | |
218 | else { |
|
221 | else { | |
219 | emit variable->updated(); |
|
222 | emit variable->updated(); | |
220 | } |
|
223 | } | |
221 | } |
|
224 | } | |
222 | else { |
|
225 | else { | |
223 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
226 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | |
224 | } |
|
227 | } | |
225 | } |
|
228 | } | |
226 |
|
229 | |||
227 |
|
230 | |||
228 | void VariableController::initialize() |
|
231 | void VariableController::initialize() | |
229 | { |
|
232 | { | |
230 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
233 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); | |
231 | impl->m_WorkingMutex.lock(); |
|
234 | impl->m_WorkingMutex.lock(); | |
232 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
235 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); | |
233 | } |
|
236 | } | |
234 |
|
237 | |||
235 | void VariableController::finalize() |
|
238 | void VariableController::finalize() | |
236 | { |
|
239 | { | |
237 | impl->m_WorkingMutex.unlock(); |
|
240 | impl->m_WorkingMutex.unlock(); | |
238 | } |
|
241 | } | |
239 |
|
242 | |||
240 | void VariableController::waitForFinish() |
|
243 | void VariableController::waitForFinish() | |
241 | { |
|
244 | { | |
242 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
245 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
243 | } |
|
246 | } | |
|
247 | ||||
|
248 | ||||
|
249 | QVector<SqpDateTime> VariableController::VariableControllerPrivate::provideNotInCacheDateTimeList( | |||
|
250 | std::shared_ptr<Variable> variable, const SqpDateTime &dateTime) | |||
|
251 | { | |||
|
252 | auto notInCache = QVector<SqpDateTime>{}; | |||
|
253 | ||||
|
254 | if (!variable->contains(dateTime)) { | |||
|
255 | auto vDateTime = variable->dateTime(); | |||
|
256 | if (dateTime.m_TEnd <= vDateTime.m_TStart || dateTime.m_TStart >= vDateTime.m_TEnd) { | |||
|
257 | notInCache << dateTime; | |||
|
258 | } | |||
|
259 | else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd <= vDateTime.m_TEnd) { | |||
|
260 | notInCache << SqpDateTime{dateTime.m_TStart, vDateTime.m_TStart}; | |||
|
261 | } | |||
|
262 | else if (dateTime.m_TStart < vDateTime.m_TStart && dateTime.m_TEnd > vDateTime.m_TEnd) { | |||
|
263 | notInCache << SqpDateTime{dateTime.m_TStart, vDateTime.m_TStart} | |||
|
264 | << SqpDateTime{vDateTime.m_TEnd, dateTime.m_TStart}; | |||
|
265 | } | |||
|
266 | else if (dateTime.m_TStart < vDateTime.m_TEnd) { | |||
|
267 | notInCache << SqpDateTime{vDateTime.m_TEnd, dateTime.m_TStart}; | |||
|
268 | } | |||
|
269 | else { | |||
|
270 | qCCritical(LOG_VariableController()) << tr("Detection of unknown case.") | |||
|
271 | << QThread::currentThread(); | |||
|
272 | } | |||
|
273 | } | |||
|
274 | ||||
|
275 | return notInCache; | |||
|
276 | } |
General Comments 0
You need to be logged in to leave comments.
Login now