qwt_picker.h 10.8 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
5
 *
pixhawk's avatar
pixhawk committed
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 31 32 33
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_PICKER
#define QWT_PICKER 1

#include <qobject.h>
#include <qpen.h>
#include <qfont.h>
#include <qrect.h>
#include "qwt_global.h"
#include "qwt_text.h"
#include "qwt_polygon.h"
#include "qwt_event_pattern.h"

class QWidget;
class QMouseEvent;
class QWheelEvent;
class QKeyEvent;
class QwtPickerMachine;

/*!
  \brief QwtPicker provides selections on a widget

  QwtPicker filters all mouse and keyboard events of a widget
  and translates them into an array of selected points. Depending
  on the QwtPicker::SelectionType the selection might be a single point,
34 35
  a rectangle or a polygon. The selection process is supported by
  optional rubberbands (rubberband selection) and position trackers.
pixhawk's avatar
pixhawk committed
36 37 38 39 40

  QwtPicker is useful for widgets where the event handlers
  can't be overloaded, like for components of composite widgets.
  It offers alternative handlers for mouse and key events.

41
  \par Example
pixhawk's avatar
pixhawk committed
42 43 44 45 46 47 48 49 50 51
  \verbatim #include <qwt_picker.h>

QwtPicker *picker = new QwtPicker(widget);
picker->setTrackerMode(QwtPicker::ActiveOnly);
connect(picker, SIGNAL(selected(const QwtPolygon &)), ...);

// emit the position of clicks on widget
picker->setSelectionFlags(QwtPicker::PointSelection | QwtPicker::ClickSelection);

    ...
52

pixhawk's avatar
pixhawk committed
53 54 55 56 57
// now select rectangles
picker->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection);
picker->setRubberBand(QwtPicker::RectRubberBand); \endverbatim\n

  The selection process uses the commands begin(), append(), move() and end().
58 59
  append() adds a new point to the selection, move() changes the position of
  the latest point.
pixhawk's avatar
pixhawk committed
60

61 62
  The commands are initiated from a small state machine (QwtPickerMachine)
  that translates mouse and key events. There are a couple of predefined
pixhawk's avatar
pixhawk committed
63
  state machines for point, rect and polygon selections. The selectionFlags()
64
  control which one should be used. It is possible to use other machines
pixhawk's avatar
pixhawk committed
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 92 93 94 95 96 97 98
  by overloading stateMachine().

  The picker is active (isActive()), between begin() and end().
  In active state the rubberband is displayed, and the tracker is visible
  in case of trackerMode is ActiveOnly or AlwaysOn.

  The cursor can be moved using the arrow keys. All selections can be aborted
  using the abort key. (QwtEventPattern::KeyPatternCode)

  \warning In case of QWidget::NoFocus the focus policy of the observed
           widget is set to QWidget::WheelFocus and mouse tracking
           will be manipulated for ClickSelection while the picker is active,
           or if trackerMode() is AlwayOn.
*/

class QWT_EXPORT QwtPicker: public QObject, public QwtEventPattern
{
    Q_OBJECT

    Q_ENUMS(RubberBand)
    Q_ENUMS(DisplayMode)
    Q_ENUMS(ResizeMode)

    Q_PROPERTY(int selectionFlags READ selectionFlags WRITE setSelectionFlags)
    Q_PROPERTY(DisplayMode trackerMode READ trackerMode WRITE setTrackerMode)
    Q_PROPERTY(QFont trackerFont READ trackerFont WRITE setTrackerFont)
    Q_PROPERTY(RubberBand rubberBand READ rubberBand WRITE setRubberBand)
    Q_PROPERTY(ResizeMode resizeMode READ resizeMode WRITE setResizeMode)
    Q_PROPERTY(bool isEnabled READ isEnabled WRITE setEnabled)

