Skip to content
qwt_picker.h 9.31 KiB
Newer Older
pixhawk's avatar
pixhawk committed
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
pixhawk's avatar
pixhawk committed
 * 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

Bryant's avatar
Bryant committed
#include "qwt_global.h"
#include "qwt_text.h"
#include "qwt_event_pattern.h"
pixhawk's avatar
pixhawk committed
#include <qobject.h>
#include <qpen.h>
#include <qfont.h>
#include <qrect.h>
Bryant's avatar
Bryant committed
#include <qpainterpath.h>
pixhawk's avatar
pixhawk committed

class QWidget;
class QMouseEvent;
class QWheelEvent;
class QKeyEvent;
class QwtPickerMachine;
Bryant's avatar
Bryant committed
class QwtWidgetOverlay;
pixhawk's avatar
pixhawk committed

/*!
  \brief QwtPicker provides selections on a widget

Bryant's avatar
Bryant committed
  QwtPicker filters all enter, leave, mouse and keyboard events of a widget
  and translates them into an array of selected points.

  The way how the points are collected depends on type of state machine
  that is connected to the picker. Qwt offers a couple of predefined
  state machines for selecting:
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  - Nothing\n
    QwtPickerTrackerMachine
  - Single points\n
    QwtPickerClickPointMachine, QwtPickerDragPointMachine
  - Rectangles\n
    QwtPickerClickRectMachine, QwtPickerDragRectMachine
  - Polygons\n
    QwtPickerPolygonMachine

  While these state machines cover the most common ways to collect points
  it is also possible to implement individual machines as well.

  QwtPicker translates the picked points into a selection using the
  adjustedPoints() method. adjustedPoints() is intended to be reimplemented
  to fix up the selection according to application specific requirements.
  (F.e. when an application accepts rectangles of a fixed aspect ratio only.)

  Optionally QwtPicker support the process of collecting points by a
  rubber band and tracker displaying a text for the current mouse
  position.
pixhawk's avatar
pixhawk committed

  \par Example
pixhawk's avatar
pixhawk committed
  \verbatim #include <qwt_picker.h>
Bryant's avatar
Bryant committed
#include <qwt_picker_machine.h>
pixhawk's avatar
pixhawk committed

QwtPicker *picker = new QwtPicker(widget);
Bryant's avatar
Bryant committed
picker->setStateMachine(new QwtPickerDragRectMachine);
pixhawk's avatar
pixhawk committed
picker->setTrackerMode(QwtPicker::ActiveOnly);
picker->setRubberBand(QwtPicker::RectRubberBand); \endverbatim\n

Bryant's avatar
Bryant committed
  The state machine triggers the following commands:
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
  - begin()\n
    Activate/Initialize the selection.
  - append()\n
    Add a new point
  - move() \n
    Change the position of the last point.
  - remove()\n
    Remove the last point.
  - end()\n
    Terminate the selection and call accept to validate the picked points.
pixhawk's avatar
pixhawk committed

  The picker is active (isActive()), between begin() and end().
Bryant's avatar
Bryant committed
  In active state the rubber band is displayed, and the tracker is visible
pixhawk's avatar
pixhawk committed
  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
Bryant's avatar
Bryant committed
           will be manipulated while the picker is active,
pixhawk's avatar
pixhawk committed
           or if trackerMode() is AlwayOn.
*/

class QWT_EXPORT QwtPicker: public QObject, public QwtEventPattern
{
    Q_OBJECT

Bryant's avatar
Bryant committed
    Q_ENUMS( RubberBand DisplayMode ResizeMode )
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    Q_PROPERTY( bool isEnabled READ isEnabled WRITE setEnabled )
    Q_PROPERTY( ResizeMode resizeMode READ resizeMode WRITE setResizeMode )
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    Q_PROPERTY( DisplayMode trackerMode READ trackerMode WRITE setTrackerMode )
    Q_PROPERTY( QPen trackerPen READ trackerPen WRITE setTrackerPen )
    Q_PROPERTY( QFont trackerFont READ trackerFont WRITE setTrackerFont )
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    Q_PROPERTY( RubberBand rubberBand READ rubberBand WRITE setRubberBand )
    Q_PROPERTY( QPen rubberBandPen READ rubberBandPen WRITE setRubberBandPen )
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
public:
Bryant's avatar
Bryant committed
      Rubber band style
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
      The default value is QwtPicker::NoRubberBand.
      \sa setRubberBand(), rubberBand()
pixhawk's avatar
pixhawk committed
    */

Bryant's avatar
Bryant committed
    enum RubberBand
    {
        //! No rubberband.
pixhawk's avatar
pixhawk committed
        NoRubberBand = 0,

Bryant's avatar
Bryant committed
        //! A horizontal line ( only for QwtPickerMachine::PointSelection )
pixhawk's avatar
pixhawk committed
        HLineRubberBand,
Bryant's avatar
Bryant committed

