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_painter.h"
00015 #include "qwt_array.h"
00016 #include "qwt_plot_picker.h"
00017
00038 QwtPlotPicker::QwtPlotPicker(QwtPlotCanvas *canvas, const char *name):
00039 QwtPicker(canvas, name),
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
00083 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis,
00084 QwtPlotCanvas *canvas, const char *name):
00085 QwtPicker(canvas, name),
00086 d_xAxis(xAxis),
00087 d_yAxis(yAxis)
00088 {
00089 }
00090
00114 QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, int selectionFlags,
00115 RubberBand rubberBand, DisplayMode cursorLabelMode,
00116 QwtPlotCanvas *canvas, const char *name):
00117 QwtPicker(selectionFlags, rubberBand, cursorLabelMode, canvas, name),
00118 d_xAxis(xAxis),
00119 d_yAxis(yAxis)
00120 {
00121 }
00122
00124 QwtPlotCanvas *QwtPlotPicker::canvas()
00125 {
00126 QWidget *w = parentWidget();
00127 if ( w && w->inherits("QwtPlotCanvas") )
00128 return (QwtPlotCanvas *)w;
00129
00130 return NULL;
00131 }
00132
00134 const QwtPlotCanvas *QwtPlotPicker::canvas() const
00135 {
00136 return ((QwtPlotPicker *)this)->canvas();
00137 }
00138
00140 QwtPlot *QwtPlotPicker::plot()
00141 {
00142 QObject *w = canvas();
00143 if ( w )
00144 {
00145 w = w->parent();
00146 if ( w && w->inherits("QwtPlot") )
00147 return (QwtPlot *)w;
00148 }
00149
00150 return NULL;
00151 }
00152
00154 const QwtPlot *QwtPlotPicker::plot() const
00155 {
00156 return ((QwtPlotPicker *)this)->plot();
00157 }
00158
00170 QwtDoubleRect QwtPlotPicker::scaleRect() const
00171 {
00172 const QwtPlot *plt = plot();
00173
00174 const QwtDoubleRect rect(
00175 plt->axisScale(xAxis())->lBound(),
00176 plt->axisScale(xAxis())->hBound(),
00177 plt->axisScale(yAxis())->lBound(),
00178 plt->axisScale(yAxis())->hBound()
00179 );
00180
00181 return rect.normalize();
00182 }
00183
00190 void QwtPlotPicker::setAxis(int xAxis, int yAxis)
00191 {
00192 const QwtPlot *plt = plot();
00193 if ( !plt )
00194 return;
00195
00196 if ( xAxis != d_xAxis || yAxis != d_yAxis )
00197 {
00198 d_xAxis = xAxis;
00199 d_yAxis = yAxis;
00200 }
00201 }
00202
00204 int QwtPlotPicker::xAxis() const
00205 {
00206 return d_xAxis;
00207 }
00208
00210 int QwtPlotPicker::yAxis() const
00211 {
00212 return d_yAxis;
00213 }
00214
00221 QString QwtPlotPicker::cursorLabel(const QPoint &pos) const
00222 {
00223 return cursorLabel(invTransform(pos));
00224 }
00225
00238 QString QwtPlotPicker::cursorLabel(const QwtDoublePoint &pos) const
00239 {
00240 switch(rubberBand())
00241 {
00242 case HLineRubberBand:
00243 return QString().sprintf("%.4f", pos.y());
00244 case VLineRubberBand:
00245 return QString().sprintf("%.4f", pos.x());
00246 default:
00247 return QString().sprintf("%.4f, %.4f", pos.x(), pos.y());
00248 }
00249 return QString::null;
00250 }
00251
00261 void QwtPlotPicker::append(const QPoint &pos)
00262 {
00263 QwtPicker::append(pos);
00264 emit appended(invTransform(pos));
00265 }
00266
00276 void QwtPlotPicker::move(const QPoint &pos)
00277 {
00278 QwtPicker::move(pos);
00279 emit moved(invTransform(pos));
00280 }
00281
00290 bool QwtPlotPicker::end(bool ok)
00291 {
00292 ok = QwtPicker::end(ok);
00293 if ( !ok )
00294 return FALSE;
00295
00296 QwtPlot *plot = QwtPlotPicker::plot();
00297 if ( !plot )
00298 return FALSE;
00299
00300 const QPointArray &pa = selection();
00301 if ( pa.count() == 0 )
00302 return FALSE;
00303
00304 if ( selectionFlags() & PointSelection )
00305 {
00306 const QwtDoublePoint pos = invTransform(pa[0]);
00307 emit selected(pos);
00308 }
00309 else if ( (selectionFlags() & RectSelection) && pa.count() >= 2 )
00310 {
00311 QPoint p1 = pa[0];
00312 QPoint p2 = pa[int(pa.count() - 1)];
00313
00314 if ( selectionFlags() & CenterToCorner )
00315 {
00316 p1.setX(p1.x() - (p2.x() - p1.x()));
00317 p1.setY(p1.y() - (p2.y() - p1.y()));
00318 }
00319 else if ( selectionFlags() & CenterToRadius )
00320 {
00321 const int radius = QMAX(QABS(p2.x() - p1.x()),
00322 QABS(p2.y() - p1.y()));
00323 p2.setX(p1.x() + radius);
00324 p2.setY(p1.y() + radius);
00325 p1.setX(p1.x() - radius);
00326 p1.setY(p1.y() - radius);
00327 }
00328
00329 emit selected(invTransform(QRect(p1, p2)).normalize());
00330 }
00331 else
00332 {
00333 QwtArray<QwtDoublePoint> dpa(pa.count());
00334 for ( int i = 0; i < int(pa.count()); i++ )
00335 dpa[i] = invTransform(pa[i]);
00336
00337 emit selected(dpa);
00338 }
00339
00340 return TRUE;
00341 }
00342
00349 QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const
00350 {
00351 QwtDiMap xMap = plot()->canvasMap(d_xAxis);
00352 QwtDiMap yMap = plot()->canvasMap(d_yAxis);
00353
00354 return QwtDoubleRect(
00355 xMap.invTransform(rect.left()),
00356 xMap.invTransform(rect.right()),
00357 yMap.invTransform(rect.top()),
00358 yMap.invTransform(rect.bottom())
00359 );
00360 }
00361
00367 QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const
00368 {
00369 QwtDiMap xMap = plot()->canvasMap(d_xAxis);
00370 QwtDiMap yMap = plot()->canvasMap(d_yAxis);
00371
00372 const int x1 = xMap.transform(rect.x1());
00373 const int x2 = xMap.transform(rect.x2());
00374 const int y1 = yMap.transform(rect.y1());
00375 const int y2 = yMap.transform(rect.y2());
00376
00377 return QRect(x1, y1, x2 - x1, y2 - y1);
00378 }
00379
00385 QwtDoublePoint QwtPlotPicker::invTransform(const QPoint &pos) const
00386 {
00387 QwtDiMap xMap = plot()->canvasMap(d_xAxis);
00388 QwtDiMap yMap = plot()->canvasMap(d_yAxis);
00389
00390 return QwtDoublePoint(
00391 xMap.invTransform(pos.x()),
00392 yMap.invTransform(pos.y())
00393 );
00394 }
00395
00401 QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const
00402 {
00403 QwtDiMap xMap = plot()->canvasMap(d_xAxis);
00404 QwtDiMap yMap = plot()->canvasMap(d_yAxis);
00405
00406 return QPoint(
00407 xMap.transform(pos.x()),
00408 yMap.transform(pos.y())
00409 );
00410 }
00411
00412
00413
00414
00415
00416