    Q_PROPERTY(QPen trackerPen READ trackerPen WRITE setTrackerPen)
    Q_PROPERTY(QPen rubberBandPen READ rubberBandPen WRITE setRubberBandPen)

public:
99
    /*!
pixhawk's avatar
pixhawk committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      This enum type describes the type of a selection. It can be or'd
      with QwtPicker::RectSelectionType and QwtPicker::SelectionMode
      and passed to QwtPicker::setSelectionFlags()
      - NoSelection\n
        Selection is disabled. Note this is different to the disabled
        state, as you might have a tracker.
      - PointSelection\n
        Select a single point.
      - RectSelection\n
        Select a rectangle.
      - PolygonSelection\n
        Select a polygon.

      The default value is NoSelection.
      \sa QwtPicker::setSelectionFlags(), QwtPicker::selectionFlags()
    */

117
    enum SelectionType {
pixhawk's avatar
pixhawk committed
118 119 120 121 122 123
        NoSelection = 0,
        PointSelection = 1,
        RectSelection = 2,
        PolygonSelection = 4
    };

124
    /*!
pixhawk's avatar
pixhawk committed
125
      \brief Selection subtype for RectSelection
126 127
      This enum type describes the type of rectangle selections.
      It can be or'd with QwtPicker::RectSelectionType and
pixhawk's avatar
pixhawk committed
128 129 130 131 132 133 134 135
      QwtPicker::SelectionMode and passed to QwtPicker::setSelectionFlags().
      - CornerToCorner\n
        The first and the second selected point are the corners
        of the rectangle.
      - CenterToCorner\n
        The first point is the center, the second a corner of the
        rectangle.
      - CenterToRadius\n
136
        The first point is the center of a quadrat, calculated by the maximum
pixhawk's avatar
pixhawk committed
137 138 139 140 141
        of the x- and y-distance.

      The default value is CornerToCorner.
      \sa QwtPicker::setSelectionFlags(), QwtPicker::selectionFlags()
    */
142
    enum RectSelectionType {
pixhawk's avatar
pixhawk committed
143 144 145 146 147
        CornerToCorner = 64,
        CenterToCorner = 128,
        CenterToRadius = 256
    };

148
    /*!
pixhawk's avatar
pixhawk committed
149 150 151 152 153 154
      Values of this enum type or'd together with a SelectionType value
      identifies which state machine should be used for the selection.

      The default value is ClickSelection.
      \sa stateMachine()
    */
155
    enum SelectionMode {
pixhawk's avatar
pixhawk committed
156 157 158 159
        ClickSelection = 1024,
        DragSelection = 2048
    };

160
    /*!
pixhawk's avatar
pixhawk committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      Rubberband style
      - NoRubberBand\n
        No rubberband.
      - HLineRubberBand & PointSelection\n
        A horizontal line.
      - VLineRubberBand & PointSelection\n
        A vertical line.
      - CrossRubberBand & PointSelection\n
        A horizontal and a vertical line.
      - RectRubberBand & RectSelection\n
        A rectangle.
      - EllipseRubberBand & RectSelection\n
        An ellipse.
      - PolygonRubberBand &PolygonSelection\n
        A polygon.
      - UserRubberBand\n
        Values >= UserRubberBand can be used to define additional
        rubber bands.

      The default value is NoRubberBand.
      \sa QwtPicker::setRubberBand(), QwtPicker::rubberBand()
    */

184
    enum RubberBand {
pixhawk's avatar
pixhawk committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
        NoRubberBand = 0,

        // Point
        HLineRubberBand,
        VLineRubberBand,
        CrossRubberBand,

        // Rect
        RectRubberBand,
        EllipseRubberBand,

        // Polygon
        PolygonRubberBand,

        UserRubberBand = 100
    };

202
    /*!
pixhawk's avatar
pixhawk committed
203 204 205 206 207 208 209
      - AlwaysOff\n
        Display never.
      - AlwaysOn\n
        Display always.
      - ActiveOnly\n
        Display only when the selection is active.

210
      \sa QwtPicker::setTrackerMode(), QwtPicker::trackerMode(),
pixhawk's avatar
pixhawk committed
211 212
          QwtPicker::isActive()
    */
213
    enum DisplayMode {
pixhawk's avatar
pixhawk committed
214 215 216 217 218
        AlwaysOff,
        AlwaysOn,
        ActiveOnly
    };

219
    /*!
pixhawk's avatar
pixhawk committed
220 221 222
      Controls what to do with the selected points of an active
         selection when the observed widget is resized.
      - Stretch\n
223
         All points are scaled according to the new size,
pixhawk's avatar
pixhawk committed
224 225 226 227 228 229 230
      - KeepSize\n
         All points remain unchanged.

      The default value is Stretch.
      \sa QwtPicker::setResizeMode(), QwtPicker::resize()
    */

231
    enum ResizeMode {
pixhawk's avatar
pixhawk committed
232 233 234 235 236 237
        Stretch,
        KeepSize
    };

