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