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