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
|
#include <stdlib.h>
#include <qtimer.h>
#include "scrollzoomer.h"
#include "randomplot.h"
const unsigned int c_rangeMax = 1000;
RandomPlot::RandomPlot(QWidget *parent):
IncrementalPlot(parent),
d_curveId(0),
d_timer(0),
d_timerCount(0)
{
setFrameStyle(QFrame::NoFrame);
setLineWidth(0);
setCanvasLineWidth(2);
enableGridX(TRUE);
enableGridY(TRUE);
setGridMajPen(QPen(Qt::gray, 0, Qt::DotLine));
setCanvasBackground(QColor(29, 100, 141)); // nice blue
setAxisScale(xBottom, 0, c_rangeMax);
setAxisScale(yLeft, 0, c_rangeMax);
for ( int i = 0; i < QwtPlot::axisCnt; i++ )
setAxisLabelFormat(i, 'g', 8);
// enable zooming
ScrollZoomer *zoomer = new ScrollZoomer(canvas());
zoomer->setRubberBandPen(QPen(Qt::yellow, 0, Qt::DotLine));
zoomer->setCursorLabelPen(QPen(Qt::yellow));
initCurve();
}
QSize RandomPlot::sizeHint() const
{
return QSize(540,400);
}
void RandomPlot::initCurve()
{
if ( d_curveId > 0 )
{
removeCurveData(d_curveId);
removeCurve(d_curveId);
}
d_curveId = insertCurve("Test Curve");
setCurveStyle(d_curveId, QwtCurve::NoCurve);
const QColor &c = Qt::white;
setCurveSymbol(d_curveId, QwtSymbol(QwtSymbol::XCross,
QBrush(c), QPen(c), QSize(5, 5)) );
}
void RandomPlot::appendPoint()
{
double x = rand() % c_rangeMax;
x += ( rand() % 100 ) / 100;
double y = rand() % c_rangeMax;
y += ( rand() % 100 ) / 100;
appendCurvePoint(d_curveId, x, y);
if ( --d_timerCount <= 0 )
stop();
}
void RandomPlot::append(int timeout, int count)
{
if ( !d_timer )
{
d_timer = new QTimer(this);
connect(d_timer, SIGNAL(timeout()), SLOT(appendPoint()));
}
d_timerCount = count;
emit running(TRUE);
d_timer->start(timeout);
}
void RandomPlot::stop()
{
if ( d_timer )
{
d_timer->stop();
emit running(FALSE);
}
}
void RandomPlot::clear()
{
initCurve();
replot();
}
|