@@ -1,44 +1,46 | |||||
1 | #ifndef SCIQLOP_VISUALIZATIONCONTROLLER_H |
|
1 | #ifndef SCIQLOP_VISUALIZATIONCONTROLLER_H | |
2 | #define SCIQLOP_VISUALIZATIONCONTROLLER_H |
|
2 | #define SCIQLOP_VISUALIZATIONCONTROLLER_H | |
3 |
|
3 | |||
4 | #include <QLoggingCategory> |
|
4 | #include <QLoggingCategory> | |
5 | #include <QObject> |
|
5 | #include <QObject> | |
6 | #include <QUuid> |
|
6 | #include <QUuid> | |
7 |
|
7 | |||
8 | #include <Common/spimpl.h> |
|
8 | #include <Common/spimpl.h> | |
9 |
|
9 | |||
10 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationController) |
|
10 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationController) | |
11 |
|
11 | |||
12 | class DataSourceItem; |
|
12 | class DataSourceItem; | |
13 | class Variable; |
|
13 | class Variable; | |
14 |
|
14 | |||
15 | /** |
|
15 | /** | |
16 | * @brief The VisualizationController class aims to make the link between SciQlop and its plugins. |
|
16 | * @brief The VisualizationController class aims to make the link between SciQlop and its plugins. | |
17 | * This is the intermediate class that SciQlop has to use in the way to connect a data source. |
|
17 | * This is the intermediate class that SciQlop has to use in the way to connect a data source. | |
18 | * Please first use register method to initialize a plugin specified by its metadata name (JSON |
|
18 | * Please first use register method to initialize a plugin specified by its metadata name (JSON | |
19 | * plugin source) then others specifics method will be able to access it. You can load a data source |
|
19 | * plugin source) then others specifics method will be able to access it. You can load a data source | |
20 | * driver plugin then create a data source. |
|
20 | * driver plugin then create a data source. | |
21 | */ |
|
21 | */ | |
22 | class VisualizationController : public QObject { |
|
22 | class VisualizationController : public QObject { | |
23 | Q_OBJECT |
|
23 | Q_OBJECT | |
24 | public: |
|
24 | public: | |
25 | explicit VisualizationController(QObject *parent = 0); |
|
25 | explicit VisualizationController(QObject *parent = 0); | |
26 | virtual ~VisualizationController(); |
|
26 | virtual ~VisualizationController(); | |
27 |
|
27 | |||
28 | signals: |
|
28 | signals: | |
|
29 | /// Signal emitted when a variable is about to be deleted from SciQlop | |||
|
30 | void variableAboutToBeDeleted(std::shared_ptr<Variable> variable); | |||
29 | /// Signal emitted when a variable has been created in SciQlop |
|
31 | /// Signal emitted when a variable has been created in SciQlop | |
30 | void variableCreated(std::shared_ptr<Variable> variable); |
|
32 | void variableCreated(std::shared_ptr<Variable> variable); | |
31 |
|
33 | |||
32 | public slots: |
|
34 | public slots: | |
33 | /// Manage init/end of the controller |
|
35 | /// Manage init/end of the controller | |
34 | void initialize(); |
|
36 | void initialize(); | |
35 | void finalize(); |
|
37 | void finalize(); | |
36 |
|
38 | |||
37 | private: |
|
39 | private: | |
38 | void waitForFinish(); |
|
40 | void waitForFinish(); | |
39 |
|
41 | |||
40 | class VisualizationControllerPrivate; |
|
42 | class VisualizationControllerPrivate; | |
41 | spimpl::unique_impl_ptr<VisualizationControllerPrivate> impl; |
|
43 | spimpl::unique_impl_ptr<VisualizationControllerPrivate> impl; | |
42 | }; |
|
44 | }; | |
43 |
|
45 | |||
44 | #endif // SCIQLOP_VISUALIZATIONCONTROLLER_H |
|
46 | #endif // SCIQLOP_VISUALIZATIONCONTROLLER_H |
@@ -1,206 +1,209 | |||||
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 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can | |||
|
93 | // make some treatments before the deletion | |||
|
94 | emit variableAboutToBeDeleted(variable); | |||
92 |
|
95 | |||
93 | // Deletes provider |
|
96 | // Deletes provider | |
94 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); |
|
97 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); | |
95 | qCDebug(LOG_VariableController()) |
|
98 | qCDebug(LOG_VariableController()) | |
96 | << tr("Number of providers deleted for variable %1: %2") |
|
99 | << tr("Number of providers deleted for variable %1: %2") | |
97 | .arg(variable->name(), QString::number(nbProvidersDeleted)); |
|
100 | .arg(variable->name(), QString::number(nbProvidersDeleted)); | |
98 |
|
101 | |||
99 | // Clears cache |
|
102 | // Clears cache | |
100 | impl->m_VariableCacheController->clear(variable); |
|
103 | impl->m_VariableCacheController->clear(variable); | |
101 |
|
104 | |||
102 | // Deletes from model |
|
105 | // Deletes from model | |
103 | impl->m_VariableModel->deleteVariable(variable); |
|
106 | impl->m_VariableModel->deleteVariable(variable); | |
104 | } |
|
107 | } | |
105 |
|
108 | |||
106 | void VariableController::deleteVariables( |
|
109 | void VariableController::deleteVariables( | |
107 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
110 | const QVector<std::shared_ptr<Variable> > &variables) noexcept | |
108 | { |
|
111 | { | |
109 | for (auto variable : qAsConst(variables)) { |
|
112 | for (auto variable : qAsConst(variables)) { | |
110 | deleteVariable(variable); |
|
113 | deleteVariable(variable); | |
111 | } |
|
114 | } | |
112 | } |
|
115 | } | |
113 |
|
116 | |||
114 | void VariableController::createVariable(const QString &name, |
|
117 | void VariableController::createVariable(const QString &name, | |
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 |
|
127 | |||
125 | /// @todo : for the moment : |
|
128 | /// @todo : for the moment : | |
126 | /// - the provider is only used to retrieve data from the variable for its initialization, but |
|
129 | /// - the provider is only used to retrieve data from the variable for its initialization, but | |
127 | /// it will be retained later |
|
130 | /// it will be retained later | |
128 | /// - default data are generated for the variable, without taking into account the timerange set |
|
131 | /// - default data are generated for the variable, without taking into account the timerange set | |
129 | /// in sciqlop |
|
132 | /// in sciqlop | |
130 | auto dateTime = impl->m_TimeController->dateTime(); |
|
133 | auto dateTime = impl->m_TimeController->dateTime(); | |
131 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { |
|
134 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { | |
132 |
|
135 | |||
133 | // store the provider |
|
136 | // store the provider | |
134 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
137 | impl->m_VariableToProviderMap[newVariable] = provider; | |
135 |
|
138 | |||
136 | auto addDateTimeAcquired |
|
139 | auto addDateTimeAcquired | |
137 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { |
|
140 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { | |
138 |
|
141 | |||
139 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); |
|
142 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); | |
140 | newVariable->setDataSeries(dataSeriesAcquired); |
|
143 | newVariable->setDataSeries(dataSeriesAcquired); | |
141 |
|
144 | |||
142 | }; |
|
145 | }; | |
143 |
|
146 | |||
144 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
147 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); | |
145 | this->onRequestDataLoading(newVariable, dateTime); |
|
148 | this->onRequestDataLoading(newVariable, dateTime); | |
146 |
|
149 | |||
147 | // notify the creation |
|
150 | // notify the creation | |
148 | emit variableCreated(newVariable); |
|
151 | emit variableCreated(newVariable); | |
149 | } |
|
152 | } | |
150 | } |
|
153 | } | |
151 |
|
154 | |||
152 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
155 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) | |
153 | { |
|
156 | { | |
154 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
157 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); | |
155 |
|
158 | |||
156 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
159 | for (const auto &selectedRow : qAsConst(selectedRows)) { | |
157 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
160 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { | |
158 | selectedVariable->setDateTime(dateTime); |
|
161 | selectedVariable->setDateTime(dateTime); | |
159 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
162 | this->onRequestDataLoading(selectedVariable, dateTime); | |
160 | } |
|
163 | } | |
161 | } |
|
164 | } | |
162 | } |
|
165 | } | |
163 |
|
166 | |||
164 |
|
167 | |||
165 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
168 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, | |
166 | const SqpDateTime &dateTime) |
|
169 | const SqpDateTime &dateTime) | |
167 | { |
|
170 | { | |
168 | // we want to load data of the variable for the dateTime. |
|
171 | // we want to load data of the variable for the dateTime. | |
169 | // First we check if the cache contains some of them. |
|
172 | // First we check if the cache contains some of them. | |
170 | // For the other, we ask the provider to give them. |
|
173 | // For the other, we ask the provider to give them. | |
171 | if (variable) { |
|
174 | if (variable) { | |
172 |
|
175 | |||
173 | auto dateTimeListNotInCache |
|
176 | auto dateTimeListNotInCache | |
174 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
177 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); | |
175 |
|
178 | |||
176 | if (!dateTimeListNotInCache.empty()) { |
|
179 | if (!dateTimeListNotInCache.empty()) { | |
177 | // Ask the provider for each data on the dateTimeListNotInCache |
|
180 | // Ask the provider for each data on the dateTimeListNotInCache | |
178 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
181 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( | |
179 | std::move(dateTimeListNotInCache)); |
|
182 | std::move(dateTimeListNotInCache)); | |
180 | } |
|
183 | } | |
181 | else { |
|
184 | else { | |
182 | emit variable->updated(); |
|
185 | emit variable->updated(); | |
183 | } |
|
186 | } | |
184 | } |
|
187 | } | |
185 | else { |
|
188 | else { | |
186 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
189 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); | |
187 | } |
|
190 | } | |
188 | } |
|
191 | } | |
189 |
|
192 | |||
190 |
|
193 | |||
191 | void VariableController::initialize() |
|
194 | void VariableController::initialize() | |
192 | { |
|
195 | { | |
193 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
196 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); | |
194 | impl->m_WorkingMutex.lock(); |
|
197 | impl->m_WorkingMutex.lock(); | |
195 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
198 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); | |
196 | } |
|
199 | } | |
197 |
|
200 | |||
198 | void VariableController::finalize() |
|
201 | void VariableController::finalize() | |
199 | { |
|
202 | { | |
200 | impl->m_WorkingMutex.unlock(); |
|
203 | impl->m_WorkingMutex.unlock(); | |
201 | } |
|
204 | } | |
202 |
|
205 | |||
203 | void VariableController::waitForFinish() |
|
206 | void VariableController::waitForFinish() | |
204 | { |
|
207 | { | |
205 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
208 | QMutexLocker locker{&impl->m_WorkingMutex}; | |
206 | } |
|
209 | } |
General Comments 0
You need to be logged in to leave comments.
Login now