qwt_plot_picker.cpp 9.65 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

// vim: expandtab

#include "qwt_plot.h"
#include "qwt_double_rect.h"
#include "qwt_scale_div.h"
#include "qwt_painter.h"
#include "qwt_scale_map.h"
#include "qwt_plot_picker.h"

/*!
  \brief Create a plot picker

  The picker is set to those x- and y-axis of the plot
  that are enabled. If both or no x-axis are enabled, the picker
  is set to QwtPlot::xBottom. If both or no y-axis are
  enabled, it is set to QwtPlot::yLeft.

  \param canvas Plot canvas to observe, also the parent object

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), QwtPlotPicker::scaleRect()
*/
31

pixhawk's avatar
pixhawk committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
QwtPlotPicker::QwtPlotPicker(QwtPlotCanvas *canvas):
    QwtPicker(canvas),
    d_xAxis(-1),
    d_yAxis(-1)
{
    if ( !canvas )
        return;

    // attach axes

    int xAxis = QwtPlot::xBottom;

    const QwtPlot *plot = QwtPlotPicker::plot();
    if ( !plot->axisEnabled(QwtPlot::xBottom) &&
46
            plot->axisEnabled(QwtPlot::xTop) ) {
pixhawk's avatar
pixhawk committed
47 48 49 50 51
        xAxis = QwtPlot::xTop;
    }

    int yAxis = QwtPlot::yLeft;
    if ( !plot->axisEnabled(QwtPlot::yLeft) &&
52
            plot->axisEnabled(QwtPlot::yRight) ) {
pixhawk's avatar
pixhawk committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        yAxis = QwtPlot::yRight;
    }

    setAxis(xAxis, yAxis);
}

/*!
  Create a plot picker

  \param xAxis Set the x axis of the picker
  \param yAxis Set the y axis of the picker
  \param canvas Plot canvas to observe, also the parent object

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), QwtPlotPicker::scaleRect()
*/
QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, QwtPlotCanvas *canvas):
    QwtPicker(canvas),
    d_xAxis(xAxis),
    d_yAxis(yAxis)
{
}

/*!
  Create a plot picker

  \param xAxis X axis of the picker
  \param yAxis Y axis of the picker
  \param selectionFlags Or'd value of SelectionType, RectSelectionType and
                        SelectionMode
  \param rubberBand Rubberband style
  \param trackerMode Tracker mode
  \param canvas Plot canvas to observe, also the parent object

  \sa QwtPicker, QwtPicker::setSelectionFlags(), QwtPicker::setRubberBand(),
      QwtPicker::setTrackerMode

  \sa QwtPlot::autoReplot(), QwtPlot::replot(), QwtPlotPicker::scaleRect()
*/
QwtPlotPicker::QwtPlotPicker(int xAxis, int yAxis, int selectionFlags,
92 93
                             RubberBand rubberBand, DisplayMode trackerMode,
                             QwtPlotCanvas *canvas):
pixhawk's avatar
pixhawk committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    QwtPicker(selectionFlags, rubberBand, trackerMode, canvas),
    d_xAxis(xAxis),
    d_yAxis(yAxis)
{
}

//! Return observed plot canvas
QwtPlotCanvas *QwtPlotPicker::canvas()
{
    QWidget *w = parentWidget();
    if ( w && w->inherits("QwtPlotCanvas") )
        return (QwtPlotCanvas *)w;

    return NULL;
}

//! Return Observed plot canvas
const QwtPlotCanvas *QwtPlotPicker::canvas() const
{
    return ((QwtPlotPicker *)this)->canvas();
}

//! Return plot widget, containing the observed plot canvas
QwtPlot *QwtPlotPicker::plot()
{
    QObject *w = canvas();
120
    if ( w ) {
pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
        w = w->parent();
        if ( w && w->inherits("QwtPlot") )
            return (QwtPlot *)w;
    }

    return NULL;
}

//! Return plot widget, containing the observed plot canvas
const QwtPlot *QwtPlotPicker::plot() const
{
    return ((QwtPlotPicker *)this)->plot();
}

