forked from Serial-Studio/Serial-Studio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphProvider.cpp
More file actions
280 lines (237 loc) · 7.35 KB
/
GraphProvider.cpp
File metadata and controls
280 lines (237 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <QTimer>
#include <QXYSeries>
#include <QMetaType>
#include "GraphProvider.h"
#include "DataProvider.h"
#include "CsvPlayer.h"
#include "Group.h"
#include "Dataset.h"
/*
* Only instance of the class
*/
static GraphProvider *INSTANCE = nullptr;
//
// Magic
//
QT_CHARTS_USE_NAMESPACE
Q_DECLARE_METATYPE(QAbstractSeries *)
Q_DECLARE_METATYPE(QAbstractAxis *)
/**
* Sets the maximum displayed points to 10, connects SIGNALS/SLOTS & calls
* QML/Qt magic functions to deal with QML charts from C++.
*/
GraphProvider::GraphProvider()
{
// Start with 10 points
m_prevFramePos = 0;
m_displayedPoints = 10;
// Register data types
qRegisterMetaType<QAbstractSeries *>();
qRegisterMetaType<QAbstractAxis *>();
// Update graph values as soon as QML Bridge interprets data
connect(DataProvider::getInstance(), SIGNAL(updated()), this, SLOT(updateValues()));
// Avoid issues when CSV player goes backwards
connect(CsvPlayer::getInstance(), SIGNAL(timestampChanged()), this,
SLOT(csvPlayerFixes()));
}
/**
* Returns the only instance of the class
*/
GraphProvider *GraphProvider::getInstance()
{
if (!INSTANCE)
INSTANCE = new GraphProvider();
return INSTANCE;
}
/**
* Returns the number of graph data sources
*/
int GraphProvider::graphCount() const
{
return datasets().count();
}
/**
* Returns the number of points that are currently displayed on the graph
*/
int GraphProvider::displayedPoints() const
{
return m_displayedPoints;
}
/**
* Returns a list with the @a Dataset objects that act as data sources for the
* graph views
*/
QList<Dataset *> GraphProvider::datasets() const
{
return m_datasets;
}
/**
* Returns the latest value graphed by the dataset at the given @a index
*/
double GraphProvider::getValue(const int index) const
{
if (index < graphCount() && index >= 0)
return getDataset(index)->value().toDouble();
return 0;
}
/**
* Returns the smallest value registered with the dataset at the given @a index
*/
double GraphProvider::minimumValue(const int index) const
{
double min = INT_MAX;
if (index < m_minimumValues.count() && index >= 0)
min = m_minimumValues.at(index);
if (min != INT_MAX)
return min;
return -1;
}
/**
* Returns the greatest value registered with the dataset at the given @a index
*/
double GraphProvider::maximumValue(const int index) const
{
double max = INT_MIN;
if (index < m_maximumValues.count() && index >= 0)
max = m_maximumValues.at(index);
if (max != INT_MIN)
return max;
return 1;
}
/**
* Returns a pointer to the dataset object at the given @a index
*/
Dataset *GraphProvider::getDataset(const int index) const
{
if (index < graphCount() && index >= 0)
return datasets().at(index);
return Q_NULLPTR;
}
/**
* Changes the maximum number of points that should be displayed in the graph
* views.
*/
void GraphProvider::setDisplayedPoints(const int points)
{
if (points != displayedPoints() && points > 0)
{
m_displayedPoints = points;
m_points.clear();
emit displayedPointsUpdated();
emit dataUpdated();
}
}
/**
* Gets the latest values from the datasets that support/need to be graphed
*/
void GraphProvider::updateValues()
{
// Clear dataset & latest values list
m_datasets.clear();
// Create list with datasets that need to be graphed
for (int i = 0; i < DataProvider::getInstance()->groupCount(); ++i)
{
auto group = DataProvider::getInstance()->getGroup(i);
for (int j = 0; j < group->count(); ++j)
{
auto dataset = group->getDataset(j);
if (dataset->graph())
m_datasets.append(dataset);
}
}
// Create list with dataset values (converted to double)
for (int i = 0; i < graphCount(); ++i)
{
// Register dataset for this graph
if (m_points.count() < (i + 1))
{
auto vector = new QVector<double>;
m_points.append(vector);
}
// Register min. values list
if (m_minimumValues.count() < (i + 1))
m_minimumValues.append(getValue(i));
// Register max. values list
if (m_maximumValues.count() < (i + 1))
m_maximumValues.append(getValue(i));
// Update minimum value
if (minimumValue(i) > getValue(i))
m_minimumValues.replace(i, getValue(i));
// Update minimum value
if (maximumValue(i) < getValue(i))
m_maximumValues.replace(i, getValue(i));
// Remove older items
if (m_points.at(i)->count() >= displayedPoints())
m_points.at(i)->remove(0, m_points.at(i)->count() - displayedPoints());
// Add values
m_points.at(i)->append(getValue(i));
}
// Update graphs
QTimer::singleShot(10, this, SIGNAL(dataUpdated()));
}
/**
* Removes graph points that are ahead of current data frame that is being
* displayed/processed by the CSV Player.
*/
void GraphProvider::csvPlayerFixes()
{
// If current frame comes before last-recorded frame, remove extra data
auto currentFrame = CsvPlayer::getInstance()->framePosition();
if (m_prevFramePos > currentFrame)
{
auto diff = m_prevFramePos - currentFrame;
for (int i = 0; i < graphCount(); ++i)
{
for (int j = 0; j < diff; ++j)
{
if (!m_points.at(i)->isEmpty())
m_points.at(i)->removeLast();
}
}
emit dataUpdated();
}
// Update frame position
m_prevFramePos = currentFrame;
}
/**
* Updates the graph for the given data @a series prorivder, the @a index is
* used to know which dataset object should be used to pull the latest data
* point.
*/
void GraphProvider::updateGraph(QAbstractSeries *series, const int index)
{
// Validation
assert(series != Q_NULLPTR);
// Update data
if (series->isVisible())
{
if (m_points.count() > index && index >= 0)
{
QVector<QPointF> data;
for (int i = 0; i < m_points.at(index)->count(); ++i)
data.append(QPointF(i, m_points.at(index)->at(i)));
static_cast<QXYSeries *>(series)->replace(data);
}
}
}