plot.cpp
4.05 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
#include <qglobal.h>
#include <qwt_painter.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_curve.h>
#include <qwt_plot_layout.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
#include "plot.h"
#include "circularbuffer.h"
#include "settings.h"
static double wave(double x)
{
const double period = 1.0;
const double c = 5.0;
double v = ::fmod(x, period);
const double amplitude = qAbs(x - qRound(x / c) * c) / ( 0.5 * c );
v = amplitude * qSin(v / period * 2 * M_PI);
return v;
}
static double noise(double)
{
return 2.0 * ( qrand() / ((double)RAND_MAX + 1) ) - 1.0;
}
Plot::Plot(QWidget *parent):
QwtPlot(parent),
d_interval(10.0), // seconds
d_timerId(-1)
{
// Assign a title
setTitle("Testing Refresh Rates");
setCanvasBackground(Qt::white);
alignScales();
// Insert grid
d_grid = new QwtPlotGrid();
d_grid->attach(this);
// Insert curve
d_curve = new QwtPlotCurve("Data Moving Right");
d_curve->setPen(QPen(Qt::black));
d_curve->setData(new CircularBuffer(d_interval, 10));
d_curve->attach(this);
// Axis
setAxisTitle(QwtPlot::xBottom, "Seconds");
setAxisScale(QwtPlot::xBottom, -d_interval, 0.0);
setAxisTitle(QwtPlot::yLeft, "Values");
setAxisScale(QwtPlot::yLeft, -1.0, 1.0);
d_clock.start();
setSettings(d_settings);
}
//
// Set a plain canvas frame and align the scales to it
//
void Plot::alignScales()
{
// The code below shows how to align the scales to
// the canvas frame, but is also a good example demonstrating
// why the spreaded API needs polishing.
canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
canvas()->setLineWidth(1);
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
{
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
if ( scaleWidget )
scaleWidget->setMargin(0);
QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
if ( scaleDraw )
scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
}
plotLayout()->setAlignCanvasToScales(true);
}
void Plot::setSettings(const Settings &s)
{
if ( d_timerId >= 0 )
killTimer(d_timerId);
d_timerId = startTimer(s.updateInterval);
d_grid->setPen(s.grid.pen);
d_grid->setVisible(s.grid.pen.style() != Qt::NoPen);
CircularBuffer *buffer = (CircularBuffer *)d_curve->data();
if ( s.curve.numPoints != buffer->size() ||
s.curve.functionType != d_settings.curve.functionType )
{
switch(s.curve.functionType)
{
case Settings::Wave:
buffer->setFunction(wave);
break;
case Settings::Noise:
buffer->setFunction(noise);
break;
default:
buffer->setFunction(NULL);
}
buffer->fill(d_interval, s.curve.numPoints);
}
d_curve->setPen(s.curve.pen);
d_curve->setBrush(s.curve.brush);
d_curve->setPaintAttribute(QwtPlotCurve::ClipPolygons,
s.curve.paintAttributes & QwtPlotCurve::ClipPolygons);
d_curve->setRenderHint(QwtPlotCurve::RenderAntialiased,
s.curve.renderHint & QwtPlotCurve::RenderAntialiased);
canvas()->setAttribute(Qt::WA_PaintOnScreen, s.canvas.paintOnScreen);
canvas()->setPaintAttribute(
QwtPlotCanvas::BackingStore, s.canvas.useBackingStore);
canvas()->setPaintAttribute(
QwtPlotCanvas::ImmediatePaint, s.canvas.immediatePaint);
QwtPainter::setPolylineSplitting(s.curve.lineSplitting);
d_settings = s;
}
void Plot::timerEvent(QTimerEvent *)
{
CircularBuffer *buffer = (CircularBuffer *)d_curve->data();
buffer->setReferenceTime(d_clock.elapsed() / 1000.0);
switch(d_settings.updateType)
{
case Settings::RepaintCanvas:
{
// the axes in this example doesn't change. So all we need to do
// is to repaint the canvas.
canvas()->replot();
break;
}
default:
{
replot();
}
}
}