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