        //! A vertical line ( only for QwtPickerMachine::PointSelection )
pixhawk's avatar
pixhawk committed
        VLineRubberBand,
Bryant's avatar
Bryant committed

        //! A crosshair ( only for QwtPickerMachine::PointSelection )
pixhawk's avatar
pixhawk committed
        CrossRubberBand,

Bryant's avatar
Bryant committed
        //! A rectangle ( only for QwtPickerMachine::RectSelection )
pixhawk's avatar
pixhawk committed
        RectRubberBand,
Bryant's avatar
Bryant committed

        //! An ellipse ( only for QwtPickerMachine::RectSelection )
pixhawk's avatar
pixhawk committed
        EllipseRubberBand,

Bryant's avatar
Bryant committed
        //! A polygon ( only for QwtPickerMachine::PolygonSelection )
pixhawk's avatar
pixhawk committed
        PolygonRubberBand,

Bryant's avatar
Bryant committed
        /*!
          Values >= UserRubberBand can be used to define additional
          rubber bands.
         */
pixhawk's avatar
pixhawk committed
        UserRubberBand = 100
    };

Bryant's avatar
Bryant committed
      \brief Display mode
      \sa setTrackerMode(), trackerMode(), isActive()
pixhawk's avatar
pixhawk committed
    */
Bryant's avatar
Bryant committed
    enum DisplayMode
    {
        //! Display never
pixhawk's avatar
pixhawk committed
        AlwaysOff,
Bryant's avatar
Bryant committed

        //! Display always
pixhawk's avatar
pixhawk committed
        AlwaysOn,
Bryant's avatar
Bryant committed

        //! Display only when the selection is active
pixhawk's avatar
pixhawk committed
        ActiveOnly
    };

pixhawk's avatar
pixhawk committed
      Controls what to do with the selected points of an active
         selection when the observed widget is resized.

Bryant's avatar
Bryant committed
      The default value is QwtPicker::Stretch.
      \sa setResizeMode()
pixhawk's avatar
pixhawk committed
    */

Bryant's avatar
Bryant committed
    enum ResizeMode
    {
        //! All points are scaled according to the new size,
pixhawk's avatar
pixhawk committed
        Stretch,
Bryant's avatar
Bryant committed

        //! All points remain unchanged.
pixhawk's avatar
pixhawk committed
        KeepSize
    };

Bryant's avatar
Bryant committed
    explicit QwtPicker( QWidget *parent );
    explicit QwtPicker( RubberBand rubberBand,
                        DisplayMode trackerMode, QWidget * );
pixhawk's avatar
pixhawk committed

    virtual ~QwtPicker();

Bryant's avatar
Bryant committed
    void setStateMachine( QwtPickerMachine * );
    const QwtPickerMachine *stateMachine() const;
    QwtPickerMachine *stateMachine();
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    void setRubberBand( RubberBand );
pixhawk's avatar
pixhawk committed
    RubberBand rubberBand() const;

Bryant's avatar
Bryant committed
    void setTrackerMode( DisplayMode );
pixhawk's avatar
pixhawk committed
    DisplayMode trackerMode() const;

Bryant's avatar
Bryant committed
    void setResizeMode( ResizeMode );
pixhawk's avatar
pixhawk committed
    ResizeMode resizeMode() const;

Bryant's avatar
Bryant committed
    void setRubberBandPen( const QPen & );
pixhawk's avatar
pixhawk committed
    QPen rubberBandPen() const;

Bryant's avatar
Bryant committed
    void setTrackerPen( const QPen & );
pixhawk's avatar
pixhawk committed
    QPen trackerPen() const;

Bryant's avatar
Bryant committed
    void setTrackerFont( const QFont & );
pixhawk's avatar
pixhawk committed
    QFont trackerFont() const;

