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