    explicit QwtPicker(QWidget *parent);
    explicit QwtPicker(int selectionFlags, RubberBand rubberBand,
238
                       DisplayMode trackerMode, QWidget *);
pixhawk's avatar
pixhawk committed
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273

    virtual ~QwtPicker();

    virtual void setSelectionFlags(int);
    int selectionFlags() const;

    virtual void setRubberBand(RubberBand);
    RubberBand rubberBand() const;

    virtual void setTrackerMode(DisplayMode);
    DisplayMode trackerMode() const;

    virtual void setResizeMode(ResizeMode);
    ResizeMode resizeMode() const;

    virtual void setRubberBandPen(const QPen &);
    QPen rubberBandPen() const;

    virtual void setTrackerPen(const QPen &);
    QPen trackerPen() const;

    virtual void setTrackerFont(const QFont &);
    QFont trackerFont() const;

    bool isEnabled() const;
    virtual void setEnabled(bool);

    bool isActive() const;

    virtual bool eventFilter(QObject *, QEvent *);

    QWidget *parentWidget();
    const QWidget *parentWidget() const;

    virtual QRect pickRect() const;
274
    const QwtPolygon &selection() const;
pixhawk's avatar
pixhawk committed
275 276 277 278 279 280 281 282 283 284 285

    virtual void drawRubberBand(QPainter *) const;
    virtual void drawTracker(QPainter *) const;

    virtual QwtText trackerText(const QPoint &pos) const;
    QPoint trackerPosition() const;
    QRect trackerRect(const QFont &) const;


signals:
    /*!
286
      A signal emitting the selected points,
pixhawk's avatar
pixhawk committed
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
      at the end of a selection.

      \param pa Selected points
    */
    void selected(const QwtPolygon &pa);

    /*!
      A signal emitted when a point has been appended to the selection

      \param pos Position of the appended point.
      \sa append(). moved()
    */
    void appended(const QPoint &pos);

    /*!
302
      A signal emitted whenever the last appended point of the
pixhawk's avatar
pixhawk committed
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      selection has been moved.

      \param pos Position of the moved last point of the selection.
      \sa move(), appended()
    */
    void moved(const QPoint &pos);

    /*!
      A signal emitted when the active selection has been changed.
      This might happen when the observed widget is resized.

      \param pa Changed selection
      \sa stretchSelection()
    */
    void changed(const QwtPolygon &pa);

protected:
    /*!
      \brief Validate and fixup the selection

      Accepts all selections unmodified
324

pixhawk's avatar
pixhawk committed
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
      \param selection Selection to validate and fixup
      \return true, when accepted, false otherwise
    */
    virtual bool accept(QwtPolygon &selection) const;

    virtual void transition(const QEvent *);

    virtual void begin();
    virtual void append(const QPoint &);
    virtual void move(const QPoint &);
    virtual bool end(bool ok = true);

    virtual void reset();

    virtual void widgetMousePressEvent(QMouseEvent *);
    virtual void widgetMouseReleaseEvent(QMouseEvent *);
341 342
    virtual void widgetMouseDoubleClickEvent(QMouseEvent *);
    virtual void widgetMouseMoveEvent(QMouseEvent *);
pixhawk's avatar
pixhawk committed
343
    virtual void widgetWheelEvent(QWheelEvent *);
344 345 346
    virtual void widgetKeyPressEvent(QKeyEvent *);
    virtual void widgetKeyReleaseEvent(QKeyEvent *);
    virtual void widgetLeaveEvent(QEvent *);
pixhawk's avatar
pixhawk committed
347

348 349
    virtual void stretchSelection(const QSize &oldSize,
                                  const QSize &newSize);
pixhawk's avatar
pixhawk committed
350 351 352 353 354 355 356 357 358 359

    virtual QwtPickerMachine *stateMachine(int) const;

    virtual void updateDisplay();

    const QWidget *rubberBandWidget() const;
    const QWidget *trackerWidget() const;

private:
    void init(QWidget *, int selectionFlags, RubberBand rubberBand,
360
              DisplayMode trackerMode);
pixhawk's avatar
pixhawk committed
361 362 363 364 365 366 367 368

    void setStateMachine(QwtPickerMachine *);
    void setMouseTracking(bool);

    class PickerWidget;
    class PrivateData;
    PrivateData *d_data;
};
369

pixhawk's avatar
pixhawk committed
370
#endif