@@ -1,61 +1,75 | |||
|
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 | /** | |
|
34 | * Deletes from the controller the variable passed in parameter. | |
|
35 | * | |
|
36 | * | |
|
37 | * @param variable the variable to delete from the controller. | |
|
38 | */ | |
|
39 | void deleteVariable(std::shared_ptr<Variable> variable) noexcept; | |
|
40 | ||
|
41 | /** | |
|
42 | * Deletes from the controller the variables passed in parameter. | |
|
43 | * @param variables the variables to delete from the controller. | |
|
44 | * @sa deleteVariable() | |
|
45 | */ | |
|
46 | void deleteVariables(const QVector<std::shared_ptr<Variable> > &variables) noexcept; | |
|
33 | 47 | |
|
34 | 48 | signals: |
|
35 | 49 | /// Signal emitted when a variable has been created |
|
36 | 50 | void variableCreated(std::shared_ptr<Variable> variable); |
|
37 | 51 | |
|
38 | 52 | public slots: |
|
39 | 53 | /// Request the data loading of the variable whithin dateTime |
|
40 | 54 | void onRequestDataLoading(std::shared_ptr<Variable> variable, const SqpDateTime &dateTime); |
|
41 | 55 | /** |
|
42 | 56 | * Creates a new variable and adds it to the model |
|
43 | 57 | * @param name the name of the new variable |
|
44 | 58 | * @param provider the data provider for the new variable |
|
45 | 59 | */ |
|
46 | 60 | void createVariable(const QString &name, std::shared_ptr<IDataProvider> provider) noexcept; |
|
47 | 61 | |
|
48 | 62 | /// Update the temporal parameters of every selected variable to dateTime |
|
49 | 63 | void onDateTimeOnSelection(const SqpDateTime &dateTime); |
|
50 | 64 | |
|
51 | 65 | void initialize(); |
|
52 | 66 | void finalize(); |
|
53 | 67 | |
|
54 | 68 | private: |
|
55 | 69 | void waitForFinish(); |
|
56 | 70 | |
|
57 | 71 | class VariableControllerPrivate; |
|
58 | 72 | spimpl::unique_impl_ptr<VariableControllerPrivate> impl; |
|
59 | 73 | }; |
|
60 | 74 | |
|
61 | 75 | #endif // SCIQLOP_VARIABLECONTROLLER_H |
@@ -1,177 +1,195 | |||
|
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 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept | |
|
86 | { | |
|
87 | if (!variable) { | |
|
88 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; | |
|
89 | return; | |
|
90 | } | |
|
91 | ||
|
92 | /// @todo ALX | |
|
93 | ||
|
94 | ||
|
95 | void VariableController::deleteVariables( | |
|
96 | const QVector<std::shared_ptr<Variable> > &variables) noexcept | |
|
97 | { | |
|
98 | for (auto variable : qAsConst(variables)) { | |
|
99 | deleteVariable(variable); | |
|
100 | } | |
|
101 | } | |
|
102 | ||
|
85 | 103 | void VariableController::createVariable(const QString &name, |
|
86 | 104 | std::shared_ptr<IDataProvider> provider) noexcept |
|
87 | 105 | { |
|
88 | 106 | |
|
89 | 107 | if (!impl->m_TimeController) { |
|
90 | 108 | qCCritical(LOG_VariableController()) |
|
91 | 109 | << tr("Impossible to create variable: The time controller is null"); |
|
92 | 110 | return; |
|
93 | 111 | } |
|
94 | 112 | |
|
95 | 113 | |
|
96 | 114 | /// @todo : for the moment : |
|
97 | 115 | /// - the provider is only used to retrieve data from the variable for its initialization, but |
|
98 | 116 | /// it will be retained later |
|
99 | 117 | /// - default data are generated for the variable, without taking into account the timerange set |
|
100 | 118 | /// in sciqlop |
|
101 | 119 | auto dateTime = impl->m_TimeController->dateTime(); |
|
102 | 120 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime)) { |
|
103 | 121 | |
|
104 | 122 | // store the provider |
|
105 | 123 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
106 | 124 | |
|
107 | 125 | auto addDateTimeAcquired |
|
108 | 126 | = [this, newVariable](auto dataSeriesAcquired, auto dateTimeToPutInCache) { |
|
109 | 127 | |
|
110 | 128 | impl->m_VariableCacheController->addDateTime(newVariable, dateTimeToPutInCache); |
|
111 | 129 | newVariable->setDataSeries(dataSeriesAcquired); |
|
112 | 130 | |
|
113 | 131 | }; |
|
114 | 132 | |
|
115 | 133 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
116 | 134 | this->onRequestDataLoading(newVariable, dateTime); |
|
117 | 135 | |
|
118 | 136 | // notify the creation |
|
119 | 137 | emit variableCreated(newVariable); |
|
120 | 138 | } |
|
121 | 139 | } |
|
122 | 140 | |
|
123 | 141 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
124 | 142 | { |
|
125 | 143 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
126 | 144 | |
|
127 | 145 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
128 | 146 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
129 | 147 | selectedVariable->setDateTime(dateTime); |
|
130 | 148 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
131 | 149 | } |
|
132 | 150 | } |
|
133 | 151 | } |
|
134 | 152 | |
|
135 | 153 | |
|
136 | 154 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
137 | 155 | const SqpDateTime &dateTime) |
|
138 | 156 | { |
|
139 | 157 | // we want to load data of the variable for the dateTime. |
|
140 | 158 | // First we check if the cache contains some of them. |
|
141 | 159 | // For the other, we ask the provider to give them. |
|
142 | 160 | if (variable) { |
|
143 | 161 | |
|
144 | 162 | auto dateTimeListNotInCache |
|
145 | 163 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
146 | 164 | |
|
147 | 165 | if (!dateTimeListNotInCache.empty()) { |
|
148 | 166 | // Ask the provider for each data on the dateTimeListNotInCache |
|
149 | 167 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
150 | 168 | std::move(dateTimeListNotInCache)); |
|
151 | 169 | } |
|
152 | 170 | else { |
|
153 | 171 | emit variable->updated(); |
|
154 | 172 | } |
|
155 | 173 | } |
|
156 | 174 | else { |
|
157 | 175 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
158 | 176 | } |
|
159 | 177 | } |
|
160 | 178 | |
|
161 | 179 | |
|
162 | 180 | void VariableController::initialize() |
|
163 | 181 | { |
|
164 | 182 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
165 | 183 | impl->m_WorkingMutex.lock(); |
|
166 | 184 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
167 | 185 | } |
|
168 | 186 | |
|
169 | 187 | void VariableController::finalize() |
|
170 | 188 | { |
|
171 | 189 | impl->m_WorkingMutex.unlock(); |
|
172 | 190 | } |
|
173 | 191 | |
|
174 | 192 | void VariableController::waitForFinish() |
|
175 | 193 | { |
|
176 | 194 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
177 | 195 | } |
General Comments 0
You need to be logged in to leave comments.
Login now