@@ -1,75 +1,77 | |||||
1 | #ifndef SCIQLOP_VARIABLECONTROLLER_H |
|
1 | #ifndef SCIQLOP_VARIABLECONTROLLER_H | |
2 | #define SCIQLOP_VARIABLECONTROLLER_H |
|
2 | #define SCIQLOP_VARIABLECONTROLLER_H | |
3 |
|
3 | |||
4 | #include <Data/SqpDateTime.h> |
|
4 | #include <Data/SqpDateTime.h> | |
5 |
|
5 | |||
6 | #include <QLoggingCategory> |
|
6 | #include <QLoggingCategory> | |
7 | #include <QObject> |
|
7 | #include <QObject> | |
8 |
|
8 | |||
9 | #include <Common/spimpl.h> |
|
9 | #include <Common/spimpl.h> | |
10 |
|
10 | |||
11 | class IDataProvider; |
|
11 | class IDataProvider; | |
12 | class QItemSelectionModel; |
|
12 | class QItemSelectionModel; | |
13 | class TimeController; |
|
13 | class TimeController; | |
14 | class Variable; |
|
14 | class Variable; | |
15 | class VariableModel; |
|
15 | class VariableModel; | |
16 |
|
16 | |||
17 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController) |
|
17 | Q_DECLARE_LOGGING_CATEGORY(LOG_VariableController) | |
18 |
|
18 | |||
19 | /** |
|
19 | /** | |
20 | * @brief The VariableController class aims to handle the variables in SciQlop. |
|
20 | * @brief The VariableController class aims to handle the variables in SciQlop. | |
21 | */ |
|
21 | */ | |
22 | class VariableController : public QObject { |
|
22 | class VariableController : public QObject { | |
23 | Q_OBJECT |
|
23 | Q_OBJECT | |
24 | public: |
|
24 | public: | |
25 | explicit VariableController(QObject *parent = 0); |
|
25 | explicit VariableController(QObject *parent = 0); | |
26 | virtual ~VariableController(); |
|
26 | virtual ~VariableController(); | |
27 |
|
27 | |||
28 | VariableModel *variableModel() noexcept; |
|
28 | VariableModel *variableModel() noexcept; | |
29 | QItemSelectionModel *variableSelectionModel() noexcept; |
|
29 | QItemSelectionModel *variableSelectionModel() noexcept; | |
30 |
|
30 | |||
31 | void setTimeController(TimeController *timeController) noexcept; |
|
31 | void setTimeController(TimeController *timeController) noexcept; | |
32 |
|
32 | |||
33 | /** |
|
33 | /** | |
34 | * Deletes from the controller the variable passed in parameter. |
|
34 | * Deletes from the controller the variable passed in parameter. | |
35 | * |
|
35 | * | |
|
36 | * Delete a variable includes: | |||
|
37 | * - the deletion of the provider associated with the variable | |||
36 | * |
|
38 | * | |
37 | * @param variable the variable to delete from the controller. |
|
39 | * @param variable the variable to delete from the controller. | |
38 | */ |
|
40 | */ | |
39 | void deleteVariable(std::shared_ptr<Variable> variable) noexcept; |
|
41 | void deleteVariable(std::shared_ptr<Variable> variable) noexcept; | |
40 |
|
42 | |||
41 | /** |
|
43 | /** | |
42 | * Deletes from the controller the variables passed in parameter. |
|
44 | * Deletes from the controller the variables passed in parameter. | |
43 | * @param variables the variables to delete from the controller. |
|
45 | * @param variables the variables to delete from the controller. | |
44 | * @sa deleteVariable() |
|
46 | * @sa deleteVariable() | |
45 | */ |
|
47 | */ | |
46 | void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept; |
|
48 | void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept; | |
47 |
|
49 | |||
48 | signals: |
|
50 | signals: | |
49 | /// Signal emitted when a variable has been created |
|
51 | /// Signal emitted when a variable has been created | |
50 | void variableCreated(std::shared_ptr<Variable> variable); |
|
52 | void variableCreated(std::shared_ptr<Variable> variable); | |
51 |
|
53 | |||
52 | public slots: |
|
54 | public slots: | |
53 | /// Request the data loading of the variable whithin dateTime |
|
55 | /// Request the data loading of the variable whithin dateTime | |
54 | void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); |
|
56 | void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); | |
55 | /** |
|
57 | /** | |
56 | * Creates a new variable and adds it to the model |
|
58 | * Creates a new variable and adds it to the model | |
57 | * @param name the name of the new variable |
|
59 | * @param name the name of the new variable | |
58 | * @param provider the data provider for the new variable |
|
60 | * @param provider the data provider for the new variable | |
59 | */ |
|
61 | */ | |
60 | void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept; |
|
62 | void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept; | |
61 |
|
63 | |||
62 | /// Update the temporal parameters of every selected variable to dateTime |
|
64 | /// Update the temporal parameters of every selected variable to dateTime | |
63 | void onDateTimeOnSelection(const SqpDateTime &dateTime); |
|
65 | void onDateTimeOnSelection(const SqpDateTime &dateTime); | |
64 |
|
66 | |||
65 | void initialize(); |
|
67 | void initialize(); | |
66 | void finalize(); |
|
68 | void finalize(); | |
67 |
|
69 | |||
68 | private: |
|
70 | private: | |
69 | void waitForFinish(); |
|
71 | void waitForFinish(); | |
70 |
|
72 | |||
71 | class VariableControllerPrivate; |
|
73 | class VariableControllerPrivate; | |
72 | spimpl::unique_impl_ptr<VariableControllerPrivate> impl; |
|
74 | spimpl::unique_impl_ptr<VariableControllerPrivate> impl; | |
73 | }; |
|
75 | }; | |
74 |
|
76 | |||
75 | #endif // SCIQLOP_VARIABLECONTROLLER_H |
|
77 | #endif // SCIQLOP_VARIABLECONTROLLER_H |
@@ -1,195 +1,200 | |||||
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 <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 | namespace { |
|
20 | namespace { | |
21 |
|
21 | |||
22 | /// @todo Generates default dataseries, according to the provider passed in parameter. This method |
|
22 | /// @todo Generates default dataseries, according to the provider passed in parameter. This method | |
23 | /// will be deleted when the timerange is recovered from SciQlop |
|
23 | /// will be deleted when the timerange is recovered from SciQlop | |
24 | std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, |
|
24 | std::shared_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider, | |
25 | const SqpDateTime &dateTime) noexcept |
|
25 | const SqpDateTime &dateTime) noexcept | |
26 | { |
|
26 | { | |
27 | auto parameters = DataProviderParameters{dateTime}; |
|
27 | auto parameters = DataProviderParameters{dateTime}; | |
28 |
|
28 | |||
29 | return provider.retrieveData(parameters); |
|
29 | return provider.retrieveData(parameters); | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | } // namespace |
|
32 | } // namespace | |
33 |
|
33 | |||
34 | struct VariableController::VariableControllerPrivate { |
|
34 | struct VariableController::VariableControllerPrivate { | |
35 | explicit VariableControllerPrivate(VariableController *parent) |
|
35 | explicit VariableControllerPrivate(VariableController *parent) | |
36 | : m_WorkingMutex{}, |
|
36 | : m_WorkingMutex{}, | |
37 | m_VariableModel{new VariableModel{parent}}, |
|
37 | m_VariableModel{new VariableModel{parent}}, | |
38 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, |
|
38 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, | |
39 | m_VariableCacheController{std::make_unique<VariableCacheController>()} |
|
39 | m_VariableCacheController{std::make_unique<VariableCacheController>()} | |
40 | { |
|
40 | { | |
41 | } |
|
41 | } | |
42 |
|
42 | |||
43 | QMutex m_WorkingMutex; |
|
43 | QMutex m_WorkingMutex; | |
44 | /// Variable model. The VariableController has the ownership |
|
44 | /// Variable model. The VariableController has the ownership | |
45 | VariableModel *m_VariableModel; |
|
45 | VariableModel *m_VariableModel; | |
46 | QItemSelectionModel *m_VariableSelectionModel; |
|
46 | QItemSelectionModel *m_VariableSelectionModel; | |
47 |
|
47 | |||
48 |
|
48 | |||
49 | TimeController *m_TimeController{nullptr}; |
|
49 | TimeController *m_TimeController{nullptr}; | |
50 | std::unique_ptr<VariableCacheController> m_VariableCacheController; |
|
50 | std::unique_ptr<VariableCacheController> m_VariableCacheController; | |
51 |
|
51 | |||
52 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
52 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > | |
53 | m_VariableToProviderMap; |
|
53 | m_VariableToProviderMap; | |
54 | }; |
|
54 | }; | |
55 |
|
55 | |||
56 | VariableController::VariableController(QObject *parent) |
|
56 | VariableController::VariableController(QObject *parent) | |
57 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
57 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} | |
58 | { |
|
58 | { | |
59 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
59 | qCDebug(LOG_VariableController()) << tr("VariableController construction") | |
60 | << QThread::currentThread(); |
|
60 | << QThread::currentThread(); | |
61 | } |
|
61 | } | |
62 |
|
62 | |||
63 | VariableController::~VariableController() |
|
63 | VariableController::~VariableController() | |
64 | { |
|
64 | { | |
65 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
65 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") | |
66 | << QThread::currentThread(); |
|
66 | << QThread::currentThread(); | |
67 | this->waitForFinish(); |
|
67 | this->waitForFinish(); | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | VariableModel *VariableController::variableModel() noexcept |
|
70 | VariableModel *VariableController::variableModel() noexcept | |
71 | { |
|
71 | { | |
72 | return impl->m_VariableModel; |
|
72 | return impl->m_VariableModel; | |
73 | } |
|
73 | } | |
74 |
|
74 | |||
75 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept |
|
75 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept | |
76 | { |
|
76 | { | |
77 | return impl->m_VariableSelectionModel; |
|
77 | return impl->m_VariableSelectionModel; | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
80 | void VariableController::setTimeController(TimeController *timeController) noexcept | |
81 | { |
|
81 | { | |
82 | impl->m_TimeController = timeController; |
|
82 | impl->m_TimeController = timeController; | |
83 | } |
|
83 | } | |
84 |
|
84 | |||
85 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
85 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | |
86 | { |
|
86 | { | |
87 | if (!variable) { |
|
87 | if (!variable) { | |
88 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; |
|
88 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | |
89 | return; |
|
89 | return; | |
90 | } |
|
90 | } | |
91 |
|
91 | |||
92 | /// @todo ALX |
|
92 | ||
|
93 | // Deletes provider | |||
|
94 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); | |||
|
95 | qCDebug(LOG_VariableController()) | |||
|
96 | << tr("Number of providers deleted for variable %1: %2") | |||
|
97 | .arg(variable->name(), QString::number(nbProvidersDeleted)); | |||
93 |
|
98 | |||
94 |
|
99 | |||
95 | void VariableController::deleteVariables( |
|
100 | void VariableController::deleteVariables( | |
96 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
101 | const QVector<std::shared_ptr<Variable> > &variables) noexcept | |
97 | { |
|
102 | { | |
98 | for (auto variable : qAsConst(variables)) { |
|
103 | for (auto variable : qAsConst(variables)) { | |
99 | deleteVariable(variable); |
|
104 | deleteVariable(variable); | |
100 | } |
|
105 | } | |
101 | } |
|
106 | } | |
102 |
|
107 | |||
103 | void VariableController::createVariable(const QString &name, |
|
108 | void VariableController::createVariable(const QString &name, | |
104 | std::shared_ptr<IDataProvider> provider) noexcept |
|
109 | std::shared_ptr<IDataProvider> provider) noexcept | |
105 | { |
|
110 | { | |
106 |
|
111 | |||
107 | if (!impl->m_TimeController) { |
|
112 | if (!impl->m_TimeController) { | |
108 | qCCritical(LOG_VariableController()) |
|
113 | qCCritical(LOG_VariableController()) | |
109 | << tr("Impossible to create variable: The time controller is null"); |
|
114 | << tr("Impossible to create variable: The time controller is null"); | |
110 | return; |
|
115 | return; | |
111 | } |
|
116 | } | |
112 |
|
117 | |||
113 |
|
118 | |||
114 | /// @todo : for the moment : |
|
119 | /// @todo : for the moment : | |
115 | /// - the provider is only used to retrieve data from the variable for its initialization, but |
|
120 | /// - the provider is only used to retrieve data from the variable for its initialization, but | |
116 | /// it will be retained later |
|
121 | /// it will be retained later | |
117 | /// - default data are generated for the variable, without taking into account the timerange set |
|
122 | /// - default data are generated for the variable, without taking into account the timerange set | |
118 | /// in sciqlop |
|
123 | /// in sciqlop | |
119 | auto dateTime = impl->m_TimeController->dateTime(); |
|
124 | auto dateTime = impl->m_TimeController->dateTime(); | |
120 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { |
|
125 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |
121 |
|
126 | |||
122 | // store the provider |
|
127 | // store the provider | |
123 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
128 | impl->m_VariableToProviderMap[newVariable] = provider; | |
124 |
|
129 | |||
125 | auto addDateTimeAcquired |
|
130 | auto addDateTimeAcquired | |
126 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { |
|
131 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { | |
127 |
|
132 | |||
128 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); |
|
133 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); | |
129 | newVariable->setDataSeries(dataSeriesAcquired); |
|
134 | newVariable->setDataSeries(dataSeriesAcquired); | |
130 |
|
135 | |||
131 | }; |
|
136 | }; | |
132 |
|
137 | |||
133 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
138 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | |
134 | this->onRequestDataLoading(newVariable, dateTime); |
|
139 | this->onRequestDataLoading(newVariable, dateTime); | |
135 |
|
140 | |||
136 | // notify the creation |
|
141 | // notify the creation | |
137 | emit variableCreated(newVariable); |
|
142 | emit variableCreated(newVariable); | |
138 | } |
|
143 | } | |
139 | } |
|
144 | } | |
140 |
|
145 | |||
141 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
146 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) | |
142 | { |
|
147 | { | |
143 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
148 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); | |
144 |
|
149 | |||
145 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
150 | for (const auto &selectedRow : qAsConst(selectedRows)) { | |
146 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
151 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { | |
147 | selectedVariable->setDateTime(dateTime); |
|
152 | selectedVariable->setDateTime(dateTime); | |
148 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
153 | this->onRequestDataLoading(selectedVariable, dateTime); | |
149 | } |
|
154 | } | |
150 | } |
|
155 | } | |
151 | } |
|
156 | } | |
152 |
|
157 | |||
153 |
|
158 | |||
154 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
159 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |
155 | const SqpDateTime &dateTime) |
|
160 | const SqpDateTime &dateTime) | |
156 | { |
|
161 | { | |
157 | // we want to load data of the variable for the dateTime. |
|
162 | // we want to load data of the variable for the dateTime. | |
158 | // First we check if the cache contains some of them. |
|
163 | // First we check if the cache contains some of them. | |
159 | // For the other, we ask the provider to give them. |
|
164 | // For the other, we ask the provider to give them. | |
160 | if (variable) { |
|
165 | if (variable) { | |
161 |
|
166 | |||
162 | auto dateTimeListNotInCache |
|
167 | auto dateTimeListNotInCache | |
163 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
168 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); | |
164 |
|
169 | |||
165 | if (!dateTimeListNotInCache.empty()) { |
|
170 | if (!dateTimeListNotInCache.empty()) { | |
166 | // Ask the provider for each data on the dateTimeListNotInCache |
|
171 | // Ask the provider for each data on the dateTimeListNotInCache | |
167 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
172 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | |
168 | std::move(dateTimeListNotInCache)); |
|
173 | std::move(dateTimeListNotInCache)); | |
169 | } |
|
174 | } | |
170 | else { |
|
175 | else { | |
171 | emit variable->updated(); |
|
176 | emit variable->updated(); | |
172 | } |
|
177 | } | |
173 | } |
|
178 | } | |
174 | else { |
|
179 | else { | |
175 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
180 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | |
176 | } |
|
181 | } | |
177 | } |
|
182 | } | |
178 |
|
183 | |||
179 |
|
184 | |||
180 | void VariableController::initialize() |
|
185 | void VariableController::initialize() | |
181 | { |
|
186 | { | |
182 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
187 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); | |
183 | impl->m_WorkingMutex.lock(); |
|
188 | impl->m_WorkingMutex.lock(); | |
184 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
189 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); | |
185 | } |
|
190 | } | |
186 |
|
191 | |||
187 | void VariableController::finalize() |
|
192 | void VariableController::finalize() | |
188 | { |
|
193 | { | |
189 | impl->m_WorkingMutex.unlock(); |
|
194 | impl->m_WorkingMutex.unlock(); | |
190 | } |
|
195 | } | |
191 |
|
196 | |||
192 | void VariableController::waitForFinish() |
|
197 | void VariableController::waitForFinish() | |
193 | { |
|
198 | { | |
194 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
199 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
195 | } |
|
200 | } |
General Comments 0
You need to be logged in to leave comments.
Login now