-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatasource.cpp
74 lines (63 loc) · 1.91 KB
/
datasource.cpp
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
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
#include "datasource.h"
#include <QtCharts/QXYSeries>
#include <QtCharts/QAreaSeries>
#include <QtQuick/QQuickView>
#include <QtQuick/QQuickItem>
#include <QtCore/QDebug>
#include <QtCore/QRandomGenerator>
#include <QtCore/QtMath>
QT_USE_NAMESPACE
Q_DECLARE_METATYPE(QAbstractSeries *)
Q_DECLARE_METATYPE(QAbstractAxis *)
DataSource::DataSource(QQuickView *appViewer, QObject *parent) :
QObject(parent),
m_appViewer(appViewer),
m_index(-1)
{
qRegisterMetaType<QAbstractSeries*>();
qRegisterMetaType<QAbstractAxis*>();
// add the first point to prevent crashing
QList<QPointF> initpoints;
initpoints.reserve(0);
initpoints.append(QPointF(0, 0));
m_data.append(initpoints);
m_data.append(initpoints);
}
void DataSource::update(QAbstractSeries *series)
{
if (series) {
QXYSeries *xySeries = static_cast<QXYSeries *>(series);
m_index++;
if (m_index > m_data.count() - 1)
m_index = 0;
QList<QPointF> points = m_data.at(m_index);
// Use replace instead of clear + append, it's optimized for performance
xySeries->replace(points);
}
}
void DataSource::addVoltage(double voltageValue)
{
static const int xMax = 100;
static int x = -1;
static QList<QPointF> points;
points.reserve(xMax);
x++;
points.append(QPointF(x, voltageValue));
m_data.replace(0, points);
}
void DataSource::addCurrent(double currentValue)
{
static const int xMax = 100;
static int x = -1;
static QList<QPointF> points;
points.reserve(xMax);
x++;
points.append(QPointF(x, currentValue/2)); // only for testing
m_data.replace(1, points);
}
int DataSource::getLastXPos()
{
return (*(m_data.at(0).end()-1)).toPoint().x();
}