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