@@ -1,121 +1,120 | |||
|
1 | 1 | #include "DataSource/DataSourceController.h" |
|
2 | 2 | #include "DataSource/DataSourceItem.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/IDataProvider.h> |
|
5 | 5 | |
|
6 | 6 | #include <QMutex> |
|
7 | 7 | #include <QThread> |
|
8 | 8 | |
|
9 | 9 | #include <QDir> |
|
10 | 10 | #include <QStandardPaths> |
|
11 | 11 | |
|
12 | 12 | Q_LOGGING_CATEGORY(LOG_DataSourceController, "DataSourceController") |
|
13 | 13 | |
|
14 | 14 | class DataSourceController::DataSourceControllerPrivate { |
|
15 | 15 | public: |
|
16 | 16 | QMutex m_WorkingMutex; |
|
17 | 17 | /// Data sources registered |
|
18 | 18 | QHash<QUuid, QString> m_DataSources; |
|
19 | 19 | /// Data sources structures |
|
20 | 20 | std::map<QUuid, std::unique_ptr<DataSourceItem> > m_DataSourceItems; |
|
21 | 21 | /// Data providers registered |
|
22 | 22 | /// @remarks Data providers are stored as shared_ptr as they can be sent to a variable and |
|
23 | 23 | /// continue to live without necessarily the data source controller |
|
24 | 24 | std::map<QUuid, std::shared_ptr<IDataProvider> > m_DataProviders; |
|
25 | 25 | }; |
|
26 | 26 | |
|
27 | 27 | DataSourceController::DataSourceController(QObject *parent) |
|
28 | 28 | : impl{spimpl::make_unique_impl<DataSourceControllerPrivate>()} |
|
29 | 29 | { |
|
30 | 30 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController construction") |
|
31 | 31 | << QThread::currentThread(); |
|
32 | 32 | } |
|
33 | 33 | |
|
34 | 34 | DataSourceController::~DataSourceController() |
|
35 | 35 | { |
|
36 | 36 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController destruction") |
|
37 | 37 | << QThread::currentThread(); |
|
38 | 38 | this->waitForFinish(); |
|
39 | 39 | } |
|
40 | 40 | |
|
41 | 41 | QUuid DataSourceController::registerDataSource(const QString &dataSourceName) noexcept |
|
42 | 42 | { |
|
43 | 43 | auto dataSourceUid = QUuid::createUuid(); |
|
44 | 44 | impl->m_DataSources.insert(dataSourceUid, dataSourceName); |
|
45 | 45 | |
|
46 | 46 | return dataSourceUid; |
|
47 | 47 | } |
|
48 | 48 | |
|
49 | 49 | void DataSourceController::setDataSourceItem( |
|
50 | 50 | const QUuid &dataSourceUid, std::unique_ptr<DataSourceItem> dataSourceItem) noexcept |
|
51 | 51 | { |
|
52 | 52 | if (!dataSourceItem) { |
|
53 | 53 | qCWarning(LOG_DataSourceController()) |
|
54 | 54 | << tr("Data source item can't be registered (null item)"); |
|
55 | 55 | return; |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
59 | 59 | // The data provider is implicitly converted to a shared_ptr |
|
60 | 60 | impl->m_DataSourceItems.insert(std::make_pair(dataSourceUid, std::move(dataSourceItem))); |
|
61 | 61 | |
|
62 | 62 | // Retrieves the data source item to emit the signal with it |
|
63 | 63 | auto it = impl->m_DataSourceItems.find(dataSourceUid); |
|
64 | 64 | if (it != impl->m_DataSourceItems.end()) { |
|
65 | 65 | emit dataSourceItemSet(it->second.get()); |
|
66 | 66 | } |
|
67 | 67 | } |
|
68 | 68 | else { |
|
69 | 69 | qCWarning(LOG_DataSourceController()) << tr("Can't set data source item for uid %1 : no " |
|
70 | 70 | "data source has been registered with the uid") |
|
71 | 71 | .arg(dataSourceUid.toString()); |
|
72 | 72 | } |
|
73 | 73 | } |
|
74 | 74 | |
|
75 | 75 | void DataSourceController::setDataProvider(const QUuid &dataSourceUid, |
|
76 | 76 | std::unique_ptr<IDataProvider> dataProvider) noexcept |
|
77 | 77 | { |
|
78 | 78 | if (impl->m_DataSources.contains(dataSourceUid)) { |
|
79 | 79 | impl->m_DataProviders.insert(std::make_pair(dataSourceUid, std::move(dataProvider))); |
|
80 | 80 | } |
|
81 | 81 | else { |
|
82 | 82 | qCWarning(LOG_DataSourceController()) << tr("Can't set data provider for uid %1 : no data " |
|
83 | 83 | "source has been registered with the uid") |
|
84 | 84 | .arg(dataSourceUid.toString()); |
|
85 | 85 | } |
|
86 | 86 | } |
|
87 | 87 | |
|
88 | 88 | void DataSourceController::loadProductItem(const QUuid &dataSourceUid, |
|
89 | 89 | const DataSourceItem &productItem) noexcept |
|
90 | 90 | { |
|
91 | 91 | if (productItem.type() == DataSourceItemType::PRODUCT |
|
92 | 92 | || productItem.type() == DataSourceItemType::COMPONENT) { |
|
93 | 93 | /// Retrieves the data provider of the data source (if any) |
|
94 | 94 | auto it = impl->m_DataProviders.find(dataSourceUid); |
|
95 | 95 | auto dataProvider = (it != impl->m_DataProviders.end()) ? it->second : nullptr; |
|
96 | 96 | |
|
97 | /// @todo retrieve timerange, and pass it to the signal | |
|
98 | 97 | emit variableCreationRequested(productItem.name(), productItem.data(), dataProvider); |
|
99 | 98 | } |
|
100 | 99 | else { |
|
101 | 100 | qCWarning(LOG_DataSourceController()) << tr("Can't load an item that is not a product"); |
|
102 | 101 | } |
|
103 | 102 | } |
|
104 | 103 | |
|
105 | 104 | void DataSourceController::initialize() |
|
106 | 105 | { |
|
107 | 106 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init") |
|
108 | 107 | << QThread::currentThread(); |
|
109 | 108 | impl->m_WorkingMutex.lock(); |
|
110 | 109 | qCDebug(LOG_DataSourceController()) << tr("DataSourceController init END"); |
|
111 | 110 | } |
|
112 | 111 | |
|
113 | 112 | void DataSourceController::finalize() |
|
114 | 113 | { |
|
115 | 114 | impl->m_WorkingMutex.unlock(); |
|
116 | 115 | } |
|
117 | 116 | |
|
118 | 117 | void DataSourceController::waitForFinish() |
|
119 | 118 | { |
|
120 | 119 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
121 | 120 | } |
@@ -1,218 +1,212 | |||
|
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 <QUuid> |
|
15 | 15 | #include <QtCore/QItemSelectionModel> |
|
16 | 16 | |
|
17 | 17 | #include <unordered_map> |
|
18 | 18 | |
|
19 | 19 | Q_LOGGING_CATEGORY(LOG_VariableController, "VariableController") |
|
20 | 20 | |
|
21 | 21 | struct VariableController::VariableControllerPrivate { |
|
22 | 22 | explicit VariableControllerPrivate(VariableController *parent) |
|
23 | 23 | : m_WorkingMutex{}, |
|
24 | 24 | m_VariableModel{new VariableModel{parent}}, |
|
25 | 25 | m_VariableSelectionModel{new QItemSelectionModel{m_VariableModel, parent}}, |
|
26 | 26 | m_VariableCacheController{std::make_unique<VariableCacheController>()} |
|
27 | 27 | { |
|
28 | 28 | } |
|
29 | 29 | |
|
30 | 30 | QMutex m_WorkingMutex; |
|
31 | 31 | /// Variable model. The VariableController has the ownership |
|
32 | 32 | VariableModel *m_VariableModel; |
|
33 | 33 | QItemSelectionModel *m_VariableSelectionModel; |
|
34 | 34 | |
|
35 | 35 | |
|
36 | 36 | TimeController *m_TimeController{nullptr}; |
|
37 | 37 | std::unique_ptr<VariableCacheController> m_VariableCacheController; |
|
38 | 38 | |
|
39 | 39 | std::unordered_map<std::shared_ptr<Variable>, std::shared_ptr<IDataProvider> > |
|
40 | 40 | m_VariableToProviderMap; |
|
41 | 41 | std::unordered_map<std::shared_ptr<Variable>, QUuid> m_VariableToIdentifier; |
|
42 | 42 | }; |
|
43 | 43 | |
|
44 | 44 | VariableController::VariableController(QObject *parent) |
|
45 | 45 | : QObject{parent}, impl{spimpl::make_unique_impl<VariableControllerPrivate>(this)} |
|
46 | 46 | { |
|
47 | 47 | qCDebug(LOG_VariableController()) << tr("VariableController construction") |
|
48 | 48 | << QThread::currentThread(); |
|
49 | 49 | } |
|
50 | 50 | |
|
51 | 51 | VariableController::~VariableController() |
|
52 | 52 | { |
|
53 | 53 | qCDebug(LOG_VariableController()) << tr("VariableController destruction") |
|
54 | 54 | << QThread::currentThread(); |
|
55 | 55 | this->waitForFinish(); |
|
56 | 56 | } |
|
57 | 57 | |
|
58 | 58 | VariableModel *VariableController::variableModel() noexcept |
|
59 | 59 | { |
|
60 | 60 | return impl->m_VariableModel; |
|
61 | 61 | } |
|
62 | 62 | |
|
63 | 63 | QItemSelectionModel *VariableController::variableSelectionModel() noexcept |
|
64 | 64 | { |
|
65 | 65 | return impl->m_VariableSelectionModel; |
|
66 | 66 | } |
|
67 | 67 | |
|
68 | 68 | void VariableController::setTimeController(TimeController *timeController) noexcept |
|
69 | 69 | { |
|
70 | 70 | impl->m_TimeController = timeController; |
|
71 | 71 | } |
|
72 | 72 | |
|
73 | 73 | void VariableController::deleteVariable(std::shared_ptr<Variable> variable) noexcept |
|
74 | 74 | { |
|
75 | 75 | if (!variable) { |
|
76 | 76 | qCCritical(LOG_VariableController()) << "Can't delete variable: variable is null"; |
|
77 | 77 | return; |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | // Spreads in SciQlop that the variable will be deleted, so that potential receivers can |
|
81 | 81 | // make some treatments before the deletion |
|
82 | 82 | emit variableAboutToBeDeleted(variable); |
|
83 | 83 | |
|
84 | 84 | // Deletes provider |
|
85 | 85 | auto nbProvidersDeleted = impl->m_VariableToProviderMap.erase(variable); |
|
86 | 86 | qCDebug(LOG_VariableController()) |
|
87 | 87 | << tr("Number of providers deleted for variable %1: %2") |
|
88 | 88 | .arg(variable->name(), QString::number(nbProvidersDeleted)); |
|
89 | 89 | |
|
90 | 90 | // Clears cache |
|
91 | 91 | impl->m_VariableCacheController->clear(variable); |
|
92 | 92 | |
|
93 | 93 | // Deletes from model |
|
94 | 94 | impl->m_VariableModel->deleteVariable(variable); |
|
95 | 95 | } |
|
96 | 96 | |
|
97 | 97 | void VariableController::deleteVariables( |
|
98 | 98 | const QVector<std::shared_ptr<Variable> > &variables) noexcept |
|
99 | 99 | { |
|
100 | 100 | for (auto variable : qAsConst(variables)) { |
|
101 | 101 | deleteVariable(variable); |
|
102 | 102 | } |
|
103 | 103 | } |
|
104 | 104 | |
|
105 | 105 | void VariableController::createVariable(const QString &name, const QVariantHash &metadata, |
|
106 | 106 | std::shared_ptr<IDataProvider> provider) noexcept |
|
107 | 107 | { |
|
108 | 108 | |
|
109 | 109 | if (!impl->m_TimeController) { |
|
110 | 110 | qCCritical(LOG_VariableController()) |
|
111 | 111 | << tr("Impossible to create variable: The time controller is null"); |
|
112 | 112 | return; |
|
113 | 113 | } |
|
114 | 114 | |
|
115 | ||
|
116 | /// @todo : for the moment : | |
|
117 | /// - the provider is only used to retrieve data from the variable for its initialization, but | |
|
118 | /// it will be retained later | |
|
119 | /// - default data are generated for the variable, without taking into account the timerange set | |
|
120 | /// in sciqlop | |
|
121 | 115 | auto dateTime = impl->m_TimeController->dateTime(); |
|
122 | 116 | |
|
123 | 117 | if (auto newVariable = impl->m_VariableModel->createVariable(name, dateTime, metadata)) { |
|
124 | 118 | auto identifier = QUuid::createUuid(); |
|
125 | 119 | |
|
126 | 120 | // store the provider |
|
127 | 121 | impl->m_VariableToProviderMap[newVariable] = provider; |
|
128 | 122 | impl->m_VariableToIdentifier[newVariable] = identifier; |
|
129 | 123 | |
|
130 | 124 | auto addDateTimeAcquired = [ this, varW = std::weak_ptr<Variable>{newVariable} ]( |
|
131 | 125 | QUuid identifier, auto dataSeriesAcquired, auto dateTimeToPutInCache) |
|
132 | 126 | { |
|
133 | 127 | if (auto variable = varW.lock()) { |
|
134 | 128 | auto varIdentifier = impl->m_VariableToIdentifier.at(variable); |
|
135 | 129 | if (varIdentifier == identifier) { |
|
136 | 130 | impl->m_VariableCacheController->addDateTime(variable, dateTimeToPutInCache); |
|
137 | 131 | variable->setDataSeries(dataSeriesAcquired); |
|
138 | 132 | } |
|
139 | 133 | } |
|
140 | 134 | }; |
|
141 | 135 | |
|
142 | 136 | connect(provider.get(), &IDataProvider::dataProvided, addDateTimeAcquired); |
|
143 | 137 | this->onRequestDataLoading(newVariable, dateTime); |
|
144 | 138 | } |
|
145 | 139 | } |
|
146 | 140 | |
|
147 | 141 | void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime) |
|
148 | 142 | { |
|
149 | 143 | qCDebug(LOG_VariableController()) << "VariableController::onDateTimeOnSelection" |
|
150 | 144 | << QThread::currentThread()->objectName(); |
|
151 | 145 | auto selectedRows = impl->m_VariableSelectionModel->selectedRows(); |
|
152 | 146 | |
|
153 | 147 | for (const auto &selectedRow : qAsConst(selectedRows)) { |
|
154 | 148 | if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) { |
|
155 | 149 | selectedVariable->setDateTime(dateTime); |
|
156 | 150 | this->onRequestDataLoading(selectedVariable, dateTime); |
|
157 | 151 | } |
|
158 | 152 | } |
|
159 | 153 | } |
|
160 | 154 | |
|
161 | 155 | void VariableController::onVariableRetrieveDataInProgress(QUuid identifier, double progress) |
|
162 | 156 | { |
|
163 | 157 | auto findReply = [identifier](const auto &entry) { return identifier == entry.second; }; |
|
164 | 158 | |
|
165 | 159 | auto end = impl->m_VariableToIdentifier.cend(); |
|
166 | 160 | auto it = std::find_if(impl->m_VariableToIdentifier.cbegin(), end, findReply); |
|
167 | 161 | if (it != end) { |
|
168 | 162 | impl->m_VariableModel->setDataProgress(it->first, progress); |
|
169 | 163 | } |
|
170 | 164 | } |
|
171 | 165 | |
|
172 | 166 | |
|
173 | 167 | void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable, |
|
174 | 168 | const SqpDateTime &dateTime) |
|
175 | 169 | { |
|
176 | 170 | qCDebug(LOG_VariableController()) << "VariableController::onRequestDataLoading" |
|
177 | 171 | << QThread::currentThread()->objectName(); |
|
178 | 172 | // we want to load data of the variable for the dateTime. |
|
179 | 173 | // First we check if the cache contains some of them. |
|
180 | 174 | // For the other, we ask the provider to give them. |
|
181 | 175 | if (variable) { |
|
182 | 176 | |
|
183 | 177 | auto dateTimeListNotInCache |
|
184 | 178 | = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime); |
|
185 | 179 | |
|
186 | 180 | if (!dateTimeListNotInCache.empty()) { |
|
187 | 181 | // Ask the provider for each data on the dateTimeListNotInCache |
|
188 | 182 | auto identifier = impl->m_VariableToIdentifier.at(variable); |
|
189 | 183 | impl->m_VariableToProviderMap.at(variable)->requestDataLoading( |
|
190 | 184 | identifier, |
|
191 | 185 | DataProviderParameters{std::move(dateTimeListNotInCache), variable->metadata()}); |
|
192 | 186 | } |
|
193 | 187 | else { |
|
194 | 188 | emit variable->updated(); |
|
195 | 189 | } |
|
196 | 190 | } |
|
197 | 191 | else { |
|
198 | 192 | qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null"); |
|
199 | 193 | } |
|
200 | 194 | } |
|
201 | 195 | |
|
202 | 196 | |
|
203 | 197 | void VariableController::initialize() |
|
204 | 198 | { |
|
205 | 199 | qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread(); |
|
206 | 200 | impl->m_WorkingMutex.lock(); |
|
207 | 201 | qCDebug(LOG_VariableController()) << tr("VariableController init END"); |
|
208 | 202 | } |
|
209 | 203 | |
|
210 | 204 | void VariableController::finalize() |
|
211 | 205 | { |
|
212 | 206 | impl->m_WorkingMutex.unlock(); |
|
213 | 207 | } |
|
214 | 208 | |
|
215 | 209 | void VariableController::waitForFinish() |
|
216 | 210 | { |
|
217 | 211 | QMutexLocker locker{&impl->m_WorkingMutex}; |
|
218 | 212 | } |
General Comments 0
You need to be logged in to leave comments.
Login now