@@ -1,313 +1,315 | |||
|
1 | 1 | #ifndef SCIQLOP_ARRAYDATA_H |
|
2 | 2 | #define SCIQLOP_ARRAYDATA_H |
|
3 | 3 | |
|
4 | 4 | #include <Common/SortUtils.h> |
|
5 | 5 | |
|
6 | 6 | #include <QReadLocker> |
|
7 | 7 | #include <QReadWriteLock> |
|
8 | 8 | #include <QVector> |
|
9 | 9 | |
|
10 | 10 | #include <memory> |
|
11 | 11 | |
|
12 | 12 | template <int Dim> |
|
13 | 13 | class ArrayData; |
|
14 | 14 | |
|
15 | 15 | using DataContainer = QVector<QVector<double> >; |
|
16 | 16 | |
|
17 | 17 | namespace arraydata_detail { |
|
18 | 18 | |
|
19 | 19 | /// Struct used to sort ArrayData |
|
20 | 20 | template <int Dim> |
|
21 | 21 | struct Sort { |
|
22 | 22 | static std::shared_ptr<ArrayData<Dim> > sort(const DataContainer &data, |
|
23 | 23 | const std::vector<int> &sortPermutation) |
|
24 | 24 | { |
|
25 | 25 | auto nbComponents = data.size(); |
|
26 | 26 | auto sortedData = DataContainer(nbComponents); |
|
27 | 27 | |
|
28 | 28 | for (auto i = 0; i < nbComponents; ++i) { |
|
29 | 29 | sortedData[i] = SortUtils::sort(data.at(i), sortPermutation); |
|
30 | 30 | } |
|
31 | 31 | |
|
32 | 32 | return std::make_shared<ArrayData<Dim> >(std::move(sortedData)); |
|
33 | 33 | } |
|
34 | 34 | }; |
|
35 | 35 | |
|
36 | 36 | /// Specialization for uni-dimensional ArrayData |
|
37 | 37 | template <> |
|
38 | 38 | struct Sort<1> { |
|
39 | 39 | static std::shared_ptr<ArrayData<1> > sort(const DataContainer &data, |
|
40 | 40 | const std::vector<int> &sortPermutation) |
|
41 | 41 | { |
|
42 | 42 | return std::make_shared<ArrayData<1> >(SortUtils::sort(data.at(0), sortPermutation)); |
|
43 | 43 | } |
|
44 | 44 | }; |
|
45 | 45 | |
|
46 | 46 | } // namespace arraydata_detail |
|
47 | 47 | |
|
48 | 48 | /** |
|
49 | 49 | * @brief The ArrayData class represents a dataset for a data series. |
|
50 | 50 | * |
|
51 | 51 | * A dataset can be unidimensional or two-dimensional. This property is determined by the Dim |
|
52 | 52 | * template-parameter. In a case of a two-dimensional dataset, each dataset component has the same |
|
53 | 53 | * number of values |
|
54 | 54 | * |
|
55 | 55 | * @tparam Dim the dimension of the ArrayData (one or two) |
|
56 | 56 | * @sa IDataSeries |
|
57 | 57 | */ |
|
58 | 58 | template <int Dim> |
|
59 | 59 | class ArrayData { |
|
60 | 60 | public: |
|
61 | 61 | class IteratorValue { |
|
62 | 62 | public: |
|
63 | 63 | explicit IteratorValue(const DataContainer &container, bool begin) : m_Its{} |
|
64 | 64 | { |
|
65 | 65 | for (auto i = 0; i < container.size(); ++i) { |
|
66 | 66 | m_Its.push_back(begin ? container.at(i).cbegin() : container.at(i).cend()); |
|
67 | 67 | } |
|
68 | 68 | } |
|
69 | 69 | |
|
70 | 70 | double at(int index) const { return *m_Its.at(index); } |
|
71 | 71 | double first() const { return *m_Its.front(); } |
|
72 | 72 | |
|
73 | 73 | void next() |
|
74 | 74 | { |
|
75 | 75 | for (auto &it : m_Its) { |
|
76 | 76 | ++it; |
|
77 | 77 | } |
|
78 | 78 | } |
|
79 | 79 | |
|
80 | 80 | bool operator==(const IteratorValue &other) const { return m_Its == other.m_Its; } |
|
81 | 81 | |
|
82 | 82 | private: |
|
83 | 83 | std::vector<DataContainer::value_type::const_iterator> m_Its; |
|
84 | 84 | }; |
|
85 | 85 | |
|
86 | 86 | class Iterator { |
|
87 | 87 | public: |
|
88 | 88 | using iterator_category = std::forward_iterator_tag; |
|
89 | 89 | using value_type = const IteratorValue; |
|
90 | 90 | using difference_type = std::ptrdiff_t; |
|
91 | 91 | using pointer = value_type *; |
|
92 | 92 | using reference = value_type &; |
|
93 | 93 | |
|
94 | 94 | Iterator(const DataContainer &container, bool begin) : m_CurrentValue{container, begin} {} |
|
95 | 95 | |
|
96 | 96 | virtual ~Iterator() noexcept = default; |
|
97 | 97 | Iterator(const Iterator &) = default; |
|
98 | 98 | Iterator(Iterator &&) = default; |
|
99 | 99 | Iterator &operator=(const Iterator &) = default; |
|
100 | 100 | Iterator &operator=(Iterator &&) = default; |
|
101 | 101 | |
|
102 | 102 | Iterator &operator++() |
|
103 | 103 | { |
|
104 | 104 | m_CurrentValue.next(); |
|
105 | 105 | return *this; |
|
106 | 106 | } |
|
107 | 107 | |
|
108 | 108 | pointer operator->() const { return &m_CurrentValue; } |
|
109 | 109 | reference operator*() const { return m_CurrentValue; } |
|
110 | 110 | |
|
111 | 111 | bool operator==(const Iterator &other) const |
|
112 | 112 | { |
|
113 | 113 | return m_CurrentValue == other.m_CurrentValue; |
|
114 | 114 | } |
|
115 | 115 | |
|
116 | 116 | bool operator!=(const Iterator &other) const { return !(*this == other); } |
|
117 | 117 | |
|
118 | 118 | private: |
|
119 | 119 | IteratorValue m_CurrentValue; |
|
120 | 120 | }; |
|
121 | 121 | |
|
122 | 122 | // ///// // |
|
123 | 123 | // Ctors // |
|
124 | 124 | // ///// // |
|
125 | 125 | |
|
126 | 126 | /** |
|
127 | 127 | * Ctor for a unidimensional ArrayData |
|
128 | 128 | * @param data the data the ArrayData will hold |
|
129 | 129 | */ |
|
130 | 130 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
131 | 131 | explicit ArrayData(QVector<double> data) : m_Data{1, QVector<double>{}} |
|
132 | 132 | { |
|
133 | 133 | m_Data[0] = std::move(data); |
|
134 | 134 | } |
|
135 | 135 | |
|
136 | 136 | /** |
|
137 | 137 | * Ctor for a two-dimensional ArrayData. The number of components (number of vectors) must be |
|
138 | 138 | * greater than 2 and each component must have the same number of values |
|
139 | 139 | * @param data the data the ArrayData will hold |
|
140 | 140 | * @throws std::invalid_argument if the number of components is less than 2 |
|
141 | 141 | * @remarks if the number of values is not the same for each component, no value is set |
|
142 | 142 | */ |
|
143 | 143 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
144 | 144 | explicit ArrayData(DataContainer data) |
|
145 | 145 | { |
|
146 | 146 | auto nbComponents = data.size(); |
|
147 | 147 | if (nbComponents < 2) { |
|
148 | 148 | throw std::invalid_argument{ |
|
149 | 149 | QString{"A multidimensional ArrayData must have at least 2 components (found: %1"} |
|
150 | 150 | .arg(data.size()) |
|
151 | 151 | .toStdString()}; |
|
152 | 152 | } |
|
153 | 153 | |
|
154 | 154 | auto nbValues = data.front().size(); |
|
155 | 155 | if (std::all_of(data.cbegin(), data.cend(), [nbValues](const auto &component) { |
|
156 | 156 | return component.size() == nbValues; |
|
157 | 157 | })) { |
|
158 | 158 | m_Data = std::move(data); |
|
159 | 159 | } |
|
160 | 160 | else { |
|
161 | 161 | m_Data = DataContainer{nbComponents, QVector<double>{}}; |
|
162 | 162 | } |
|
163 | 163 | } |
|
164 | 164 | |
|
165 | 165 | /// Copy ctor |
|
166 | 166 | explicit ArrayData(const ArrayData &other) |
|
167 | 167 | { |
|
168 | 168 | QReadLocker otherLocker{&other.m_Lock}; |
|
169 | 169 | m_Data = other.m_Data; |
|
170 | 170 | } |
|
171 | 171 | |
|
172 | 172 | // /////////////// // |
|
173 | 173 | // General methods // |
|
174 | 174 | // /////////////// // |
|
175 | 175 | |
|
176 | 176 | /** |
|
177 | 177 | * Merges into the array data an other array data. The two array datas must have the same number |
|
178 | 178 | * of components so the merge can be done |
|
179 | 179 | * @param other the array data to merge with |
|
180 | 180 | * @param prepend if true, the other array data is inserted at the beginning, otherwise it is |
|
181 | 181 | * inserted at the end |
|
182 | 182 | */ |
|
183 | 183 | void add(const ArrayData<Dim> &other, bool prepend = false) |
|
184 | 184 | { |
|
185 | 185 | QWriteLocker locker{&m_Lock}; |
|
186 | 186 | QReadLocker otherLocker{&other.m_Lock}; |
|
187 | 187 | |
|
188 | 188 | auto nbComponents = m_Data.size(); |
|
189 | 189 | if (nbComponents != other.m_Data.size()) { |
|
190 | 190 | return; |
|
191 | 191 | } |
|
192 | 192 | |
|
193 | 193 | for (auto componentIndex = 0; componentIndex < nbComponents; ++componentIndex) { |
|
194 | 194 | if (prepend) { |
|
195 | 195 | const auto &otherData = other.data(componentIndex); |
|
196 | 196 | const auto otherDataSize = otherData.size(); |
|
197 | 197 | |
|
198 | 198 | auto &data = m_Data[componentIndex]; |
|
199 | 199 | data.insert(data.begin(), otherDataSize, 0.); |
|
200 | 200 | |
|
201 | 201 | for (auto i = 0; i < otherDataSize; ++i) { |
|
202 | 202 | data.replace(i, otherData.at(i)); |
|
203 | 203 | } |
|
204 | 204 | } |
|
205 | 205 | else { |
|
206 | 206 | m_Data[componentIndex] += other.data(componentIndex); |
|
207 | 207 | } |
|
208 | 208 | } |
|
209 | 209 | } |
|
210 | 210 | |
|
211 | 211 | void clear() |
|
212 | 212 | { |
|
213 | 213 | QWriteLocker locker{&m_Lock}; |
|
214 | 214 | |
|
215 | 215 | auto nbComponents = m_Data.size(); |
|
216 | 216 | for (auto i = 0; i < nbComponents; ++i) { |
|
217 | 217 | m_Data[i].clear(); |
|
218 | 218 | } |
|
219 | 219 | } |
|
220 | 220 | |
|
221 | int componentCount() const noexcept { return m_Data.size(); } | |
|
222 | ||
|
221 | 223 | /** |
|
222 | 224 | * @return the data of a component |
|
223 | 225 | * @param componentIndex the index of the component to retrieve the data |
|
224 | 226 | * @return the component's data, empty vector if the index is invalid |
|
225 | 227 | */ |
|
226 | 228 | QVector<double> data(int componentIndex) const noexcept |
|
227 | 229 | { |
|
228 | 230 | QReadLocker locker{&m_Lock}; |
|
229 | 231 | |
|
230 | 232 | return (componentIndex >= 0 && componentIndex < m_Data.size()) ? m_Data.at(componentIndex) |
|
231 | 233 | : QVector<double>{}; |
|
232 | 234 | } |
|
233 | 235 | |
|
234 | 236 | /// @return the size (i.e. number of values) of a single component |
|
235 | 237 | /// @remarks in a case of a two-dimensional ArrayData, each component has the same size |
|
236 | 238 | int size() const |
|
237 | 239 | { |
|
238 | 240 | QReadLocker locker{&m_Lock}; |
|
239 | 241 | return m_Data[0].size(); |
|
240 | 242 | } |
|
241 | 243 | |
|
242 | 244 | std::shared_ptr<ArrayData<Dim> > sort(const std::vector<int> &sortPermutation) |
|
243 | 245 | { |
|
244 | 246 | QReadLocker locker{&m_Lock}; |
|
245 | 247 | return arraydata_detail::Sort<Dim>::sort(m_Data, sortPermutation); |
|
246 | 248 | } |
|
247 | 249 | |
|
248 | 250 | // ///////// // |
|
249 | 251 | // Iterators // |
|
250 | 252 | // ///////// // |
|
251 | 253 | |
|
252 | 254 | Iterator cbegin() const { return Iterator{m_Data, true}; } |
|
253 | 255 | Iterator cend() const { return Iterator{m_Data, false}; } |
|
254 | 256 | |
|
255 | 257 | // ///////////// // |
|
256 | 258 | // 1-dim methods // |
|
257 | 259 | // ///////////// // |
|
258 | 260 | |
|
259 | 261 | /** |
|
260 | 262 | * @return the data at a specified index |
|
261 | 263 | * @remarks index must be a valid position |
|
262 | 264 | * @remarks this method is only available for a unidimensional ArrayData |
|
263 | 265 | */ |
|
264 | 266 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
265 | 267 | double at(int index) const noexcept |
|
266 | 268 | { |
|
267 | 269 | QReadLocker locker{&m_Lock}; |
|
268 | 270 | return m_Data[0].at(index); |
|
269 | 271 | } |
|
270 | 272 | |
|
271 | 273 | /** |
|
272 | 274 | * @return the data as a vector, as a const reference |
|
273 | 275 | * @remarks this method is only available for a unidimensional ArrayData |
|
274 | 276 | */ |
|
275 | 277 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
276 | 278 | const QVector<double> &cdata() const noexcept |
|
277 | 279 | { |
|
278 | 280 | QReadLocker locker{&m_Lock}; |
|
279 | 281 | return m_Data.at(0); |
|
280 | 282 | } |
|
281 | 283 | |
|
282 | 284 | /** |
|
283 | 285 | * @return the data as a vector |
|
284 | 286 | * @remarks this method is only available for a unidimensional ArrayData |
|
285 | 287 | */ |
|
286 | 288 | template <int D = Dim, typename = std::enable_if_t<D == 1> > |
|
287 | 289 | QVector<double> data() const noexcept |
|
288 | 290 | { |
|
289 | 291 | QReadLocker locker{&m_Lock}; |
|
290 | 292 | return m_Data[0]; |
|
291 | 293 | } |
|
292 | 294 | |
|
293 | 295 | // ///////////// // |
|
294 | 296 | // 2-dim methods // |
|
295 | 297 | // ///////////// // |
|
296 | 298 | |
|
297 | 299 | /** |
|
298 | 300 | * @return the data |
|
299 | 301 | * @remarks this method is only available for a two-dimensional ArrayData |
|
300 | 302 | */ |
|
301 | 303 | template <int D = Dim, typename = std::enable_if_t<D == 2> > |
|
302 | 304 | DataContainer data() const noexcept |
|
303 | 305 | { |
|
304 | 306 | QReadLocker locker{&m_Lock}; |
|
305 | 307 | return m_Data; |
|
306 | 308 | } |
|
307 | 309 | |
|
308 | 310 | private: |
|
309 | 311 | DataContainer m_Data; |
|
310 | 312 | mutable QReadWriteLock m_Lock; |
|
311 | 313 | }; |
|
312 | 314 | |
|
313 | 315 | #endif // SCIQLOP_ARRAYDATA_H |
@@ -1,195 +1,234 | |||
|
1 | 1 | #include "Visualization/VisualizationGraphHelper.h" |
|
2 | 2 | #include "Visualization/qcustomplot.h" |
|
3 | 3 | |
|
4 | 4 | #include <Data/ScalarSeries.h> |
|
5 | #include <Data/VectorSeries.h> | |
|
5 | 6 | |
|
6 | 7 | #include <Variable/Variable.h> |
|
7 | 8 | |
|
8 | 9 | Q_LOGGING_CATEGORY(LOG_VisualizationGraphHelper, "VisualizationGraphHelper") |
|
9 | 10 | |
|
10 | 11 | namespace { |
|
11 | 12 | |
|
12 | 13 | class SqpDataContainer : public QCPGraphDataContainer { |
|
13 | 14 | public: |
|
14 | 15 | void appendGraphData(const QCPGraphData &data) { mData.append(data); } |
|
15 | 16 | }; |
|
16 | 17 | |
|
17 | 18 | |
|
18 | 19 | /// Format for datetimes on a axis |
|
19 | 20 | const auto DATETIME_TICKER_FORMAT = QStringLiteral("yyyy/MM/dd \nhh:mm:ss"); |
|
20 | 21 | |
|
21 | 22 | /// Generates the appropriate ticker for an axis, depending on whether the axis displays time or |
|
22 | 23 | /// non-time data |
|
23 | 24 | QSharedPointer<QCPAxisTicker> axisTicker(bool isTimeAxis) |
|
24 | 25 | { |
|
25 | 26 | if (isTimeAxis) { |
|
26 | 27 | auto dateTicker = QSharedPointer<QCPAxisTickerDateTime>::create(); |
|
27 | 28 | dateTicker->setDateTimeFormat(DATETIME_TICKER_FORMAT); |
|
28 | 29 | dateTicker->setDateTimeSpec(Qt::UTC); |
|
29 | 30 | |
|
30 | 31 | return dateTicker; |
|
31 | 32 | } |
|
32 | 33 | else { |
|
33 | 34 | // default ticker |
|
34 | 35 | return QSharedPointer<QCPAxisTicker>::create(); |
|
35 | 36 | } |
|
36 | 37 | } |
|
37 | 38 | |
|
38 | void updateScalarData(QCPAbstractPlottable *component, std::shared_ptr<ScalarSeries> scalarSeries, | |
|
39 | const SqpRange &range) | |
|
39 | /// Sets axes properties according to the properties of a data series | |
|
40 | template <int Dim> | |
|
41 | void setAxesProperties(const DataSeries<Dim> &dataSeries, QCustomPlot &plot) noexcept | |
|
40 | 42 | { |
|
41 | qCDebug(LOG_VisualizationGraphHelper()) << "TORM: updateScalarData" | |
|
42 | << QThread::currentThread()->objectName(); | |
|
43 | if (auto qcpGraph = dynamic_cast<QCPGraph *>(component)) { | |
|
44 | scalarSeries->lockRead(); | |
|
45 | { | |
|
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 | 43 |
|
|
75 | 44 |
|
|
76 | ||
|
77 | 45 |
|
|
78 | 46 |
|
|
79 | 47 |
|
|
80 | 48 | |
|
81 | 49 |
|
|
82 | 50 |
|
|
83 | 51 |
|
|
84 |
|
|
|
85 |
|
|
|
86 | } | |
|
87 | return component; | |
|
52 | setAxisProperties(plot.xAxis, dataSeries.xAxisUnit()); | |
|
53 | setAxisProperties(plot.yAxis, dataSeries.valuesUnit()); | |
|
54 | } | |
|
55 | ||
|
56 | /** | |
|
57 | * Struct used to create plottables, depending on the type of the data series from which to create them | |
|
58 | * @tparam T the data series' type | |
|
59 | * @remarks Default implementation can't create plottables | |
|
60 | */ | |
|
61 | template <typename T, typename Enabled = void> | |
|
62 | struct PlottablesCreator { | |
|
63 | static PlottablesMap createPlottables(T &, QCustomPlot &) | |
|
64 | { | |
|
65 | qCCritical(LOG_DataSeries()) | |
|
66 | << QObject::tr("Can't create plottables: unmanaged data series type"); | |
|
67 | return {}; | |
|
88 | 68 | } |
|
69 | }; | |
|
89 | 70 | |
|
90 | QCPAbstractPlottable *createScalarSeriesComponent(std::shared_ptr<ScalarSeries> scalarSeries, | |
|
91 | QCustomPlot &plot, const SqpRange &dateTime) | |
|
71 | /** | |
|
72 | * Specialization of PlottablesCreator for scalars and vectors | |
|
73 | * @sa ScalarSeries | |
|
74 | * @sa VectorSeries | |
|
75 | */ | |
|
76 | template <typename T> | |
|
77 | struct PlottablesCreator<T, | |
|
78 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |
|
79 | or std::is_base_of<VectorSeries, T>::value> > { | |
|
80 | static PlottablesMap createPlottables(T &dataSeries, QCustomPlot &plot) | |
|
92 | 81 | { |
|
93 | auto component = plot.addGraph(); | |
|
82 | PlottablesMap result{}; | |
|
94 | 83 | |
|
95 | if (component) { | |
|
96 | // // Graph data | |
|
97 | component->setData(scalarSeries->xAxisData()->data(), scalarSeries->valuesData()->data(), | |
|
98 | true); | |
|
84 | // Gets the number of components of the data series | |
|
85 | auto componentCount = dataSeries.valuesData()->componentCount(); | |
|
99 | 86 | |
|
100 | updateScalarData(component, scalarSeries, dateTime); | |
|
87 | // For each component of the data series, creates a QCPGraph to add to the plot | |
|
88 | for (auto i = 0; i < componentCount; ++i) { | |
|
89 | auto graph = plot.addGraph(); | |
|
90 | ||
|
91 | result.insert({i, graph}); | |
|
92 | } | |
|
101 | 93 | |
|
102 | 94 | // Axes properties |
|
103 | /// @todo : for the moment, no control is performed on the axes: the units and the tickers | |
|
104 | /// are fixed for the default x-axis and y-axis of the plot, and according to the new graph | |
|
95 | setAxesProperties(dataSeries, plot); | |
|
105 | 96 | |
|
106 | auto setAxisProperties = [](auto axis, const auto &unit) { | |
|
107 | // label (unit name) | |
|
108 | axis->setLabel(unit.m_Name); | |
|
97 | plot.replot(); | |
|
109 | 98 | |
|
110 | // ticker (depending on the type of unit) | |
|
111 | axis->setTicker(axisTicker(unit.m_TimeUnit)); | |
|
99 | return result; | |
|
100 | } | |
|
112 | 101 | }; |
|
113 | setAxisProperties(plot.xAxis, scalarSeries->xAxisUnit()); | |
|
114 | setAxisProperties(plot.yAxis, scalarSeries->valuesUnit()); | |
|
115 | 102 | |
|
116 | // Display all data | |
|
117 | component->rescaleAxes(); | |
|
118 | plot.replot(); | |
|
103 | /** | |
|
104 | * Struct used to update plottables, depending on the type of the data series from which to update them | |
|
105 | * @tparam T the data series' type | |
|
106 | * @remarks Default implementation can't update plottables | |
|
107 | */ | |
|
108 | template <typename T, typename Enabled = void> | |
|
109 | struct PlottablesUpdater { | |
|
110 | static void updatePlottables(T &, PlottablesMap &, const SqpRange &, bool) | |
|
111 | { | |
|
112 | qCCritical(LOG_DataSeries()) | |
|
113 | << QObject::tr("Can't update plottables: unmanaged data series type"); | |
|
114 | } | |
|
115 | }; | |
|
116 | ||
|
117 | /** | |
|
118 | * Specialization of PlottablesUpdater for scalars and vectors | |
|
119 | * @sa ScalarSeries | |
|
120 | * @sa VectorSeries | |
|
121 | */ | |
|
122 | template <typename T> | |
|
123 | struct PlottablesUpdater<T, | |
|
124 | typename std::enable_if_t<std::is_base_of<ScalarSeries, T>::value | |
|
125 | or std::is_base_of<VectorSeries, T>::value> > { | |
|
126 | static void updatePlottables(T &dataSeries, PlottablesMap &plottables, const SqpRange &range, | |
|
127 | bool rescaleAxes) | |
|
128 | { | |
|
129 | dataSeries.lockRead(); | |
|
130 | ||
|
131 | // For each plottable to update, resets its data | |
|
132 | std::map<int, QSharedPointer<SqpDataContainer> > dataContainers{}; | |
|
133 | for (const auto &plottable : plottables) { | |
|
134 | if (auto graph = dynamic_cast<QCPGraph *>(plottable.second)) { | |
|
135 | auto dataContainer = QSharedPointer<SqpDataContainer>::create(); | |
|
136 | graph->setData(dataContainer); | |
|
137 | ||
|
138 | dataContainers.insert({plottable.first, dataContainer}); | |
|
119 | 139 | } |
|
120 | else { | |
|
121 | qCDebug(LOG_VisualizationGraphHelper()) | |
|
122 | << QObject::tr("Can't create graph for the scalar series"); | |
|
123 | 140 | } |
|
124 | 141 | |
|
125 | return component; | |
|
142 | // - Gets the data of the series included in the current range | |
|
143 | // - 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 | |
|
144 | auto subDataIts = dataSeries.subData(range.m_TStart, range.m_TEnd); | |
|
145 | for (auto it = subDataIts.first; it != subDataIts.second; ++it) { | |
|
146 | for (const auto &dataContainer : dataContainers) { | |
|
147 | auto componentIndex = dataContainer.first; | |
|
148 | dataContainer.second->appendGraphData( | |
|
149 | QCPGraphData(it->x(), it->value(componentIndex))); | |
|
150 | } | |
|
126 | 151 | } |
|
127 | 152 | |
|
128 | } // namespace | |
|
153 | dataSeries.unlock(); | |
|
129 | 154 | |
|
130 | QVector<QCPAbstractPlottable *> | |
|
131 | VisualizationGraphHelper::createV2(std::shared_ptr<Variable> variable, QCustomPlot &plot) noexcept | |
|
132 | { | |
|
133 | auto result = QVector<QCPAbstractPlottable *>{}; | |
|
155 | if (!plottables.empty()) { | |
|
156 | auto plot = plottables.begin()->second->parentPlot(); | |
|
134 | 157 | |
|
135 | if (variable) { | |
|
136 | // Gets the data series of the variable to call the creation of the right components | |
|
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"); | |
|
158 | if (rescaleAxes) { | |
|
159 | plot->rescaleAxes(); | |
|
144 | 160 | } |
|
161 | ||
|
162 | plot->replot(); | |
|
145 | 163 | } |
|
146 | else { | |
|
147 | qCDebug(LOG_VisualizationGraphHelper()) | |
|
148 | << QObject::tr("Can't create graph plottables : the variable is null"); | |
|
149 | 164 | } |
|
165 | }; | |
|
150 | 166 | |
|
151 | return result; | |
|
167 | /** | |
|
168 | * Helper used to create/update plottables | |
|
169 | */ | |
|
170 | struct IPlottablesHelper { | |
|
171 | virtual ~IPlottablesHelper() noexcept = default; | |
|
172 | virtual PlottablesMap create(QCustomPlot &plot) const = 0; | |
|
173 | virtual void update(PlottablesMap &plottables, const SqpRange &range, | |
|
174 | bool rescaleAxes = false) const = 0; | |
|
175 | }; | |
|
176 | ||
|
177 | /** | |
|
178 | * Default implementation of IPlottablesHelper, which takes data series to create/update plottables | |
|
179 | * @tparam T the data series' type | |
|
180 | */ | |
|
181 | template <typename T> | |
|
182 | struct PlottablesHelper : public IPlottablesHelper { | |
|
183 | explicit PlottablesHelper(T &dataSeries) : m_DataSeries{dataSeries} {} | |
|
184 | ||
|
185 | PlottablesMap create(QCustomPlot &plot) const override | |
|
186 | { | |
|
187 | return PlottablesCreator<T>::createPlottables(m_DataSeries, plot); | |
|
152 | 188 | } |
|
153 | 189 | |
|
154 | QVector<QCPAbstractPlottable *> VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |
|
155 | QCustomPlot &plot) noexcept | |
|
190 | void update(PlottablesMap &plottables, const SqpRange &range, bool rescaleAxes) const override | |
|
156 | 191 | { |
|
157 | auto result = QVector<QCPAbstractPlottable *>{}; | |
|
192 | PlottablesUpdater<T>::updatePlottables(m_DataSeries, plottables, range, rescaleAxes); | |
|
193 | } | |
|
158 | 194 | |
|
159 | if (variable) { | |
|
160 | // Gets the data series of the variable to call the creation of the right components | |
|
161 | // according to its type | |
|
162 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(variable->dataSeries())) { | |
|
163 | result.append(createScalarSeriesComponent(scalarSeries, plot, variable->range())); | |
|
195 | T &m_DataSeries; | |
|
196 | }; | |
|
197 | ||
|
198 | /// Creates IPlottablesHelper according to a data series | |
|
199 | std::unique_ptr<IPlottablesHelper> createHelper(IDataSeries *dataSeries) noexcept | |
|
200 | { | |
|
201 | if (auto scalarSeries = dynamic_cast<ScalarSeries *>(dataSeries)) { | |
|
202 | return std::make_unique<PlottablesHelper<ScalarSeries> >(*scalarSeries); | |
|
203 | } | |
|
204 | else if (auto vectorSeries = dynamic_cast<VectorSeries *>(dataSeries)) { | |
|
205 | return std::make_unique<PlottablesHelper<VectorSeries> >(*vectorSeries); | |
|
164 | 206 |
|
|
165 | 207 |
|
|
166 | qCDebug(LOG_VisualizationGraphHelper()) | |
|
167 | << QObject::tr("Can't create graph plottables : unmanaged data series type"); | |
|
208 | return std::make_unique<PlottablesHelper<IDataSeries> >(*dataSeries); | |
|
168 | 209 |
|
|
169 | 210 | } |
|
211 | ||
|
212 | } // namespace | |
|
213 | ||
|
214 | PlottablesMap VisualizationGraphHelper::create(std::shared_ptr<Variable> variable, | |
|
215 | QCustomPlot &plot) noexcept | |
|
216 | { | |
|
217 | if (variable) { | |
|
218 | auto helper = createHelper(variable->dataSeries().get()); | |
|
219 | auto plottables = helper->create(plot); | |
|
220 | return plottables; | |
|
221 | } | |
|
170 | 222 | else { |
|
171 | 223 | qCDebug(LOG_VisualizationGraphHelper()) |
|
172 | 224 | << QObject::tr("Can't create graph plottables : the variable is null"); |
|
225 | return PlottablesMap{}; | |
|
173 | 226 | } |
|
174 | ||
|
175 | return result; | |
|
176 | 227 | } |
|
177 | 228 | |
|
178 |
void VisualizationGraphHelper::updateData( |
|
|
179 | std::shared_ptr<IDataSeries> dataSeries, | |
|
229 | void VisualizationGraphHelper::updateData(PlottablesMap &plottables, IDataSeries *dataSeries, | |
|
180 | 230 | const SqpRange &dateTime) |
|
181 | 231 | { |
|
182 | if (auto scalarSeries = std::dynamic_pointer_cast<ScalarSeries>(dataSeries)) { | |
|
183 | if (plotableVect.size() == 1) { | |
|
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 | } | |
|
232 | auto helper = createHelper(dataSeries); | |
|
233 | helper->update(plottables, dateTime); | |
|
195 | 234 | } |
General Comments 0
You need to be logged in to leave comments.
Login now