/
linegraph.cpp
175 lines (141 loc) · 3.84 KB
/
linegraph.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
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
#include "linegraph.h"
#include "figure.h"
LineGraph::LineGraph()
{
setDataSource(&m_defaultDataSource);
}
LineGraph::~LineGraph()
{
}
bool LineGraph::isInValueRange(const QPointF &point, const float &xMin, const float &xMax, const float &yMin, const float &yMax) {
return point.x()>=xMin && point.x() <= xMax && point.y() >= yMin && point.y() <= yMax;
}
void LineGraph::paint(Figure *figure, QPainter *painter)
{
painter->save();
QPen pen(m_style);
pen.setColor(m_color);
pen.setCapStyle(Qt::RoundCap);
pen.setWidth(m_width);
painter->setPen(pen);
QVector<QPointF> scaledPoints;
float rangeX = figure->xMax()-figure->xMin();
float rangeY = figure->yMax()-figure->yMin();
float xMin = figure->xMin();
float yMin = figure->yMin();
float xMax = figure->xMax();
float yMax = figure->yMax();
for(int i=0; i< m_dataSource->size()-1; i++) {
const QPointF &p1 = m_dataSource->get(i);
const QPointF &p2 = m_dataSource->get(i+1);
if(!isInValueRange(p1, xMin, xMax, yMin, yMax) || !isInValueRange(p2, xMin, xMax, yMin, yMax)) {
continue;
}
float x1 = (p1.x()-xMin)/rangeX;
float x2 = (p2.x()-xMin)/rangeX;
float y1 = 1.0 - (p1.y()-yMin)/rangeY; // y is flipped
float y2 = 1.0 - (p2.y()-yMin)/rangeY;
scaledPoints.push_back(figure->scaled(QPointF(x1,y1), figure->figureRectangle(), figure->figureRectangle().topLeft()));
scaledPoints.push_back(figure->scaled(QPointF(x2,y2), figure->figureRectangle(), figure->figureRectangle().topLeft()));
}
painter->drawLines(scaledPoints);
scaledPoints.clear();
setDirty(false);
painter->restore();
}
LineGraphDataSource *LineGraph::dataSource() const
{
return m_dataSource;
}
QColor LineGraph::color() const
{
return m_color;
}
Qt::PenStyle LineGraph::style() const
{
return m_style;
}
int LineGraph::width() const
{
return m_width;
}
void LineGraphDataSource::cleanupMemory() {
m_points.erase(m_points.begin(), m_points.begin()+m_firstIndex-1);
m_firstIndex = 0;
m_numberOfPoints = m_points.size();
}
void LineGraph::addPoint(float x, float y)
{
m_dataSource->addPoint(x,y);
}
void LineGraph::setDataSource(LineGraphDataSource *dataSource)
{
if (m_dataSource == dataSource)
return;
if(m_dataSource) {
m_dataSource->disconnect(this);
}
m_dataSource = dataSource;
connect(m_dataSource, SIGNAL(dataChanged()), this, SLOT(markDirty()));
emit dataSourceChanged(dataSource);
}
void LineGraph::setColor(QColor color)
{
if (m_color == color)
return;
m_color = color;
emit colorChanged(color);
}
void LineGraph::setStyle(Qt::PenStyle style)
{
if (m_style == style)
return;
m_style = style;
emit styleChanged(style);
}
void LineGraph::setWidth(int width)
{
if (m_width == width)
return;
m_width = width;
emit widthChanged(width);
}
void LineGraphDataSource::addPoint(float x, float y)
{
m_points.push_back(QPointF(x,y));
if(m_saveMemory) {
if(++m_numberOfPoints > m_maxNumberOfPoints) {
m_numberOfPoints = m_maxNumberOfPoints;
m_firstIndex++;
}
if(m_points.size() > 2*m_maxNumberOfPoints) {
cleanupMemory();
}
}
emit dataChanged();
}
int LineGraphDataSource::size() {
if(m_saveMemory) return m_numberOfPoints;
else return m_points.size();
}
bool LineGraphDataSource::saveMemory() const
{
return m_saveMemory;
}
QVector<QPointF> &LineGraphDataSource::points()
{
return m_points;
}
void LineGraphDataSource::setSaveMemory(bool saveMemory)
{
if (m_saveMemory == saveMemory)
return;
m_saveMemory = saveMemory;
emit saveMemoryChanged(saveMemory);
}
void LineGraphDataSource::reset()
{
m_points.clear();
m_numberOfPoints = 0;
m_firstIndex = 0;
}