Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #include "qwt_plot_picker.h"
00011 #include "qwt_plot.h"
00012 #include "qwt_scale_div.h"
00013 #include "qwt_painter.h"
00014 #include "qwt_scale_map.h"
00015 #include "qwt_picker_machine.h"
00016
00030 QwtPlotPicker::QwtPlotPicker( QWidget *canvas ):
00031 QwtPicker( canvas ),
00032 d_xAxis( -1 ),
00033 d_yAxis( -1 )
00034 {
00035 if ( !canvas )
00036 return;
00037
00038
00039
00040 int xAxis = QwtPlot::xBottom;
00041
00042 const QwtPlot *plot = QwtPlotPicker::plot();
00043 if ( !plot->axisEnabled( QwtPlot::xBottom ) &&
00044 plot->axisEnabled( QwtPlot::xTop ) )
00045 {
00046 xAxis = QwtPlot::xTop;
00047 }
00048
00049 int yAxis = QwtPlot::yLeft;
00050 if ( !plot->axisEnabled( QwtPlot::yLeft ) &&
00051 plot->axisEnabled( QwtPlot::yRight ) )
00052 {
00053 yAxis = QwtPlot::yRight;
00054 }
00055
00056 setAxis( xAxis, yAxis );
00057 }
00058
00068 QwtPlotPicker::QwtPlotPicker( int xAxis, int yAxis, QWidget *canvas ):
00069 QwtPicker( canvas ),
00070 d_xAxis( xAxis ),
00071 d_yAxis( yAxis )
00072 {
00073 }
00074
00089 QwtPlotPicker::QwtPlotPicker( int xAxis, int yAxis,
00090 RubberBand rubberBand, DisplayMode trackerMode,
00091 QWidget *canvas ):
00092 QwtPicker( rubberBand, trackerMode, canvas ),
00093 d_xAxis( xAxis ),
00094 d_yAxis( yAxis )
00095 {
00096 }
00097
00099 QwtPlotPicker::~QwtPlotPicker()
00100 {
00101 }
00102
00104 QWidget *QwtPlotPicker::canvas()
00105 {
00106 return parentWidget();
00107 }
00108
00110 const QWidget *QwtPlotPicker::canvas() const
00111 {
00112 return parentWidget();
00113 }
00114
00116 QwtPlot *QwtPlotPicker::plot()
00117 {
00118 QWidget *w = canvas();
00119 if ( w )
00120 w = w->parentWidget();
00121
00122 return qobject_cast<QwtPlot *>( w );
00123 }
00124
00126 const QwtPlot *QwtPlotPicker::plot() const
00127 {
00128 const QWidget *w = canvas();
00129 if ( w )
00130 w = w->parentWidget();
00131
00132 return qobject_cast<const QwtPlot *>( w );
00133 }
00134
00139 QRectF QwtPlotPicker::scaleRect() const
00140 {
00141 QRectF rect;
00142
00143 if ( plot() )
00144 {
00145 const QwtScaleDiv &xs = plot()->axisScaleDiv( xAxis() );
00146 const QwtScaleDiv &ys = plot()->axisScaleDiv( yAxis() );
00147
00148 rect = QRectF( xs.lowerBound(), ys.lowerBound(),
00149 xs.range(), ys.range() );
00150 rect = rect.normalized();
00151 }
00152
00153 return rect;
00154 }
00155
00162 void QwtPlotPicker::setAxis( int xAxis, int yAxis )
00163 {
00164 const QwtPlot *plt = plot();
00165 if ( !plt )
00166 return;
00167
00168 if ( xAxis != d_xAxis || yAxis != d_yAxis )
00169 {
00170 d_xAxis = xAxis;
00171 d_yAxis = yAxis;
00172 }
00173 }
00174
00176 int QwtPlotPicker::xAxis() const
00177 {
00178 return d_xAxis;
00179 }
00180
00182 int QwtPlotPicker::yAxis() const
00183 {
00184 return d_yAxis;
00185 }
00186
00193 QwtText QwtPlotPicker::trackerText( const QPoint &pos ) const
00194 {
00195 return trackerTextF( invTransform( pos ) );
00196 }
00197
00210 QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
00211 {
00212 QString text;
00213
00214 switch ( rubberBand() )
00215 {
00216 case HLineRubberBand:
00217 text.sprintf( "%.4f", pos.y() );
00218 break;
00219 case VLineRubberBand:
00220 text.sprintf( "%.4f", pos.x() );
00221 break;
00222 default:
00223 text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
00224 }
00225 return QwtText( text );
00226 }
00227
00237 void QwtPlotPicker::append( const QPoint &pos )
00238 {
00239 QwtPicker::append( pos );
00240 Q_EMIT appended( invTransform( pos ) );
00241 }
00242
00252 void QwtPlotPicker::move( const QPoint &pos )
00253 {
00254 QwtPicker::move( pos );
00255 Q_EMIT moved( invTransform( pos ) );
00256 }
00257
00266 bool QwtPlotPicker::end( bool ok )
00267 {
00268 ok = QwtPicker::end( ok );
00269 if ( !ok )
00270 return false;
00271
00272 QwtPlot *plot = QwtPlotPicker::plot();
00273 if ( !plot )
00274 return false;
00275
00276 const QPolygon points = selection();
00277 if ( points.count() == 0 )
00278 return false;
00279
00280 QwtPickerMachine::SelectionType selectionType =
00281 QwtPickerMachine::NoSelection;
00282
00283 if ( stateMachine() )
00284 selectionType = stateMachine()->selectionType();
00285
00286 switch ( selectionType )
00287 {
00288 case QwtPickerMachine::PointSelection:
00289 {
00290 const QPointF pos = invTransform( points.first() );
00291 Q_EMIT selected( pos );
00292 break;
00293 }
00294 case QwtPickerMachine::RectSelection:
00295 {
00296 if ( points.count() >= 2 )
00297 {
00298 const QPoint p1 = points.first();
00299 const QPoint p2 = points.last();
00300
00301 const QRect rect = QRect( p1, p2 ).normalized();
00302 Q_EMIT selected( invTransform( rect ) );
00303 }
00304 break;
00305 }
00306 case QwtPickerMachine::PolygonSelection:
00307 {
00308 QVector<QPointF> dpa( points.count() );
00309 for ( int i = 0; i < points.count(); i++ )
00310 dpa[i] = invTransform( points[i] );
00311
00312 Q_EMIT selected( dpa );
00313 }
00314 default:
00315 break;
00316 }
00317
00318 return true;
00319 }
00320
00327 QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
00328 {
00329 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00330 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00331
00332 return QwtScaleMap::invTransform( xMap, yMap, rect );
00333 }
00334
00340 QRect QwtPlotPicker::transform( const QRectF &rect ) const
00341 {
00342 const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00343 const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00344
00345 return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
00346 }
00347
00353 QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
00354 {
00355 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00356 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00357
00358 return QPointF(
00359 xMap.invTransform( pos.x() ),
00360 yMap.invTransform( pos.y() )
00361 );
00362 }
00363
00369 QPoint QwtPlotPicker::transform( const QPointF &pos ) const
00370 {
00371 QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00372 QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00373
00374 const QPointF p( xMap.transform( pos.x() ),
00375 yMap.transform( pos.y() ) );
00376
00377 return p.toPoint();
00378 }