2
#include <qwt_painter.h>
3
#include <qwt_plot_canvas.h>
4
#include <qwt_plot_marker.h>
5
#include <qwt_plot_curve.h>
6
#include <qwt_scale_widget.h>
7
#include <qwt_legend.h>
8
#include <qwt_scale_draw.h>
10
#include "data_plot.h"
13
// Initialize main window
15
DataPlot::DataPlot(QWidget *parent):
20
// Disable polygon clipping
21
QwtPainter::setDeviceClipping(false);
23
// We don't need the cache here
24
canvas()->setPaintAttribute(QwtPlotCanvas::PaintCached, false);
25
canvas()->setPaintAttribute(QwtPlotCanvas::PaintPacked, false);
27
#if QT_VERSION >= 0x040000
30
Qt::WA_PaintOnScreen is only supported for X11, but leads
31
to substantial bugs with Qt 4.2.x/Windows
33
canvas()->setAttribute(Qt::WA_PaintOnScreen, true);
40
for (int i = 0; i< PLOT_SIZE; i++)
42
d_x[i] = 0.5 * i; // time axis
48
setTitle("A Test for High Refresh Rates");
49
insertLegend(new QwtLegend(), QwtPlot::BottomLegend);
52
QwtPlotCurve *cRight = new QwtPlotCurve("Data Moving Right");
55
QwtPlotCurve *cLeft = new QwtPlotCurve("Data Moving Left");
59
cRight->setPen(QPen(Qt::red));
60
cLeft->setPen(QPen(Qt::blue));
62
// Attach (don't copy) data. Both curves use the same x array.
63
cRight->setRawData(d_x, d_y, PLOT_SIZE);
64
cLeft->setRawData(d_x, d_z, PLOT_SIZE);
67
// Insert zero line at y = 0
68
QwtPlotMarker *mY = new QwtPlotMarker();
69
mY->setLabelAlignment(Qt::AlignRight|Qt::AlignTop);
70
mY->setLineStyle(QwtPlotMarker::HLine);
76
setAxisTitle(QwtPlot::xBottom, "Time/seconds");
77
setAxisScale(QwtPlot::xBottom, 0, 100);
79
setAxisTitle(QwtPlot::yLeft, "Values");
80
setAxisScale(QwtPlot::yLeft, -1.5, 1.5);
82
setTimerInterval(0.0);
86
// Set a plain canvas frame and align the scales to it
88
void DataPlot::alignScales()
90
// The code below shows how to align the scales to
91
// the canvas frame, but is also a good example demonstrating
92
// why the spreaded API needs polishing.
94
canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
95
canvas()->setLineWidth(1);
97
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
99
QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
101
scaleWidget->setMargin(0);
103
QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
105
scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
109
void DataPlot::setTimerInterval(double ms)
111
d_interval = qRound(ms);
113
if ( d_timerId >= 0 )
115
killTimer(d_timerId);
118
if (d_interval >= 0 )
119
d_timerId = startTimer(d_interval);
122
// Generate new values
123
void DataPlot::timerEvent(QTimerEvent *)
125
static double phase = 0.0;
127
if (phase > (M_PI - 0.0001))
130
// y moves from left to right:
131
// Shift y array right and assign new value to y[0].
133
for ( int i = PLOT_SIZE - 1; i > 0; i-- )
135
d_y[0] = sin(phase) * (-1.0 + 2.0 * double(rand()) / double(RAND_MAX));
137
for ( int j = 0; j < PLOT_SIZE - 1; j++ )
140
d_z[PLOT_SIZE - 1] = 0.8 - (2.0 * phase/M_PI) + 0.4 *
141
double(rand()) / double(RAND_MAX);
143
// update the display
146
phase += M_PI * 0.02;