##// END OF EJS Templates
Some refactoring on Variable class...
jeandet -
r15:bdcac701ba37
parent child
Show More
@@ -45,16 +45,17 namespace DateTimeRangeHelper {
45 * @return trnaformation applied to range1 to get range2 or an object of type
45 * @return trnaformation applied to range1 to get range2 or an object of type
46 * InvalidDateTimeRangeTransformation if the transformation has NaN or forbiden values
46 * InvalidDateTimeRangeTransformation if the transformation has NaN or forbiden values
47 */
47 */
48 inline std::variant<DateTimeRangeTransformation, InvalidDateTimeRangeTransformation>
48 inline std::optional<DateTimeRangeTransformation>
49 computeTransformation(const DateTimeRange& range1, const DateTimeRange& range2)
49 computeTransformation(const DateTimeRange& range1, const DateTimeRange& range2)
50 {
50 {
51 std::optional<DateTimeRangeTransformation> transformation;
51 double zoom = range2.delta()/range1.delta();
52 double zoom = range2.delta()/range1.delta();
52 Seconds<double> shift = range2.center() - (range1*zoom).center();
53 Seconds<double> shift = range2.center() - (range1*zoom).center();
53 bool zoomValid = zoom!=0. && !std::isnan(zoom) && !std::isinf(zoom);
54 bool zoomValid = zoom!=0. && !std::isnan(zoom) && !std::isinf(zoom);
54 bool shiftValid = !std::isnan(shift.value) && !std::isinf(shift.value);
55 bool shiftValid = !std::isnan(shift.value) && !std::isinf(shift.value);
55 if(zoomValid && shiftValid)
56 if(zoomValid && shiftValid)
56 return DateTimeRangeTransformation{zoom, shift};
57 transformation = DateTimeRangeTransformation{zoom, shift};
57 return InvalidDateTimeRangeTransformation{};
58 return transformation;
58 }
59 }
59
60
60 }
61 }
@@ -52,7 +52,7 public:
52 /// @return the real range, invalid range if the data series is null or empty
52 /// @return the real range, invalid range if the data series is null or empty
53 /// @sa setDataSeries()
53 /// @sa setDataSeries()
54 /// @sa setRange()
54 /// @sa setRange()
55 DateTimeRange realRange() const noexcept;
55 std::optional<DateTimeRange> realRange() const noexcept;
56
56
57 /// @return the data of the variable, nullptr if there is no data
57 /// @return the data of the variable, nullptr if there is no data
58 std::shared_ptr<IDataSeries> dataSeries() const noexcept;
58 std::shared_ptr<IDataSeries> dataSeries() const noexcept;
@@ -1,5 +1,6
1 #include <memory>
1 #include <memory>
2 #include <vector>
2 #include <vector>
3 #include <set>
3 #include <QHash>
4 #include <QHash>
4 #include <QObject>
5 #include <QObject>
5 #include <QMutexLocker>
6 #include <QMutexLocker>
@@ -7,7 +8,7
7 #include <QItemSelectionModel>
8 #include <QItemSelectionModel>
8 #include <Common/spimpl.h>
9 #include <Common/spimpl.h>
9 #include <Variable/Variable.h>
10 #include <Variable/Variable.h>
10 #include <Variable/VariableSynchronizationGroup.h>
11 //#include <Variable/VariableSynchronizationGroup.h>
11 #include <Variable/VariableModel.h>
12 #include <Variable/VariableModel.h>
12 #include <Data/IDataProvider.h>
13 #include <Data/IDataProvider.h>
13 #include "Data/DateTimeRange.h"
14 #include "Data/DateTimeRange.h"
@@ -1,11 +1,13
1 #include <optional>
2 #include <QMutex>
3 #include <QReadWriteLock>
4 #include <QThread>
5
1 #include "Variable/Variable.h"
6 #include "Variable/Variable.h"
2
7
3 #include <Data/IDataSeries.h>
8 #include <Data/IDataSeries.h>
4 #include <Data/DateTimeRange.h>
9 #include <Data/DateTimeRange.h>
5
10
6 #include <QMutex>
7 #include <QReadWriteLock>
8 #include <QThread>
9 #include <Common/debug.h>
11 #include <Common/debug.h>
10
12
11 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
13 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
@@ -33,6 +35,29 DataSeriesType findDataSeriesType(const QVariantHash &metadata)
33
35
34 } // namespace
36 } // namespace
35
37
38 #define VP_PROPERTY(property,getter,setter,type) \
39 type getter() noexcept\
40 {\
41 QReadLocker lock{&m_Lock};\
42 return property;\
43 }\
44 void setter(const type& getter) noexcept\
45 {\
46 QWriteLocker lock{&m_Lock};\
47 property = getter;\
48 }\
49 type property;\
50
51 #define V_FW_GETTER_SETTER(getter,setter, type)\
52 type Variable::getter() const noexcept \
53 {\
54 return impl->getter();\
55 }\
56 void Variable::setter(const type& getter) noexcept \
57 {\
58 impl->setter(getter);\
59 }\
60
36 struct Variable::VariablePrivate {
61 struct Variable::VariablePrivate {
37 explicit VariablePrivate(const QString &name, const QVariantHash &metadata)
62 explicit VariablePrivate(const QString &name, const QVariantHash &metadata)
38 : m_Name{name},
63 : m_Name{name},
@@ -89,20 +114,18 struct Variable::VariablePrivate {
89 m_DataSeries->unlock();
114 m_DataSeries->unlock();
90 }
115 }
91 else {
116 else {
92 m_RealRange = INVALID_RANGE;
117 m_RealRange = std::nullopt;
93 }
118 }
94 }
119 }
95
120
96 QString m_Name;
121 VP_PROPERTY(m_Name, name, setName, QString)
97
122 VP_PROPERTY(m_Range, range, setRange, DateTimeRange)
98 DateTimeRange m_Range;
123 VP_PROPERTY(m_CacheRange, cacheRange, setCacheRange, DateTimeRange)
99 DateTimeRange m_CacheRange;
124 VP_PROPERTY(m_Metadata, metadata, setMetadata, QVariantHash)
100 QVariantHash m_Metadata;
125 VP_PROPERTY(m_DataSeries, dataSeries, setDataSeries, std::shared_ptr<IDataSeries>)
101 std::shared_ptr<IDataSeries> m_DataSeries;
126 VP_PROPERTY(m_RealRange, realRange, setRealRange, std::optional<DateTimeRange>)
102 DateTimeRange m_RealRange;
103 unsigned int m_NbPoints;
127 unsigned int m_NbPoints;
104 DataSeriesType m_Type;
128 VP_PROPERTY(m_Type, type, setType, DataSeriesType)
105
106 QReadWriteLock m_Lock;
129 QReadWriteLock m_Lock;
107 };
130 };
108
131
@@ -123,64 +146,31 std::shared_ptr<Variable> Variable::clone() const
123 return std::make_shared<Variable>(*this);
146 return std::make_shared<Variable>(*this);
124 }
147 }
125
148
126 QString Variable::name() const noexcept
149 V_FW_GETTER_SETTER(name,setName,QString)
127 {
128 impl->lockRead();
129 auto name = impl->m_Name;
130 impl->unlock();
131 return name;
132 }
133
134 void Variable::setName(const QString &name) noexcept
135 {
136 impl->lockWrite();
137 impl->m_Name = name;
138 impl->unlock();
139 }
140
150
141 DateTimeRange Variable::range() const noexcept
151 DateTimeRange Variable::range() const noexcept
142 {
152 {
143 impl->lockRead();
153 return impl->range();
144 auto range = impl->m_Range;
145 impl->unlock();
146 return range;
147 }
154 }
148
155
149 void Variable::setRange(const DateTimeRange &range, bool notify) noexcept
156 void Variable::setRange(const DateTimeRange &range, bool notify) noexcept
150 {
157 {
151 impl->lockWrite();
158 impl->setRange(range);
152 impl->m_Range = range;
153 impl->updateRealRange();
159 impl->updateRealRange();
154 impl->unlock();
155 if(notify)
160 if(notify)
156 emit this->updated();
161 emit this->updated();
157 }
162 }
158
163
159 DateTimeRange Variable::cacheRange() const noexcept
164 V_FW_GETTER_SETTER(cacheRange, setCacheRange, DateTimeRange)
160 {
161 impl->lockRead();
162 auto cacheRange = impl->m_CacheRange;
163 impl->unlock();
164 return cacheRange;
165 }
166
167 void Variable::setCacheRange(const DateTimeRange &cacheRange) noexcept
168 {
169 impl->lockWrite();
170 if (cacheRange != impl->m_CacheRange) {
171 impl->m_CacheRange = cacheRange;
172 }
173 impl->unlock();
174 }
175
165
176 unsigned int Variable::nbPoints() const noexcept
166 unsigned int Variable::nbPoints() const noexcept
177 {
167 {
178 return impl->m_NbPoints;
168 return impl->m_NbPoints;
179 }
169 }
180
170
181 DateTimeRange Variable::realRange() const noexcept
171 std::optional<DateTimeRange> Variable::realRange() const noexcept
182 {
172 {
183 return impl->m_RealRange;
173 return impl->realRange();
184 }
174 }
185
175
186 void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
176 void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
@@ -244,20 +234,12 void Variable::mergeDataSeries(IDataSeries *dataSeries, bool notify) noexcept
244
234
245 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
235 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
246 {
236 {
247 impl->lockRead();
237 return impl->dataSeries();
248 auto dataSeries = impl->m_DataSeries;
249 impl->unlock();
250
251 return dataSeries;
252 }
238 }
253
239
254 DataSeriesType Variable::type() const noexcept
240 DataSeriesType Variable::type() const noexcept
255 {
241 {
256 impl->lockRead();
242 return impl->type();
257 auto type = impl->m_Type;
258 impl->unlock();
259
260 return type;
261 }
243 }
262
244
263 QVariantHash Variable::metadata() const noexcept
245 QVariantHash Variable::metadata() const noexcept
@@ -80,20 +80,25 public:
80 SCIQLOP_ERROR(VariableController2Private, "No provider found for given variable");
80 SCIQLOP_ERROR(VariableController2Private, "No provider found for given variable");
81 }
81 }
82
82
83 void asyncChangeRange(const std::shared_ptr<Variable>& variable, const DateTimeRange& r)
84 {
85
86 }
87
83 void changeRange(const std::shared_ptr<Variable>& variable, DateTimeRange r)
88 void changeRange(const std::shared_ptr<Variable>& variable, DateTimeRange r)
84 {
89 {
85 if(p_contains(variable))
90 if(p_contains(variable))
86 {
91 {
87 if(!DateTimeRangeHelper::hasnan(r))
92 if(!DateTimeRangeHelper::hasnan(r))
88 {
93 {
89 auto transformation = DateTimeRangeHelper::computeTransformation(variable->range(),r);
90 auto group = _synchronizationGroups[variable->ID()];
94 auto group = _synchronizationGroups[variable->ID()];
91 if(std::holds_alternative<DateTimeRangeTransformation>(transformation))
95 if(auto transformation = DateTimeRangeHelper::computeTransformation(variable->range(),r);
96 transformation.has_value())
92 {
97 {
93 for(auto varId:group->variables())
98 for(auto varId:group->variables())
94 {
99 {
95 auto var = _variables[varId];
100 auto var = _variables[varId];
96 auto newRange = var->range().transform(std::get<DateTimeRangeTransformation>(transformation));
101 auto newRange = var->range().transform(transformation.value());
97 _changeRange(var,newRange);
102 _changeRange(var,newRange);
98 }
103 }
99 }
104 }
@@ -178,6 +183,11 void VariableController2::changeRange(const std::shared_ptr<Variable>& variable,
178 impl->changeRange(variable, r);
183 impl->changeRange(variable, r);
179 }
184 }
180
185
186 void VariableController2::asyncChangeRange(const std::shared_ptr<Variable> &variable, const DateTimeRange &r)
187 {
188 impl->asyncChangeRange(variable, r);
189 }
190
181 const std::set<std::shared_ptr<Variable> > VariableController2::variables()
191 const std::set<std::shared_ptr<Variable> > VariableController2::variables()
182 {
192 {
183 return impl->variables();
193 return impl->variables();
@@ -196,16 +196,14 QVariant VariableModel::data(const QModelIndex &index, int role) const
196 case NAME_COLUMN:
196 case NAME_COLUMN:
197 return variable->name();
197 return variable->name();
198 case TSTART_COLUMN: {
198 case TSTART_COLUMN: {
199 auto range = variable->realRange();
199 if(auto range = variable->realRange(); range.has_value())
200 return range != INVALID_RANGE
200 return DateUtils::dateTime(range.value().m_TStart).toString(DATETIME_FORMAT);
201 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
201 return QVariant{};
202 : QVariant{};
203 }
202 }
204 case TEND_COLUMN: {
203 case TEND_COLUMN: {
205 auto range = variable->realRange();
204 if(auto range = variable->realRange(); range.has_value())
206 return range != INVALID_RANGE
205 return DateUtils::dateTime(range.value().m_TEnd).toString(DATETIME_FORMAT);
207 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
206 return QVariant{};
208 : QVariant{};
209 }
207 }
210 case NBPOINTS_COLUMN:
208 case NBPOINTS_COLUMN:
211 return variable->nbPoints();
209 return variable->nbPoints();
@@ -180,7 +180,7 private slots:
180 QFETCH(DateTimeRange,range1);
180 QFETCH(DateTimeRange,range1);
181 QFETCH(DateTimeRange,range2);
181 QFETCH(DateTimeRange,range2);
182 QFETCH(DateTimeRangeTransformation, transformation);
182 QFETCH(DateTimeRangeTransformation, transformation);
183 auto computed_tr = std::get<DateTimeRangeTransformation>(DateTimeRangeHelper::computeTransformation(range1,range2));
183 auto computed_tr = DateTimeRangeHelper::computeTransformation(range1,range2).value();
184 QCOMPARE(computed_tr, transformation);
184 QCOMPARE(computed_tr, transformation);
185 QCOMPARE(range1.transform(transformation),range2);
185 QCOMPARE(range1.transform(transformation),range2);
186 QCOMPARE(range1.transform(computed_tr),range2);
186 QCOMPARE(range1.transform(computed_tr),range2);
General Comments 0
You need to be logged in to leave comments. Login now