qwt_plot_picker.cpp
Go to the documentation of this file.
00001 /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
00002  * Qwt Widget Library
00003  * Copyright (C) 1997   Josef Wilgen
00004  * Copyright (C) 2002   Uwe Rathmann
00005  *
00006  * This library is free software; you can redistribute it and/or
00007  * modify it under the terms of the Qwt License, Version 1.0
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     // attach axes
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     if ( plot() == NULL )
00196         return QwtText();
00197 
00198     return trackerTextF( invTransform( pos ) );
00199 }
00200 
00213 QwtText QwtPlotPicker::trackerTextF( const QPointF &pos ) const
00214 {
00215     QString text;
00216 
00217     switch ( rubberBand() )
00218     {
00219         case HLineRubberBand:
00220             text.sprintf( "%.4f", pos.y() );
00221             break;
00222         case VLineRubberBand:
00223             text.sprintf( "%.4f", pos.x() );
00224             break;
00225         default:
00226             text.sprintf( "%.4f, %.4f", pos.x(), pos.y() );
00227     }
00228     return QwtText( text );
00229 }
00230 
00240 void QwtPlotPicker::append( const QPoint &pos )
00241 {
00242     QwtPicker::append( pos );
00243     Q_EMIT appended( invTransform( pos ) );
00244 }
00245 
00255 void QwtPlotPicker::move( const QPoint &pos )
00256 {
00257     QwtPicker::move( pos );
00258     Q_EMIT moved( invTransform( pos ) );
00259 }
00260 
00269 bool QwtPlotPicker::end( bool ok )
00270 {
00271     ok = QwtPicker::end( ok );
00272     if ( !ok )
00273         return false;
00274 
00275     QwtPlot *plot = QwtPlotPicker::plot();
00276     if ( !plot )
00277         return false;
00278 
00279     const QPolygon points = selection();
00280     if ( points.count() == 0 )
00281         return false;
00282 
00283     QwtPickerMachine::SelectionType selectionType =
00284         QwtPickerMachine::NoSelection;
00285 
00286     if ( stateMachine() )
00287         selectionType = stateMachine()->selectionType();
00288 
00289     switch ( selectionType )
00290     {
00291         case QwtPickerMachine::PointSelection:
00292         {
00293             const QPointF pos = invTransform( points.first() );
00294             Q_EMIT selected( pos );
00295             break;
00296         }
00297         case QwtPickerMachine::RectSelection:
00298         {
00299             if ( points.count() >= 2 )
00300             {
00301                 const QPoint p1 = points.first();
00302                 const QPoint p2 = points.last();
00303 
00304                 const QRect rect = QRect( p1, p2 ).normalized();
00305                 Q_EMIT selected( invTransform( rect ) );
00306             }
00307             break;
00308         }
00309         case QwtPickerMachine::PolygonSelection:
00310         {
00311             QVector<QPointF> dpa( points.count() );
00312             for ( int i = 0; i < points.count(); i++ )
00313                 dpa[i] = invTransform( points[i] );
00314 
00315             Q_EMIT selected( dpa );
00316         }
00317         default:
00318             break;
00319     }
00320 
00321     return true;
00322 }
00323 
00330 QRectF QwtPlotPicker::invTransform( const QRect &rect ) const
00331 {
00332     const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00333     const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00334 
00335     return QwtScaleMap::invTransform( xMap, yMap, rect );
00336 }
00337 
00343 QRect QwtPlotPicker::transform( const QRectF &rect ) const
00344 {
00345     const QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00346     const QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00347 
00348     return QwtScaleMap::transform( xMap, yMap, rect ).toRect();
00349 }
00350 
00356 QPointF QwtPlotPicker::invTransform( const QPoint &pos ) const
00357 {
00358     QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00359     QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00360 
00361     return QPointF(
00362         xMap.invTransform( pos.x() ),
00363         yMap.invTransform( pos.y() )
00364     );
00365 }
00366 
00372 QPoint QwtPlotPicker::transform( const QPointF &pos ) const
00373 {
00374     QwtScaleMap xMap = plot()->canvasMap( d_xAxis );
00375     QwtScaleMap yMap = plot()->canvasMap( d_yAxis );
00376 
00377     const QPointF p( xMap.transform( pos.x() ),
00378         yMap.transform( pos.y() ) );
00379 
00380     return p.toPoint();
00381 }


plotjuggler
Author(s): Davide Faconti
autogenerated on Wed Jul 3 2019 19:28:05