##// END OF EJS Templates
The data of the variable is now requested with the new provided...
perrinel -
r305:88a58e69d383
parent child
Show More
@@ -1,176 +1,177
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::unique_ptr<IDataSeries> generateDefaultDataSeries(const IDataProvider &provider,
24 std::unique_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::createVariable(const QString &name,
85 void VariableController::createVariable(const QString &name,
86 std::shared_ptr<IDataProvider> provider) noexcept
86 std::shared_ptr<IDataProvider> provider) noexcept
87 {
87 {
88
88
89 if (!impl->m_TimeController) {
89 if (!impl->m_TimeController) {
90 qCCritical(LOG_VariableController())
90 qCCritical(LOG_VariableController())
91 << tr("Impossible to create variable: The time controller is null");
91 << tr("Impossible to create variable: The time controller is null");
92 return;
92 return;
93 }
93 }
94
94
95
95
96 /// @todo : for the moment :
96 /// @todo : for the moment :
97 /// - the provider is only used to retrieve data from the variable for its initialization, but
97 /// - the provider is only used to retrieve data from the variable for its initialization, but
98 /// it will be retained later
98 /// it will be retained later
99 /// - default data are generated for the variable, without taking into account the timerange set
99 /// - default data are generated for the variable, without taking into account the timerange set
100 /// in sciqlop
100 /// in sciqlop
101 auto dateTime = impl->m_TimeController->dateTime();
101 auto dateTime = impl->m_TimeController->dateTime();
102 if (auto newVariable = impl->m_VariableModel->createVariable(
102 if (auto newVariable = impl->m_VariableModel->createVariable(
103 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
103 name, dateTime, generateDefaultDataSeries(*provider, dateTime))) {
104
104
105 // store the provider
105 // store the provider
106 impl->m_VariableToProviderMap[newVariable] = provider;
106 impl->m_VariableToProviderMap[newVariable] = provider;
107 qRegisterMetaType<std::shared_ptr<IDataSeries> >();
107 qRegisterMetaType<std::shared_ptr<IDataSeries> >();
108 qRegisterMetaType<SqpDateTime>();
108 qRegisterMetaType<SqpDateTime>();
109 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
109 connect(provider.get(), &IDataProvider::dataProvided, newVariable.get(),
110 &Variable::onAddDataSeries);
110 &Variable::onAddDataSeries);
111
111
112
112
113 // store in cache
113 // store in cache
114 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
114 impl->m_VariableCacheController->addDateTime(newVariable, dateTime);
115
115
116 // notify the creation
116 // notify the creation
117 emit variableCreated(newVariable);
117 emit variableCreated(newVariable);
118 }
118 }
119 }
119 }
120
120
121 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
121 void VariableController::onDateTimeOnSelection(const SqpDateTime &dateTime)
122 {
122 {
123 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
123 auto selectedRows = impl->m_VariableSelectionModel->selectedRows();
124
124
125 for (const auto &selectedRow : qAsConst(selectedRows)) {
125 for (const auto &selectedRow : qAsConst(selectedRows)) {
126 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
126 if (auto selectedVariable = impl->m_VariableModel->variable(selectedRow.row())) {
127 selectedVariable->setDateTime(dateTime);
127 selectedVariable->setDateTime(dateTime);
128 this->onRequestDataLoading(selectedVariable, dateTime);
128 }
129 }
129 }
130 }
130 }
131 }
131
132
132
133
133 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
134 void VariableController::onRequestDataLoading(std::shared_ptr<Variable> variable,
134 const SqpDateTime &dateTime)
135 const SqpDateTime &dateTime)
135 {
136 {
136 // we want to load data of the variable for the dateTime.
137 // we want to load data of the variable for the dateTime.
137 // First we check if the cache contains some of them.
138 // First we check if the cache contains some of them.
138 // For the other, we ask the provider to give them.
139 // For the other, we ask the provider to give them.
139 if (variable) {
140 if (variable) {
140
141
141 auto dateTimeListNotInCache
142 auto dateTimeListNotInCache
142 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
143 = impl->m_VariableCacheController->provideNotInCacheDateTimeList(variable, dateTime);
143
144
144 if (!dateTimeListNotInCache.empty()) {
145 if (!dateTimeListNotInCache.empty()) {
145 // Ask the provider for each data on the dateTimeListNotInCache
146 // Ask the provider for each data on the dateTimeListNotInCache
146 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
147 impl->m_VariableToProviderMap.at(variable)->requestDataLoading(
147 std::move(dateTimeListNotInCache));
148 std::move(dateTimeListNotInCache));
148 // store in cache
149 // store in cache
149 impl->m_VariableCacheController->addDateTime(variable, dateTime);
150 impl->m_VariableCacheController->addDateTime(variable, dateTime);
150 }
151 }
151 else {
152 else {
152 emit variable->updated();
153 emit variable->updated();
153 }
154 }
154 }
155 }
155 else {
156 else {
156 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
157 qCCritical(LOG_VariableController()) << tr("Impossible to load data of a variable null");
157 }
158 }
158 }
159 }
159
160
160
161
161 void VariableController::initialize()
162 void VariableController::initialize()
162 {
163 {
163 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
164 qCDebug(LOG_VariableController()) << tr("VariableController init") << QThread::currentThread();
164 impl->m_WorkingMutex.lock();
165 impl->m_WorkingMutex.lock();
165 qCDebug(LOG_VariableController()) << tr("VariableController init END");
166 qCDebug(LOG_VariableController()) << tr("VariableController init END");
166 }
167 }
167
168
168 void VariableController::finalize()
169 void VariableController::finalize()
169 {
170 {
170 impl->m_WorkingMutex.unlock();
171 impl->m_WorkingMutex.unlock();
171 }
172 }
172
173
173 void VariableController::waitForFinish()
174 void VariableController::waitForFinish()
174 {
175 {
175 QMutexLocker locker{&impl->m_WorkingMutex};
176 QMutexLocker locker{&impl->m_WorkingMutex};
176 }
177 }
General Comments 0
You need to be logged in to leave comments. Login now