/*!
  Return normalized bounding rect of the axes

  \sa QwtPlot::autoReplot(), QwtPlot::replot().
*/
QwtDoubleRect QwtPlotPicker::scaleRect() const
{
    QwtDoubleRect rect;

144
    if ( plot() ) {
pixhawk's avatar
pixhawk committed
145 146 147
        const QwtScaleDiv *xs = plot()->axisScaleDiv(xAxis());
        const QwtScaleDiv *ys = plot()->axisScaleDiv(yAxis());

148 149 150
        if ( xs && ys ) {
            rect = QwtDoubleRect( xs->lBound(), ys->lBound(),
                                  xs->range(), ys->range() );
pixhawk's avatar
pixhawk committed
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            rect = rect.normalized();
        }
    }

    return rect;
}

/*!
  Set the x and y axes of the picker

  \param xAxis X axis
  \param yAxis Y axis
*/
void QwtPlotPicker::setAxis(int xAxis, int yAxis)
{
    const QwtPlot *plt = plot();
    if ( !plt )
        return;

170
    if ( xAxis != d_xAxis || yAxis != d_yAxis ) {
pixhawk's avatar
pixhawk committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        d_xAxis = xAxis;
        d_yAxis = yAxis;
    }
}

//! Return x axis
int QwtPlotPicker::xAxis() const
{
    return d_xAxis;
}

//! Return y axis
int QwtPlotPicker::yAxis() const
{
    return d_yAxis;
}

/*!
  Translate a pixel position into a position string

  \param pos Position in pixel coordinates
  \return Position string
*/
QwtText QwtPlotPicker::trackerText(const QPoint &pos) const
{
    return trackerText(invTransform(pos));
}

/*!
  \brief Translate a position into a position string

  In case of HLineRubberBand the label is the value of the
  y position, in case of VLineRubberBand the value of the x position.
  Otherwise the label contains x and y position separated by a ',' .

  The format for the double to string conversion is "%.4f".

  \param pos Position
  \return Position string
*/
QwtText QwtPlotPicker::trackerText(const QwtDoublePoint &pos) const
{
    QString text;

215 216 217 218 219 220 221 222 223
    switch(rubberBand()) {
    case HLineRubberBand:
        text.sprintf("%.4f", pos.y());
        break;
    case VLineRubberBand:
        text.sprintf("%.4f", pos.x());
        break;
    default:
        text.sprintf("%.4f, %.4f", pos.x(), pos.y());
pixhawk's avatar
pixhawk committed
224 225 226 227
    }
    return QwtText(text);
}

228
/*!
pixhawk's avatar
pixhawk committed
229
  Append a point to the selection and update rubberband and tracker.
230

pixhawk's avatar
pixhawk committed
231 232 233
  \param pos Additional point
  \sa isActive, begin(), end(), move(), appended()

234
  \note The appended(const QPoint &), appended(const QDoublePoint &)
pixhawk's avatar
pixhawk committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248
        signals are emitted.
*/
void QwtPlotPicker::append(const QPoint &pos)
{
    QwtPicker::append(pos);
    emit appended(invTransform(pos));
}

/*!
  Move the last point of the selection

  \param pos New position
  \sa isActive, begin(), end(), append()

249
  \note The moved(const QPoint &), moved(const QDoublePoint &)
pixhawk's avatar
pixhawk committed
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        signals are emitted.
*/
void QwtPlotPicker::move(const QPoint &pos)
{
    QwtPicker::move(pos);
    emit moved(invTransform(pos));
}

/*!
  Close a selection setting the state to inactive.

  \param ok If true, complete the selection and emit selected signals
            otherwise discard the selection.
  \return true if the selection is accepted, false otherwise
*/

