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