qwt_plot_picker.cpp

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 // vim: expandtab
00011 
00012 #include "qwt_plot.h"
00013 #include "qwt_double_rect.h"
00014 #include "qwt_scale_div.h"
00015 #include "qwt_painter.h"
00016 #include "qwt_scale_map.h"
00017 #include "qwt_plot_picker.h"
00018 
00032 QwtPlotPicker::QwtPlotPicker(QwtPlotCanvas *canvas):
00033     QwtPicker(canvas),
00034     d_xAxis(-1),
00035     d_yAxis(-1)
00036 {
00037     if ( !canvas )
00038         return;
00039 
00040     // attach axes
00041 
00042     int xAxis = QwtPlot::xBottom;
00043 
00044     const QwtPlot *plot = QwtPlotPicker::plot();
00045     if ( !plot->axisEnabled(QwtPlot::xBottom) &&
00046         plot->axisEnabled(QwtPlot::xTop) )
00047     {
00048         xAxis = QwtPlot::xTop;
00049     }
00050 
00051     int yAxis = QwtPlot::yLeft;
00052     if ( !plot->axisEnabled(QwtPlot::yLeft) &&
00053         plot->axisEnabled(QwtPlot::yRight) )
00054     {
00055         yAxis = QwtPlot::yRight;
00056     }
00057 
00058     setAxis(xAxis, yAxis);
00059 }
00060 
00070 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, QwtPlotCanvas *canvas):
00071     QwtPicker(canvas),
00072     d_xAxis(xAxis),
00073     d_yAxis(yAxis)
00074 {
00075 }
00076 
00093 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, int selectionFlags,
00094         RubberBand rubberBand, DisplayMode trackerMode,
00095         QwtPlotCanvas *canvas):
00096     QwtPicker(selectionFlags, rubberBand, trackerMode, canvas),
00097     d_xAxis(xAxis),
00098     d_yAxis(yAxis)
00099 {
00100 }
00101 
00103 QwtPlotCanvas *QwtPlotPicker::canvas()
00104 {
00105     QWidget *w = parentWidget();
00106     if ( w && w->inherits("QwtPlotCanvas") )
00107         return (QwtPlotCanvas *)w;
00108 
00109     return NULL;
00110 }
00111 
00113 const QwtPlotCanvas *QwtPlotPicker::canvas() const
00114 {
00115     return ((QwtPlotPicker *)this)->canvas();
00116 }
00117 
00119 QwtPlot *QwtPlotPicker::plot()
00120 {
00121     QObject *w = canvas();
00122     if ( w )
00123     {
00124         w = w->parent();
00125         if ( w && w->inherits("QwtPlot") )
00126             return (QwtPlot *)w;
00127     }
00128 
00129     return NULL;
00130 }
00131 
00133 const QwtPlot *QwtPlotPicker::plot() const
00134 {
00135     return ((QwtPlotPicker *)this)->plot();
00136 }
00137 
00143 QwtDoubleRect QwtPlotPicker::scaleRect() const
00144 {
00145     const QwtScaleDiv *xs = plot()->axisScaleDiv(xAxis());
00146     const QwtScaleDiv *ys = plot()->axisScaleDiv(yAxis());
00147 
00148     const QwtDoubleRect rect( xs->lBound(), ys->lBound(), 
00149         xs->range(), ys->range() );
00150 
00151     return rect.normalized();
00152 }
00153 
00160 void QwtPlotPicker::setAxis(int xAxis, int yAxis)
00161 {
00162     const QwtPlot *plt = plot();
00163     if ( !plt )
00164         return;
00165 
00166     if ( xAxis != d_xAxis || yAxis != d_yAxis )
00167     {
00168         d_xAxis = xAxis;
00169         d_yAxis = yAxis;
00170     }
00171 }
00172 
00174 int QwtPlotPicker::xAxis() const
00175 {
00176     return d_xAxis;
00177 }
00178 
00180 int QwtPlotPicker::yAxis() const
00181 {
00182     return d_yAxis;
00183 }
00184 
00191 QwtText QwtPlotPicker::trackerText(const QPoint &pos) const
00192 {
00193     return trackerText(invTransform(pos));
00194 }
00195 
00208 QwtText QwtPlotPicker::trackerText(const QwtDoublePoint &pos) const
00209 {
00210     switch(rubberBand())
00211     {
00212         case HLineRubberBand:
00213             return QString().sprintf("%.4f", pos.y());
00214         case VLineRubberBand:
00215             return QString().sprintf("%.4f", pos.x());
00216         default:
00217             return QString().sprintf("%.4f, %.4f", pos.x(), pos.y());
00218     }
00219     return QwtText(); // make some dumb compilers happy
00220 }
00221 
00231 void QwtPlotPicker::append(const QPoint &pos)
00232 {
00233     QwtPicker::append(pos);
00234     emit appended(invTransform(pos));
00235 }
00236 
00246 void QwtPlotPicker::move(const QPoint &pos)
00247 {
00248     QwtPicker::move(pos);
00249     emit moved(invTransform(pos));
00250 }
00251 
00260 bool QwtPlotPicker::end(bool ok)
00261 {
00262     ok = QwtPicker::end(ok);
00263     if ( !ok )
00264         return false;
00265 
00266     QwtPlot *plot = QwtPlotPicker::plot();
00267     if ( !plot )
00268         return false;
00269 
00270     const QwtPolygon &pa = selection();
00271     if ( pa.count() == 0 )
00272         return false;
00273 
00274     if ( selectionFlags() & PointSelection )
00275     {
00276         const QwtDoublePoint pos = invTransform(pa[0]);
00277         emit selected(pos);
00278     }
00279     else if ( (selectionFlags() & RectSelection) && pa.count() >= 2 )
00280     {
00281         QPoint p1 = pa[0];
00282         QPoint p2 = pa[int(pa.count() - 1)];
00283 
00284         if ( selectionFlags() & CenterToCorner )
00285         {
00286             p1.setX(p1.x() - (p2.x() - p1.x()));
00287             p1.setY(p1.y() - (p2.y() - p1.y()));
00288         }
00289         else if ( selectionFlags() & CenterToRadius )
00290         {
00291             const int radius = qwtMax(qwtAbs(p2.x() - p1.x()),
00292                 qwtAbs(p2.y() - p1.y()));
00293             p2.setX(p1.x() + radius);
00294             p2.setY(p1.y() + radius);
00295             p1.setX(p1.x() - radius);
00296             p1.setY(p1.y() - radius);
00297         }
00298 
00299         emit selected(invTransform(QRect(p1, p2)).normalized());
00300     }
00301     else 
00302     {
00303         QwtArray<QwtDoublePoint> dpa(pa.count());
00304         for ( int i = 0; i < int(pa.count()); i++ )
00305             dpa[i] = invTransform(pa[i]);
00306 
00307         emit selected(dpa);
00308     }
00309 
00310     return true;
00311 }
00312 
00319 QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const
00320 {
00321     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00322     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00323 
00324     const double left = xMap.invTransform(rect.left());
00325     const double right = xMap.invTransform(rect.right());
00326     const double top = yMap.invTransform(rect.top());
00327     const double bottom = yMap.invTransform(rect.bottom());
00328 
00329     return QwtDoubleRect(left, top,
00330         right - left, bottom - top);
00331 }
00332 
00338 QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const
00339 {
00340     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00341     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00342 
00343     const int left = xMap.transform(rect.left());
00344     const int right = xMap.transform(rect.right());
00345     const int top = yMap.transform(rect.top());
00346     const int bottom = yMap.transform(rect.bottom());
00347 
00348     return QRect(left, top, right - left, bottom - top);
00349 }
00350 
00356 QwtDoublePoint QwtPlotPicker::invTransform(const QPoint &pos) const
00357 {
00358     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00359     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00360 
00361     return QwtDoublePoint(
00362         xMap.invTransform(pos.x()),
00363         yMap.invTransform(pos.y())
00364     );
00365 }
00366 
00372 QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const
00373 {
00374     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00375     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00376 
00377     return QPoint(
00378         xMap.transform(pos.x()),
00379         yMap.transform(pos.y())
00380     );
00381 }

Generated on Mon Nov 6 20:32:57 2006 for Qwt User's Guide by  doxygen 1.4.6