bool QwtPlotPicker::end(bool ok)
{
    ok = QwtPicker::end(ok);
    if ( !ok )
        return false;

    QwtPlot *plot = QwtPlotPicker::plot();
    if ( !plot )
        return false;

    const QwtPolygon &pa = selection();
    if ( pa.count() == 0 )
        return false;

280
    if ( selectionFlags() & PointSelection ) {
pixhawk's avatar
pixhawk committed
281 282
        const QwtDoublePoint pos = invTransform(pa[0]);
        emit selected(pos);
283
    } else if ( (selectionFlags() & RectSelection) && pa.count() >= 2 ) {
pixhawk's avatar
pixhawk committed
284 285 286
        QPoint p1 = pa[0];
        QPoint p2 = pa[int(pa.count() - 1)];

287
        if ( selectionFlags() & CenterToCorner ) {
pixhawk's avatar
pixhawk committed
288 289
            p1.setX(p1.x() - (p2.x() - p1.x()));
            p1.setY(p1.y() - (p2.y() - p1.y()));
290
        } else if ( selectionFlags() & CenterToRadius ) {
pixhawk's avatar
pixhawk committed
291
            const int radius = qwtMax(qwtAbs(p2.x() - p1.x()),
292
                                      qwtAbs(p2.y() - p1.y()));
pixhawk's avatar
pixhawk committed
293 294 295 296 297 298 299
            p2.setX(p1.x() + radius);
            p2.setY(p1.y() + radius);
            p1.setX(p1.x() - radius);
            p1.setY(p1.y() - radius);
        }

        emit selected(invTransform(QRect(p1, p2)).normalized());
300
    } else {
pixhawk's avatar
pixhawk committed
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        QwtArray<QwtDoublePoint> dpa(pa.count());
        for ( int i = 0; i < int(pa.count()); i++ )
            dpa[i] = invTransform(pa[i]);

        emit selected(dpa);
    }

    return true;
}

/*!
    Translate a rectangle from pixel into plot coordinates

    \return Rectangle in plot coordinates
    \sa QwtPlotPicker::transform()
*/
QwtDoubleRect QwtPlotPicker::invTransform(const QRect &rect) const
{
    QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
    QwtScaleMap yMap = plot()->canvasMap(d_yAxis);

    const double left = xMap.invTransform(rect.left());
    const double right = xMap.invTransform(rect.right());
    const double top = yMap.invTransform(rect.top());
    const double bottom = yMap.invTransform(rect.bottom());

    return QwtDoubleRect(left, top,
328
                         right - left, bottom - top);
pixhawk's avatar
pixhawk committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}

/*!
    Translate a rectangle from plot into pixel coordinates
    \return Rectangle in pixel coordinates
    \sa QwtPlotPicker::invTransform()
*/
QRect QwtPlotPicker::transform(const QwtDoubleRect &rect) const
{
    QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
    QwtScaleMap yMap = plot()->canvasMap(d_yAxis);

    const int left = xMap.transform(rect.left());
    const int right = xMap.transform(rect.right());
    const int top = yMap.transform(rect.top());
    const int bottom = yMap.transform(rect.bottom());

    return QRect(left, top, right - left, bottom - top);
}

/*!
    Translate a point from pixel into plot coordinates
    \return Point in plot coordinates
    \sa QwtPlotPicker::transform()
*/
QwtDoublePoint QwtPlotPicker::invTransform(const QPoint &pos) const
{
    QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
    QwtScaleMap yMap = plot()->canvasMap(d_yAxis);

    return QwtDoublePoint(
360 361 362
               xMap.invTransform(pos.x()),
               yMap.invTransform(pos.y())
           );
pixhawk's avatar
pixhawk committed
363 364 365 366 367 368 369 370 371 372 373 374 375
}

/*!
    Translate a point from plot into pixel coordinates
    \return Point in pixel coordinates
    \sa QwtPlotPicker::invTransform()
*/
QPoint QwtPlotPicker::transform(const QwtDoublePoint &pos) const
{
    QwtScaleMap xMap = plot()->canvasMap(d_xAxis);
    QwtScaleMap yMap = plot()->canvasMap(d_yAxis);

    return QPoint(
376 377 378
               xMap.transform(pos.x()),
               yMap.transform(pos.y())
           );
pixhawk's avatar
pixhawk committed
379
}