@@ -0,0 +1,19 | |||||
|
1 | #ifndef SCIQLOP_COLORUTILS_H | |||
|
2 | #define SCIQLOP_COLORUTILS_H | |||
|
3 | ||||
|
4 | #include <vector> | |||
|
5 | ||||
|
6 | class QColor; | |||
|
7 | ||||
|
8 | /** | |||
|
9 | * Utility class with methods for colors | |||
|
10 | */ | |||
|
11 | struct ColorUtils { | |||
|
12 | /// Generates a color scale from min / max values and a number of colors. | |||
|
13 | /// The algorithm uses the HSV color model to generate color variations (see | |||
|
14 | /// http://doc.qt.io/qt-4.8/qcolor.html#the-hsv-color-model) | |||
|
15 | static std::vector<QColor> colors(const QColor &minColor, const QColor &maxColor, | |||
|
16 | int nbColors) noexcept; | |||
|
17 | }; | |||
|
18 | ||||
|
19 | #endif // SCIQLOP_COLORUTILS_H |
@@ -0,0 +1,10 | |||||
|
1 | #ifndef SCIQLOP_VISUALIZATIONDEFS_H | |||
|
2 | #define SCIQLOP_VISUALIZATIONDEFS_H | |||
|
3 | ||||
|
4 | #include <map> | |||
|
5 | ||||
|
6 | class QCPAbstractPlottable; | |||
|
7 | ||||
|
8 | using PlottablesMap = std::map<int, QCPAbstractPlottable *>; | |||
|
9 | ||||
|
10 | #endif // SCIQLOP_VISUALIZATIONDEFS_H |
@@ -0,0 +1,29 | |||||
|
1 | #include "Common/ColorUtils.h" | |||
|
2 | ||||
|
3 | #include <QtGui/QColor> | |||
|
4 | ||||
|
5 | std::vector<QColor> ColorUtils::colors(const QColor &minColor, const QColor &maxColor, | |||
|
6 | int nbColors) noexcept | |||
|
7 | { | |||
|
8 | auto result = std::vector<QColor>{}; | |||
|
9 | ||||
|
10 | if (nbColors == 1) { | |||
|
11 | result.push_back(minColor); | |||
|
12 | } | |||
|
13 | else if (nbColors > 0) { | |||
|
14 | const auto nbSteps = static_cast<double>(nbColors - 1); | |||
|
15 | ||||
|
16 | const auto colorHStep = (maxColor.hue() - minColor.hue()) / nbSteps; | |||
|
17 | const auto colorSStep = (maxColor.saturation() - minColor.saturation()) / nbSteps; | |||
|
18 | const auto colorVStep = (maxColor.value() - minColor.value()) / nbSteps; | |||
|
19 | const auto colorAStep = (maxColor.alpha() - minColor.alpha()) / nbSteps; | |||
|
20 | ||||
|
21 | for (auto i = 0; i < nbColors; ++i) { | |||
|
22 | result.push_back(QColor::fromHsv( | |||
|
23 | minColor.hue() + i * colorHStep, minColor.saturation() + i * colorSStep, | |||
|
24 | minColor.value() + i * colorVStep, minColor.alpha() + i * colorAStep)); | |||
|
25 | } | |||
|
26 | } | |||
|
27 | ||||
|
28 | return result; | |||
|
29 | } |
@@ -1,313 +1,315 | |||||
1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
1 | #ifndef SCIQLOP_ARRAYDATA_H | |
2 | #define SCIQLOP_ARRAYDATA_H |
|
2 | #define SCIQLOP_ARRAYDATA_H | |
3 |
|
3 | |||
4 | #include <Common/SortUtils.h> |
|
4 | #include <Common/SortUtils.h> | |
5 |
|
5 | |||
6 | #include <QReadLocker> |
|
6 | #include <QReadLocker> | |
7 | #include <QReadWriteLock> |
|
7 | #include <QReadWriteLock> | |
8 | #include <QVector> |
|
8 | #include <QVector> | |
9 |
|
9 | |||
10 | #include <memory> |
|
10 | #include <memory> | |
11 |
|
11 | |||
12 | template <int Dim> |
|
12 | template <int Dim> | |
13 | class ArrayData; |
|
13 | class ArrayData; | |
14 |
|
14 | |||
15 | using DataContainer = QVector<QVector<double> >; |
|
15 | using DataContainer = QVector<QVector<double> >; | |
16 |
|
16 | |||
17 | namespace arraydata_detail { |
|
17 | namespace arraydata_detail { | |
18 |
|
18 | |||
19 | /// Struct used to sort ArrayData |
|
19 | /// Struct used to sort ArrayData | |
20 | template <int Dim> |
|
20 | template <int Dim> | |
21 | struct Sort { |
|
21 | struct Sort { | |
22 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, |
|
22 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, | |
23 | const std::vector<int> &sortPermutation) |
|
23 | const std::vector<int> &sortPermutation) | |
24 | { |
|
24 | { | |
25 | auto nbComponents = data.size(); |
|
25 | auto nbComponents = data.size(); | |
26 | auto sortedData = DataContainer(nbComponents); |
|
26 | auto sortedData = DataContainer(nbComponents); | |
27 |
|
27 | |||
28 | for (auto i = 0; i < nbComponents; ++i) { |
|
28 | for (auto i = 0; i < nbComponents; ++i) { | |
29 | sortedData[i] = SortUtils::sort(data.at(i), sortPermutation); |
|
29 | sortedData[i] = SortUtils::sort(data.at(i), sortPermutation); | |
30 | } |
|
30 | } | |
31 |
|
31 | |||
32 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); |
|
32 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); | |
33 | } |
|
33 | } | |
34 | }; |
|
34 | }; | |
35 |
|
35 | |||
36 | /// Specialization for uni-dimensional ArrayData |
|
36 | /// Specialization for uni-dimensional ArrayData | |
37 | template <> |
|
37 | template <> | |
38 | struct Sort<1> { |
|
38 | struct Sort<1> { | |
39 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, |
|
39 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, | |
40 | const std::vector<int> &sortPermutation) |
|
40 | const std::vector<int> &sortPermutation) | |
41 | { |
|
41 | { | |
42 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation)); |
|
42 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation)); | |
43 | } |
|
43 | } | |
44 | }; |
|
44 | }; | |
45 |
|
45 | |||
46 | } // namespace arraydata_detail |
|
46 | } // namespace arraydata_detail | |
47 |
|
47 | |||
48 | /** |
|
48 | /** | |
49 | * @brief The ArrayData class represents a dataset for a data series. |
|
49 | * @brief The ArrayData class represents a dataset for a data series. | |
50 | * |
|
50 | * | |
51 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
51 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim | |
52 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same |
|
52 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same | |
53 | * number of values |
|
53 | * number of values | |
54 | * |
|
54 | * | |
55 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
55 | * @tparam Dim the dimension of the ArrayData (one or two) | |
56 | * @sa IDataSeries |
|
56 | * @sa IDataSeries | |
57 | */ |
|
57 | */ | |
58 | template <int Dim> |
|
58 | template <int Dim> | |
59 | class ArrayData { |
|
59 | class ArrayData { | |
60 | public: |
|
60 | public: | |
61 | class IteratorValue { |
|
61 | class IteratorValue { | |
62 | public: |
|
62 | public: | |
63 | explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} |
|
63 | explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} | |
64 | { |
|
64 | { | |
65 | for (auto i = 0; i < container.size(); ++i) { |
|
65 | for (auto i = 0; i < container.size(); ++i) { | |
66 | m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); |
|
66 | m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); | |
67 | } |
|
67 | } | |
68 | } |
|
68 | } | |
69 |
|
69 | |||
70 | double at(int index) const { return *m_Its.at(index); } |
|
70 | double at(int index) const { return *m_Its.at(index); } | |
71 | double first() const { return *m_Its.front(); } |
|
71 | double first() const { return *m_Its.front(); } | |
72 |
|
72 | |||
73 | void next() |
|
73 | void next() | |
74 | { |
|
74 | { | |
75 | for (auto &it : m_Its) { |
|
75 | for (auto &it : m_Its) { | |
76 | ++it; |
|
76 | ++it; | |
77 | } |
|
77 | } | |
78 | } |
|
78 | } | |
79 |
|
79 | |||
80 | bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; } |
|
80 | bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; } | |
81 |
|
81 | |||
82 | private: |
|
82 | private: | |
83 | std::vector<DataContainer::value_type::const_iterator> m_Its; |
|
83 | std::vector<DataContainer::value_type::const_iterator> m_Its; | |
84 | }; |
|
84 | }; | |
85 |
|
85 | |||
86 | class Iterator { |
|
86 | class Iterator { | |
87 | public: |
|
87 | public: | |
88 | using iterator_category = std::forward_iterator_tag; |
|
88 | using iterator_category = std::forward_iterator_tag; | |
89 | using value_type = const IteratorValue; |
|
89 | using value_type = const IteratorValue; | |
90 | using difference_type = std::ptrdiff_t; |
|
90 | using difference_type = std::ptrdiff_t; | |
91 | using pointer = value_type *; |
|
91 | using pointer = value_type *; | |
92 | using reference = value_type &; |
|
92 | using reference = value_type &; | |
93 |
|
93 | |||
94 | Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {} |
|
94 | Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {} | |
95 |
|
95 | |||
96 | virtual ~Iterator() noexcept = default; |
|
96 | virtual ~Iterator() noexcept = default; | |
97 | Iterator(const Iterator &) = default; |
|
97 | Iterator(const Iterator &) = default; | |
98 | Iterator(Iterator &&) = default; |
|
98 | Iterator(Iterator &&) = default; | |
99 | Iterator &operator=(const Iterator &) = default; |
|
99 | Iterator &operator=(const Iterator &) = default; | |
100 | Iterator &operator=(Iterator &&) = default; |
|
100 | Iterator &operator=(Iterator &&) = default; | |
101 |
|
101 | |||
102 | Iterator &operator++() |
|
102 | Iterator &operator++() | |
103 | { |
|
103 | { | |
104 | m_CurrentValue.next(); |
|
104 | m_CurrentValue.next(); | |
105 | return *this; |
|
105 | return *this; | |
106 | } |
|
106 | } | |
107 |
|
107 | |||
108 | pointer operator->() const { return &m_CurrentValue; } |
|
108 | pointer operator->() const { return &m_CurrentValue; } | |
109 | reference operator*() const { return m_CurrentValue; } |
|
109 | reference operator*() const { return m_CurrentValue; } | |
110 |
|
110 | |||
111 | bool operator==(const Iterator &other) const |
|
111 | bool operator==(const Iterator &other) const | |
112 | { |
|
112 | { | |
113 | return m_CurrentValue == other.m_CurrentValue; |
|
113 | return m_CurrentValue == other.m_CurrentValue; | |
114 | } |
|
114 | } | |
115 |
|
115 | |||
116 | bool operator!=(const Iterator &other) const { return !(*this == other); } |
|
116 | bool operator!=(const Iterator &other) const { return !(*this == other); } | |
117 |
|
117 | |||
118 | private: |
|
118 | private: | |
119 | IteratorValue m_CurrentValue; |
|
119 | IteratorValue m_CurrentValue; | |
120 | }; |
|
120 | }; | |
121 |
|
121 | |||
122 | // ///// // |
|
122 | // ///// // | |
123 | // Ctors // |
|
123 | // Ctors // | |
124 | // ///// // |
|
124 | // ///// // | |
125 |
|
125 | |||
126 | /** |
|
126 | /** | |
127 | * Ctor for a unidimensional ArrayData |
|
127 | * Ctor for a unidimensional ArrayData | |
128 | * @param data the data the ArrayData will hold |
|
128 | * @param data the data the ArrayData will hold | |
129 | */ |
|
129 | */ | |
130 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
130 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
131 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} |
|
131 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} | |
132 | { |
|
132 | { | |
133 | m_Data[0] = std::move(data); |
|
133 | m_Data[0] = std::move(data); | |
134 | } |
|
134 | } | |
135 |
|
135 | |||
136 | /** |
|
136 | /** | |
137 | * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be |
|
137 | * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be | |
138 | * greater than 2 and each component must have the same number of values |
|
138 | * greater than 2 and each component must have the same number of values | |
139 | * @param data the data the ArrayData will hold |
|
139 | * @param data the data the ArrayData will hold | |
140 | * @throws std::invalid_argument if the number of components is less than 2 |
|
140 | * @throws std::invalid_argument if the number of components is less than 2 | |
141 | * @remarks if the number of values is not the same for each component, no value is set |
|
141 | * @remarks if the number of values is not the same for each component, no value is set | |
142 | */ |
|
142 | */ | |
143 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
143 | template <int D = Dim, typename = std::enable_if_t<D == 2> > | |
144 | explicit ArrayData(DataContainer data) |
|
144 | explicit ArrayData(DataContainer data) | |
145 | { |
|
145 | { | |
146 | auto nbComponents = data.size(); |
|
146 | auto nbComponents = data.size(); | |
147 | if (nbComponents < 2) { |
|
147 | if (nbComponents < 2) { | |
148 | throw std::invalid_argument{ |
|
148 | throw std::invalid_argument{ | |
149 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1"} |
|
149 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1"} | |
150 | .arg(data.size()) |
|
150 | .arg(data.size()) | |
151 | .toStdString()}; |
|
151 | .toStdString()}; | |
152 | } |
|
152 | } | |
153 |
|
153 | |||
154 | auto nbValues = data.front().size(); |
|
154 | auto nbValues = data.front().size(); | |
155 | if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) { |
|
155 | if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) { | |
156 | return component.size() == nbValues; |
|
156 | return component.size() == nbValues; | |
157 | })) { |
|
157 | })) { | |
158 | m_Data = std::move(data); |
|
158 | m_Data = std::move(data); | |
159 | } |
|
159 | } | |
160 | else { |
|
160 | else { | |
161 | m_Data = DataContainer{nbComponents, QVector<double>{}}; |
|
161 | m_Data = DataContainer{nbComponents, QVector<double>{}}; | |
162 | } |
|
162 | } | |
163 | } |
|
163 | } | |
164 |
|
164 | |||
165 | /// Copy ctor |
|
165 | /// Copy ctor | |
166 | explicit ArrayData(const ArrayData &other) |
|
166 | explicit ArrayData(const ArrayData &other) | |
167 | { |
|
167 | { | |
168 | QReadLocker otherLocker{&other.m_Lock}; |
|
168 | QReadLocker otherLocker{&other.m_Lock}; | |
169 | m_Data = other.m_Data; |
|
169 | m_Data = other.m_Data; | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | // /////////////// // |
|
172 | // /////////////// // | |
173 | // General methods // |
|
173 | // General methods // | |
174 | // /////////////// // |
|
174 | // /////////////// // | |
175 |
|
175 | |||
176 | /** |
|
176 | /** | |
177 | * Merges into the array data an other array data. The two array datas must have the same number |
|
177 | * Merges into the array data an other array data. The two array datas must have the same number | |
178 | * of components so the merge can be done |
|
178 | * of components so the merge can be done | |
179 | * @param other the array data to merge with |
|
179 | * @param other the array data to merge with | |
180 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is |
|
180 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is | |
181 | * inserted at the end |
|
181 | * inserted at the end | |
182 | */ |
|
182 | */ | |
183 | void add(const ArrayData<Dim> &other, bool prepend = false) |
|
183 | void add(const ArrayData<Dim> &other, bool prepend = false) | |
184 | { |
|
184 | { | |
185 | QWriteLocker locker{&m_Lock}; |
|
185 | QWriteLocker locker{&m_Lock}; | |
186 | QReadLocker otherLocker{&other.m_Lock}; |
|
186 | QReadLocker otherLocker{&other.m_Lock}; | |
187 |
|
187 | |||
188 | auto nbComponents = m_Data.size(); |
|
188 | auto nbComponents = m_Data.size(); | |
189 | if (nbComponents != other.m_Data.size()) { |
|
189 | if (nbComponents != other.m_Data.size()) { | |
190 | return; |
|
190 | return; | |
191 | } |
|
191 | } | |
192 |
|
192 | |||
193 | for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) { |
|
193 | for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) { | |
194 | if (prepend) { |
|
194 | if (prepend) { | |
195 | const auto &otherData = other.data(componentIndex); |
|
195 | const auto &otherData = other.data(componentIndex); | |
196 | const auto otherDataSize = otherData.size(); |
|
196 | const auto otherDataSize = otherData.size(); | |
197 |
|
197 | |||
198 | auto &data = m_Data[componentIndex]; |
|
198 | auto &data = m_Data[componentIndex]; | |
199 | data.insert(data.begin(), otherDataSize, 0.); |
|
199 | data.insert(data.begin(), otherDataSize, 0.); | |
200 |
|
200 | |||
201 | for (auto i = 0; i < otherDataSize; ++i) { |
|
201 | for (auto i = 0; i < otherDataSize; ++i) { | |
202 | data.replace(i, otherData.at(i)); |
|
202 | data.replace(i, otherData.at(i)); | |
203 | } |
|
203 | } | |
204 | } |
|
204 | } | |
205 | else { |
|
205 | else { | |
206 | m_Data[componentIndex] += other.data(componentIndex); |
|
206 | m_Data[componentIndex] += other.data(componentIndex); | |
207 | } |
|
207 | } | |
208 | } |
|
208 | } | |
209 | } |
|
209 | } | |
210 |
|
210 | |||
211 | void clear() |
|
211 | void clear() | |
212 | { |
|
212 | { | |
213 | QWriteLocker locker{&m_Lock}; |
|
213 | QWriteLocker locker{&m_Lock}; | |
214 |
|
214 | |||
215 | auto nbComponents = m_Data.size(); |
|
215 | auto nbComponents = m_Data.size(); | |
216 | for (auto i = 0; i < nbComponents; ++i) { |
|
216 | for (auto i = 0; i < nbComponents; ++i) { | |
217 | m_Data[i].clear(); |
|
217 | m_Data[i].clear(); | |
218 | } |
|
218 | } | |
219 | } |
|
219 | } | |
220 |
|
220 | |||
|
221 | int componentCount() const noexcept { return m_Data.size(); } | |||
|
222 | ||||
221 | /** |
|
223 | /** | |
222 | * @return the data of a component |
|
224 | * @return the data of a component | |
223 | * @param componentIndex the index of the component to retrieve the data |
|
225 | * @param componentIndex the index of the component to retrieve the data | |
224 | * @return the component's data, empty vector if the index is invalid |
|
226 | * @return the component's data, empty vector if the index is invalid | |
225 | */ |
|
227 | */ | |
226 | QVector<double> data(int componentIndex) const noexcept |
|
228 | QVector<double> data(int componentIndex) const noexcept | |
227 | { |
|
229 | { | |
228 | QReadLocker locker{&m_Lock}; |
|
230 | QReadLocker locker{&m_Lock}; | |
229 |
|
231 | |||
230 | return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex) |
|
232 | return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex) | |
231 | : QVector<double>{}; |
|
233 | : QVector<double>{}; | |
232 | } |
|
234 | } | |
233 |
|
235 | |||
234 | /// @return the size (i.e. number of values) of a single component |
|
236 | /// @return the size (i.e. number of values) of a single component | |
235 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size |
|
237 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size | |
236 | int size() const |
|
238 | int size() const | |
237 | { |
|
239 | { | |
238 | QReadLocker locker{&m_Lock}; |
|
240 | QReadLocker locker{&m_Lock}; | |
239 | return m_Data[0].size(); |
|
241 | return m_Data[0].size(); | |
240 | } |
|
242 | } | |
241 |
|
243 | |||
242 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) |
|
244 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) | |
243 | { |
|
245 | { | |
244 | QReadLocker locker{&m_Lock}; |
|
246 | QReadLocker locker{&m_Lock}; | |
245 | return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation); |
|
247 | return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation); | |
246 | } |
|
248 | } | |
247 |
|
249 | |||
248 | // ///////// // |
|
250 | // ///////// // | |
249 | // Iterators // |
|
251 | // Iterators // | |
250 | // ///////// // |
|
252 | // ///////// // | |
251 |
|
253 | |||
252 | Iterator cbegin() const { return Iterator{m_Data, true}; } |
|
254 | Iterator cbegin() const { return Iterator{m_Data, true}; } | |
253 | Iterator cend() const { return Iterator{m_Data, false}; } |
|
255 | Iterator cend() const { return Iterator{m_Data, false}; } | |
254 |
|
256 | |||
255 | // ///////////// // |
|
257 | // ///////////// // | |
256 | // 1-dim methods // |
|
258 | // 1-dim methods // | |
257 | // ///////////// // |
|
259 | // ///////////// // | |
258 |
|
260 | |||
259 | /** |
|
261 | /** | |
260 | * @return the data at a specified index |
|
262 | * @return the data at a specified index | |
261 | * @remarks index must be a valid position |
|
263 | * @remarks index must be a valid position | |
262 | * @remarks this method is only available for a unidimensional ArrayData |
|
264 | * @remarks this method is only available for a unidimensional ArrayData | |
263 | */ |
|
265 | */ | |
264 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
266 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
265 | double at(int index) const noexcept |
|
267 | double at(int index) const noexcept | |
266 | { |
|
268 | { | |
267 | QReadLocker locker{&m_Lock}; |
|
269 | QReadLocker locker{&m_Lock}; | |
268 | return m_Data[0].at(index); |
|
270 | return m_Data[0].at(index); | |
269 | } |
|
271 | } | |
270 |
|
272 | |||
271 | /** |
|
273 | /** | |
272 | * @return the data as a vector, as a const reference |
|
274 | * @return the data as a vector, as a const reference | |
273 | * @remarks this method is only available for a unidimensional ArrayData |
|
275 | * @remarks this method is only available for a unidimensional ArrayData | |
274 | */ |
|
276 | */ | |
275 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
277 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
276 | const QVector<double> &cdata() const noexcept |
|
278 | const QVector<double> &cdata() const noexcept | |
277 | { |
|
279 | { | |
278 | QReadLocker locker{&m_Lock}; |
|
280 | QReadLocker locker{&m_Lock}; | |
279 | return m_Data.at(0); |
|
281 | return m_Data.at(0); | |
280 | } |
|
282 | } | |
281 |
|
283 | |||
282 | /** |
|
284 | /** | |
283 | * @return the data as a vector |
|
285 | * @return the data as a vector | |
284 | * @remarks this method is only available for a unidimensional ArrayData |
|
286 | * @remarks this method is only available for a unidimensional ArrayData | |
285 | */ |
|
287 | */ | |
286 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
288 | template <int D = Dim, typename = std::enable_if_t<D == 1> > | |
287 | QVector<double> data() const noexcept |
|
289 | QVector<double> data() const noexcept | |
288 | { |
|
290 | { | |
289 | QReadLocker locker{&m_Lock}; |
|
291 | QReadLocker locker{&m_Lock}; | |
290 | return m_Data[0]; |
|
292 | return m_Data[0]; | |
291 | } |
|
293 | } | |
292 |
|
294 | |||
293 | // ///////////// // |
|
295 | // ///////////// // | |
294 | // 2-dim methods // |
|
296 | // 2-dim methods // | |
295 | // ///////////// // |
|
297 | // ///////////// // | |
296 |
|
298 | |||
297 | /** |
|
299 | /** | |
298 | * @return the data |
|
300 | * @return the data | |
299 | * @remarks this method is only available for a two-dimensional ArrayData |
|
301 | * @remarks this method is only available for a two-dimensional ArrayData | |
300 | */ |
|
302 | */ | |
301 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
303 | template <int D = Dim, typename = std::enable_if_t<D == 2> > | |
302 | DataContainer data() const noexcept |
|
304 | DataContainer data() const noexcept | |
303 | { |
|
305 | { | |
304 | QReadLocker locker{&m_Lock}; |
|
306 | QReadLocker locker{&m_Lock}; | |
305 | return m_Data; |
|
307 | return m_Data; | |
306 | } |
|
308 | } | |
307 |
|
309 | |||
308 | private: |
|
310 | private: | |
309 | DataContainer m_Data; |
|
311 | DataContainer m_Data; | |
310 | mutable QReadWriteLock m_Lock; |
|
312 | mutable QReadWriteLock m_Lock; | |
311 | }; |
|
313 | }; | |
312 |
|
314 | |||
313 | #endif // SCIQLOP_ARRAYDATA_H |
|
315 | #endif // SCIQLOP_ARRAYDATA_H |
@@ -1,293 +1,293 | |||||
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/IDataSeries.h> |
|
9 | #include <Data/IDataSeries.h> | |
10 |
|
10 | |||
11 | #include <QLoggingCategory> |
|
11 | #include <QLoggingCategory> | |
12 | #include <QReadLocker> |
|
12 | #include <QReadLocker> | |
13 | #include <QReadWriteLock> |
|
13 | #include <QReadWriteLock> | |
14 | #include <memory> |
|
14 | #include <memory> | |
15 |
|
15 | |||
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log |
|
16 | // We don't use the Qt macro since the log is used in the header file, which causes multiple log | |
17 | // definitions with inheritance. Inline method is used instead |
|
17 | // definitions with inheritance. Inline method is used instead | |
18 | inline const QLoggingCategory &LOG_DataSeries() |
|
18 | inline const QLoggingCategory &LOG_DataSeries() | |
19 | { |
|
19 | { | |
20 | static const QLoggingCategory category{"DataSeries"}; |
|
20 | static const QLoggingCategory category{"DataSeries"}; | |
21 | return category; |
|
21 | return category; | |
22 | } |
|
22 | } | |
23 |
|
23 | |||
24 | /** |
|
24 | /** | |
25 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. |
|
25 | * @brief The DataSeries class is the base (abstract) implementation of IDataSeries. | |
26 | * |
|
26 | * | |
27 | * It proposes to set a dimension for the values ββdata. |
|
27 | * It proposes to set a dimension for the values ββdata. | |
28 | * |
|
28 | * | |
29 | * A DataSeries is always sorted on its x-axis data. |
|
29 | * A DataSeries is always sorted on its x-axis data. | |
30 | * |
|
30 | * | |
31 | * @tparam Dim The dimension of the values data |
|
31 | * @tparam Dim The dimension of the values data | |
32 | * |
|
32 | * | |
33 | */ |
|
33 | */ | |
34 | template <int Dim> |
|
34 | template <int Dim> | |
35 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { |
|
35 | class SCIQLOP_CORE_EXPORT DataSeries : public IDataSeries { | |
36 | public: |
|
36 | public: | |
37 | class IteratorValue { |
|
37 | class IteratorValue { | |
38 | public: |
|
38 | public: | |
39 | explicit IteratorValue(const DataSeries &dataSeries, bool begin) |
|
39 | explicit IteratorValue(const DataSeries &dataSeries, bool begin) | |
40 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), |
|
40 | : m_XIt(begin ? dataSeries.xAxisData()->cbegin() : dataSeries.xAxisData()->cend()), | |
41 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() |
|
41 | m_ValuesIt(begin ? dataSeries.valuesData()->cbegin() | |
42 | : dataSeries.valuesData()->cend()) |
|
42 | : dataSeries.valuesData()->cend()) | |
43 | { |
|
43 | { | |
44 | } |
|
44 | } | |
45 |
|
45 | |||
46 | double x() const { return m_XIt->at(0); } |
|
46 | double x() const { return m_XIt->at(0); } | |
47 | double value() const { return m_ValuesIt->at(0); } |
|
47 | double value() const { return m_ValuesIt->at(0); } | |
48 | double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); } |
|
48 | double value(int componentIndex) const { return m_ValuesIt->at(componentIndex); } | |
49 |
|
49 | |||
50 | void next() |
|
50 | void next() | |
51 | { |
|
51 | { | |
52 | ++m_XIt; |
|
52 | ++m_XIt; | |
53 | ++m_ValuesIt; |
|
53 | ++m_ValuesIt; | |
54 | } |
|
54 | } | |
55 |
|
55 | |||
56 | bool operator==(const IteratorValue &other) const |
|
56 | bool operator==(const IteratorValue &other) const | |
57 | { |
|
57 | { | |
58 | return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt); |
|
58 | return std::tie(m_XIt, m_ValuesIt) == std::tie(other.m_XIt, other.m_ValuesIt); | |
59 | } |
|
59 | } | |
60 |
|
60 | |||
61 | private: |
|
61 | private: | |
62 | ArrayData<1>::Iterator m_XIt; |
|
62 | ArrayData<1>::Iterator m_XIt; | |
63 | typename ArrayData<Dim>::Iterator m_ValuesIt; |
|
63 | typename ArrayData<Dim>::Iterator m_ValuesIt; | |
64 | }; |
|
64 | }; | |
65 |
|
65 | |||
66 | class Iterator { |
|
66 | class Iterator { | |
67 | public: |
|
67 | public: | |
68 | using iterator_category = std::forward_iterator_tag; |
|
68 | using iterator_category = std::forward_iterator_tag; | |
69 | using value_type = const IteratorValue; |
|
69 | using value_type = const IteratorValue; | |
70 | using difference_type = std::ptrdiff_t; |
|
70 | using difference_type = std::ptrdiff_t; | |
71 | using pointer = value_type *; |
|
71 | using pointer = value_type *; | |
72 | using reference = value_type &; |
|
72 | using reference = value_type &; | |
73 |
|
73 | |||
74 | Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {} |
|
74 | Iterator(const DataSeries &dataSeries, bool begin) : m_CurrentValue{dataSeries, begin} {} | |
75 | virtual ~Iterator() noexcept = default; |
|
75 | virtual ~Iterator() noexcept = default; | |
76 | Iterator(const Iterator &) = default; |
|
76 | Iterator(const Iterator &) = default; | |
77 | Iterator(Iterator &&) = default; |
|
77 | Iterator(Iterator &&) = default; | |
78 | Iterator &operator=(const Iterator &) = default; |
|
78 | Iterator &operator=(const Iterator &) = default; | |
79 | Iterator &operator=(Iterator &&) = default; |
|
79 | Iterator &operator=(Iterator &&) = default; | |
80 |
|
80 | |||
81 | Iterator &operator++() |
|
81 | Iterator &operator++() | |
82 | { |
|
82 | { | |
83 | m_CurrentValue.next(); |
|
83 | m_CurrentValue.next(); | |
84 | return *this; |
|
84 | return *this; | |
85 | } |
|
85 | } | |
86 |
|
86 | |||
87 | pointer operator->() const { return &m_CurrentValue; } |
|
87 | pointer operator->() const { return &m_CurrentValue; } | |
88 |
|
88 | |||
89 | reference operator*() const { return m_CurrentValue; } |
|
89 | reference operator*() const { return m_CurrentValue; } | |
90 |
|
90 | |||
91 | bool operator==(const Iterator &other) const |
|
91 | bool operator==(const Iterator &other) const | |
92 | { |
|
92 | { | |
93 | return m_CurrentValue == other.m_CurrentValue; |
|
93 | return m_CurrentValue == other.m_CurrentValue; | |
94 | } |
|
94 | } | |
95 |
|
95 | |||
96 | bool operator!=(const Iterator &other) const { return !(*this == other); } |
|
96 | bool operator!=(const Iterator &other) const { return !(*this == other); } | |
97 |
|
97 | |||
98 | private: |
|
98 | private: | |
99 | IteratorValue m_CurrentValue; |
|
99 | IteratorValue m_CurrentValue; | |
100 | }; |
|
100 | }; | |
101 |
|
101 | |||
102 | /// @sa IDataSeries::xAxisData() |
|
102 | /// @sa IDataSeries::xAxisData() | |
103 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } |
|
103 | std::shared_ptr<ArrayData<1> > xAxisData() override { return m_XAxisData; } | |
104 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } |
|
104 | const std::shared_ptr<ArrayData<1> > xAxisData() const { return m_XAxisData; } | |
105 |
|
105 | |||
106 | /// @sa IDataSeries::xAxisUnit() |
|
106 | /// @sa IDataSeries::xAxisUnit() | |
107 | Unit xAxisUnit() const override { return m_XAxisUnit; } |
|
107 | Unit xAxisUnit() const override { return m_XAxisUnit; } | |
108 |
|
108 | |||
109 | /// @return the values dataset |
|
109 | /// @return the values dataset | |
110 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } |
|
110 | std::shared_ptr<ArrayData<Dim> > valuesData() { return m_ValuesData; } | |
111 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } |
|
111 | const std::shared_ptr<ArrayData<Dim> > valuesData() const { return m_ValuesData; } | |
112 |
|
112 | |||
113 | /// @sa IDataSeries::valuesUnit() |
|
113 | /// @sa IDataSeries::valuesUnit() | |
114 | Unit valuesUnit() const override { return m_ValuesUnit; } |
|
114 | Unit valuesUnit() const override { return m_ValuesUnit; } | |
115 |
|
115 | |||
116 |
|
116 | |||
117 | SqpRange range() const override |
|
117 | SqpRange range() const override | |
118 | { |
|
118 | { | |
119 | if (!m_XAxisData->cdata().isEmpty()) { |
|
119 | if (!m_XAxisData->cdata().isEmpty()) { | |
120 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; |
|
120 | return SqpRange{m_XAxisData->cdata().first(), m_XAxisData->cdata().last()}; | |
121 | } |
|
121 | } | |
122 |
|
122 | |||
123 | return SqpRange{}; |
|
123 | return SqpRange{}; | |
124 | } |
|
124 | } | |
125 |
|
125 | |||
126 | void clear() |
|
126 | void clear() | |
127 | { |
|
127 | { | |
128 | m_XAxisData->clear(); |
|
128 | m_XAxisData->clear(); | |
129 | m_ValuesData->clear(); |
|
129 | m_ValuesData->clear(); | |
130 | } |
|
130 | } | |
131 |
|
131 | |||
132 | /// Merges into the data series an other data series |
|
132 | /// Merges into the data series an other data series | |
133 | /// @remarks the data series to merge with is cleared after the operation |
|
133 | /// @remarks the data series to merge with is cleared after the operation | |
134 | void merge(IDataSeries *dataSeries) override |
|
134 | void merge(IDataSeries *dataSeries) override | |
135 | { |
|
135 | { | |
136 | dataSeries->lockWrite(); |
|
136 | dataSeries->lockWrite(); | |
137 | lockWrite(); |
|
137 | lockWrite(); | |
138 |
|
138 | |||
139 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { |
|
139 | if (auto other = dynamic_cast<DataSeries<Dim> *>(dataSeries)) { | |
140 | const auto &otherXAxisData = other->xAxisData()->cdata(); |
|
140 | const auto &otherXAxisData = other->xAxisData()->cdata(); | |
141 | const auto &xAxisData = m_XAxisData->cdata(); |
|
141 | const auto &xAxisData = m_XAxisData->cdata(); | |
142 |
|
142 | |||
143 | // As data series are sorted, we can improve performances of merge, by call the sort |
|
143 | // As data series are sorted, we can improve performances of merge, by call the sort | |
144 | // method only if the two data series overlap. |
|
144 | // method only if the two data series overlap. | |
145 | if (!otherXAxisData.empty()) { |
|
145 | if (!otherXAxisData.empty()) { | |
146 | auto firstValue = otherXAxisData.front(); |
|
146 | auto firstValue = otherXAxisData.front(); | |
147 | auto lastValue = otherXAxisData.back(); |
|
147 | auto lastValue = otherXAxisData.back(); | |
148 |
|
148 | |||
149 | auto xAxisDataBegin = xAxisData.cbegin(); |
|
149 | auto xAxisDataBegin = xAxisData.cbegin(); | |
150 | auto xAxisDataEnd = xAxisData.cend(); |
|
150 | auto xAxisDataEnd = xAxisData.cend(); | |
151 |
|
151 | |||
152 | bool prepend; |
|
152 | bool prepend; | |
153 | bool sortNeeded; |
|
153 | bool sortNeeded; | |
154 |
|
154 | |||
155 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { |
|
155 | if (std::lower_bound(xAxisDataBegin, xAxisDataEnd, firstValue) == xAxisDataEnd) { | |
156 | // Other data series if after data series |
|
156 | // Other data series if after data series | |
157 | prepend = false; |
|
157 | prepend = false; | |
158 | sortNeeded = false; |
|
158 | sortNeeded = false; | |
159 | } |
|
159 | } | |
160 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) |
|
160 | else if (std::upper_bound(xAxisDataBegin, xAxisDataEnd, lastValue) | |
161 | == xAxisDataBegin) { |
|
161 | == xAxisDataBegin) { | |
162 | // Other data series if before data series |
|
162 | // Other data series if before data series | |
163 | prepend = true; |
|
163 | prepend = true; | |
164 | sortNeeded = false; |
|
164 | sortNeeded = false; | |
165 | } |
|
165 | } | |
166 | else { |
|
166 | else { | |
167 | // The two data series overlap |
|
167 | // The two data series overlap | |
168 | prepend = false; |
|
168 | prepend = false; | |
169 | sortNeeded = true; |
|
169 | sortNeeded = true; | |
170 | } |
|
170 | } | |
171 |
|
171 | |||
172 | // Makes the merge |
|
172 | // Makes the merge | |
173 | m_XAxisData->add(*other->xAxisData(), prepend); |
|
173 | m_XAxisData->add(*other->xAxisData(), prepend); | |
174 | m_ValuesData->add(*other->valuesData(), prepend); |
|
174 | m_ValuesData->add(*other->valuesData(), prepend); | |
175 |
|
175 | |||
176 | if (sortNeeded) { |
|
176 | if (sortNeeded) { | |
177 | sort(); |
|
177 | sort(); | |
178 | } |
|
178 | } | |
179 | } |
|
179 | } | |
180 |
|
180 | |||
181 | // Clears the other data series |
|
181 | // Clears the other data series | |
182 | other->clear(); |
|
182 | other->clear(); | |
183 | } |
|
183 | } | |
184 | else { |
|
184 | else { | |
185 | qCWarning(LOG_DataSeries()) |
|
185 | qCWarning(LOG_DataSeries()) | |
186 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); |
|
186 | << QObject::tr("Detection of a type of IDataSeries we cannot merge with !"); | |
187 | } |
|
187 | } | |
188 | unlock(); |
|
188 | unlock(); | |
189 | dataSeries->unlock(); |
|
189 | dataSeries->unlock(); | |
190 | } |
|
190 | } | |
191 |
|
191 | |||
192 | // ///////// // |
|
192 | // ///////// // | |
193 | // Iterators // |
|
193 | // Iterators // | |
194 | // ///////// // |
|
194 | // ///////// // | |
195 |
|
195 | |||
196 | Iterator cbegin() const { return Iterator{*this, true}; } |
|
196 | Iterator cbegin() const { return Iterator{*this, true}; } | |
197 |
|
197 | |||
198 | Iterator cend() const { return Iterator{*this, false}; } |
|
198 | Iterator cend() const { return Iterator{*this, false}; } | |
199 |
|
199 | |||
200 | std::pair<Iterator, Iterator> subData(double min, double max) const |
|
200 | std::pair<Iterator, Iterator> subData(double min, double max) const | |
201 | { |
|
201 | { | |
202 | if (min > max) { |
|
202 | if (min > max) { | |
203 | std::swap(min, max); |
|
203 | std::swap(min, max); | |
204 | } |
|
204 | } | |
205 |
|
205 | |||
206 | auto begin = cbegin(); |
|
206 | auto begin = cbegin(); | |
207 | auto end = cend(); |
|
207 | auto end = cend(); | |
208 |
|
208 | |||
209 | auto lowerIt |
|
209 | auto lowerIt | |
210 | = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) { |
|
210 | = std::lower_bound(begin, end, min, [](const auto &itValue, const auto &value) { | |
211 |
return itValue.x() |
|
211 | return itValue.x() < value; | |
212 | }); |
|
212 | }); | |
213 | auto upperIt |
|
213 | auto upperIt | |
214 | = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) { |
|
214 | = std::upper_bound(begin, end, max, [](const auto &value, const auto &itValue) { | |
215 |
return itValue.x() |
|
215 | return value < itValue.x(); | |
216 | }); |
|
216 | }); | |
217 |
|
217 | |||
218 | return std::make_pair(lowerIt, upperIt); |
|
218 | return std::make_pair(lowerIt, upperIt); | |
219 | } |
|
219 | } | |
220 |
|
220 | |||
221 | // /////// // |
|
221 | // /////// // | |
222 | // Mutexes // |
|
222 | // Mutexes // | |
223 | // /////// // |
|
223 | // /////// // | |
224 |
|
224 | |||
225 | virtual void lockRead() { m_Lock.lockForRead(); } |
|
225 | virtual void lockRead() { m_Lock.lockForRead(); } | |
226 | virtual void lockWrite() { m_Lock.lockForWrite(); } |
|
226 | virtual void lockWrite() { m_Lock.lockForWrite(); } | |
227 | virtual void unlock() { m_Lock.unlock(); } |
|
227 | virtual void unlock() { m_Lock.unlock(); } | |
228 |
|
228 | |||
229 | protected: |
|
229 | protected: | |
230 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a |
|
230 | /// Protected ctor (DataSeries is abstract). The vectors must have the same size, otherwise a | |
231 | /// DataSeries with no values will be created. |
|
231 | /// DataSeries with no values will be created. | |
232 | /// @remarks data series is automatically sorted on its x-axis data |
|
232 | /// @remarks data series is automatically sorted on its x-axis data | |
233 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, |
|
233 | explicit DataSeries(std::shared_ptr<ArrayData<1> > xAxisData, const Unit &xAxisUnit, | |
234 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) |
|
234 | std::shared_ptr<ArrayData<Dim> > valuesData, const Unit &valuesUnit) | |
235 | : m_XAxisData{xAxisData}, |
|
235 | : m_XAxisData{xAxisData}, | |
236 | m_XAxisUnit{xAxisUnit}, |
|
236 | m_XAxisUnit{xAxisUnit}, | |
237 | m_ValuesData{valuesData}, |
|
237 | m_ValuesData{valuesData}, | |
238 | m_ValuesUnit{valuesUnit} |
|
238 | m_ValuesUnit{valuesUnit} | |
239 | { |
|
239 | { | |
240 | if (m_XAxisData->size() != m_ValuesData->size()) { |
|
240 | if (m_XAxisData->size() != m_ValuesData->size()) { | |
241 | clear(); |
|
241 | clear(); | |
242 | } |
|
242 | } | |
243 |
|
243 | |||
244 | // Sorts data if it's not the case |
|
244 | // Sorts data if it's not the case | |
245 | const auto &xAxisCData = m_XAxisData->cdata(); |
|
245 | const auto &xAxisCData = m_XAxisData->cdata(); | |
246 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { |
|
246 | if (!std::is_sorted(xAxisCData.cbegin(), xAxisCData.cend())) { | |
247 | sort(); |
|
247 | sort(); | |
248 | } |
|
248 | } | |
249 | } |
|
249 | } | |
250 |
|
250 | |||
251 | /// Copy ctor |
|
251 | /// Copy ctor | |
252 | explicit DataSeries(const DataSeries<Dim> &other) |
|
252 | explicit DataSeries(const DataSeries<Dim> &other) | |
253 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, |
|
253 | : m_XAxisData{std::make_shared<ArrayData<1> >(*other.m_XAxisData)}, | |
254 | m_XAxisUnit{other.m_XAxisUnit}, |
|
254 | m_XAxisUnit{other.m_XAxisUnit}, | |
255 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, |
|
255 | m_ValuesData{std::make_shared<ArrayData<Dim> >(*other.m_ValuesData)}, | |
256 | m_ValuesUnit{other.m_ValuesUnit} |
|
256 | m_ValuesUnit{other.m_ValuesUnit} | |
257 | { |
|
257 | { | |
258 | // Since a series is ordered from its construction and is always ordered, it is not |
|
258 | // Since a series is ordered from its construction and is always ordered, it is not | |
259 | // necessary to call the sort method here ('other' is sorted) |
|
259 | // necessary to call the sort method here ('other' is sorted) | |
260 | } |
|
260 | } | |
261 |
|
261 | |||
262 | /// Assignment operator |
|
262 | /// Assignment operator | |
263 | template <int D> |
|
263 | template <int D> | |
264 | DataSeries &operator=(DataSeries<D> other) |
|
264 | DataSeries &operator=(DataSeries<D> other) | |
265 | { |
|
265 | { | |
266 | std::swap(m_XAxisData, other.m_XAxisData); |
|
266 | std::swap(m_XAxisData, other.m_XAxisData); | |
267 | std::swap(m_XAxisUnit, other.m_XAxisUnit); |
|
267 | std::swap(m_XAxisUnit, other.m_XAxisUnit); | |
268 | std::swap(m_ValuesData, other.m_ValuesData); |
|
268 | std::swap(m_ValuesData, other.m_ValuesData); | |
269 | std::swap(m_ValuesUnit, other.m_ValuesUnit); |
|
269 | std::swap(m_ValuesUnit, other.m_ValuesUnit); | |
270 |
|
270 | |||
271 | return *this; |
|
271 | return *this; | |
272 | } |
|
272 | } | |
273 |
|
273 | |||
274 | private: |
|
274 | private: | |
275 | /** |
|
275 | /** | |
276 | * Sorts data series on its x-axis data |
|
276 | * Sorts data series on its x-axis data | |
277 | */ |
|
277 | */ | |
278 | void sort() noexcept |
|
278 | void sort() noexcept | |
279 | { |
|
279 | { | |
280 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); |
|
280 | auto permutation = SortUtils::sortPermutation(*m_XAxisData, std::less<double>()); | |
281 | m_XAxisData = m_XAxisData->sort(permutation); |
|
281 | m_XAxisData = m_XAxisData->sort(permutation); | |
282 | m_ValuesData = m_ValuesData->sort(permutation); |
|
282 | m_ValuesData = m_ValuesData->sort(permutation); | |
283 | } |
|
283 | } | |
284 |
|
284 | |||
285 | std::shared_ptr<ArrayData<1> > m_XAxisData; |
|
285 | std::shared_ptr<ArrayData<1> > m_XAxisData; | |
286 | Unit m_XAxisUnit; |
|
286 | Unit m_XAxisUnit; | |
287 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; |
|
287 | std::shared_ptr<ArrayData<Dim> > m_ValuesData; | |
288 | Unit m_ValuesUnit; |
|
288 | Unit m_ValuesUnit; | |
289 |
|
289 | |||
290 | QReadWriteLock m_Lock; |
|
290 | QReadWriteLock m_Lock; | |
291 | }; |
|
291 | }; | |
292 |
|
292 | |||
293 | #endif // SCIQLOP_DATASERIES_H |
|
293 | #endif // SCIQLOP_DATASERIES_H |
@@ -1,164 +1,232 | |||||
1 | #include "Data/DataSeries.h" |
|
1 | #include "Data/DataSeries.h" | |
2 | #include "Data/ScalarSeries.h" |
|
2 | #include "Data/ScalarSeries.h" | |
3 |
|
3 | |||
4 | #include <QObject> |
|
4 | #include <QObject> | |
5 | #include <QtTest> |
|
5 | #include <QtTest> | |
6 |
|
6 | |||
7 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) |
|
7 | Q_DECLARE_METATYPE(std::shared_ptr<ScalarSeries>) | |
8 |
|
8 | |||
9 | class TestDataSeries : public QObject { |
|
9 | class TestDataSeries : public QObject { | |
10 | Q_OBJECT |
|
10 | Q_OBJECT | |
11 | private slots: |
|
11 | private slots: | |
12 | /// Input test data |
|
12 | /// Input test data | |
13 | /// @sa testCtor() |
|
13 | /// @sa testCtor() | |
14 | void testCtor_data(); |
|
14 | void testCtor_data(); | |
15 |
|
15 | |||
16 | /// Tests construction of a data series |
|
16 | /// Tests construction of a data series | |
17 | void testCtor(); |
|
17 | void testCtor(); | |
18 |
|
18 | |||
19 | /// Input test data |
|
19 | /// Input test data | |
20 | /// @sa testMerge() |
|
20 | /// @sa testMerge() | |
21 | void testMerge_data(); |
|
21 | void testMerge_data(); | |
22 |
|
22 | |||
23 | /// Tests merge of two data series |
|
23 | /// Tests merge of two data series | |
24 | void testMerge(); |
|
24 | void testMerge(); | |
|
25 | ||||
|
26 | /// Input test data | |||
|
27 | /// @sa testSubdata() | |||
|
28 | void testSubdata_data(); | |||
|
29 | ||||
|
30 | /// Tests get subdata of two data series | |||
|
31 | void testSubdata(); | |||
25 | }; |
|
32 | }; | |
26 |
|
33 | |||
27 | void TestDataSeries::testCtor_data() |
|
34 | void TestDataSeries::testCtor_data() | |
28 | { |
|
35 | { | |
29 | // ////////////// // |
|
36 | // ////////////// // | |
30 | // Test structure // |
|
37 | // Test structure // | |
31 | // ////////////// // |
|
38 | // ////////////// // | |
32 |
|
39 | |||
33 | // x-axis data |
|
40 | // x-axis data | |
34 | QTest::addColumn<QVector<double> >("xAxisData"); |
|
41 | QTest::addColumn<QVector<double> >("xAxisData"); | |
35 | // values data |
|
42 | // values data | |
36 | QTest::addColumn<QVector<double> >("valuesData"); |
|
43 | QTest::addColumn<QVector<double> >("valuesData"); | |
37 |
|
44 | |||
38 | // expected x-axis data |
|
45 | // expected x-axis data | |
39 | QTest::addColumn<QVector<double> >("expectedXAxisData"); |
|
46 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
40 | // expected values data |
|
47 | // expected values data | |
41 | QTest::addColumn<QVector<double> >("expectedValuesData"); |
|
48 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
42 |
|
49 | |||
43 | // ////////// // |
|
50 | // ////////// // | |
44 | // Test cases // |
|
51 | // Test cases // | |
45 | // ////////// // |
|
52 | // ////////// // | |
46 |
|
53 | |||
47 | QTest::newRow("invalidData (different sizes of vectors)") |
|
54 | QTest::newRow("invalidData (different sizes of vectors)") | |
48 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.} |
|
55 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 200., 300.} | |
49 | << QVector<double>{} << QVector<double>{}; |
|
56 | << QVector<double>{} << QVector<double>{}; | |
50 |
|
57 | |||
51 | QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.} |
|
58 | QTest::newRow("sortedData") << QVector<double>{1., 2., 3., 4., 5.} | |
52 | << QVector<double>{100., 200., 300., 400., 500.} |
|
59 | << QVector<double>{100., 200., 300., 400., 500.} | |
53 | << QVector<double>{1., 2., 3., 4., 5.} |
|
60 | << QVector<double>{1., 2., 3., 4., 5.} | |
54 | << QVector<double>{100., 200., 300., 400., 500.}; |
|
61 | << QVector<double>{100., 200., 300., 400., 500.}; | |
55 |
|
62 | |||
56 | QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.} |
|
63 | QTest::newRow("unsortedData") << QVector<double>{5., 4., 3., 2., 1.} | |
57 | << QVector<double>{100., 200., 300., 400., 500.} |
|
64 | << QVector<double>{100., 200., 300., 400., 500.} | |
58 | << QVector<double>{1., 2., 3., 4., 5.} |
|
65 | << QVector<double>{1., 2., 3., 4., 5.} | |
59 | << QVector<double>{500., 400., 300., 200., 100.}; |
|
66 | << QVector<double>{500., 400., 300., 200., 100.}; | |
60 |
|
67 | |||
61 | QTest::newRow("unsortedData2") |
|
68 | QTest::newRow("unsortedData2") | |
62 | << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.} |
|
69 | << QVector<double>{1., 4., 3., 5., 2.} << QVector<double>{100., 200., 300., 400., 500.} | |
63 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.}; |
|
70 | << QVector<double>{1., 2., 3., 4., 5.} << QVector<double>{100., 500., 300., 200., 400.}; | |
64 | } |
|
71 | } | |
65 |
|
72 | |||
66 | void TestDataSeries::testCtor() |
|
73 | void TestDataSeries::testCtor() | |
67 | { |
|
74 | { | |
68 | // Creates series |
|
75 | // Creates series | |
69 | QFETCH(QVector<double>, xAxisData); |
|
76 | QFETCH(QVector<double>, xAxisData); | |
70 | QFETCH(QVector<double>, valuesData); |
|
77 | QFETCH(QVector<double>, valuesData); | |
71 |
|
78 | |||
72 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), |
|
79 | auto series = std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), | |
73 | Unit{}, Unit{}); |
|
80 | Unit{}, Unit{}); | |
74 |
|
81 | |||
75 | // Validates results : we check that the data series is sorted on its x-axis data |
|
82 | // Validates results : we check that the data series is sorted on its x-axis data | |
76 | QFETCH(QVector<double>, expectedXAxisData); |
|
83 | QFETCH(QVector<double>, expectedXAxisData); | |
77 | QFETCH(QVector<double>, expectedValuesData); |
|
84 | QFETCH(QVector<double>, expectedValuesData); | |
78 |
|
85 | |||
79 | auto seriesXAxisData = series->xAxisData()->data(); |
|
86 | auto seriesXAxisData = series->xAxisData()->data(); | |
80 | auto seriesValuesData = series->valuesData()->data(); |
|
87 | auto seriesValuesData = series->valuesData()->data(); | |
81 |
|
88 | |||
82 | QVERIFY( |
|
89 | QVERIFY( | |
83 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); |
|
90 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
84 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), |
|
91 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
85 | seriesValuesData.cbegin())); |
|
92 | seriesValuesData.cbegin())); | |
86 | } |
|
93 | } | |
87 |
|
94 | |||
88 | namespace { |
|
95 | namespace { | |
89 |
|
96 | |||
90 | std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData) |
|
97 | std::shared_ptr<ScalarSeries> createSeries(QVector<double> xAxisData, QVector<double> valuesData) | |
91 | { |
|
98 | { | |
92 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, |
|
99 | return std::make_shared<ScalarSeries>(std::move(xAxisData), std::move(valuesData), Unit{}, | |
93 | Unit{}); |
|
100 | Unit{}); | |
94 | } |
|
101 | } | |
95 |
|
102 | |||
96 | } // namespace |
|
103 | } // namespace | |
97 |
|
104 | |||
98 | void TestDataSeries::testMerge_data() |
|
105 | void TestDataSeries::testMerge_data() | |
99 | { |
|
106 | { | |
100 | // ////////////// // |
|
107 | // ////////////// // | |
101 | // Test structure // |
|
108 | // Test structure // | |
102 | // ////////////// // |
|
109 | // ////////////// // | |
103 |
|
110 | |||
104 | // Data series to merge |
|
111 | // Data series to merge | |
105 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); |
|
112 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |
106 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); |
|
113 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries2"); | |
107 |
|
114 | |||
108 | // Expected values in the first data series after merge |
|
115 | // Expected values in the first data series after merge | |
109 | QTest::addColumn<QVector<double> >("expectedXAxisData"); |
|
116 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |
110 | QTest::addColumn<QVector<double> >("expectedValuesData"); |
|
117 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |
111 |
|
118 | |||
112 | // ////////// // |
|
119 | // ////////// // | |
113 | // Test cases // |
|
120 | // Test cases // | |
114 | // ////////// // |
|
121 | // ////////// // | |
115 |
|
122 | |||
116 | QTest::newRow("sortedMerge") |
|
123 | QTest::newRow("sortedMerge") | |
117 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
124 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
118 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
125 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
119 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
126 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
120 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; |
|
127 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
121 |
|
128 | |||
122 | QTest::newRow("unsortedMerge") |
|
129 | QTest::newRow("unsortedMerge") | |
123 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) |
|
130 | << createSeries({6., 7., 8., 9., 10.}, {600., 700., 800., 900., 1000.}) | |
124 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) |
|
131 | << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |
125 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
132 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
126 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; |
|
133 | << QVector<double>{100., 200., 300., 400., 500., 600., 700., 800., 900., 1000.}; | |
127 |
|
134 | |||
128 | QTest::newRow("unsortedMerge2") |
|
135 | QTest::newRow("unsortedMerge2") | |
129 | << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.}) |
|
136 | << createSeries({1., 2., 8., 9., 10}, {100., 200., 300., 400., 500.}) | |
130 | << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.}) |
|
137 | << createSeries({3., 4., 5., 6., 7.}, {600., 700., 800., 900., 1000.}) | |
131 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
138 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
132 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; |
|
139 | << QVector<double>{100., 200., 600., 700., 800., 900., 1000., 300., 400., 500.}; | |
133 |
|
140 | |||
134 | QTest::newRow("unsortedMerge3") |
|
141 | QTest::newRow("unsortedMerge3") | |
135 | << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.}) |
|
142 | << createSeries({3., 5., 8., 7., 2}, {100., 200., 300., 400., 500.}) | |
136 | << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.}) |
|
143 | << createSeries({6., 4., 9., 10., 1.}, {600., 700., 800., 900., 1000.}) | |
137 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} |
|
144 | << QVector<double>{1., 2., 3., 4., 5., 6., 7., 8., 9., 10.} | |
138 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; |
|
145 | << QVector<double>{1000., 500., 100., 700., 200., 600., 400., 300., 800., 900.}; | |
139 | } |
|
146 | } | |
140 |
|
147 | |||
141 | void TestDataSeries::testMerge() |
|
148 | void TestDataSeries::testMerge() | |
142 | { |
|
149 | { | |
143 | // Merges series |
|
150 | // Merges series | |
144 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); |
|
151 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |
145 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); |
|
152 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries2); | |
146 |
|
153 | |||
147 | dataSeries->merge(dataSeries2.get()); |
|
154 | dataSeries->merge(dataSeries2.get()); | |
148 |
|
155 | |||
149 | // Validates results : we check that the merge is valid and the data series is sorted on its |
|
156 | // Validates results : we check that the merge is valid and the data series is sorted on its | |
150 | // x-axis data |
|
157 | // x-axis data | |
151 | QFETCH(QVector<double>, expectedXAxisData); |
|
158 | QFETCH(QVector<double>, expectedXAxisData); | |
152 | QFETCH(QVector<double>, expectedValuesData); |
|
159 | QFETCH(QVector<double>, expectedValuesData); | |
153 |
|
160 | |||
154 | auto seriesXAxisData = dataSeries->xAxisData()->data(); |
|
161 | auto seriesXAxisData = dataSeries->xAxisData()->data(); | |
155 | auto seriesValuesData = dataSeries->valuesData()->data(); |
|
162 | auto seriesValuesData = dataSeries->valuesData()->data(); | |
156 |
|
163 | |||
157 | QVERIFY( |
|
164 | QVERIFY( | |
158 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); |
|
165 | std::equal(expectedXAxisData.cbegin(), expectedXAxisData.cend(), seriesXAxisData.cbegin())); | |
159 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), |
|
166 | QVERIFY(std::equal(expectedValuesData.cbegin(), expectedValuesData.cend(), | |
160 | seriesValuesData.cbegin())); |
|
167 | seriesValuesData.cbegin())); | |
161 | } |
|
168 | } | |
162 |
|
169 | |||
|
170 | void TestDataSeries::testSubdata_data() | |||
|
171 | { | |||
|
172 | // ////////////// // | |||
|
173 | // Test structure // | |||
|
174 | // ////////////// // | |||
|
175 | ||||
|
176 | // Data series to get subdata | |||
|
177 | QTest::addColumn<std::shared_ptr<ScalarSeries> >("dataSeries"); | |||
|
178 | ||||
|
179 | // Min/max values | |||
|
180 | QTest::addColumn<double>("min"); | |||
|
181 | QTest::addColumn<double>("max"); | |||
|
182 | ||||
|
183 | // Expected values after subdata | |||
|
184 | QTest::addColumn<QVector<double> >("expectedXAxisData"); | |||
|
185 | QTest::addColumn<QVector<double> >("expectedValuesData"); | |||
|
186 | ||||
|
187 | // ////////// // | |||
|
188 | // Test cases // | |||
|
189 | // ////////// // | |||
|
190 | ||||
|
191 | QTest::newRow("subData1") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
192 | << -1. << 3.2 << QVector<double>{1., 2., 3.} | |||
|
193 | << QVector<double>{100., 200., 300.}; | |||
|
194 | QTest::newRow("subData2") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
195 | << 1. << 4. << QVector<double>{1., 2., 3., 4.} | |||
|
196 | << QVector<double>{100., 200., 300., 400.}; | |||
|
197 | QTest::newRow("subData3") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
198 | << 1. << 3.9 << QVector<double>{1., 2., 3.} | |||
|
199 | << QVector<double>{100., 200., 300.}; | |||
|
200 | QTest::newRow("subData4") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
201 | << 0. << 0.9 << QVector<double>{} << QVector<double>{}; | |||
|
202 | QTest::newRow("subData5") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
203 | << 0. << 1. << QVector<double>{1.} << QVector<double>{100.}; | |||
|
204 | QTest::newRow("subData6") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
205 | << 2.1 << 6. << QVector<double>{3., 4., 5.} | |||
|
206 | << QVector<double>{300., 400., 500.}; | |||
|
207 | QTest::newRow("subData7") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
208 | << 6. << 9. << QVector<double>{} << QVector<double>{}; | |||
|
209 | QTest::newRow("subData8") << createSeries({1., 2., 3., 4., 5.}, {100., 200., 300., 400., 500.}) | |||
|
210 | << 5. << 9. << QVector<double>{5.} << QVector<double>{500.}; | |||
|
211 | } | |||
|
212 | ||||
|
213 | void TestDataSeries::testSubdata() | |||
|
214 | { | |||
|
215 | QFETCH(std::shared_ptr<ScalarSeries>, dataSeries); | |||
|
216 | QFETCH(double, min); | |||
|
217 | QFETCH(double, max); | |||
|
218 | ||||
|
219 | QFETCH(QVector<double>, expectedXAxisData); | |||
|
220 | QFETCH(QVector<double>, expectedValuesData); | |||
|
221 | ||||
|
222 | auto bounds = dataSeries->subData(min, max); | |||
|
223 | QVERIFY(std::equal(bounds.first, bounds.second, expectedXAxisData.cbegin(), | |||
|
224 | expectedXAxisData.cend(), | |||
|
225 | [](const auto &it, const auto &expectedX) { return it.x() == expectedX; })); | |||
|
226 | QVERIFY(std::equal( | |||
|
227 | bounds.first, bounds.second, expectedValuesData.cbegin(), expectedValuesData.cend(), | |||
|
228 | [](const auto &it, const auto &expectedVal) { return it.value() == expectedVal; })); | |||
|
229 | } | |||
|
230 | ||||
163 | QTEST_MAIN(TestDataSeries) |
|
231 | QTEST_MAIN(TestDataSeries) | |
164 | #include "TestDataSeries.moc" |
|
232 | #include "TestDataSeries.moc" |
@@ -1,40 +1,39 | |||||
1 | #ifndef SCIQLOP_VISUALIZATIONGRAPHHELPER_H |
|
1 | #ifndef SCIQLOP_VISUALIZATIONGRAPHHELPER_H | |
2 | #define SCIQLOP_VISUALIZATIONGRAPHHELPER_H |
|
2 | #define SCIQLOP_VISUALIZATIONGRAPHHELPER_H | |
3 |
|
3 | |||
|
4 | #include "Visualization/VisualizationDefs.h" | |||
|
5 | ||||
4 | #include <Data/SqpRange.h> |
|
6 | #include <Data/SqpRange.h> | |
5 |
|
7 | |||
6 | #include <QLoggingCategory> |
|
8 | #include <QLoggingCategory> | |
7 | #include <QVector> |
|
9 | #include <QVector> | |
8 |
|
10 | |||
9 | #include <memory> |
|
11 | #include <memory> | |
10 |
|
12 | |||
11 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphHelper) |
|
13 | Q_DECLARE_LOGGING_CATEGORY(LOG_VisualizationGraphHelper) | |
12 |
|
14 | |||
13 | class IDataSeries; |
|
15 | class IDataSeries; | |
14 | class QCPAbstractPlottable; |
|
16 | class QCPAbstractPlottable; | |
15 | class QCustomPlot; |
|
17 | class QCustomPlot; | |
16 | class Variable; |
|
18 | class Variable; | |
17 |
|
19 | |||
18 | /** |
|
20 | /** | |
19 | * @brief The VisualizationGraphHelper class aims to create the QCustomPlot components relative to a |
|
21 | * @brief The VisualizationGraphHelper class aims to create the QCustomPlot components relative to a | |
20 | * variable, depending on the data series of this variable |
|
22 | * variable, depending on the data series of this variable | |
21 | */ |
|
23 | */ | |
22 | struct VisualizationGraphHelper { |
|
24 | struct VisualizationGraphHelper { | |
23 | /** |
|
25 | /** | |
24 | * Creates (if possible) the QCustomPlot components relative to the variable passed in |
|
26 | * Creates (if possible) the QCustomPlot components relative to the variable passed in | |
25 | * parameter, and adds these to the plot passed in parameter. |
|
27 | * parameter, and adds these to the plot passed in parameter. | |
26 | * @param variable the variable for which to create the components |
|
28 | * @param variable the variable for which to create the components | |
27 | * @param plot the plot in which to add the created components. It takes ownership of these |
|
29 | * @param plot the plot in which to add the created components. It takes ownership of these | |
28 | * components. |
|
30 | * components. | |
29 | * @return the list of the components created |
|
31 | * @return the list of the components created | |
30 | */ |
|
32 | */ | |
31 |
static |
|
33 | static PlottablesMap create(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept; | |
32 | QCustomPlot &plot) noexcept; |
|
|||
33 | static QVector<QCPAbstractPlottable *> createV2(std::shared_ptr<Variable> variable, |
|
|||
34 | QCustomPlot &plot) noexcept; |
|
|||
35 |
|
34 | |||
36 | static void updateData(QVector<QCPAbstractPlottable *> plotableVect, |
|
35 | static void updateData(PlottablesMap &plottables, std::shared_ptr<IDataSeries> dataSeries, | |
37 |
|
|
36 | const SqpRange &dateTime); | |
38 | }; |
|
37 | }; | |
39 |
|
38 | |||
40 | #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H |
|
39 | #endif // SCIQLOP_VISUALIZATIONGRAPHHELPER_H |
@@ -1,195 +1,240 | |||||
1 | #include "Visualization/VisualizationGraphHelper.h" |
|
1 | #include "Visualization/VisualizationGraphHelper.h" | |
2 | #include "Visualization/qcustomplot.h" |
|
2 | #include "Visualization/qcustomplot.h" | |
3 |
|
3 | |||
|
4 | #include <Common/ColorUtils.h> | |||
|
5 | ||||
4 | #include <Data/ScalarSeries.h> |
|
6 | #include <Data/ScalarSeries.h> | |
|
7 | #include <Data/VectorSeries.h> | |||
5 |
|
8 | |||
6 | #include <Variable/Variable.h> |
|
9 | #include <Variable/Variable.h> | |
7 |
|
10 | |||
8 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") |
|
11 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") | |
9 |
|
12 | |||
10 | namespace { |
|
13 | namespace { | |
11 |
|
14 | |||
12 | class SqpDataContainer : public QCPGraphDataContainer { |
|
15 | class SqpDataContainer : public QCPGraphDataContainer { | |
13 | public: |
|
16 | public: | |
14 | void appendGraphData(const QCPGraphData &data) { mData.append(data); } |
|
17 | void appendGraphData(const QCPGraphData &data) { mData.append(data); } | |
15 | }; |
|
18 | }; | |
16 |
|
19 | |||
17 |
|
20 | |||
18 | /// Format for datetimes on a axis |
|
21 | /// Format for datetimes on a axis | |
19 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
22 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); | |
20 |
|
23 | |||
21 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
24 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or | |
22 | /// non-time data |
|
25 | /// non-time data | |
23 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) |
|
26 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) | |
24 | { |
|
27 | { | |
25 | if (isTimeAxis) { |
|
28 | if (isTimeAxis) { | |
26 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
29 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); | |
27 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
30 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); | |
28 | dateTicker->setDateTimeSpec(Qt::UTC); |
|
31 | dateTicker->setDateTimeSpec(Qt::UTC); | |
29 |
|
32 | |||
30 | return dateTicker; |
|
33 | return dateTicker; | |
31 | } |
|
34 | } | |
32 | else { |
|
35 | else { | |
33 | // default ticker |
|
36 | // default ticker | |
34 | return QSharedPointer<QCPAxisTicker>::create(); |
|
37 | return QSharedPointer<QCPAxisTicker>::create(); | |
35 | } |
|
38 | } | |
36 | } |
|
39 | } | |
37 |
|
40 | |||
38 | void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr<ScalarSeries> scalarSeries, |
|
41 | /// Sets axes properties according to the properties of a data series | |
39 | const SqpRange &range) |
|
42 | template <int Dim> | |
40 | { |
|
43 | void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept | |
41 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" |
|
|||
42 | << QThread::currentThread()->objectName(); |
|
|||
43 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { |
|
|||
44 | scalarSeries->lockRead(); |
|
|||
45 | { |
|
44 | { | |
46 | auto sqpDataContainer = QSharedPointer<SqpDataContainer>::create(); |
|
|||
47 | qcpGraph->setData(sqpDataContainer); |
|
|||
48 | auto bounds = scalarSeries->subData(range.m_TStart, range.m_TEnd); |
|
|||
49 | for (auto it = bounds.first; it != bounds.second; ++it) { |
|
|||
50 | sqpDataContainer->appendGraphData(QCPGraphData(it->x(), it->value())); |
|
|||
51 | } |
|
|||
52 |
|
||||
53 | qCInfo(LOG_VisualizationGraphHelper()) << "TODEBUG: Current points displayed" |
|
|||
54 | << sqpDataContainer->size(); |
|
|||
55 | } |
|
|||
56 | scalarSeries->unlock(); |
|
|||
57 |
|
||||
58 |
|
||||
59 | // Display all data |
|
|||
60 | component->parentPlot()->replot(); |
|
|||
61 | } |
|
|||
62 | else { |
|
|||
63 | /// @todo DEBUG |
|
|||
64 | } |
|
|||
65 | } |
|
|||
66 |
|
||||
67 | QCPAbstractPlottable *createScalarSeriesComponentV2(std::shared_ptr<ScalarSeries> scalarSeries, |
|
|||
68 | QCustomPlot &plot) |
|
|||
69 | { |
|
|||
70 | auto component = plot.addGraph(); |
|
|||
71 |
|
||||
72 | if (component) { |
|
|||
73 | // Axes properties |
|
|||
74 |
|
|
45 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
75 |
|
|
46 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
76 |
|
||||
77 |
|
|
47 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
78 |
|
|
48 | // label (unit name) | |
79 |
|
|
49 | axis->setLabel(unit.m_Name); | |
80 |
|
50 | |||
81 |
|
|
51 | // ticker (depending on the type of unit) | |
82 |
|
|
52 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
83 |
|
|
53 | }; | |
84 |
|
|
54 | setAxisProperties(plot.xAxis, dataSeries.xAxisUnit()); | |
85 |
|
|
55 | setAxisProperties(plot.yAxis, dataSeries.valuesUnit()); | |
86 | } |
|
56 | } | |
87 | return component; |
|
57 | ||
|
58 | /** | |||
|
59 | * Struct used to create plottables, depending on the type of the data series from which to create them | |||
|
60 | * @tparam T the data series' type | |||
|
61 | * @remarks Default implementation can't create plottables | |||
|
62 | */ | |||
|
63 | template <typename T, typename Enabled = void> | |||
|
64 | struct PlottablesCreator { | |||
|
65 | static PlottablesMap createPlottables(T &, QCustomPlot &) | |||
|
66 | { | |||
|
67 | qCCritical(LOG_DataSeries()) | |||
|
68 | << QObject::tr("Can't create plottables: unmanaged data series type"); | |||
|
69 | return {}; | |||
88 | } |
|
70 | } | |
|
71 | }; | |||
89 |
|
72 | |||
90 | QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries, |
|
73 | /** | |
91 | QCustomPlot &plot, const SqpRange &dateTime) |
|
74 | * Specialization of PlottablesCreator for scalars and vectors | |
|
75 | * @sa ScalarSeries | |||
|
76 | * @sa VectorSeries | |||
|
77 | */ | |||
|
78 | template <typename T> | |||
|
79 | struct PlottablesCreator<T, | |||
|
80 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |||
|
81 | or std::is_base_of<VectorSeries, T>::value> > { | |||
|
82 | static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) | |||
92 | { |
|
83 | { | |
93 | auto component = plot.addGraph(); |
|
84 | PlottablesMap result{}; | |
|
85 | ||||
|
86 | // Gets the number of components of the data series | |||
|
87 | auto componentCount = dataSeries.valuesData()->componentCount(); | |||
|
88 | ||||
|
89 | auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount); | |||
94 |
|
90 | |||
95 | if (component) { |
|
91 | // For each component of the data series, creates a QCPGraph to add to the plot | |
96 | // // Graph data |
|
92 | for (auto i = 0; i < componentCount; ++i) { | |
97 | component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(), |
|
93 | auto graph = plot.addGraph(); | |
98 | true); |
|
94 | graph->setPen(QPen{colors.at(i)}); | |
99 |
|
95 | |||
100 | updateScalarData(component, scalarSeries, dateTime); |
|
96 | result.insert({i, graph}); | |
|
97 | } | |||
101 |
|
98 | |||
102 | // Axes properties |
|
99 | // Axes properties | |
103 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers |
|
100 | setAxesProperties(dataSeries, plot); | |
104 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph |
|
|||
105 |
|
101 | |||
106 | auto setAxisProperties = [](auto axis, const auto &unit) { |
|
102 | plot.replot(); | |
107 | // label (unit name) |
|
|||
108 | axis->setLabel(unit.m_Name); |
|
|||
109 |
|
103 | |||
110 | // ticker (depending on the type of unit) |
|
104 | return result; | |
111 | axis->setTicker(axisTicker(unit.m_TimeUnit)); |
|
105 | } | |
112 | }; |
|
106 | }; | |
113 | setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); |
|
|||
114 | setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); |
|
|||
115 |
|
107 | |||
116 | // Display all data |
|
108 | /** | |
117 | component->rescaleAxes(); |
|
109 | * Struct used to update plottables, depending on the type of the data series from which to update them | |
118 | plot.replot(); |
|
110 | * @tparam T the data series' type | |
|
111 | * @remarks Default implementation can't update plottables | |||
|
112 | */ | |||
|
113 | template <typename T, typename Enabled = void> | |||
|
114 | struct PlottablesUpdater { | |||
|
115 | static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool) | |||
|
116 | { | |||
|
117 | qCCritical(LOG_DataSeries()) | |||
|
118 | << QObject::tr("Can't update plottables: unmanaged data series type"); | |||
|
119 | } | |||
|
120 | }; | |||
|
121 | ||||
|
122 | /** | |||
|
123 | * Specialization of PlottablesUpdater for scalars and vectors | |||
|
124 | * @sa ScalarSeries | |||
|
125 | * @sa VectorSeries | |||
|
126 | */ | |||
|
127 | template <typename T> | |||
|
128 | struct PlottablesUpdater<T, | |||
|
129 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |||
|
130 | or std::is_base_of<VectorSeries, T>::value> > { | |||
|
131 | static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range, | |||
|
132 | bool rescaleAxes) | |||
|
133 | { | |||
|
134 | dataSeries.lockRead(); | |||
|
135 | ||||
|
136 | // For each plottable to update, resets its data | |||
|
137 | std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{}; | |||
|
138 | for (const auto &plottable : plottables) { | |||
|
139 | if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) { | |||
|
140 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); | |||
|
141 | graph->setData(dataContainer); | |||
|
142 | ||||
|
143 | dataContainers.insert({plottable.first, dataContainer}); | |||
119 | } |
|
144 | } | |
120 | else { |
|
|||
121 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
122 | << QObject::tr("Can't create graph for the scalar series"); |
|
|||
123 | } |
|
145 | } | |
124 |
|
146 | |||
125 | return component; |
|
147 | // - Gets the data of the series included in the current range | |
|
148 | // - Updates each plottable by adding, for each data item, a point that takes x-axis data and value data. The correct value is retrieved according to the index of the component | |||
|
149 | auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd); | |||
|
150 | for (auto it = subDataIts.first; it != subDataIts.second; ++it) { | |||
|
151 | for (const auto &dataContainer : dataContainers) { | |||
|
152 | auto componentIndex = dataContainer.first; | |||
|
153 | dataContainer.second->appendGraphData( | |||
|
154 | QCPGraphData(it->x(), it->value(componentIndex))); | |||
|
155 | } | |||
126 | } |
|
156 | } | |
127 |
|
157 | |||
128 | } // namespace |
|
158 | dataSeries.unlock(); | |
129 |
|
159 | |||
130 | QVector<QCPAbstractPlottable *> |
|
160 | if (!plottables.empty()) { | |
131 | VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept |
|
161 | auto plot = plottables.begin()->second->parentPlot(); | |
132 | { |
|
|||
133 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
|||
134 |
|
162 | |||
135 | if (variable) { |
|
163 | if (rescaleAxes) { | |
136 | // Gets the data series of the variable to call the creation of the right components |
|
164 | plot->rescaleAxes(); | |
137 | // according to its type |
|
|||
138 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) { |
|
|||
139 | result.append(createScalarSeriesComponentV2(scalarSeries, plot)); |
|
|||
140 | } |
|
|||
141 | else { |
|
|||
142 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
143 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
|||
144 | } |
|
165 | } | |
|
166 | ||||
|
167 | plot->replot(); | |||
145 | } |
|
168 | } | |
146 | else { |
|
|||
147 | qCDebug(LOG_VisualizationGraphHelper()) |
|
|||
148 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
|||
149 | } |
|
169 | } | |
|
170 | }; | |||
150 |
|
171 | |||
151 | return result; |
|
172 | /** | |
|
173 | * Helper used to create/update plottables | |||
|
174 | */ | |||
|
175 | struct IPlottablesHelper { | |||
|
176 | virtual ~IPlottablesHelper() noexcept = default; | |||
|
177 | virtual PlottablesMap create(QCustomPlot &plot) const = 0; | |||
|
178 | virtual void update(PlottablesMap &plottables, const SqpRange &range, | |||
|
179 | bool rescaleAxes = false) const = 0; | |||
|
180 | }; | |||
|
181 | ||||
|
182 | /** | |||
|
183 | * Default implementation of IPlottablesHelper, which takes data series to create/update plottables | |||
|
184 | * @tparam T the data series' type | |||
|
185 | */ | |||
|
186 | template <typename T> | |||
|
187 | struct PlottablesHelper : public IPlottablesHelper { | |||
|
188 | explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} | |||
|
189 | ||||
|
190 | PlottablesMap create(QCustomPlot &plot) const override | |||
|
191 | { | |||
|
192 | return PlottablesCreator<T>::createPlottables(m_DataSeries, plot); | |||
152 | } |
|
193 | } | |
153 |
|
194 | |||
154 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, |
|
195 | void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override | |
155 | QCustomPlot &plot) noexcept |
|
|||
156 | { |
|
196 | { | |
157 | auto result = QVector<QCPAbstractPlottable *>{}; |
|
197 | PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); | |
|
198 | } | |||
158 |
|
199 | |||
159 | if (variable) { |
|
200 | T &m_DataSeries; | |
160 | // Gets the data series of the variable to call the creation of the right components |
|
201 | }; | |
161 | // according to its type |
|
202 | ||
162 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) { |
|
203 | /// Creates IPlottablesHelper according to a data series | |
163 | result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range())); |
|
204 | std::unique_ptr<IPlottablesHelper> createHelper(std::shared_ptr<IDataSeries> dataSeries) noexcept | |
|
205 | { | |||
|
206 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) { | |||
|
207 | return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries); | |||
|
208 | } | |||
|
209 | else if (auto vectorSeries = std::dynamic_pointer_cast<VectorSeries>(dataSeries)) { | |||
|
210 | return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries); | |||
164 |
|
|
211 | } | |
165 |
|
|
212 | else { | |
166 | qCDebug(LOG_VisualizationGraphHelper()) |
|
213 | return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries); | |
167 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); |
|
|||
168 |
|
|
214 | } | |
169 | } |
|
215 | } | |
|
216 | ||||
|
217 | } // namespace | |||
|
218 | ||||
|
219 | PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |||
|
220 | QCustomPlot &plot) noexcept | |||
|
221 | { | |||
|
222 | if (variable) { | |||
|
223 | auto helper = createHelper(variable->dataSeries()); | |||
|
224 | auto plottables = helper->create(plot); | |||
|
225 | return plottables; | |||
|
226 | } | |||
170 | else { |
|
227 | else { | |
171 | qCDebug(LOG_VisualizationGraphHelper()) |
|
228 | qCDebug(LOG_VisualizationGraphHelper()) | |
172 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
229 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
|
230 | return PlottablesMap{}; | |||
173 | } |
|
231 | } | |
174 |
|
||||
175 | return result; |
|
|||
176 | } |
|
232 | } | |
177 |
|
233 | |||
178 |
void VisualizationGraphHelper::updateData( |
|
234 | void VisualizationGraphHelper::updateData(PlottablesMap &plottables, | |
179 | std::shared_ptr<IDataSeries> dataSeries, |
|
235 | std::shared_ptr<IDataSeries> dataSeries, | |
180 | const SqpRange &dateTime) |
|
236 | const SqpRange &dateTime) | |
181 | { |
|
237 | { | |
182 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) { |
|
238 | auto helper = createHelper(dataSeries); | |
183 | if (plotableVect.size() == 1) { |
|
239 | helper->update(plottables, dateTime); | |
184 | updateScalarData(plotableVect.at(0), scalarSeries, dateTime); |
|
|||
185 | } |
|
|||
186 | else { |
|
|||
187 | qCCritical(LOG_VisualizationGraphHelper()) << QObject::tr( |
|
|||
188 | "Can't update Data of a scalarSeries because there is not only one component " |
|
|||
189 | "associated"); |
|
|||
190 | } |
|
|||
191 | } |
|
|||
192 | else { |
|
|||
193 | /// @todo DEBUG |
|
|||
194 | } |
|
|||
195 | } |
|
240 | } |
@@ -1,320 +1,313 | |||||
1 | #include "Visualization/VisualizationGraphWidget.h" |
|
1 | #include "Visualization/VisualizationGraphWidget.h" | |
2 | #include "Visualization/IVisualizationWidgetVisitor.h" |
|
2 | #include "Visualization/IVisualizationWidgetVisitor.h" | |
|
3 | #include "Visualization/VisualizationDefs.h" | |||
3 | #include "Visualization/VisualizationGraphHelper.h" |
|
4 | #include "Visualization/VisualizationGraphHelper.h" | |
4 | #include "Visualization/VisualizationGraphRenderingDelegate.h" |
|
5 | #include "Visualization/VisualizationGraphRenderingDelegate.h" | |
5 | #include "ui_VisualizationGraphWidget.h" |
|
6 | #include "ui_VisualizationGraphWidget.h" | |
6 |
|
7 | |||
7 | #include <Data/ArrayData.h> |
|
8 | #include <Data/ArrayData.h> | |
8 | #include <Data/IDataSeries.h> |
|
9 | #include <Data/IDataSeries.h> | |
9 | #include <Settings/SqpSettingsDefs.h> |
|
10 | #include <Settings/SqpSettingsDefs.h> | |
10 | #include <SqpApplication.h> |
|
11 | #include <SqpApplication.h> | |
11 | #include <Variable/Variable.h> |
|
12 | #include <Variable/Variable.h> | |
12 | #include <Variable/VariableController.h> |
|
13 | #include <Variable/VariableController.h> | |
13 |
|
14 | |||
14 | #include <unordered_map> |
|
15 | #include <unordered_map> | |
15 |
|
16 | |||
16 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") |
|
17 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphWidget, "VisualizationGraphWidget") | |
17 |
|
18 | |||
18 | namespace { |
|
19 | namespace { | |
19 |
|
20 | |||
20 | /// Key pressed to enable zoom on horizontal axis |
|
21 | /// Key pressed to enable zoom on horizontal axis | |
21 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; |
|
22 | const auto HORIZONTAL_ZOOM_MODIFIER = Qt::NoModifier; | |
22 |
|
23 | |||
23 | /// Key pressed to enable zoom on vertical axis |
|
24 | /// Key pressed to enable zoom on vertical axis | |
24 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; |
|
25 | const auto VERTICAL_ZOOM_MODIFIER = Qt::ControlModifier; | |
25 |
|
26 | |||
26 | } // namespace |
|
27 | } // namespace | |
27 |
|
28 | |||
28 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { |
|
29 | struct VisualizationGraphWidget::VisualizationGraphWidgetPrivate { | |
29 |
|
30 | |||
30 | explicit VisualizationGraphWidgetPrivate() |
|
31 | explicit VisualizationGraphWidgetPrivate() | |
31 | : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} |
|
32 | : m_DoAcquisition{true}, m_IsCalibration{false}, m_RenderingDelegate{nullptr} | |
32 | { |
|
33 | { | |
33 | } |
|
34 | } | |
34 |
|
35 | |||
35 | // 1 variable -> n qcpplot |
|
36 | // 1 variable -> n qcpplot | |
36 |
std:: |
|
37 | std::map<std::shared_ptr<Variable>, PlottablesMap> m_VariableToPlotMultiMap; | |
37 | bool m_DoAcquisition; |
|
38 | bool m_DoAcquisition; | |
38 | bool m_IsCalibration; |
|
39 | bool m_IsCalibration; | |
39 | QCPItemTracer *m_TextTracer; |
|
40 | QCPItemTracer *m_TextTracer; | |
40 | /// Delegate used to attach rendering features to the plot |
|
41 | /// Delegate used to attach rendering features to the plot | |
41 | std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; |
|
42 | std::unique_ptr<VisualizationGraphRenderingDelegate> m_RenderingDelegate; | |
42 | }; |
|
43 | }; | |
43 |
|
44 | |||
44 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) |
|
45 | VisualizationGraphWidget::VisualizationGraphWidget(const QString &name, QWidget *parent) | |
45 | : QWidget{parent}, |
|
46 | : QWidget{parent}, | |
46 | ui{new Ui::VisualizationGraphWidget}, |
|
47 | ui{new Ui::VisualizationGraphWidget}, | |
47 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} |
|
48 | impl{spimpl::make_unique_impl<VisualizationGraphWidgetPrivate>()} | |
48 | { |
|
49 | { | |
49 | ui->setupUi(this); |
|
50 | ui->setupUi(this); | |
50 |
|
51 | |||
51 | // The delegate must be initialized after the ui as it uses the plot |
|
52 | // The delegate must be initialized after the ui as it uses the plot | |
52 | impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget); |
|
53 | impl->m_RenderingDelegate = std::make_unique<VisualizationGraphRenderingDelegate>(*ui->widget); | |
53 |
|
54 | |||
54 | ui->graphNameLabel->setText(name); |
|
55 | ui->graphNameLabel->setText(name); | |
55 |
|
56 | |||
56 | // 'Close' options : widget is deleted when closed |
|
57 | // 'Close' options : widget is deleted when closed | |
57 | setAttribute(Qt::WA_DeleteOnClose); |
|
58 | setAttribute(Qt::WA_DeleteOnClose); | |
58 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); |
|
59 | connect(ui->closeButton, &QToolButton::clicked, this, &VisualizationGraphWidget::close); | |
59 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); |
|
60 | ui->closeButton->setIcon(sqpApp->style()->standardIcon(QStyle::SP_TitleBarCloseButton)); | |
60 |
|
61 | |||
61 | // Set qcpplot properties : |
|
62 | // Set qcpplot properties : | |
62 | // - Drag (on x-axis) and zoom are enabled |
|
63 | // - Drag (on x-axis) and zoom are enabled | |
63 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation |
|
64 | // - Mouse wheel on qcpplot is intercepted to determine the zoom orientation | |
64 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); |
|
65 | ui->widget->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom); | |
65 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); |
|
66 | ui->widget->axisRect()->setRangeDrag(Qt::Horizontal); | |
66 |
|
67 | |||
67 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); |
|
68 | connect(ui->widget, &QCustomPlot::mousePress, this, &VisualizationGraphWidget::onMousePress); | |
68 | connect(ui->widget, &QCustomPlot::mouseRelease, this, |
|
69 | connect(ui->widget, &QCustomPlot::mouseRelease, this, | |
69 | &VisualizationGraphWidget::onMouseRelease); |
|
70 | &VisualizationGraphWidget::onMouseRelease); | |
70 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); |
|
71 | connect(ui->widget, &QCustomPlot::mouseMove, this, &VisualizationGraphWidget::onMouseMove); | |
71 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); |
|
72 | connect(ui->widget, &QCustomPlot::mouseWheel, this, &VisualizationGraphWidget::onMouseWheel); | |
72 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( |
|
73 | connect(ui->widget->xAxis, static_cast<void (QCPAxis::*)(const QCPRange &, const QCPRange &)>( | |
73 | &QCPAxis::rangeChanged), |
|
74 | &QCPAxis::rangeChanged), | |
74 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); |
|
75 | this, &VisualizationGraphWidget::onRangeChanged, Qt::DirectConnection); | |
75 |
|
76 | |||
76 | // Activates menu when right clicking on the graph |
|
77 | // Activates menu when right clicking on the graph | |
77 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); |
|
78 | ui->widget->setContextMenuPolicy(Qt::CustomContextMenu); | |
78 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, |
|
79 | connect(ui->widget, &QCustomPlot::customContextMenuRequested, this, | |
79 | &VisualizationGraphWidget::onGraphMenuRequested); |
|
80 | &VisualizationGraphWidget::onGraphMenuRequested); | |
80 |
|
81 | |||
81 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), |
|
82 | connect(this, &VisualizationGraphWidget::requestDataLoading, &sqpApp->variableController(), | |
82 | &VariableController::onRequestDataLoading); |
|
83 | &VariableController::onRequestDataLoading); | |
83 |
|
84 | |||
84 | connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this, |
|
85 | connect(&sqpApp->variableController(), &VariableController::updateVarDisplaying, this, | |
85 | &VisualizationGraphWidget::onUpdateVarDisplaying); |
|
86 | &VisualizationGraphWidget::onUpdateVarDisplaying); | |
86 | } |
|
87 | } | |
87 |
|
88 | |||
88 |
|
89 | |||
89 | VisualizationGraphWidget::~VisualizationGraphWidget() |
|
90 | VisualizationGraphWidget::~VisualizationGraphWidget() | |
90 | { |
|
91 | { | |
91 | delete ui; |
|
92 | delete ui; | |
92 | } |
|
93 | } | |
93 |
|
94 | |||
94 | void VisualizationGraphWidget::enableAcquisition(bool enable) |
|
95 | void VisualizationGraphWidget::enableAcquisition(bool enable) | |
95 | { |
|
96 | { | |
96 | impl->m_DoAcquisition = enable; |
|
97 | impl->m_DoAcquisition = enable; | |
97 | } |
|
98 | } | |
98 |
|
99 | |||
99 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range) |
|
100 | void VisualizationGraphWidget::addVariable(std::shared_ptr<Variable> variable, SqpRange range) | |
100 | { |
|
101 | { | |
101 | // Uses delegate to create the qcpplot components according to the variable |
|
102 | // Uses delegate to create the qcpplot components according to the variable | |
102 |
auto createdPlottables = VisualizationGraphHelper::create |
|
103 | auto createdPlottables = VisualizationGraphHelper::create(variable, *ui->widget); | |
103 |
|
104 | impl->m_VariableToPlotMultiMap.insert({variable, std::move(createdPlottables)}); | ||
104 | for (auto createdPlottable : qAsConst(createdPlottables)) { |
|
|||
105 | impl->m_VariableToPlotMultiMap.insert({variable, createdPlottable}); |
|
|||
106 | } |
|
|||
107 |
|
105 | |||
108 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); |
|
106 | connect(variable.get(), SIGNAL(updated()), this, SLOT(onDataCacheVariableUpdated())); | |
109 |
|
107 | |||
110 | auto varRange = variable->range(); |
|
108 | auto varRange = variable->range(); | |
111 |
|
109 | |||
112 | this->enableAcquisition(false); |
|
110 | this->enableAcquisition(false); | |
113 | this->setGraphRange(range); |
|
111 | this->setGraphRange(range); | |
114 | this->enableAcquisition(true); |
|
112 | this->enableAcquisition(true); | |
115 |
|
113 | |||
116 | emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange, |
|
114 | emit requestDataLoading(QVector<std::shared_ptr<Variable> >() << variable, range, varRange, | |
117 | false); |
|
115 | false); | |
118 |
|
116 | |||
119 | emit variableAdded(variable); |
|
117 | emit variableAdded(variable); | |
120 | } |
|
118 | } | |
121 |
|
119 | |||
122 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept |
|
120 | void VisualizationGraphWidget::removeVariable(std::shared_ptr<Variable> variable) noexcept | |
123 | { |
|
121 | { | |
124 | // Each component associated to the variable : |
|
122 | // Each component associated to the variable : | |
125 | // - is removed from qcpplot (which deletes it) |
|
123 | // - is removed from qcpplot (which deletes it) | |
126 | // - is no longer referenced in the map |
|
124 | // - is no longer referenced in the map | |
127 |
auto |
|
125 | auto variableIt = impl->m_VariableToPlotMultiMap.find(variable); | |
128 | for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
126 | if (variableIt != impl->m_VariableToPlotMultiMap.cend()) { | |
129 |
|
|
127 | auto &plottablesMap = variableIt->second; | |
130 | it = impl->m_VariableToPlotMultiMap.erase(it); |
|
128 | ||
|
129 | for (auto plottableIt = plottablesMap.cbegin(), plottableEnd = plottablesMap.cend(); | |||
|
130 | plottableIt != plottableEnd;) { | |||
|
131 | ui->widget->removePlottable(plottableIt->second); | |||
|
132 | plottableIt = plottablesMap.erase(plottableIt); | |||
|
133 | } | |||
|
134 | ||||
|
135 | impl->m_VariableToPlotMultiMap.erase(variableIt); | |||
131 | } |
|
136 | } | |
132 |
|
137 | |||
133 | // Updates graph |
|
138 | // Updates graph | |
134 | ui->widget->replot(); |
|
139 | ui->widget->replot(); | |
135 | } |
|
140 | } | |
136 |
|
141 | |||
137 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range) |
|
142 | void VisualizationGraphWidget::setRange(std::shared_ptr<Variable> variable, const SqpRange &range) | |
138 | { |
|
143 | { | |
139 | // Note: in case of different axes that depends on variable, we could start with a code like |
|
144 | // Note: in case of different axes that depends on variable, we could start with a code like | |
140 | // that: |
|
145 | // that: | |
141 | // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); |
|
146 | // auto componentsIt = impl->m_VariableToPlotMultiMap.equal_range(variable); | |
142 | // for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
147 | // for (auto it = componentsIt.first; it != componentsIt.second;) { | |
143 | // } |
|
148 | // } | |
144 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); |
|
149 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | |
145 | ui->widget->replot(); |
|
150 | ui->widget->replot(); | |
146 | } |
|
151 | } | |
147 |
|
152 | |||
148 | void VisualizationGraphWidget::setYRange(const SqpRange &range) |
|
153 | void VisualizationGraphWidget::setYRange(const SqpRange &range) | |
149 | { |
|
154 | { | |
150 | ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd); |
|
155 | ui->widget->yAxis->setRange(range.m_TStart, range.m_TEnd); | |
151 | } |
|
156 | } | |
152 |
|
157 | |||
153 | SqpRange VisualizationGraphWidget::graphRange() const noexcept |
|
158 | SqpRange VisualizationGraphWidget::graphRange() const noexcept | |
154 | { |
|
159 | { | |
155 | auto graphRange = ui->widget->xAxis->range(); |
|
160 | auto graphRange = ui->widget->xAxis->range(); | |
156 | return SqpRange{graphRange.lower, graphRange.upper}; |
|
161 | return SqpRange{graphRange.lower, graphRange.upper}; | |
157 | } |
|
162 | } | |
158 |
|
163 | |||
159 | void VisualizationGraphWidget::setGraphRange(const SqpRange &range) |
|
164 | void VisualizationGraphWidget::setGraphRange(const SqpRange &range) | |
160 | { |
|
165 | { | |
161 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START"); |
|
166 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange START"); | |
162 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); |
|
167 | ui->widget->xAxis->setRange(range.m_TStart, range.m_TEnd); | |
163 | ui->widget->replot(); |
|
168 | ui->widget->replot(); | |
164 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); |
|
169 | qCDebug(LOG_VisualizationGraphWidget()) << tr("VisualizationGraphWidget::setGraphRange END"); | |
165 | } |
|
170 | } | |
166 |
|
171 | |||
167 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) |
|
172 | void VisualizationGraphWidget::accept(IVisualizationWidgetVisitor *visitor) | |
168 | { |
|
173 | { | |
169 | if (visitor) { |
|
174 | if (visitor) { | |
170 | visitor->visit(this); |
|
175 | visitor->visit(this); | |
171 | } |
|
176 | } | |
172 | else { |
|
177 | else { | |
173 | qCCritical(LOG_VisualizationGraphWidget()) |
|
178 | qCCritical(LOG_VisualizationGraphWidget()) | |
174 | << tr("Can't visit widget : the visitor is null"); |
|
179 | << tr("Can't visit widget : the visitor is null"); | |
175 | } |
|
180 | } | |
176 | } |
|
181 | } | |
177 |
|
182 | |||
178 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const |
|
183 | bool VisualizationGraphWidget::canDrop(const Variable &variable) const | |
179 | { |
|
184 | { | |
180 | /// @todo : for the moment, a graph can always accomodate a variable |
|
185 | /// @todo : for the moment, a graph can always accomodate a variable | |
181 | Q_UNUSED(variable); |
|
186 | Q_UNUSED(variable); | |
182 | return true; |
|
187 | return true; | |
183 | } |
|
188 | } | |
184 |
|
189 | |||
185 | bool VisualizationGraphWidget::contains(const Variable &variable) const |
|
190 | bool VisualizationGraphWidget::contains(const Variable &variable) const | |
186 | { |
|
191 | { | |
187 | // Finds the variable among the keys of the map |
|
192 | // Finds the variable among the keys of the map | |
188 | auto variablePtr = &variable; |
|
193 | auto variablePtr = &variable; | |
189 | auto findVariable |
|
194 | auto findVariable | |
190 | = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); }; |
|
195 | = [variablePtr](const auto &entry) { return variablePtr == entry.first.get(); }; | |
191 |
|
196 | |||
192 | auto end = impl->m_VariableToPlotMultiMap.cend(); |
|
197 | auto end = impl->m_VariableToPlotMultiMap.cend(); | |
193 | auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable); |
|
198 | auto it = std::find_if(impl->m_VariableToPlotMultiMap.cbegin(), end, findVariable); | |
194 | return it != end; |
|
199 | return it != end; | |
195 | } |
|
200 | } | |
196 |
|
201 | |||
197 | QString VisualizationGraphWidget::name() const |
|
202 | QString VisualizationGraphWidget::name() const | |
198 | { |
|
203 | { | |
199 | return ui->graphNameLabel->text(); |
|
204 | return ui->graphNameLabel->text(); | |
200 | } |
|
205 | } | |
201 |
|
206 | |||
202 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept |
|
207 | void VisualizationGraphWidget::onGraphMenuRequested(const QPoint &pos) noexcept | |
203 | { |
|
208 | { | |
204 | QMenu graphMenu{}; |
|
209 | QMenu graphMenu{}; | |
205 |
|
210 | |||
206 | // Iterates on variables (unique keys) |
|
211 | // Iterates on variables (unique keys) | |
207 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), |
|
212 | for (auto it = impl->m_VariableToPlotMultiMap.cbegin(), | |
208 | end = impl->m_VariableToPlotMultiMap.cend(); |
|
213 | end = impl->m_VariableToPlotMultiMap.cend(); | |
209 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
214 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { | |
210 | // 'Remove variable' action |
|
215 | // 'Remove variable' action | |
211 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), |
|
216 | graphMenu.addAction(tr("Remove variable %1").arg(it->first->name()), | |
212 | [ this, var = it->first ]() { removeVariable(var); }); |
|
217 | [ this, var = it->first ]() { removeVariable(var); }); | |
213 | } |
|
218 | } | |
214 |
|
219 | |||
215 | if (!graphMenu.isEmpty()) { |
|
220 | if (!graphMenu.isEmpty()) { | |
216 | graphMenu.exec(mapToGlobal(pos)); |
|
221 | graphMenu.exec(mapToGlobal(pos)); | |
217 | } |
|
222 | } | |
218 | } |
|
223 | } | |
219 |
|
224 | |||
220 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) |
|
225 | void VisualizationGraphWidget::onRangeChanged(const QCPRange &t1, const QCPRange &t2) | |
221 | { |
|
226 | { | |
222 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") |
|
227 | qCDebug(LOG_VisualizationGraphWidget()) << tr("TORM: VisualizationGraphWidget::onRangeChanged") | |
223 | << QThread::currentThread()->objectName() << "DoAcqui" |
|
228 | << QThread::currentThread()->objectName() << "DoAcqui" | |
224 | << impl->m_DoAcquisition; |
|
229 | << impl->m_DoAcquisition; | |
225 |
|
230 | |||
226 | auto graphRange = SqpRange{t1.lower, t1.upper}; |
|
231 | auto graphRange = SqpRange{t1.lower, t1.upper}; | |
227 | auto oldGraphRange = SqpRange{t2.lower, t2.upper}; |
|
232 | auto oldGraphRange = SqpRange{t2.lower, t2.upper}; | |
228 |
|
233 | |||
229 | if (impl->m_DoAcquisition) { |
|
234 | if (impl->m_DoAcquisition) { | |
230 | QVector<std::shared_ptr<Variable> > variableUnderGraphVector; |
|
235 | QVector<std::shared_ptr<Variable> > variableUnderGraphVector; | |
231 |
|
236 | |||
232 | for (auto it = impl->m_VariableToPlotMultiMap.begin(), |
|
237 | for (auto it = impl->m_VariableToPlotMultiMap.begin(), | |
233 | end = impl->m_VariableToPlotMultiMap.end(); |
|
238 | end = impl->m_VariableToPlotMultiMap.end(); | |
234 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { |
|
239 | it != end; it = impl->m_VariableToPlotMultiMap.upper_bound(it->first)) { | |
235 | variableUnderGraphVector.push_back(it->first); |
|
240 | variableUnderGraphVector.push_back(it->first); | |
236 | } |
|
241 | } | |
237 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange, |
|
242 | emit requestDataLoading(std::move(variableUnderGraphVector), graphRange, oldGraphRange, | |
238 | !impl->m_IsCalibration); |
|
243 | !impl->m_IsCalibration); | |
239 |
|
244 | |||
240 | if (!impl->m_IsCalibration) { |
|
245 | if (!impl->m_IsCalibration) { | |
241 | qCDebug(LOG_VisualizationGraphWidget()) |
|
246 | qCDebug(LOG_VisualizationGraphWidget()) | |
242 | << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") |
|
247 | << tr("TORM: VisualizationGraphWidget::Synchronize notify !!") | |
243 | << QThread::currentThread()->objectName() << graphRange << oldGraphRange; |
|
248 | << QThread::currentThread()->objectName() << graphRange << oldGraphRange; | |
244 | emit synchronize(graphRange, oldGraphRange); |
|
249 | emit synchronize(graphRange, oldGraphRange); | |
245 | } |
|
250 | } | |
246 | } |
|
251 | } | |
247 | } |
|
252 | } | |
248 |
|
253 | |||
249 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept |
|
254 | void VisualizationGraphWidget::onMouseMove(QMouseEvent *event) noexcept | |
250 | { |
|
255 | { | |
251 | // Handles plot rendering when mouse is moving |
|
256 | // Handles plot rendering when mouse is moving | |
252 | impl->m_RenderingDelegate->onMouseMove(event); |
|
257 | impl->m_RenderingDelegate->onMouseMove(event); | |
253 | } |
|
258 | } | |
254 |
|
259 | |||
255 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept |
|
260 | void VisualizationGraphWidget::onMouseWheel(QWheelEvent *event) noexcept | |
256 | { |
|
261 | { | |
257 | auto zoomOrientations = QFlags<Qt::Orientation>{}; |
|
262 | auto zoomOrientations = QFlags<Qt::Orientation>{}; | |
258 |
|
263 | |||
259 | // Lambda that enables a zoom orientation if the key modifier related to this orientation |
|
264 | // Lambda that enables a zoom orientation if the key modifier related to this orientation | |
260 | // has |
|
265 | // has | |
261 | // been pressed |
|
266 | // been pressed | |
262 | auto enableOrientation |
|
267 | auto enableOrientation | |
263 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { |
|
268 | = [&zoomOrientations, event](const auto &orientation, const auto &modifier) { | |
264 | auto orientationEnabled = event->modifiers().testFlag(modifier); |
|
269 | auto orientationEnabled = event->modifiers().testFlag(modifier); | |
265 | zoomOrientations.setFlag(orientation, orientationEnabled); |
|
270 | zoomOrientations.setFlag(orientation, orientationEnabled); | |
266 | }; |
|
271 | }; | |
267 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); |
|
272 | enableOrientation(Qt::Vertical, VERTICAL_ZOOM_MODIFIER); | |
268 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); |
|
273 | enableOrientation(Qt::Horizontal, HORIZONTAL_ZOOM_MODIFIER); | |
269 |
|
274 | |||
270 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); |
|
275 | ui->widget->axisRect()->setRangeZoom(zoomOrientations); | |
271 | } |
|
276 | } | |
272 |
|
277 | |||
273 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept |
|
278 | void VisualizationGraphWidget::onMousePress(QMouseEvent *event) noexcept | |
274 | { |
|
279 | { | |
275 | impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier); |
|
280 | impl->m_IsCalibration = event->modifiers().testFlag(Qt::ControlModifier); | |
276 | } |
|
281 | } | |
277 |
|
282 | |||
278 | void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept |
|
283 | void VisualizationGraphWidget::onMouseRelease(QMouseEvent *event) noexcept | |
279 | { |
|
284 | { | |
280 | impl->m_IsCalibration = false; |
|
285 | impl->m_IsCalibration = false; | |
281 | } |
|
286 | } | |
282 |
|
287 | |||
283 | void VisualizationGraphWidget::onDataCacheVariableUpdated() |
|
288 | void VisualizationGraphWidget::onDataCacheVariableUpdated() | |
284 | { |
|
289 | { | |
285 | // NOTE: |
|
|||
286 | // We don't want to call the method for each component of a variable unitarily, but for |
|
|||
287 | // all |
|
|||
288 | // its components at once (eg its three components in the case of a vector). |
|
|||
289 |
|
||||
290 | // The unordered_multimap does not do this easily, so the question is whether to: |
|
|||
291 | // - use an ordered_multimap and the algos of std to group the values by key |
|
|||
292 | // - use a map (unique keys) and store as values directly the list of components |
|
|||
293 |
|
||||
294 | auto graphRange = ui->widget->xAxis->range(); |
|
290 | auto graphRange = ui->widget->xAxis->range(); | |
295 | auto dateTime = SqpRange{graphRange.lower, graphRange.upper}; |
|
291 | auto dateTime = SqpRange{graphRange.lower, graphRange.upper}; | |
296 |
|
292 | |||
297 |
for (auto it |
|
293 | for (auto &variableEntry : impl->m_VariableToPlotMultiMap) { | |
298 | it != impl->m_VariableToPlotMultiMap.cend(); ++it) { |
|
294 | auto variable = variableEntry.first; | |
299 | auto variable = it->first; |
|
|||
300 | qCDebug(LOG_VisualizationGraphWidget()) |
|
295 | qCDebug(LOG_VisualizationGraphWidget()) | |
301 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); |
|
296 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated S" << variable->range(); | |
302 | qCDebug(LOG_VisualizationGraphWidget()) |
|
297 | qCDebug(LOG_VisualizationGraphWidget()) | |
303 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; |
|
298 | << "TORM: VisualizationGraphWidget::onDataCacheVariableUpdated E" << dateTime; | |
304 | if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { |
|
299 | if (dateTime.contains(variable->range()) || dateTime.intersect(variable->range())) { | |
305 |
|
300 | VisualizationGraphHelper::updateData(variableEntry.second, variable->dataSeries(), | ||
306 | VisualizationGraphHelper::updateData(QVector<QCPAbstractPlottable *>{} << it->second, |
|
301 | variable->range()); | |
307 | variable->dataSeries(), variable->range()); |
|
|||
308 | } |
|
302 | } | |
309 | } |
|
303 | } | |
310 | } |
|
304 | } | |
311 |
|
305 | |||
312 | void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable, |
|
306 | void VisualizationGraphWidget::onUpdateVarDisplaying(std::shared_ptr<Variable> variable, | |
313 | const SqpRange &range) |
|
307 | const SqpRange &range) | |
314 | { |
|
308 | { | |
315 |
auto |
|
309 | auto it = impl->m_VariableToPlotMultiMap.find(variable); | |
316 | for (auto it = componentsIt.first; it != componentsIt.second;) { |
|
310 | if (it != impl->m_VariableToPlotMultiMap.end()) { | |
317 |
VisualizationGraphHelper::updateData( |
|
311 | VisualizationGraphHelper::updateData(it->second, variable->dataSeries(), range); | |
318 | variable->dataSeries(), range); |
|
|||
319 | } |
|
312 | } | |
320 | } |
|
313 | } |
General Comments 0
You need to be logged in to leave comments.
Login now