Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
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
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 QwtPlotPicker::~QwtPlotPicker()
00104 {
00105 }
00106
00108 QwtPlotCanvas *QwtPlotPicker::canvas()
00109 {
00110 QWidget *w = parentWidget();
00111 if ( w && w->inherits("QwtPlotCanvas") )
00112 return (QwtPlotCanvas *)w;
00113
00114 return NULL;
00115 }
00116
00118 const QwtPlotCanvas *QwtPlotPicker::canvas() const
00119 {
00120 return ((QwtPlotPicker *)this)->canvas();
00121 }
00122
00124 QwtPlot *QwtPlotPicker::plot()
00125 {
00126 QObject *w = canvas();
00127 if ( w )
00128 {
00129 w = w->parent();
00130 if ( w && w->inherits("QwtPlot") )
00131 return (QwtPlot *)w;
00132 }
00133
00134 return NULL;
00135 }
00136
00138 const QwtPlot *QwtPlotPicker::plot() const
00139 {
00140 return ((QwtPlotPicker *)this)->plot();
00141 }
00142
00148 QwtDoubleRect QwtPlotPicker::scaleRect() const
00149 {
00150 QwtDoubleRect rect;
00151
00152 if ( plot() )
00153 {
00154 const QwtScaleDiv *xs = plot()->axisScaleDiv(xAxis());
00155 const QwtScaleDiv *ys = plot()->axisScaleDiv(yAxis());
00156
00157 if ( xs && ys )
00158 {
00159 rect = QwtDoubleRect( xs->lowerBound(), ys->lowerBound(),
00160 xs->range(), ys->range() );
00161 rect = rect.normalized();
00162 }
00163 }
00164
00165 return rect;
00166 }
00167
00174 void QwtPlotPicker::setAxis(int xAxis, int yAxis)
00175 {
00176 const QwtPlot *plt = plot();
00177 if ( !plt )
00178 return;
00179
00180 if ( xAxis != d_xAxis || yAxis != d_yAxis )
00181 {
00182 d_xAxis = xAxis;
00183 d_yAxis = yAxis;
00184 }
00185 }
00186
00188 int QwtPlotPicker::xAxis() const
00189 {
00190 return d_xAxis;
00191 }
00192
00194 int QwtPlotPicker::yAxis() const
00195 {
00196 return d_yAxis;
00197 }
00198
00205 QwtText QwtPlotPicker::trackerText(const QPoint &pos) const
00206 {
00207 return trackerText(invTransform(pos));
00208 }
00209
00222 QwtText QwtPlotPicker::trackerText(const QwtDoublePoint &pos) const
00223 {
00224 QString text;
00225
00226 switch(rubberBand())
00227 {
00228 case HLineRubberBand:
00229 text.sprintf("%.4f", pos.y());
00230 break;
00231 case VLineRubberBand:
00232 text.sprintf("%.4f", pos.x());
00233 break;
00234 default:
00235 text.sprintf("%.4f, %.4f", pos.x(), pos.y());
00236 }
00237 return QwtText(text);
00238 }
00239
00249 void QwtPlotPicker::append(const QPoint &pos)
00250 {
00251 QwtPicker::append(pos);
00252 emit appended(invTransform(pos));
00253 }
00254
00264 void QwtPlotPicker::move(const QPoint &pos)
00265 {
00266 QwtPicker::move(pos);
00267 emit moved(invTransform(pos));
00268 }
00269
00278 bool QwtPlotPicker::end(bool ok)
00279 {
00280 ok = QwtPicker::end(ok);
00281 if ( !ok )
00282 return false;
00283
00284 QwtPlot *plot = QwtPlotPicker::plot();
00285 if ( !plot )
00286 return false;
00287
00288 const QwtPolygon &pa = selection();
00289 if ( pa.count() == 0 )
00290 return false;
00291
00292 if ( selectionFlags() & PointSelection )
00293 {
00294 const QwtDoublePoint pos = invTransform(pa[0]);
00295 emit selected(pos);
00296 }
00297 else if ( (selectionFlags() & RectSelection) && pa.count() >= 2 )
00298 {
00299 QPoint p1 = pa[0];
00300 QPoint p2 = pa[int(pa.count() - 1)];
00301
00302 if ( selectionFlags() & CenterToCorner )
00303 {
00304 p1.setX(p1.x() - (p2.x() - p1.x()));
00305 p1.setY(p1.y() - (p2.y() - p1.y()));
00306 }
00307 else if ( selectionFlags() & CenterToRadius )
00308 {
00309 const int radius = qwtMax(qwtAbs(p2.x() - p1.x()),
00310 qwtAbs(p2.y() - p1.y()));
00311 p2.setX(p1.x() + radius);
00312 p2.setY(p1.y() + radius);
00313 p1.setX(p1.x() - radius);
00314 p1.setY(p1.y() - radius);
00315 }
00316
00317 emit selected(invTransform(QRect(p1, p2)).normalized());
00318 }
00319 else
00320 {
00321 QwtArray<QwtDoublePoint> dpa(pa.count());
00322 for ( int i = 0; i < int(pa.count()); i++ )
00323 dpa[i] = invTransform(pa[i]);
00324
00325 emit selected(dpa);
00326 }
00327
00328 return true;
00329 }
00330
00337 QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const
00338 {
00339 QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00340 QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00341
00342 const double left = xMap.invTransform(rect.left());
00343 const double right = xMap.invTransform(rect.right());
00344 const double top = yMap.invTransform(rect.top());
00345 const double bottom = yMap.invTransform(rect.bottom());
00346
00347 return QwtDoubleRect(left, top,
00348 right - left, bottom - top);
00349 }
00350
00356 QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const
00357 {
00358 QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00359 QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00360
00361 const int left = xMap.transform(rect.left());
00362 const int right = xMap.transform(rect.right());
00363 const int top = yMap.transform(rect.top());
00364 const int bottom = yMap.transform(rect.bottom());
00365
00366 return QRect(left, top, right - left, bottom - top);
00367 }
00368
00374 QwtDoublePoint QwtPlotPicker::invTransform(const QPoint &pos) const
00375 {
00376 QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00377 QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00378
00379 return QwtDoublePoint(
00380 xMap.invTransform(pos.x()),
00381 yMap.invTransform(pos.y())
00382 );
00383 }
00384
00390 QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const
00391 {
00392 QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
00393 QwtScaleMap yMap = plot()->canvasMap(d_yAxis);
00394
00395 return QPoint(
00396 xMap.transform(pos.x()),
00397 yMap.transform(pos.y())
00398 );
00399 }