-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlinegraph.cpp
More file actions
298 lines (248 loc) · 8.12 KB
/
Copy pathlinegraph.cpp
File metadata and controls
298 lines (248 loc) · 8.12 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "linegraph.h"
#include "linenode_p.h"
#include <QDateTime>
#include <QtQml/qqmlengine.h>
LineGraph::LineGraph()
{
setFlag(ItemHasContents, true);
setAntialiasing(true);
}
void LineGraph::registerMetaType()
{
qmlRegisterType<LineGraph>("LineGraph", 1, 0, "LineGraph");
}
void LineGraph::appendSample(qreal value)
{
m_model->appendSampleMs(value);
}
void LineGraph::removeFirstSample()
{
m_model->removeFirstSample();
}
void LineGraph::setLineWidth(qreal lineWidth)
{
if (m_lineWidth == lineWidth)
return;
m_lineWidth = lineWidth;
m_propertiesChanged = true;
emit lineWidthChanged();
update();
}
void LineGraph::setColor(QColor color)
{
if (m_color == color)
return;
m_color = color;
m_propertiesChanged = true;
emit colorChanged();
update();
}
void LineGraph::setFillColorBelow(QColor fillColorBelow)
{
if (m_fillColorBelow == fillColorBelow)
return;
m_fillColorBelow = fillColorBelow;
m_propertiesChanged = true;
emit fillColorBelowChanged();
update();
}
void LineGraph::setFillColorAbove(QColor fillColorAbove)
{
if (m_fillColorAbove == fillColorAbove)
return;
m_fillColorAbove = fillColorAbove;
m_propertiesChanged = true;
emit fillColorAboveChanged();
update();
}
void LineGraph::setWarningMinColor(QColor warningMinColor)
{
if (m_warningMinColor == warningMinColor)
return;
m_warningMinColor = warningMinColor;
m_propertiesChanged = true;
emit warningMinColorChanged();
update();
}
void LineGraph::setWarningMaxColor(QColor warningMaxColor)
{
if (m_warningMaxColor == warningMaxColor)
return;
m_warningMaxColor = warningMaxColor;
m_propertiesChanged = true;
emit warningMaxColorChanged();
update();
}
void LineGraph::setMinValue(qreal minValue)
{
if (m_model)
m_model->setMinValue(minValue);
m_geometryChanged = true;
m_propertiesChanged = true;
emit minValueChanged();
update();
}
void LineGraph::setMaxValue(qreal maxValue)
{
if (m_model)
m_model->setMaxValue(maxValue);
m_geometryChanged = true;
m_propertiesChanged = true;
emit maxValueChanged();
update();
}
void LineGraph::setTimeSpan(qreal timeSpan)
{
if (m_timeSpan == timeSpan)
return;
m_timeSpan = timeSpan;
m_geometryChanged = true;
emit timeSpanChanged();
if (m_model)
m_model->setDownsampleInterval(timeSpan / width());
update();
}
qreal LineGraph::xAtTime(qreal time)
{
return width() - (m_model->maxSampleTime() - time) / m_timeSpan * width();
}
QJSValue LineGraph::sampleNearestX(qreal x)
{
static QJSValue nullJS(QJSValue::NullValue);
QJSEngine *engine = qmlEngine(this);
qreal time = m_model->maxSampleTime() - m_timeSpan + x / width() * m_timeSpan;
LineNode::LineVertex v = m_model->sampleNearest(time);
if (qIsNaN(v.y))
return nullJS;
qreal vscale = height() / (m_model->maxValue() - m_model->minValue());
QJSValue ret = engine->newObject();
ret.setProperty(QLatin1String("time"), v.x); // TODO add m_timeOffset
ret.setProperty(QLatin1String("value"), v.y);
ret.setProperty(QLatin1String("x"), xAtTime(v.x));
ret.setProperty(QLatin1String("y"), height() - vscale * (v.y - m_model->minValue()));
return ret;
}
void LineGraph::setModel(LineGraphModel *model)
{
if (m_model == model)
return;
if (m_model) {
disconnect(m_model, &LineGraphModel::samplesChanged, this, &LineGraph::updateVertices);
disconnect(m_model, &LineGraphModel::minValueChanged, this, &LineGraph::minValueChanged);
disconnect(m_model, &LineGraphModel::maxValueChanged, this, &LineGraph::maxValueChanged);
}
m_model = model;
connect(model, &LineGraphModel::samplesChanged, this, &LineGraph::updateVertices);
connect(model, &LineGraphModel::minValueChanged, this, &LineGraph::minValueChanged);
connect(model, &LineGraphModel::maxValueChanged, this, &LineGraph::maxValueChanged);
if (m_model)
m_model->setDownsampleInterval(m_timeSpan / width());
emit modelChanged();
updateVertices();
}
void LineGraph::setWireframe(bool wireframe)
{
if (m_wireframe == wireframe)
return;
m_wireframe = wireframe;
m_propertiesChanged = true;
emit wireframeChanged();
}
/*!
\qmlproperty enumeration LineGraph::joinStyle
How line segments are joined at each datapoint, and how the ends are capped.
This is a \c Qt::PenJoinStyle, but only two values are meaningfully distinct:
\value Qt.RoundJoin (the default) Each segment is drawn as a capsule with round
caps, and neighbouring capsules overlap into a round join. Joins stay full
width at any angle, so sharp spikes never pinch, and there is no miter limit.
Caveat: because the capsules overlap, a \e thick, \e translucent stroke blends
twice where they meet and shows a slightly denser blob at each datapoint. This
is invisible for opaque or thin strokes (the common case).
\value Qt.BevelJoin The line is drawn as a single, non-overlapping ribbon, so a
translucent stroke blends exactly once everywhere - no blobs. The trade-off is
that joins are beveled rather than truly round, so a very sharp spike can look
slightly clipped or thinned at its tip. Cheapest to render (no per-segment
overlap or extra pass).
Other \c Qt::PenJoinStyle values (\c MiterJoin, \c SvgMiterJoin) are treated as
\c BevelJoin.
Both styles are antialiased and stay a consistent width across a wide range of
line widths.
*/
void LineGraph::setJoinStyle(Qt::PenJoinStyle joinStyle)
{
if (m_joinStyle == joinStyle)
return;
m_joinStyle = joinStyle;
m_propertiesChanged = true;
m_geometryChanged = true; // the geometry layout (indexed quads vs ribbon) depends on it
emit joinStyleChanged();
update();
}
void LineGraph::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
{
m_geometryChanged = true;
if (m_model)
m_model->setDownsampleInterval(m_timeSpan / width());
update();
QQuickItem::geometryChange(newGeometry, oldGeometry);
}
void LineGraph::updateVertices()
{
m_samplesChanged = true;
update();
}
class GraphNode : public QSGNode
{
public:
LineNode *line;
};
QSGNode *LineGraph::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
if (!m_model)
return oldNode;
GraphNode *n = static_cast<GraphNode *>(oldNode);
QRectF rect = boundingRect();
if (rect.isEmpty()) {
delete n;
return nullptr;
}
if (!n) {
n = new GraphNode();
n->line = new LineNode();
n->appendChildNode(n->line);
}
if (m_propertiesChanged) {
float fillDirection = 0;
QColor fillColor = m_color;
if (m_fillColorAbove != Qt::transparent) {
fillDirection = -1;
fillColor = m_fillColorAbove;
}
if (m_fillColorBelow != Qt::transparent) {
fillDirection = 1;
fillColor = m_fillColorBelow;
}
n->line->setHeight(height());
n->line->setLineWidth(m_lineWidth);
n->line->setColor(fillColor);
n->line->setWarningMinColor(m_warningMinColor);
n->line->setWarningMaxColor(m_warningMaxColor);
n->line->setWarningMinValue(m_model->normalMinValue());
n->line->setWarningMaxValue(m_model->normalMaxValue());
n->line->setFillDirection(fillDirection);
n->line->setMinValue(m_model->minValue());
n->line->setMaxValue(m_model->maxValue());
n->line->setWireframe(m_wireframe);
n->line->setJoinStyle(m_joinStyle);
}
// aa: 1 = antialiased stroke, 0 = hard-edged stroke, -1 = wireframe (solid fill of the
// whole expanded mesh so the triangles are visible)
n->line->setSpread(m_wireframe ? -1 : (antialiasing() ? 1 : 0));
if ((m_geometryChanged || m_samplesChanged) && !m_model->vertices()->isEmpty())
n->line->updateGeometry(rect, m_model->vertices(), width() / m_timeSpan);
m_geometryChanged = false;
m_samplesChanged = false;
m_propertiesChanged = false;
emit samplesChanged();
return n;
}