    bool isEnabled() const;
    bool isActive() const;

Bryant's avatar
Bryant committed
    virtual bool eventFilter( QObject *, QEvent * );
pixhawk's avatar
pixhawk committed

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

Bryant's avatar
Bryant committed
    virtual QPainterPath pickArea() const;

    virtual void drawRubberBand( QPainter * ) const;
    virtual void drawTracker( QPainter * ) const;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    virtual QRegion rubberBandMask() const;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    virtual QwtText trackerText( const QPoint &pos ) const;
pixhawk's avatar
pixhawk committed
    QPoint trackerPosition() const;
Bryant's avatar
Bryant committed
    virtual QRect trackerRect( const QFont & ) const;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    QPolygon selection() const;

public Q_SLOTS:
    void setEnabled( bool );

Q_SIGNALS:
    /*!
      A signal indicating, when the picker has been activated.
      Together with setEnabled() it can be used to implement
      selections with more than one picker.

      \param on True, when the picker has been activated
    */
    void activated( bool on );
pixhawk's avatar
pixhawk committed

    /*!
      A signal emitting the selected points,
pixhawk's avatar
pixhawk committed
      at the end of a selection.

Bryant's avatar
Bryant committed
      \param polygon Selected points
pixhawk's avatar
pixhawk committed
    */
Bryant's avatar
Bryant committed
    void selected( const QPolygon &polygon );
pixhawk's avatar
pixhawk committed

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

      \param pos Position of the appended point.
      \sa append(). moved()
    */
Bryant's avatar
Bryant committed
    void appended( const QPoint &pos );
pixhawk's avatar
pixhawk committed

    /*!
      A signal emitted whenever the last appended point of the
pixhawk's avatar
pixhawk committed
      selection has been moved.

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

    /*!
      A signal emitted whenever the last appended point of the
      selection has been removed.
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
      \param pos Position of the point, that has been removed
      \sa remove(), appended()
    */
    void removed( const QPoint &pos );
pixhawk's avatar
pixhawk committed
    /*!
      A signal emitted when the active selection has been changed.
      This might happen when the observed widget is resized.

Bryant's avatar
Bryant committed
      \param selection Changed selection
pixhawk's avatar
pixhawk committed
      \sa stretchSelection()
    */
Bryant's avatar
Bryant committed
    void changed( const QPolygon &selection );
pixhawk's avatar
pixhawk committed

protected:
Bryant's avatar
Bryant committed
    virtual QPolygon adjustedPoints( const QPolygon & ) const;
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    virtual void transition( const QEvent * );
pixhawk's avatar
pixhawk committed

    virtual void begin();
Bryant's avatar
Bryant committed
    virtual void append( const QPoint & );
    virtual void move( const QPoint & );
    virtual void remove();
    virtual bool end( bool ok = true );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    virtual bool accept( QPolygon & ) const;
pixhawk's avatar
pixhawk committed
    virtual void reset();

Bryant's avatar
Bryant committed
    virtual void widgetMousePressEvent( QMouseEvent * );
    virtual void widgetMouseReleaseEvent( QMouseEvent * );
    virtual void widgetMouseDoubleClickEvent( QMouseEvent * );
    virtual void widgetMouseMoveEvent( QMouseEvent * );
    virtual void widgetWheelEvent( QWheelEvent * );
    virtual void widgetKeyPressEvent( QKeyEvent * );
    virtual void widgetKeyReleaseEvent( QKeyEvent * );
    virtual void widgetEnterEvent( QEvent * );
    virtual void widgetLeaveEvent( QEvent * );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    virtual void stretchSelection( const QSize &oldSize,
                                   const QSize &newSize );
pixhawk's avatar
pixhawk committed

    virtual void updateDisplay();

Bryant's avatar
Bryant committed
    const QwtWidgetOverlay *rubberBandOverlay() const;
    const QwtWidgetOverlay *trackerOverlay() const;

    const QPolygon &pickedPoints() const;
pixhawk's avatar
pixhawk committed

private:
Bryant's avatar
Bryant committed
    void init( QWidget *, RubberBand rubberBand, DisplayMode trackerMode );
pixhawk's avatar
pixhawk committed

Bryant's avatar
Bryant committed
    void setMouseTracking( bool );
pixhawk's avatar
pixhawk committed

    class PrivateData;
    PrivateData *d_data;
};
pixhawk's avatar
pixhawk committed
#endif