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
|
#include <qglobal.h>
#include <qtimer.h>
#include <qwt_plot_grid.h>
#include <qwt_plot_canvas.h>
#include <qwt_plot_layout.h>
#include <qwt_scale_widget.h>
#include <qwt_scale_draw.h>
#include "scrollzoomer.h"
#include "randomplot.h"
const unsigned int c_rangeMax = 1000;
class Zoomer: public ScrollZoomer
{
public:
Zoomer( QWidget *canvas ):
ScrollZoomer( canvas )
{
#if 0
setRubberBandPen( QPen( Qt::red, 2, Qt::DotLine ) );
#else
setRubberBandPen( QPen( Qt::red ) );
#endif
}
virtual QwtText trackerTextF( const QPointF &pos ) const
{
QColor bg( Qt::white );
QwtText text = QwtPlotZoomer::trackerTextF( pos );
text.setBackgroundBrush( QBrush( bg ) );
return text;
}
virtual void rescale()
{
QwtScaleWidget *scaleWidget = plot()->axisWidget( yAxis() );
QwtScaleDraw *sd = scaleWidget->scaleDraw();
double minExtent = 0.0;
if ( zoomRectIndex() > 0 )
{
// When scrolling in vertical direction
// the plot is jumping in horizontal direction
// because of the different widths of the labels
// So we better use a fixed extent.
minExtent = sd->spacing() + sd->maxTickLength() + 1;
minExtent += sd->labelSize(
scaleWidget->font(), c_rangeMax ).width();
}
sd->setMinimumExtent( minExtent );
ScrollZoomer::rescale();
}
};
RandomPlot::RandomPlot( QWidget *parent ):
IncrementalPlot( parent ),
d_timer( 0 ),
d_timerCount( 0 )
{
setFrameStyle( QFrame::NoFrame );
setLineWidth( 0 );
plotLayout()->setAlignCanvasToScales( true );
QwtPlotGrid *grid = new QwtPlotGrid;
grid->setMajorPen( Qt::gray, 0, Qt::DotLine );
grid->attach( this );
setCanvasBackground( QColor( 29, 100, 141 ) ); // nice blue
setAxisScale( xBottom, 0, c_rangeMax );
setAxisScale( yLeft, 0, c_rangeMax );
replot();
// enable zooming
( void ) new Zoomer( canvas() );
}
QSize RandomPlot::sizeHint() const
{
return QSize( 540, 400 );
}
void RandomPlot::appendPoint()
{
double x = qrand() % c_rangeMax;
x += ( qrand() % 100 ) / 100;
double y = qrand() % c_rangeMax;
y += ( qrand() % 100 ) / 100;
IncrementalPlot::appendPoint( QPointF( 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;
Q_EMIT running( true );
d_timeStamp.start();
QwtPlotCanvas *plotCanvas = qobject_cast<QwtPlotCanvas *>( canvas() );
plotCanvas->setPaintAttribute( QwtPlotCanvas::BackingStore, false );
d_timer->start( timeout );
}
void RandomPlot::stop()
{
Q_EMIT elapsed( d_timeStamp.elapsed() );
if ( d_timer )
{
d_timer->stop();
Q_EMIT running( false );
}
QwtPlotCanvas *plotCanvas = qobject_cast<QwtPlotCanvas *>( canvas() );
plotCanvas->setPaintAttribute( QwtPlotCanvas::BackingStore, true );
}
void RandomPlot::clear()
{
clearPoints();
replot();
}
|