##// END OF EJS Templates
Merge pull request 267 from SCIQLOP-Initialisation develop...
leroux -
r721:ba7f1665a341 merge
parent child
Show More
@@ -1,367 +1,374
1 1 #ifndef SCIQLOP_ARRAYDATA_H
2 2 #define SCIQLOP_ARRAYDATA_H
3 3
4 4 #include "Data/ArrayDataIterator.h"
5 5 #include <Common/SortUtils.h>
6 6
7 7 #include <QReadLocker>
8 8 #include <QReadWriteLock>
9 9 #include <QVector>
10 10
11 11 #include <memory>
12 12
13 13 template <int Dim>
14 14 class ArrayData;
15 15
16 16 using DataContainer = std::vector<double>;
17 17
18 18 namespace arraydata_detail {
19 19
20 20 /// Struct used to sort ArrayData
21 21 template <int Dim>
22 22 struct Sort {
23 23 static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, int nbComponents,
24 24 const std::vector<int> &sortPermutation)
25 25 {
26 26 return std::make_shared<ArrayData<Dim> >(
27 27 SortUtils::sort(data, nbComponents, sortPermutation), nbComponents);
28 28 }
29 29 };
30 30
31 31 /// Specialization for uni-dimensional ArrayData
32 32 template <>
33 33 struct Sort<1> {
34 34 static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, int nbComponents,
35 35 const std::vector<int> &sortPermutation)
36 36 {
37 37 Q_UNUSED(nbComponents)
38 38 return std::make_shared<ArrayData<1> >(SortUtils::sort(data, 1, sortPermutation));
39 39 }
40 40 };
41 41
42 42 template <int Dim, bool IsConst>
43 43 class IteratorValue;
44 44
45 45 template <int Dim, bool IsConst>
46 46 struct IteratorValueBuilder {
47 47 };
48 48
49 49 template <int Dim>
50 50 struct IteratorValueBuilder<Dim, true> {
51 51 using DataContainerIterator = DataContainer::const_iterator;
52 52
53 53 static void swap(IteratorValue<Dim, true> &o1, IteratorValue<Dim, true> &o2) {}
54 54 };
55 55
56 56 template <int Dim>
57 57 struct IteratorValueBuilder<Dim, false> {
58 58 using DataContainerIterator = DataContainer::iterator;
59 59
60 60 static void swap(IteratorValue<Dim, false> &o1, IteratorValue<Dim, false> &o2)
61 61 {
62 62 for (auto i = 0; i < o1.m_NbComponents; ++i) {
63 63 std::iter_swap(o1.m_It + i, o2.m_It + i);
64 64 }
65 65 }
66 66 };
67 67
68 68 template <int Dim, bool IsConst>
69 69 class IteratorValue : public ArrayDataIteratorValue::Impl {
70 70 public:
71 71 friend class ArrayData<Dim>;
72 72 friend class IteratorValueBuilder<Dim, IsConst>;
73 73
74 74 using DataContainerIterator =
75 75 typename IteratorValueBuilder<Dim, IsConst>::DataContainerIterator;
76 76
77 77 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
78 78 explicit IteratorValue(const DataContainer &container, int nbComponents, bool begin)
79 79 : m_It{begin ? container.cbegin() : container.cend()}, m_NbComponents{nbComponents}
80 80 {
81 81 }
82 82
83 83 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
84 84 explicit IteratorValue(DataContainer &container, int nbComponents, bool begin)
85 85 : m_It{begin ? container.begin() : container.end()}, m_NbComponents{nbComponents}
86 86 {
87 87 }
88 88
89 89 IteratorValue(const IteratorValue &other) = default;
90 90
91 91 std::unique_ptr<ArrayDataIteratorValue::Impl> clone() const override
92 92 {
93 93 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
94 94 }
95 95
96 96 int distance(const ArrayDataIteratorValue::Impl &other) const override try {
97 97 /// @todo ALX : validate
98 98 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
99 99 return std::distance(otherImpl.m_It, m_It) / m_NbComponents;
100 100 }
101 101 catch (const std::bad_cast &) {
102 102 return 0;
103 103 }
104 104
105 105 bool equals(const ArrayDataIteratorValue::Impl &other) const override try {
106 106 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
107 107 return std::tie(m_It, m_NbComponents) == std::tie(otherImpl.m_It, otherImpl.m_NbComponents);
108 108 }
109 109 catch (const std::bad_cast &) {
110 110 return false;
111 111 }
112 112
113 113 bool lowerThan(const ArrayDataIteratorValue::Impl &other) const override try {
114 114 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
115 115 return m_It < otherImpl.m_It;
116 116 }
117 117 catch (const std::bad_cast &) {
118 118 return false;
119 119 }
120 120
121 121 std::unique_ptr<ArrayDataIteratorValue::Impl> advance(int offset) const override
122 122 {
123 123 auto result = clone();
124 124 result->next(offset);
125 125 return result;
126 126 }
127 127
128 128 void next(int offset) override { std::advance(m_It, offset * m_NbComponents); }
129 129 void prev() override { std::advance(m_It, -m_NbComponents); }
130 130
131 131 double at(int componentIndex) const override { return *(m_It + componentIndex); }
132 132 double first() const override { return *m_It; }
133 133 double min() const override
134 134 {
135 135 auto values = this->values();
136 136 auto end = values.cend();
137 137 auto it = std::min_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
138 138 return SortUtils::minCompareWithNaN(v1, v2);
139 139 });
140 140
141 141 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
142 142 }
143 143 double max() const override
144 144 {
145 145 auto values = this->values();
146 146 auto end = values.cend();
147 147 auto it = std::max_element(values.cbegin(), end, [](const auto &v1, const auto &v2) {
148 148 return SortUtils::maxCompareWithNaN(v1, v2);
149 149 });
150 150 return it != end ? *it : std::numeric_limits<double>::quiet_NaN();
151 151 }
152 152
153 153 QVector<double> values() const override
154 154 {
155 155 auto result = QVector<double>{};
156 156 for (auto i = 0; i < m_NbComponents; ++i) {
157 157 result.push_back(*(m_It + i));
158 158 }
159 159
160 160 return result;
161 161 }
162 162
163 163 void swap(ArrayDataIteratorValue::Impl &other) override
164 164 {
165 165 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
166 166 IteratorValueBuilder<Dim, IsConst>::swap(*this, otherImpl);
167 167 }
168 168
169 169 private:
170 170 DataContainerIterator m_It;
171 171 int m_NbComponents;
172 172 };
173 173
174 174 } // namespace arraydata_detail
175 175
176 176 /**
177 177 * @brief The ArrayData class represents a dataset for a data series.
178 178 *
179 179 * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim
180 180 * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same
181 181 * number of values
182 182 *
183 183 * @tparam Dim the dimension of the ArrayData (one or two)
184 184 * @sa IDataSeries
185 185 */
186 186 template <int Dim>
187 187 class ArrayData {
188 188 public:
189 189 // ///// //
190 190 // Ctors //
191 191 // ///// //
192 192
193 193 /**
194 194 * Ctor for a unidimensional ArrayData
195 195 * @param data the data the ArrayData will hold
196 196 */
197 197 template <int D = Dim, typename = std::enable_if_t<D == 1> >
198 198 explicit ArrayData(DataContainer data) : m_Data{std::move(data)}, m_NbComponents{1}
199 199 {
200 200 }
201 201
202 202 /**
203 203 * Ctor for a two-dimensional ArrayData. The number of components (number of lines) must be
204 204 * greater than 2 and must be a divisor of the total number of data in the vector
205 205 * @param data the data the ArrayData will hold
206 206 * @param nbComponents the number of components
207 207 * @throws std::invalid_argument if the number of components is less than 2 or is not a divisor
208 208 * of the size of the data
209 209 */
210 210 template <int D = Dim, typename = std::enable_if_t<D == 2> >
211 211 explicit ArrayData(DataContainer data, int nbComponents)
212 212 : m_Data{std::move(data)}, m_NbComponents{nbComponents}
213 213 {
214 214 if (nbComponents < 2) {
215 215 throw std::invalid_argument{
216 216 QString{"A multidimensional ArrayData must have at least 2 components (found: %1)"}
217 217 .arg(nbComponents)
218 218 .toStdString()};
219 219 }
220 220
221 221 if (m_Data.size() % m_NbComponents != 0) {
222 222 throw std::invalid_argument{QString{
223 223 "The number of components (%1) is inconsistent with the total number of data (%2)"}
224 224 .arg(m_Data.size(), nbComponents)
225 225 .toStdString()};
226 226 }
227 227 }
228 228
229 229 /// Copy ctor
230 230 explicit ArrayData(const ArrayData &other)
231 231 {
232 232 QReadLocker otherLocker{&other.m_Lock};
233 233 m_Data = other.m_Data;
234 234 m_NbComponents = other.m_NbComponents;
235 235 }
236 236
237 237 // /////////////// //
238 238 // General methods //
239 239 // /////////////// //
240 240
241 241 /**
242 242 * Merges into the array data an other array data. The two array datas must have the same number
243 243 * of components so the merge can be done
244 244 * @param other the array data to merge with
245 245 * @param prepend if true, the other array data is inserted at the beginning, otherwise it is
246 246 * inserted at the end
247 247 */
248 248 void add(const ArrayData<Dim> &other, bool prepend = false)
249 249 {
250 250 QWriteLocker locker{&m_Lock};
251 251 QReadLocker otherLocker{&other.m_Lock};
252 252
253 253 if (m_NbComponents != other.componentCount()) {
254 254 return;
255 255 }
256 256
257 257 insert(other.cbegin(), other.cend(), prepend);
258 258 }
259 259
260 260 void clear()
261 261 {
262 262 QWriteLocker locker{&m_Lock};
263 263 m_Data.clear();
264 264 }
265 265
266 266 int componentCount() const noexcept { return m_NbComponents; }
267 267
268 268 /// @return the size (i.e. number of values) of a single component
269 269 /// @remarks in a case of a two-dimensional ArrayData, each component has the same size
270 270 int size() const
271 271 {
272 272 QReadLocker locker{&m_Lock};
273 273 return m_Data.size() / m_NbComponents;
274 274 }
275 275
276 /// @return the total size (i.e. number of values) of the array data
277 int totalSize() const
278 {
279 QReadLocker locker{&m_Lock};
280 return m_Data.size();
281 }
282
276 283 std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation)
277 284 {
278 285 QReadLocker locker{&m_Lock};
279 286 return arraydata_detail::Sort<Dim>::sort(m_Data, m_NbComponents, sortPermutation);
280 287 }
281 288
282 289 // ///////// //
283 290 // Iterators //
284 291 // ///////// //
285 292
286 293 ArrayDataIterator begin()
287 294 {
288 295 return ArrayDataIterator{
289 296 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
290 297 m_Data, m_NbComponents, true)}};
291 298 }
292 299
293 300 ArrayDataIterator end()
294 301 {
295 302 return ArrayDataIterator{
296 303 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, false> >(
297 304 m_Data, m_NbComponents, false)}};
298 305 }
299 306
300 307 ArrayDataIterator cbegin() const
301 308 {
302 309 return ArrayDataIterator{
303 310 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
304 311 m_Data, m_NbComponents, true)}};
305 312 }
306 313
307 314 ArrayDataIterator cend() const
308 315 {
309 316 return ArrayDataIterator{
310 317 ArrayDataIteratorValue{std::make_unique<arraydata_detail::IteratorValue<Dim, true> >(
311 318 m_Data, m_NbComponents, false)}};
312 319 }
313 320
314 321 void erase(ArrayDataIterator first, ArrayDataIterator last)
315 322 {
316 323 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(first->impl());
317 324 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, false> *>(last->impl());
318 325
319 326 if (firstImpl && lastImpl) {
320 327 m_Data.erase(firstImpl->m_It, lastImpl->m_It);
321 328 }
322 329 }
323 330
324 331 void insert(ArrayDataIterator first, ArrayDataIterator last, bool prepend = false)
325 332 {
326 333 auto firstImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(first->impl());
327 334 auto lastImpl = dynamic_cast<arraydata_detail::IteratorValue<Dim, true> *>(last->impl());
328 335
329 336 if (firstImpl && lastImpl) {
330 337 auto insertIt = prepend ? m_Data.begin() : m_Data.end();
331 338
332 339 m_Data.insert(insertIt, firstImpl->m_It, lastImpl->m_It);
333 340 }
334 341 }
335 342
336 343 /**
337 344 * @return the data at a specified index
338 345 * @remarks index must be a valid position
339 346 */
340 347 double at(int index) const noexcept
341 348 {
342 349 QReadLocker locker{&m_Lock};
343 350 return m_Data.at(index);
344 351 }
345 352
346 353 // ///////////// //
347 354 // 1-dim methods //
348 355 // ///////////// //
349 356
350 357 /**
351 358 * @return the data as a vector, as a const reference
352 359 * @remarks this method is only available for a unidimensional ArrayData
353 360 */
354 361 template <int D = Dim, typename = std::enable_if_t<D == 1> >
355 362 DataContainer cdata() const noexcept
356 363 {
357 364 return m_Data;
358 365 }
359 366
360 367 private:
361 368 DataContainer m_Data;
362 369 /// Number of components (lines). Is always 1 in a 1-dim ArrayData
363 370 int m_NbComponents;
364 371 mutable QReadWriteLock m_Lock;
365 372 };
366 373
367 374 #endif // SCIQLOP_ARRAYDATA_H
@@ -1,399 +1,400
1 1 #ifndef SCIQLOP_DATASERIES_H
2 2 #define SCIQLOP_DATASERIES_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Common/SortUtils.h>
7 7
8 8 #include <Data/ArrayData.h>
9 9 #include <Data/DataSeriesMergeHelper.h>
10 10 #include <Data/IDataSeries.h>
11 11
12 12 #include <QLoggingCategory>
13 13 #include <QReadLocker>
14 14 #include <QReadWriteLock>
15 15 #include <memory>
16 16
17 17 // We don't use the Qt macro since the log is used in the header file, which causes multiple log
18 18 // definitions with inheritance. Inline method is used instead
19 19 inline const QLoggingCategory &LOG_DataSeries()
20 20 {
21 21 static const QLoggingCategory category{"DataSeries"};
22 22 return category;
23 23 }
24 24
25 25 template <int Dim>
26 26 class DataSeries;
27 27
28 28 namespace dataseries_detail {
29 29
30 30 template <int Dim, bool IsConst>
31 31 class IteratorValue : public DataSeriesIteratorValue::Impl {
32 32 public:
33 33 friend class DataSeries<Dim>;
34 34
35 35 template <bool IC = IsConst, typename = std::enable_if_t<IC == false> >
36 36 explicit IteratorValue(DataSeries<Dim> &dataSeries, bool begin)
37 37 : m_XIt(begin ? dataSeries.xAxisData()->begin() : dataSeries.xAxisData()->end()),
38 38 m_ValuesIt(begin ? dataSeries.valuesData()->begin() : dataSeries.valuesData()->end())
39 39 {
40 40 }
41 41
42 42 template <bool IC = IsConst, typename = std::enable_if_t<IC == true> >
43 43 explicit IteratorValue(const DataSeries<Dim> &dataSeries, bool begin)
44 44 : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()),
45 45 m_ValuesIt(begin ? dataSeries.valuesData()->cbegin()
46 46 : dataSeries.valuesData()->cend())
47 47 {
48 48 }
49 49
50 50 IteratorValue(const IteratorValue &other) = default;
51 51
52 52 std::unique_ptr<DataSeriesIteratorValue::Impl> clone() const override
53 53 {
54 54 return std::make_unique<IteratorValue<Dim, IsConst> >(*this);
55 55 }
56 56
57 57 int distance(const DataSeriesIteratorValue::Impl &other) const override try {
58 58 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
59 59 return m_XIt->distance(*otherImpl.m_XIt);
60 60 }
61 61 catch (const std::bad_cast &) {
62 62 return 0;
63 63 }
64 64
65 65 bool equals(const DataSeriesIteratorValue::Impl &other) const override try {
66 66 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
67 67 return std::tie(m_XIt, m_ValuesIt) == std::tie(otherImpl.m_XIt, otherImpl.m_ValuesIt);
68 68 }
69 69 catch (const std::bad_cast &) {
70 70 return false;
71 71 }
72 72
73 73 bool lowerThan(const DataSeriesIteratorValue::Impl &other) const override try {
74 74 const auto &otherImpl = dynamic_cast<const IteratorValue &>(other);
75 75 return m_XIt->lowerThan(*otherImpl.m_XIt);
76 76 }
77 77 catch (const std::bad_cast &) {
78 78 return false;
79 79 }
80 80
81 81 std::unique_ptr<DataSeriesIteratorValue::Impl> advance(int offset) const override
82 82 {
83 83 auto result = clone();
84 84 result->next(offset);
85 85 return result;
86 86 }
87 87
88 88 void next(int offset) override
89 89 {
90 90 m_XIt->next(offset);
91 91 m_ValuesIt->next(offset);
92 92 }
93 93
94 94 void prev() override
95 95 {
96 96 --m_XIt;
97 97 --m_ValuesIt;
98 98 }
99 99
100 100 double x() const override { return m_XIt->at(0); }
101 101 double value() const override { return m_ValuesIt->at(0); }
102 102 double value(int componentIndex) const override { return m_ValuesIt->at(componentIndex); }
103 103 double minValue() const override { return m_ValuesIt->min(); }
104 104 double maxValue() const override { return m_ValuesIt->max(); }
105 105 QVector<double> values() const override { return m_ValuesIt->values(); }
106 106
107 107 void swap(DataSeriesIteratorValue::Impl &other) override
108 108 {
109 109 auto &otherImpl = dynamic_cast<IteratorValue &>(other);
110 110 m_XIt->impl()->swap(*otherImpl.m_XIt->impl());
111 111 m_ValuesIt->impl()->swap(*otherImpl.m_ValuesIt->impl());
112 112 }
113 113
114 114 private:
115 115 ArrayDataIterator m_XIt;
116 116 ArrayDataIterator m_ValuesIt;
117 117 };
118 118 } // namespace dataseries_detail
119 119
120 120 /**
121 121 * @brief The DataSeries class is the base (abstract) implementation of IDataSeries.
122 122 *
123 123 * It proposes to set a dimension for the values ​​data.
124 124 *
125 125 * A DataSeries is always sorted on its x-axis data.
126 126 *
127 127 * @tparam Dim The dimension of the values data
128 128 *
129 129 */
130 130 template <int Dim>
131 131 class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries {
132 132 friend class DataSeriesMergeHelper;
133 133
134 134 public:
135 135 /// @sa IDataSeries::xAxisData()
136 136 std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; }
137 137 const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; }
138 138
139 139 /// @sa IDataSeries::xAxisUnit()
140 140 Unit xAxisUnit() const override { return m_XAxisUnit; }
141 141
142 142 /// @return the values dataset
143 143 std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; }
144 144 const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; }
145 145
146 146 /// @sa IDataSeries::valuesUnit()
147 147 Unit valuesUnit() const override { return m_ValuesUnit; }
148 148
149 int nbPoints() const override { return m_XAxisData->totalSize() + m_ValuesData->totalSize(); }
149 150
150 151 SqpRange range() const override
151 152 {
152 153 if (!m_XAxisData->cdata().empty()) {
153 154 return SqpRange{m_XAxisData->cdata().front(), m_XAxisData->cdata().back()};
154 155 }
155 156
156 157 return SqpRange{};
157 158 }
158 159
159 160 void clear()
160 161 {
161 162 m_XAxisData->clear();
162 163 m_ValuesData->clear();
163 164 }
164 165
165 166 bool isEmpty() const noexcept { return m_XAxisData->size() == 0; }
166 167
167 168 /// Merges into the data series an other data series
168 169 /// @remarks the data series to merge with is cleared after the operation
169 170 void merge(IDataSeries *dataSeries) override
170 171 {
171 172 dataSeries->lockWrite();
172 173 lockWrite();
173 174
174 175 if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) {
175 176 DataSeriesMergeHelper::merge(*other, *this);
176 177 }
177 178 else {
178 179 qCWarning(LOG_DataSeries())
179 180 << QObject::tr("Detection of a type of IDataSeries we cannot merge with !");
180 181 }
181 182 unlock();
182 183 dataSeries->unlock();
183 184 }
184 185
185 186 void purge(double min, double max) override
186 187 {
187 188 // Nothing to purge if series is empty
188 189 if (isEmpty()) {
189 190 return;
190 191 }
191 192
192 193 if (min > max) {
193 194 std::swap(min, max);
194 195 }
195 196
196 197 // Nothing to purge if series min/max are inside purge range
197 198 auto xMin = cbegin()->x();
198 199 auto xMax = (--cend())->x();
199 200 if (xMin >= min && xMax <= max) {
200 201 return;
201 202 }
202 203
203 204 auto lowerIt = std::lower_bound(
204 205 begin(), end(), min, [](const auto &it, const auto &val) { return it.x() < val; });
205 206 erase(begin(), lowerIt);
206 207 auto upperIt = std::upper_bound(
207 208 begin(), end(), max, [](const auto &val, const auto &it) { return val < it.x(); });
208 209 erase(upperIt, end());
209 210 }
210 211
211 212 // ///////// //
212 213 // Iterators //
213 214 // ///////// //
214 215
215 216 DataSeriesIterator begin() override
216 217 {
217 218 return DataSeriesIterator{DataSeriesIteratorValue{
218 219 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, true)}};
219 220 }
220 221
221 222 DataSeriesIterator end() override
222 223 {
223 224 return DataSeriesIterator{DataSeriesIteratorValue{
224 225 std::make_unique<dataseries_detail::IteratorValue<Dim, false> >(*this, false)}};
225 226 }
226 227
227 228 DataSeriesIterator cbegin() const override
228 229 {
229 230 return DataSeriesIterator{DataSeriesIteratorValue{
230 231 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, true)}};
231 232 }
232 233
233 234 DataSeriesIterator cend() const override
234 235 {
235 236 return DataSeriesIterator{DataSeriesIteratorValue{
236 237 std::make_unique<dataseries_detail::IteratorValue<Dim, true> >(*this, false)}};
237 238 }
238 239
239 240 void erase(DataSeriesIterator first, DataSeriesIterator last)
240 241 {
241 242 auto firstImpl
242 243 = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(first->impl());
243 244 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, false> *>(last->impl());
244 245
245 246 if (firstImpl && lastImpl) {
246 247 m_XAxisData->erase(firstImpl->m_XIt, lastImpl->m_XIt);
247 248 m_ValuesData->erase(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt);
248 249 }
249 250 }
250 251
251 252 void insert(DataSeriesIterator first, DataSeriesIterator last, bool prepend = false)
252 253 {
253 254 auto firstImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(first->impl());
254 255 auto lastImpl = dynamic_cast<dataseries_detail::IteratorValue<Dim, true> *>(last->impl());
255 256
256 257 if (firstImpl && lastImpl) {
257 258 m_XAxisData->insert(firstImpl->m_XIt, lastImpl->m_XIt, prepend);
258 259 m_ValuesData->insert(firstImpl->m_ValuesIt, lastImpl->m_ValuesIt, prepend);
259 260 }
260 261 }
261 262
262 263 /// @sa IDataSeries::minXAxisData()
263 264 DataSeriesIterator minXAxisData(double minXAxisData) const override
264 265 {
265 266 return std::lower_bound(
266 267 cbegin(), cend(), minXAxisData,
267 268 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
268 269 }
269 270
270 271 /// @sa IDataSeries::maxXAxisData()
271 272 DataSeriesIterator maxXAxisData(double maxXAxisData) const override
272 273 {
273 274 // Gets the first element that greater than max value
274 275 auto it = std::upper_bound(
275 276 cbegin(), cend(), maxXAxisData,
276 277 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
277 278
278 279 return it == cbegin() ? cend() : --it;
279 280 }
280 281
281 282 std::pair<DataSeriesIterator, DataSeriesIterator> xAxisRange(double minXAxisData,
282 283 double maxXAxisData) const override
283 284 {
284 285 if (minXAxisData > maxXAxisData) {
285 286 std::swap(minXAxisData, maxXAxisData);
286 287 }
287 288
288 289 auto begin = cbegin();
289 290 auto end = cend();
290 291
291 292 auto lowerIt = std::lower_bound(
292 293 begin, end, minXAxisData,
293 294 [](const auto &itValue, const auto &value) { return itValue.x() < value; });
294 295 auto upperIt = std::upper_bound(
295 296 lowerIt, end, maxXAxisData,
296 297 [](const auto &value, const auto &itValue) { return value < itValue.x(); });
297 298
298 299 return std::make_pair(lowerIt, upperIt);
299 300 }
300 301
301 302 std::pair<DataSeriesIterator, DataSeriesIterator>
302 303 valuesBounds(double minXAxisData, double maxXAxisData) const override
303 304 {
304 305 // Places iterators to the correct x-axis range
305 306 auto xAxisRangeIts = xAxisRange(minXAxisData, maxXAxisData);
306 307
307 308 // Returns end iterators if the range is empty
308 309 if (xAxisRangeIts.first == xAxisRangeIts.second) {
309 310 return std::make_pair(cend(), cend());
310 311 }
311 312
312 313 // Gets the iterator on the min of all values data
313 314 auto minIt = std::min_element(
314 315 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
315 316 return SortUtils::minCompareWithNaN(it1.minValue(), it2.minValue());
316 317 });
317 318
318 319 // Gets the iterator on the max of all values data
319 320 auto maxIt = std::max_element(
320 321 xAxisRangeIts.first, xAxisRangeIts.second, [](const auto &it1, const auto &it2) {
321 322 return SortUtils::maxCompareWithNaN(it1.maxValue(), it2.maxValue());
322 323 });
323 324
324 325 return std::make_pair(minIt, maxIt);
325 326 }
326 327
327 328 // /////// //
328 329 // Mutexes //
329 330 // /////// //
330 331
331 332 virtual void lockRead() { m_Lock.lockForRead(); }
332 333 virtual void lockWrite() { m_Lock.lockForWrite(); }
333 334 virtual void unlock() { m_Lock.unlock(); }
334 335
335 336 protected:
336 337 /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a
337 338 /// DataSeries with no values will be created.
338 339 /// @remarks data series is automatically sorted on its x-axis data
339 340 explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit,
340 341 std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit)
341 342 : m_XAxisData{xAxisData},
342 343 m_XAxisUnit{xAxisUnit},
343 344 m_ValuesData{valuesData},
344 345 m_ValuesUnit{valuesUnit}
345 346 {
346 347 if (m_XAxisData->size() != m_ValuesData->size()) {
347 348 clear();
348 349 }
349 350
350 351 // Sorts data if it's not the case
351 352 const auto &xAxisCData = m_XAxisData->cdata();
352 353 if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) {
353 354 sort();
354 355 }
355 356 }
356 357
357 358 /// Copy ctor
358 359 explicit DataSeries(const DataSeries<Dim> &other)
359 360 : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)},
360 361 m_XAxisUnit{other.m_XAxisUnit},
361 362 m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)},
362 363 m_ValuesUnit{other.m_ValuesUnit}
363 364 {
364 365 // Since a series is ordered from its construction and is always ordered, it is not
365 366 // necessary to call the sort method here ('other' is sorted)
366 367 }
367 368
368 369 /// Assignment operator
369 370 template <int D>
370 371 DataSeries &operator=(DataSeries<D> other)
371 372 {
372 373 std::swap(m_XAxisData, other.m_XAxisData);
373 374 std::swap(m_XAxisUnit, other.m_XAxisUnit);
374 375 std::swap(m_ValuesData, other.m_ValuesData);
375 376 std::swap(m_ValuesUnit, other.m_ValuesUnit);
376 377
377 378 return *this;
378 379 }
379 380
380 381 private:
381 382 /**
382 383 * Sorts data series on its x-axis data
383 384 */
384 385 void sort() noexcept
385 386 {
386 387 auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>());
387 388 m_XAxisData = m_XAxisData->sort(permutation);
388 389 m_ValuesData = m_ValuesData->sort(permutation);
389 390 }
390 391
391 392 std::shared_ptr<ArrayData<1> > m_XAxisData;
392 393 Unit m_XAxisUnit;
393 394 std::shared_ptr<ArrayData<Dim> > m_ValuesData;
394 395 Unit m_ValuesUnit;
395 396
396 397 QReadWriteLock m_Lock;
397 398 };
398 399
399 400 #endif // SCIQLOP_DATASERIES_H
@@ -1,109 +1,113
1 1 #ifndef SCIQLOP_IDATASERIES_H
2 2 #define SCIQLOP_IDATASERIES_H
3 3
4 4 #include <Common/MetaTypes.h>
5 5 #include <Data/DataSeriesIterator.h>
6 6 #include <Data/SqpRange.h>
7 7
8 8 #include <memory>
9 9
10 10 #include <QString>
11 11
12 12 template <int Dim>
13 13 class ArrayData;
14 14
15 15 struct Unit {
16 16 explicit Unit(const QString &name = {}, bool timeUnit = false)
17 17 : m_Name{name}, m_TimeUnit{timeUnit}
18 18 {
19 19 }
20 20
21 21 inline bool operator==(const Unit &other) const
22 22 {
23 23 return std::tie(m_Name, m_TimeUnit) == std::tie(other.m_Name, other.m_TimeUnit);
24 24 }
25 25 inline bool operator!=(const Unit &other) const { return !(*this == other); }
26 26
27 27 QString m_Name; ///< Unit name
28 28 bool m_TimeUnit; ///< The unit is a unit of time (UTC)
29 29 };
30 30
31 31 /**
32 32 * @brief The IDataSeries aims to declare a data series.
33 33 *
34 34 * A data series is an entity that contains at least :
35 35 * - one dataset representing the x-axis
36 36 * - one dataset representing the values
37 37 *
38 38 * Each dataset is represented by an ArrayData, and is associated with a unit.
39 39 *
40 40 * An ArrayData can be unidimensional or two-dimensional, depending on the implementation of the
41 41 * IDataSeries. The x-axis dataset is always unidimensional.
42 42 *
43 43 * @sa ArrayData
44 44 */
45 45 class IDataSeries {
46 46 public:
47 47 virtual ~IDataSeries() noexcept = default;
48 48
49 49 /// Returns the x-axis dataset
50 50 virtual std::shared_ptr<ArrayData<1> > xAxisData() = 0;
51 51
52 52 /// Returns the x-axis dataset (as const)
53 53 virtual const std::shared_ptr<ArrayData<1> > xAxisData() const = 0;
54 54
55 55 virtual Unit xAxisUnit() const = 0;
56 56
57 57 virtual Unit valuesUnit() const = 0;
58 58
59 59 virtual void merge(IDataSeries *dataSeries) = 0;
60 60 /// Removes from data series all entries whose value on the x-axis is not between min and max
61 61 virtual void purge(double min, double max) = 0;
62 62
63 63 /// @todo Review the name and signature of this method
64 64 virtual std::shared_ptr<IDataSeries> subDataSeries(const SqpRange &range) = 0;
65 65
66 66 virtual std::unique_ptr<IDataSeries> clone() const = 0;
67
68 /// @return the total number of points contained in the data series
69 virtual int nbPoints() const = 0;
70
67 71 virtual SqpRange range() const = 0;
68 72
69 73 // ///////// //
70 74 // Iterators //
71 75 // ///////// //
72 76
73 77 virtual DataSeriesIterator cbegin() const = 0;
74 78 virtual DataSeriesIterator cend() const = 0;
75 79 virtual DataSeriesIterator begin() = 0;
76 80 virtual DataSeriesIterator end() = 0;
77 81
78 82 /// @return the iterator to the first entry of the data series whose x-axis data is greater than
79 83 /// or equal to the value passed in parameter, or the end iterator if there is no matching value
80 84 virtual DataSeriesIterator minXAxisData(double minXAxisData) const = 0;
81 85
82 86 /// @return the iterator to the last entry of the data series whose x-axis data is less than or
83 87 /// equal to the value passed in parameter, or the end iterator if there is no matching value
84 88 virtual DataSeriesIterator maxXAxisData(double maxXAxisData) const = 0;
85 89
86 90 /// @return the iterators pointing to the range of data whose x-axis values are between min and
87 91 /// max passed in parameters
88 92 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
89 93 xAxisRange(double minXAxisData, double maxXAxisData) const = 0;
90 94
91 95 /// @return two iterators pointing to the data that have respectively the min and the max value
92 96 /// data of a data series' range. The search is performed for a given x-axis range.
93 97 /// @sa xAxisRange()
94 98 virtual std::pair<DataSeriesIterator, DataSeriesIterator>
95 99 valuesBounds(double minXAxisData, double maxXAxisData) const = 0;
96 100
97 101 // /////// //
98 102 // Mutexes //
99 103 // /////// //
100 104
101 105 virtual void lockRead() = 0;
102 106 virtual void lockWrite() = 0;
103 107 virtual void unlock() = 0;
104 108 };
105 109
106 110 // Required for using shared_ptr in signals/slots
107 111 SCIQLOP_REGISTER_META_TYPE(IDATASERIES_PTR_REGISTRY, std::shared_ptr<IDataSeries>)
108 112
109 113 #endif // SCIQLOP_IDATASERIES_H
@@ -1,80 +1,84
1 1 #ifndef SCIQLOP_VARIABLE_H
2 2 #define SCIQLOP_VARIABLE_H
3 3
4 4 #include "CoreGlobal.h"
5 5
6 6 #include <Data/DataSeriesIterator.h>
7 7 #include <Data/SqpRange.h>
8 8
9 9 #include <QLoggingCategory>
10 10 #include <QObject>
11 11
12 12 #include <Common/MetaTypes.h>
13 13 #include <Common/spimpl.h>
14 14
15 15 Q_DECLARE_LOGGING_CATEGORY(LOG_Variable)
16 16
17 17 class IDataSeries;
18 18 class QString;
19 19
20 20 /**
21 21 * @brief The Variable class represents a variable in SciQlop.
22 22 */
23 23 class SCIQLOP_CORE_EXPORT Variable : public QObject {
24 24
25 25 Q_OBJECT
26 26
27 27 public:
28 28 explicit Variable(const QString &name, const SqpRange &dateTime,
29 29 const QVariantHash &metadata = {});
30 30
31 31 /// Copy ctor
32 32 explicit Variable(const Variable &other);
33 33
34 34 std::shared_ptr<Variable> clone() const;
35 35
36 36 QString name() const noexcept;
37 37 void setName(const QString &name) noexcept;
38 38 SqpRange range() const noexcept;
39 39 void setRange(const SqpRange &range) noexcept;
40 40 SqpRange cacheRange() const noexcept;
41 41 void setCacheRange(const SqpRange &cacheRange) noexcept;
42 42
43 /// @return the number of points hold by the variable. The number of points is updated each time
44 /// the data series changes
45 int nbPoints() const noexcept;
46
43 47 /// Returns the real range of the variable, i.e. the min and max x-axis values of the data
44 48 /// series between the range of the variable. The real range is updated each time the variable
45 49 /// range or the data series changed
46 50 /// @return the real range, invalid range if the data series is null or empty
47 51 /// @sa setDataSeries()
48 52 /// @sa setRange()
49 53 SqpRange realRange() const noexcept;
50 54
51 55 /// @return the data of the variable, nullptr if there is no data
52 56 std::shared_ptr<IDataSeries> dataSeries() const noexcept;
53 57
54 58 QVariantHash metadata() const noexcept;
55 59
56 60 bool contains(const SqpRange &range) const noexcept;
57 61 bool intersect(const SqpRange &range) const noexcept;
58 62 bool isInside(const SqpRange &range) const noexcept;
59 63
60 64 bool cacheContains(const SqpRange &range) const noexcept;
61 65 bool cacheIntersect(const SqpRange &range) const noexcept;
62 66 bool cacheIsInside(const SqpRange &range) const noexcept;
63 67
64 68 QVector<SqpRange> provideNotInCacheRangeList(const SqpRange &range) const noexcept;
65 69 QVector<SqpRange> provideInCacheRangeList(const SqpRange &range) const noexcept;
66 70 void mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept;
67 71
68 72 signals:
69 73 void updated();
70 74
71 75 private:
72 76 class VariablePrivate;
73 77 spimpl::unique_impl_ptr<VariablePrivate> impl;
74 78 };
75 79
76 80 // Required for using shared_ptr in signals/slots
77 81 SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_REGISTRY, std::shared_ptr<Variable>)
78 82 SCIQLOP_REGISTER_META_TYPE(VARIABLE_PTR_VECTOR_REGISTRY, QVector<std::shared_ptr<Variable> >)
79 83
80 84 #endif // SCIQLOP_VARIABLE_H
@@ -1,296 +1,307
1 1 #include "Variable/Variable.h"
2 2
3 3 #include <Data/IDataSeries.h>
4 4 #include <Data/SqpRange.h>
5 5
6 6 #include <QMutex>
7 7 #include <QReadWriteLock>
8 8 #include <QThread>
9 9
10 10 Q_LOGGING_CATEGORY(LOG_Variable, "Variable")
11 11
12 12 struct Variable::VariablePrivate {
13 13 explicit VariablePrivate(const QString &name, const SqpRange &dateTime,
14 14 const QVariantHash &metadata)
15 15 : m_Name{name},
16 16 m_Range{dateTime},
17 17 m_Metadata{metadata},
18 18 m_DataSeries{nullptr},
19 m_RealRange{INVALID_RANGE}
19 m_RealRange{INVALID_RANGE},
20 m_NbPoints{0}
20 21 {
21 22 }
22 23
23 24 VariablePrivate(const VariablePrivate &other)
24 25 : m_Name{other.m_Name},
25 26 m_Range{other.m_Range},
26 27 m_Metadata{other.m_Metadata},
27 28 m_DataSeries{other.m_DataSeries != nullptr ? other.m_DataSeries->clone() : nullptr},
28 m_RealRange{other.m_RealRange}
29 m_RealRange{other.m_RealRange},
30 m_NbPoints{other.m_NbPoints}
29 31 {
30 32 }
31 33
32 34 void lockRead() { m_Lock.lockForRead(); }
33 35 void lockWrite() { m_Lock.lockForWrite(); }
34 36 void unlock() { m_Lock.unlock(); }
35 37
36 38 void purgeDataSeries()
37 39 {
38 40 if (m_DataSeries) {
39 41 m_DataSeries->purge(m_CacheRange.m_TStart, m_CacheRange.m_TEnd);
40 42 }
41 43 updateRealRange();
44 updateNbPoints();
42 45 }
43 46
47 void updateNbPoints() { m_NbPoints = m_DataSeries ? m_DataSeries->nbPoints() : 0; }
48
44 49 /// Updates real range according to current variable range and data series
45 50 void updateRealRange()
46 51 {
47 52 if (m_DataSeries) {
48 53 m_DataSeries->lockRead();
49 54 auto end = m_DataSeries->cend();
50 55 auto minXAxisIt = m_DataSeries->minXAxisData(m_Range.m_TStart);
51 56 auto maxXAxisIt = m_DataSeries->maxXAxisData(m_Range.m_TEnd);
52 57
53 58 m_RealRange = (minXAxisIt != end && maxXAxisIt != end)
54 59 ? SqpRange{minXAxisIt->x(), maxXAxisIt->x()}
55 60 : INVALID_RANGE;
56 61 m_DataSeries->unlock();
57 62 }
58 63 else {
59 64 m_RealRange = INVALID_RANGE;
60 65 }
61 66 }
62 67
63 68 QString m_Name;
64 69
65 70 SqpRange m_Range;
66 71 SqpRange m_CacheRange;
67 72 QVariantHash m_Metadata;
68 73 std::shared_ptr<IDataSeries> m_DataSeries;
69 74 SqpRange m_RealRange;
75 int m_NbPoints;
70 76
71 77 QReadWriteLock m_Lock;
72 78 };
73 79
74 80 Variable::Variable(const QString &name, const SqpRange &dateTime, const QVariantHash &metadata)
75 81 : impl{spimpl::make_unique_impl<VariablePrivate>(name, dateTime, metadata)}
76 82 {
77 83 }
78 84
79 85 Variable::Variable(const Variable &other)
80 86 : impl{spimpl::make_unique_impl<VariablePrivate>(*other.impl)}
81 87 {
82 88 }
83 89
84 90 std::shared_ptr<Variable> Variable::clone() const
85 91 {
86 92 return std::make_shared<Variable>(*this);
87 93 }
88 94
89 95 QString Variable::name() const noexcept
90 96 {
91 97 impl->lockRead();
92 98 auto name = impl->m_Name;
93 99 impl->unlock();
94 100 return name;
95 101 }
96 102
97 103 void Variable::setName(const QString &name) noexcept
98 104 {
99 105 impl->lockWrite();
100 106 impl->m_Name = name;
101 107 impl->unlock();
102 108 }
103 109
104 110 SqpRange Variable::range() const noexcept
105 111 {
106 112 impl->lockRead();
107 113 auto range = impl->m_Range;
108 114 impl->unlock();
109 115 return range;
110 116 }
111 117
112 118 void Variable::setRange(const SqpRange &range) noexcept
113 119 {
114 120 impl->lockWrite();
115 121 impl->m_Range = range;
116 122 impl->updateRealRange();
117 123 impl->unlock();
118 124 }
119 125
120 126 SqpRange Variable::cacheRange() const noexcept
121 127 {
122 128 impl->lockRead();
123 129 auto cacheRange = impl->m_CacheRange;
124 130 impl->unlock();
125 131 return cacheRange;
126 132 }
127 133
128 134 void Variable::setCacheRange(const SqpRange &cacheRange) noexcept
129 135 {
130 136 impl->lockWrite();
131 137 if (cacheRange != impl->m_CacheRange) {
132 138 impl->m_CacheRange = cacheRange;
133 139 impl->purgeDataSeries();
134 140 }
135 141 impl->unlock();
136 142 }
137 143
144 int Variable::nbPoints() const noexcept
145 {
146 return impl->m_NbPoints;
147 }
148
138 149 SqpRange Variable::realRange() const noexcept
139 150 {
140 151 return impl->m_RealRange;
141 152 }
142 153
143 154 void Variable::mergeDataSeries(std::shared_ptr<IDataSeries> dataSeries) noexcept
144 155 {
145 156 qCDebug(LOG_Variable()) << "TORM Variable::mergeDataSeries"
146 157 << QThread::currentThread()->objectName();
147 158 if (!dataSeries) {
148 159 /// @todo ALX : log
149 160 return;
150 161 }
151 162
152 163 // Add or merge the data
153 164 impl->lockWrite();
154 165 if (!impl->m_DataSeries) {
155 166 impl->m_DataSeries = dataSeries->clone();
156 167 }
157 168 else {
158 169 impl->m_DataSeries->merge(dataSeries.get());
159 170 }
160 171 impl->purgeDataSeries();
161 172 impl->unlock();
162 173 }
163 174
164 175 std::shared_ptr<IDataSeries> Variable::dataSeries() const noexcept
165 176 {
166 177 impl->lockRead();
167 178 auto dataSeries = impl->m_DataSeries;
168 179 impl->unlock();
169 180
170 181 return dataSeries;
171 182 }
172 183
173 184 QVariantHash Variable::metadata() const noexcept
174 185 {
175 186 impl->lockRead();
176 187 auto metadata = impl->m_Metadata;
177 188 impl->unlock();
178 189 return metadata;
179 190 }
180 191
181 192 bool Variable::contains(const SqpRange &range) const noexcept
182 193 {
183 194 impl->lockRead();
184 195 auto res = impl->m_Range.contains(range);
185 196 impl->unlock();
186 197 return res;
187 198 }
188 199
189 200 bool Variable::intersect(const SqpRange &range) const noexcept
190 201 {
191 202
192 203 impl->lockRead();
193 204 auto res = impl->m_Range.intersect(range);
194 205 impl->unlock();
195 206 return res;
196 207 }
197 208
198 209 bool Variable::isInside(const SqpRange &range) const noexcept
199 210 {
200 211 impl->lockRead();
201 212 auto res = range.contains(SqpRange{impl->m_Range.m_TStart, impl->m_Range.m_TEnd});
202 213 impl->unlock();
203 214 return res;
204 215 }
205 216
206 217 bool Variable::cacheContains(const SqpRange &range) const noexcept
207 218 {
208 219 impl->lockRead();
209 220 auto res = impl->m_CacheRange.contains(range);
210 221 impl->unlock();
211 222 return res;
212 223 }
213 224
214 225 bool Variable::cacheIntersect(const SqpRange &range) const noexcept
215 226 {
216 227 impl->lockRead();
217 228 auto res = impl->m_CacheRange.intersect(range);
218 229 impl->unlock();
219 230 return res;
220 231 }
221 232
222 233 bool Variable::cacheIsInside(const SqpRange &range) const noexcept
223 234 {
224 235 impl->lockRead();
225 236 auto res = range.contains(SqpRange{impl->m_CacheRange.m_TStart, impl->m_CacheRange.m_TEnd});
226 237 impl->unlock();
227 238 return res;
228 239 }
229 240
230 241
231 242 QVector<SqpRange> Variable::provideNotInCacheRangeList(const SqpRange &range) const noexcept
232 243 {
233 244 // This code assume that cach in contigue. Can return 0, 1 or 2 SqpRange
234 245
235 246 auto notInCache = QVector<SqpRange>{};
236 247
237 248 if (!this->cacheContains(range)) {
238 249 if (range.m_TEnd <= impl->m_CacheRange.m_TStart
239 250 || range.m_TStart >= impl->m_CacheRange.m_TEnd) {
240 251 notInCache << range;
241 252 }
242 253 else if (range.m_TStart < impl->m_CacheRange.m_TStart
243 254 && range.m_TEnd <= impl->m_CacheRange.m_TEnd) {
244 255 notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart};
245 256 }
246 257 else if (range.m_TStart < impl->m_CacheRange.m_TStart
247 258 && range.m_TEnd > impl->m_CacheRange.m_TEnd) {
248 259 notInCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TStart}
249 260 << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd};
250 261 }
251 262 else if (range.m_TStart < impl->m_CacheRange.m_TEnd) {
252 263 notInCache << SqpRange{impl->m_CacheRange.m_TEnd, range.m_TEnd};
253 264 }
254 265 else {
255 266 qCCritical(LOG_Variable()) << tr("Detection of unknown case.")
256 267 << QThread::currentThread();
257 268 }
258 269 }
259 270
260 271 return notInCache;
261 272 }
262 273
263 274 QVector<SqpRange> Variable::provideInCacheRangeList(const SqpRange &range) const noexcept
264 275 {
265 276 // This code assume that cach in contigue. Can return 0 or 1 SqpRange
266 277
267 278 auto inCache = QVector<SqpRange>{};
268 279
269 280
270 281 if (this->intersect(range)) {
271 282 if (range.m_TStart <= impl->m_CacheRange.m_TStart
272 283 && range.m_TEnd >= impl->m_CacheRange.m_TStart
273 284 && range.m_TEnd < impl->m_CacheRange.m_TEnd) {
274 285 inCache << SqpRange{impl->m_CacheRange.m_TStart, range.m_TEnd};
275 286 }
276 287
277 288 else if (range.m_TStart >= impl->m_CacheRange.m_TStart
278 289 && range.m_TEnd <= impl->m_CacheRange.m_TEnd) {
279 290 inCache << range;
280 291 }
281 292 else if (range.m_TStart > impl->m_CacheRange.m_TStart
282 293 && range.m_TEnd > impl->m_CacheRange.m_TEnd) {
283 294 inCache << SqpRange{range.m_TStart, impl->m_CacheRange.m_TEnd};
284 295 }
285 296 else if (range.m_TStart <= impl->m_CacheRange.m_TStart
286 297 && range.m_TEnd >= impl->m_CacheRange.m_TEnd) {
287 298 inCache << impl->m_CacheRange;
288 299 }
289 300 else {
290 301 qCCritical(LOG_Variable()) << tr("Detection of unknown case.")
291 302 << QThread::currentThread();
292 303 }
293 304 }
294 305
295 306 return inCache;
296 307 }
@@ -1,290 +1,294
1 1 #include <Variable/Variable.h>
2 2 #include <Variable/VariableModel.h>
3 3
4 4 #include <Common/DateUtils.h>
5 5 #include <Common/StringUtils.h>
6 6
7 7 #include <Data/IDataSeries.h>
8 8
9 9 #include <QSize>
10 10 #include <unordered_map>
11 11
12 12 Q_LOGGING_CATEGORY(LOG_VariableModel, "VariableModel")
13 13
14 14 namespace {
15 15
16 16 // Column indexes
17 17 const auto NAME_COLUMN = 0;
18 18 const auto TSTART_COLUMN = 1;
19 19 const auto TEND_COLUMN = 2;
20 const auto UNIT_COLUMN = 3;
21 const auto MISSION_COLUMN = 4;
22 const auto PLUGIN_COLUMN = 5;
23 const auto NB_COLUMNS = 6;
20 const auto NBPOINTS_COLUMN = 3;
21 const auto UNIT_COLUMN = 4;
22 const auto MISSION_COLUMN = 5;
23 const auto PLUGIN_COLUMN = 6;
24 const auto NB_COLUMNS = 7;
24 25
25 26 // Column properties
26 27 const auto DEFAULT_HEIGHT = 25;
27 28 const auto DEFAULT_WIDTH = 100;
28 29
29 30 struct ColumnProperties {
30 31 ColumnProperties(const QString &name = {}, int width = DEFAULT_WIDTH,
31 32 int height = DEFAULT_HEIGHT)
32 33 : m_Name{name}, m_Width{width}, m_Height{height}
33 34 {
34 35 }
35 36
36 37 QString m_Name;
37 38 int m_Width;
38 39 int m_Height;
39 40 };
40 41
41 42 const auto COLUMN_PROPERTIES = QHash<int, ColumnProperties>{
42 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
43 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {UNIT_COLUMN, {QObject::tr("Unit")}},
44 {MISSION_COLUMN, {QObject::tr("Mission")}}, {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
43 {NAME_COLUMN, {QObject::tr("Name")}}, {TSTART_COLUMN, {QObject::tr("tStart"), 180}},
44 {TEND_COLUMN, {QObject::tr("tEnd"), 180}}, {NBPOINTS_COLUMN, {QObject::tr("Nb points")}},
45 {UNIT_COLUMN, {QObject::tr("Unit")}}, {MISSION_COLUMN, {QObject::tr("Mission")}},
46 {PLUGIN_COLUMN, {QObject::tr("Plugin")}}};
45 47
46 48 /// Format for datetimes
47 49 const auto DATETIME_FORMAT = QStringLiteral("dd/MM/yyyy \nhh:mm:ss:zzz");
48 50
49 51 QString uniqueName(const QString &defaultName,
50 52 const std::vector<std::shared_ptr<Variable> > &variables)
51 53 {
52 54 auto forbiddenNames = std::vector<QString>(variables.size());
53 55 std::transform(variables.cbegin(), variables.cend(), forbiddenNames.begin(),
54 56 [](const auto &variable) { return variable->name(); });
55 57 auto uniqueName = StringUtils::uniqueName(defaultName, forbiddenNames);
56 58 Q_ASSERT(!uniqueName.isEmpty());
57 59
58 60 return uniqueName;
59 61 }
60 62
61 63 } // namespace
62 64
63 65 struct VariableModel::VariableModelPrivate {
64 66 /// Variables created in SciQlop
65 67 std::vector<std::shared_ptr<Variable> > m_Variables;
66 68 std::unordered_map<std::shared_ptr<Variable>, double> m_VariableToProgress;
67 69
68 70 /// Return the row index of the variable. -1 if it's not found
69 71 int indexOfVariable(Variable *variable) const noexcept;
70 72 };
71 73
72 74 VariableModel::VariableModel(QObject *parent)
73 75 : QAbstractTableModel{parent}, impl{spimpl::make_unique_impl<VariableModelPrivate>()}
74 76 {
75 77 }
76 78
77 79 void VariableModel::addVariable(std::shared_ptr<Variable> variable) noexcept
78 80 {
79 81 auto insertIndex = rowCount();
80 82 beginInsertRows({}, insertIndex, insertIndex);
81 83
82 84 // Generates unique name for the variable
83 85 variable->setName(uniqueName(variable->name(), impl->m_Variables));
84 86
85 87 impl->m_Variables.push_back(variable);
86 88 connect(variable.get(), &Variable::updated, this, &VariableModel::onVariableUpdated);
87 89
88 90 endInsertRows();
89 91 }
90 92
91 93 bool VariableModel::containsVariable(std::shared_ptr<Variable> variable) const noexcept
92 94 {
93 95 auto end = impl->m_Variables.cend();
94 96 return std::find(impl->m_Variables.cbegin(), end, variable) != end;
95 97 }
96 98
97 99 std::shared_ptr<Variable> VariableModel::createVariable(const QString &name,
98 100 const SqpRange &dateTime,
99 101 const QVariantHash &metadata) noexcept
100 102 {
101 103 auto variable = std::make_shared<Variable>(name, dateTime, metadata);
102 104 addVariable(variable);
103 105
104 106 return variable;
105 107 }
106 108
107 109 void VariableModel::deleteVariable(std::shared_ptr<Variable> variable) noexcept
108 110 {
109 111 if (!variable) {
110 112 qCCritical(LOG_Variable()) << "Can't delete a null variable from the model";
111 113 return;
112 114 }
113 115
114 116 // Finds variable in the model
115 117 auto begin = impl->m_Variables.cbegin();
116 118 auto end = impl->m_Variables.cend();
117 119 auto it = std::find(begin, end, variable);
118 120 if (it != end) {
119 121 auto removeIndex = std::distance(begin, it);
120 122
121 123 // Deletes variable
122 124 beginRemoveRows({}, removeIndex, removeIndex);
123 125 impl->m_Variables.erase(it);
124 126 endRemoveRows();
125 127 }
126 128 else {
127 129 qCritical(LOG_VariableModel())
128 130 << tr("Can't delete variable %1 from the model: the variable is not in the model")
129 131 .arg(variable->name());
130 132 }
131 133
132 134 // Removes variable from progress map
133 135 impl->m_VariableToProgress.erase(variable);
134 136 }
135 137
136 138
137 139 std::shared_ptr<Variable> VariableModel::variable(int index) const
138 140 {
139 141 return (index >= 0 && index < impl->m_Variables.size()) ? impl->m_Variables[index] : nullptr;
140 142 }
141 143
142 144 std::vector<std::shared_ptr<Variable> > VariableModel::variables() const
143 145 {
144 146 return impl->m_Variables;
145 147 }
146 148
147 149 void VariableModel::setDataProgress(std::shared_ptr<Variable> variable, double progress)
148 150 {
149 151 if (progress > 0.0) {
150 152 impl->m_VariableToProgress[variable] = progress;
151 153 }
152 154 else {
153 155 impl->m_VariableToProgress.erase(variable);
154 156 }
155 157 auto modelIndex = createIndex(impl->indexOfVariable(variable.get()), NAME_COLUMN);
156 158
157 159 emit dataChanged(modelIndex, modelIndex);
158 160 }
159 161
160 162 int VariableModel::columnCount(const QModelIndex &parent) const
161 163 {
162 164 Q_UNUSED(parent);
163 165
164 166 return NB_COLUMNS;
165 167 }
166 168
167 169 int VariableModel::rowCount(const QModelIndex &parent) const
168 170 {
169 171 Q_UNUSED(parent);
170 172
171 173 return impl->m_Variables.size();
172 174 }
173 175
174 176 QVariant VariableModel::data(const QModelIndex &index, int role) const
175 177 {
176 178 if (!index.isValid()) {
177 179 return QVariant{};
178 180 }
179 181
180 182 if (index.row() < 0 || index.row() >= rowCount()) {
181 183 return QVariant{};
182 184 }
183 185
184 186 if (role == Qt::DisplayRole) {
185 187 if (auto variable = impl->m_Variables.at(index.row()).get()) {
186 188 switch (index.column()) {
187 189 case NAME_COLUMN:
188 190 return variable->name();
189 191 case TSTART_COLUMN: {
190 192 auto range = variable->realRange();
191 193 return range != INVALID_RANGE
192 194 ? DateUtils::dateTime(range.m_TStart).toString(DATETIME_FORMAT)
193 195 : QVariant{};
194 196 }
195 197 case TEND_COLUMN: {
196 198 auto range = variable->realRange();
197 199 return range != INVALID_RANGE
198 200 ? DateUtils::dateTime(range.m_TEnd).toString(DATETIME_FORMAT)
199 201 : QVariant{};
200 202 }
203 case NBPOINTS_COLUMN:
204 return variable->nbPoints();
201 205 case UNIT_COLUMN:
202 206 return variable->metadata().value(QStringLiteral("units"));
203 207 case MISSION_COLUMN:
204 208 return variable->metadata().value(QStringLiteral("mission"));
205 209 case PLUGIN_COLUMN:
206 210 return variable->metadata().value(QStringLiteral("plugin"));
207 211 default:
208 212 // No action
209 213 break;
210 214 }
211 215
212 216 qWarning(LOG_VariableModel())
213 217 << tr("Can't get data (unknown column %1)").arg(index.column());
214 218 }
215 219 else {
216 220 qWarning(LOG_VariableModel()) << tr("Can't get data (no variable)");
217 221 }
218 222 }
219 223 else if (role == VariableRoles::ProgressRole) {
220 224 if (auto variable = impl->m_Variables.at(index.row())) {
221 225
222 226 auto it = impl->m_VariableToProgress.find(variable);
223 227 if (it != impl->m_VariableToProgress.cend()) {
224 228 return it->second;
225 229 }
226 230 }
227 231 }
228 232
229 233 return QVariant{};
230 234 }
231 235
232 236 QVariant VariableModel::headerData(int section, Qt::Orientation orientation, int role) const
233 237 {
234 238 if (role != Qt::DisplayRole && role != Qt::SizeHintRole) {
235 239 return QVariant{};
236 240 }
237 241
238 242 if (orientation == Qt::Horizontal) {
239 243 auto propertiesIt = COLUMN_PROPERTIES.find(section);
240 244 if (propertiesIt != COLUMN_PROPERTIES.cend()) {
241 245 // Role is either DisplayRole or SizeHintRole
242 246 return (role == Qt::DisplayRole)
243 247 ? QVariant{propertiesIt->m_Name}
244 248 : QVariant{QSize{propertiesIt->m_Width, propertiesIt->m_Height}};
245 249 }
246 250 else {
247 251 qWarning(LOG_VariableModel())
248 252 << tr("Can't get header data (unknown column %1)").arg(section);
249 253 }
250 254 }
251 255
252 256 return QVariant{};
253 257 }
254 258
255 259 void VariableModel::abortProgress(const QModelIndex &index)
256 260 {
257 261 if (auto variable = impl->m_Variables.at(index.row())) {
258 262 emit abortProgessRequested(variable);
259 263 }
260 264 }
261 265
262 266 void VariableModel::onVariableUpdated() noexcept
263 267 {
264 268 // Finds variable that has been updated in the model
265 269 if (auto updatedVariable = dynamic_cast<Variable *>(sender())) {
266 270 auto updatedVariableIndex = impl->indexOfVariable(updatedVariable);
267 271
268 272 if (updatedVariableIndex > -1) {
269 273 emit dataChanged(createIndex(updatedVariableIndex, 0),
270 274 createIndex(updatedVariableIndex, columnCount() - 1));
271 275 }
272 276 }
273 277 }
274 278
275 279 int VariableModel::VariableModelPrivate::indexOfVariable(Variable *variable) const noexcept
276 280 {
277 281 auto begin = std::cbegin(m_Variables);
278 282 auto end = std::cend(m_Variables);
279 283 auto it
280 284 = std::find_if(begin, end, [variable](const auto &var) { return var.get() == variable; });
281 285
282 286 if (it != end) {
283 287 // Gets the index of the variable in the model: we assume here that views have the same
284 288 // order as the model
285 289 return std::distance(begin, it);
286 290 }
287 291 else {
288 292 return -1;
289 293 }
290 294 }
General Comments 0
You need to be logged in to leave comments. Login now