1 #include "plot.h"
2 #include <qwt_plot_magnifier.h>
3 #include <qwt_plot_panner.h>
4 #include <qwt_plot_picker.h>
5 #include <qwt_picker_machine.h>
6 #include <qwt_plot_curve.h>
7 
8 class DistancePicker: public QwtPlotPicker
9 {
10 public:
DistancePicker(QWidget * canvas)11     DistancePicker( QWidget *canvas ):
12         QwtPlotPicker( canvas )
13     {
14         setTrackerMode( QwtPicker::ActiveOnly );
15         setStateMachine( new QwtPickerDragLineMachine() );
16         setRubberBand( QwtPlotPicker::PolygonRubberBand );
17     }
18 
trackerTextF(const QPointF & pos) const19     virtual QwtText trackerTextF( const QPointF &pos ) const
20     {
21         QwtText text;
22 
23         const QPolygon points = selection();
24         if ( !points.isEmpty() )
25         {
26             QString num;
27             num.setNum( QLineF( pos, invTransform( points[0] ) ).length() );
28 
29             QColor bg( Qt::white );
30             bg.setAlpha( 200 );
31 
32             text.setBackgroundBrush( QBrush( bg ) );
33             text.setText( num );
34         }
35         return text;
36     }
37 };
38 
Plot(QWidget * parent)39 Plot::Plot( QWidget *parent ):
40     QwtPlot( parent ),
41     d_curve( NULL )
42 {
43     canvas()->setStyleSheet(
44         "border: 2px solid Black;"
45         "border-radius: 15px;"
46         "background-color: qlineargradient( x1: 0, y1: 0, x2: 0, y2: 1,"
47             "stop: 0 LemonChiffon, stop: 1 PaleGoldenrod );"
48     );
49 
50     // attach curve
51     d_curve = new QwtPlotCurve( "Scattered Points" );
52     d_curve->setPen( QColor( "Purple" ) );
53 
54     // when using QwtPlotCurve::ImageBuffer simple dots can be
55     // rendered in parallel on multicore systems.
56     d_curve->setRenderThreadCount( 0 ); // 0: use QThread::idealThreadCount()
57 
58     d_curve->attach( this );
59 
60     setSymbol( NULL );
61 
62     // panning with the left mouse button
63     (void )new QwtPlotPanner( canvas() );
64 
65     // zoom in/out with the wheel
66     QwtPlotMagnifier *magnifier = new QwtPlotMagnifier( canvas() );
67     magnifier->setMouseButton( Qt::NoButton );
68 
69     // distanve measurement with the right mouse button
70     DistancePicker *picker = new DistancePicker( canvas() );
71     picker->setMousePattern( QwtPlotPicker::MouseSelect1, Qt::RightButton );
72     picker->setRubberBandPen( QPen( Qt::blue ) );
73 }
74 
setSymbol(QwtSymbol * symbol)75 void Plot::setSymbol( QwtSymbol *symbol )
76 {
77     d_curve->setSymbol( symbol );
78 
79     if ( symbol == NULL )
80     {
81         d_curve->setStyle( QwtPlotCurve::Dots );
82     }
83 }
84 
setSamples(const QVector<QPointF> & samples)85 void Plot::setSamples( const QVector<QPointF> &samples )
86 {
87     d_curve->setPaintAttribute(
88         QwtPlotCurve::ImageBuffer, samples.size() > 1000 );
89 
90     d_curve->setSamples( samples );
91 }
92