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

Auto status change to "Under Review"

You need to be logged in to leave comments. Login now