Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members | Related Pages

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 
00038 QwtPlotPicker::QwtPlotPicker(QwtPlotCanvas *canvas):
00039     QwtPicker(canvas),
00040     d_xAxis(-1),
00041     d_yAxis(-1)
00042 {
00043     if ( !canvas )
00044         return;
00045 
00046     // attach axes
00047 
00048     int xAxis = QwtPlot::xBottom;
00049 
00050     const QwtPlot *plot = QwtPlotPicker::plot();
00051     if ( !plot->axisEnabled(QwtPlot::xBottom) &&
00052         plot->axisEnabled(QwtPlot::xTop) )
00053     {
00054         xAxis = QwtPlot::xTop;
00055     }
00056 
00057     int yAxis = QwtPlot::yLeft;
00058     if ( !plot->axisEnabled(QwtPlot::yLeft) &&
00059         plot->axisEnabled(QwtPlot::yRight) )
00060     {
00061         yAxis = QwtPlot::yRight;
00062     }
00063 
00064     setAxis(xAxis, yAxis);
00065 }
00066 
00082 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, QwtPlotCanvas *canvas):
00083     QwtPicker(canvas),
00084     d_xAxis(xAxis),
00085     d_yAxis(yAxis)
00086 {
00087 }
00088 
00111 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, int selectionFlags,
00112         RubberBand rubberBand, DisplayMode trackerMode,
00113         QwtPlotCanvas *canvas):
00114     QwtPicker(selectionFlags, rubberBand, trackerMode, canvas),
00115     d_xAxis(xAxis),
00116     d_yAxis(yAxis)
00117 {
00118 }
00119 
00121 QwtPlotCanvas *QwtPlotPicker::canvas()
00122 {
00123     QWidget *w = parentWidget();
00124     if ( w && w->inherits("QwtPlotCanvas") )
00125         return (QwtPlotCanvas *)w;
00126 
00127     return NULL;
00128 }
00129 
00131 const QwtPlotCanvas *QwtPlotPicker::canvas() const
00132 {
00133     return ((QwtPlotPicker *)this)->canvas();
00134 }
00135 
00137 QwtPlot *QwtPlotPicker::plot()
00138 {
00139     QObject *w = canvas();
00140     if ( w )
00141     {
00142         w = w->parent();
00143         if ( w && w->inherits("QwtPlot") )
00144             return (QwtPlot *)w;
00145     }
00146 
00147     return NULL;
00148 }
00149 
00151 const QwtPlot *QwtPlotPicker::plot() const
00152 {
00153     return ((QwtPlotPicker *)this)->plot();
00154 }
00155 
00167 QwtDoubleRect QwtPlotPicker::scaleRect() const
00168 {
00169     const QwtScaleDiv *xs = plot()->axisScaleDiv(xAxis());
00170     const QwtScaleDiv *ys = plot()->axisScaleDiv(yAxis());
00171 
00172     const QwtDoubleRect rect( xs->lBound(), ys->lBound(), 
00173         xs->range(), ys->range() );
00174 
00175     return rect.normalized();
00176 }
00177 
00184 void QwtPlotPicker::setAxis(int xAxis, int yAxis)
00185 {
00186     const QwtPlot *plt = plot();
00187     if ( !plt )
00188         return;
00189 
00190     if ( xAxis != d_xAxis || yAxis != d_yAxis )
00191     {
00192         d_xAxis = xAxis;
00193         d_yAxis = yAxis;
00194     }
00195 }
00196 
00198 int QwtPlotPicker::xAxis() const
00199 {
00200     return d_xAxis;
00201 }
00202 
00204 int QwtPlotPicker::yAxis() const
00205 {
00206     return d_yAxis;
00207 }
00208 
00215 QwtText QwtPlotPicker::trackerText(const QPoint &pos) const
00216 {
00217     return trackerText(invTransform(pos));
00218 }
00219 
00232 QwtText QwtPlotPicker::trackerText(const QwtDoublePoint &pos) const
00233 {
00234     switch(rubberBand())
00235     {
00236         case HLineRubberBand:
00237             return QString().sprintf("%.4f", pos.y());
00238         case VLineRubberBand:
00239             return QString().sprintf("%.4f", pos.x());
00240         default:
00241             return QString().sprintf("%.4f, %.4f", pos.x(), pos.y());
00242     }
00243     return QwtText(); // make some dumb compilers happy
00244 }
00245 
00255 void QwtPlotPicker::append(const QPoint &pos)
00256 {
00257     QwtPicker::append(pos);
00258     emit appended(invTransform(pos));
00259 }
00260 
00270 void QwtPlotPicker::move(const QPoint &pos)
00271 {
00272     QwtPicker::move(pos);
00273     emit moved(invTransform(pos));
00274 }
00275 
00284 bool QwtPlotPicker::end(bool ok)
00285 {
00286     ok = QwtPicker::end(ok);
00287     if ( !ok )
00288         return false;
00289 
00290     QwtPlot *plot = QwtPlotPicker::plot();
00291     if ( !plot )
00292         return false;
00293 
00294     const SelectedPoints &pa = selection();
00295     if ( pa.count() == 0 )
00296         return false;
00297 
00298     if ( selectionFlags() & PointSelection )
00299     {
00300         const QwtDoublePoint pos = invTransform(pa[0]);
00301         emit selected(pos);
00302     }
00303     else if ( (selectionFlags() & RectSelection) && pa.count() >= 2 )
00304     {
00305         QPoint p1 = pa[0];
00306         QPoint p2 = pa[int(pa.count() - 1)];
00307 
00308         if ( selectionFlags() & CenterToCorner )
00309         {
00310             p1.setX(p1.x() - (p2.x() - p1.x()));
00311             p1.setY(p1.y() - (p2.y() - p1.y()));
00312         }
00313         else if ( selectionFlags() & CenterToRadius )
00314         {
00315             const int radius = qwtMax(qwtAbs(p2.x() - p1.x()),
00316                 qwtAbs(p2.y() - p1.y()));
00317             p2.setX(p1.x() + radius);
00318             p2.setY(p1.y() + radius);
00319             p1.setX(p1.x() - radius);
00320             p1.setY(p1.y() - radius);
00321         }
00322 
00323         emit selected(invTransform(QRect(p1, p2)).normalized());
00324     }
00325     else 
00326     {
00327         QwtArray<QwtDoublePoint> dpa(pa.count());
00328         for ( int i = 0; i < int(pa.count()); i++ )
00329             dpa[i] = invTransform(pa[i]);
00330 
00331         emit selected(dpa);
00332     }
00333 
00334     return true;
00335 }
00336 
00343 QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const
00344 {
00345     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00346     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00347 
00348     const double left = xMap.invTransform(rect.left());
00349     const double right = xMap.invTransform(rect.right());
00350     const double top = yMap.invTransform(rect.top());
00351     const double bottom = yMap.invTransform(rect.bottom());
00352 
00353     return QwtDoubleRect(left, top,
00354         right - left, bottom - top);
00355 }
00356 
00362 QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const
00363 {
00364     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00365     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00366 
00367     const int left = xMap.transform(rect.left());
00368     const int right = xMap.transform(rect.right());
00369     const int top = yMap.transform(rect.top());
00370     const int bottom = yMap.transform(rect.bottom());
00371 
00372     return QRect(left, top, right - left, bottom - top);
00373 }
00374 
00380 QwtDoublePoint QwtPlotPicker::invTransform(const QPoint &pos) const
00381 {
00382     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00383     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00384 
00385     return QwtDoublePoint(
00386         xMap.invTransform(pos.x()),
00387         yMap.invTransform(pos.y())
00388     );
00389 }
00390 
00396 QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const
00397 {
00398     QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00399     QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00400 
00401     return QPoint(
00402         xMap.transform(pos.x()),
00403         yMap.transform(pos.y())
00404     );
00405 }

Generated on Mon Jan 30 22:16:25 2006 for Qwt User's Guide by  doxygen 1.4.4