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, a rectangle or a polygon. The selection process is supported by optional rubberbands (rubberband selection) and position trackers.
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.
#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); ... // now select rectangles picker->setSelectionFlags(QwtPicker::RectSelection | QwtPicker::DragSelection); picker->setRubberBand(QwtPicker::RectRubberBand);
The commands are initiated from a small state machine (QwtPickerMachine) that translates mouse and key events. There are a couple of predefined state machines for point, rect and polygon selections. The selectionFlags() control which one should be used. It is possible to use other machines 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)
Definition at line 80 of file qwt_picker.h.
Public Types | |
enum | SelectionType { NoSelection = 0, PointSelection = 1, RectSelection = 2, PolygonSelection = 4 } |
enum | RectSelectionType { CornerToCorner = 64, CenterToCorner = 128, CenterToRadius = 256 } |
enum | SelectionMode { ClickSelection = 1024, DragSelection = 2048 } |
enum | RubberBand { NoRubberBand = 0, HLineRubberBand, VLineRubberBand, CrossRubberBand, RectRubberBand, EllipseRubberBand, PolygonRubberBand, UserRubberBand = 100 } |
enum | DisplayMode { AlwaysOff, AlwaysOn, ActiveOnly, ImageMode = 1, ContourMode = 2 } |
enum | ResizeMode { Stretch, KeepSize } |
Signals | |
void | selected (const QwtPolygon &pa) |
void | appended (const QPoint &pos) |
void | moved (const QPoint &pos) |
void | changed (const QwtPolygon &pa) |
Public Member Functions | |
QwtPicker (QWidget *parent) | |
QwtPicker (int selectionFlags, RubberBand rubberBand, DisplayMode trackerMode, QWidget *) | |
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 |
const QwtPolygon & | selection () const |
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 |
Protected Member Functions | |
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 *) |
virtual void | widgetMouseDoubleClickEvent (QMouseEvent *) |
virtual void | widgetMouseMoveEvent (QMouseEvent *) |
virtual void | widgetWheelEvent (QWheelEvent *) |
virtual void | widgetKeyPressEvent (QKeyEvent *) |
virtual void | widgetKeyReleaseEvent (QKeyEvent *) |
virtual void | widgetLeaveEvent (QEvent *) |
virtual void | stretchSelection (const QSize &oldSize, const QSize &newSize) |
virtual QwtPickerMachine * | stateMachine (int) const |
virtual void | updateDisplay () |
const QWidget * | rubberBandWidget () const |
const QWidget * | trackerWidget () const |
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()
The default value is NoSelection.
Definition at line 117 of file qwt_picker.h.
Selection subtype for RectSelection This enum type describes the type of rectangle selections. It can be or'd with QwtPicker::RectSelectionType and QwtPicker::SelectionMode and passed to QwtPicker::setSelectionFlags().
The default value is CornerToCorner.
Definition at line 143 of file qwt_picker.h.
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.
Definition at line 157 of file qwt_picker.h.
Rubberband style
The default value is NoRubberBand.
Definition at line 187 of file qwt_picker.h.
Definition at line 217 of file qwt_picker.h.
Controls what to do with the selected points of an active selection when the observed widget is resized.
The default value is Stretch.
Definition at line 236 of file qwt_picker.h.
QwtPicker::QwtPicker | ( | QWidget * | parent | ) | [explicit] |
Constructor
Creates an picker that is enabled, but where selection flag is set to NoSelection, rubberband and tracker are disabled.
parent | Parent widget, that will be observed |
Definition at line 249 of file qwt_picker.cpp.
QwtPicker::QwtPicker | ( | int | selectionFlags, | |
RubberBand | rubberBand, | |||
DisplayMode | trackerMode, | |||
QWidget * | parent | |||
) | [explicit] |
Constructor
selectionFlags | Or'd value of SelectionType, RectSelectionType and SelectionMode | |
rubberBand | Rubberband style | |
trackerMode | Tracker mode | |
parent | Parent widget, that will be observed |
Definition at line 264 of file qwt_picker.cpp.
QwtPicker::~QwtPicker | ( | ) | [virtual] |
void QwtPicker::setSelectionFlags | ( | int | flags | ) | [virtual] |
Set the selection flags
flags | Or'd value of SelectionType, RectSelectionType and SelectionMode. The default value is NoSelection. |
Reimplemented in QwtPlotZoomer.
Definition at line 403 of file qwt_picker.cpp.
References stateMachine().
Referenced by QwtPlotZoomer::setSelectionFlags().
int QwtPicker::selectionFlags | ( | ) | const |
Definition at line 414 of file qwt_picker.cpp.
Referenced by drawRubberBand(), and QwtPlotPicker::end().
void QwtPicker::setRubberBand | ( | RubberBand | rubberBand | ) | [virtual] |
Set the rubberband style
rubberBand | Rubberband style The default value is NoRubberBand. |
Definition at line 427 of file qwt_picker.cpp.
QwtPicker::RubberBand QwtPicker::rubberBand | ( | ) | const |
Definition at line 436 of file qwt_picker.cpp.
Referenced by drawRubberBand(), trackerRect(), QwtPlotPicker::trackerText(), trackerText(), and updateDisplay().
void QwtPicker::setTrackerMode | ( | DisplayMode | mode | ) | [virtual] |
Set the display mode of the tracker.
A tracker displays information about current position of the cursor as a string. The display mode controls if the tracker has to be displayed whenever the observed widget has focus and cursor (AlwaysOn), never (AlwaysOff), or only when the selection is active (ActiveOnly).
mode | Tracker display mode |
Definition at line 457 of file qwt_picker.cpp.
QwtPicker::DisplayMode QwtPicker::trackerMode | ( | ) | const |
Definition at line 470 of file qwt_picker.cpp.
Referenced by begin(), end(), trackerRect(), and updateDisplay().
void QwtPicker::setResizeMode | ( | ResizeMode | mode | ) | [virtual] |
Set the resize mode.
The resize mode controls what to do with the selected points of an active selection when the observed widget is resized.
Stretch means the points are scaled according to the new size, KeepSize means the points remain unchanged.
The default mode is Stretch.
mode | Resize mode |
Definition at line 489 of file qwt_picker.cpp.
QwtPicker::ResizeMode QwtPicker::resizeMode | ( | ) | const |
Definition at line 499 of file qwt_picker.cpp.
void QwtPicker::setRubberBandPen | ( | const QPen & | pen | ) | [virtual] |
Set the pen for the rubberband
pen | Rubberband pen |
Definition at line 597 of file qwt_picker.cpp.
References updateDisplay().
QPen QwtPicker::rubberBandPen | ( | ) | const |
Definition at line 610 of file qwt_picker.cpp.
Referenced by drawRubberBand(), and updateDisplay().
void QwtPicker::setTrackerPen | ( | const QPen & | pen | ) | [virtual] |
Set the pen for the tracker
pen | Tracker pen |
Definition at line 573 of file qwt_picker.cpp.
References updateDisplay().
QPen QwtPicker::trackerPen | ( | ) | const |
Definition at line 586 of file qwt_picker.cpp.
Referenced by updateDisplay().
void QwtPicker::setTrackerFont | ( | const QFont & | font | ) | [virtual] |
Set the font for the tracker
font | Tracker font |
Definition at line 548 of file qwt_picker.cpp.
References updateDisplay().
QFont QwtPicker::trackerFont | ( | ) | const |
Definition at line 562 of file qwt_picker.cpp.
bool QwtPicker::isEnabled | ( | ) | const |
Definition at line 537 of file qwt_picker.cpp.
void QwtPicker::setEnabled | ( | bool | enabled | ) | [virtual] |
En/disable the picker.
When enabled is true an event filter is installed for the observed widget, otherwise the event filter is removed.
enabled | true or false |
Definition at line 513 of file qwt_picker.cpp.
References parentWidget(), and updateDisplay().
bool QwtPicker::isActive | ( | ) | const |
A picker is active between begin() and end().
Definition at line 1265 of file qwt_picker.cpp.
Referenced by drawRubberBand(), reset(), trackerRect(), updateDisplay(), QwtPlotZoomer::widgetKeyPressEvent(), widgetLeaveEvent(), and widgetMouseMoveEvent().
bool QwtPicker::eventFilter | ( | QObject * | o, | |
QEvent * | e | |||
) | [virtual] |
Event filter.
When isEnabled() == true all events of the observed widget are filtered. Mouse and keyboard events are translated into widgetMouse- and widgetKey- and widgetWheel-events. Paint and Resize events are handled to keep rubberband and tracker up to date.
Definition at line 852 of file qwt_picker.cpp.
References parentWidget(), stretchSelection(), widgetKeyPressEvent(), widgetKeyReleaseEvent(), widgetLeaveEvent(), widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(), widgetMousePressEvent(), widgetMouseReleaseEvent(), and widgetWheelEvent().
QWidget * QwtPicker::parentWidget | ( | ) |
Return the parent widget, where the selection happens.
Definition at line 375 of file qwt_picker.cpp.
Referenced by begin(), QwtPlotPicker::canvas(), eventFilter(), pickRect(), setEnabled(), transition(), updateDisplay(), and widgetKeyPressEvent().
const QWidget * QwtPicker::parentWidget | ( | ) | const |
Return the parent widget, where the selection happens.
Definition at line 385 of file qwt_picker.cpp.
QRect QwtPicker::pickRect | ( | ) | const [virtual] |
Find the area of the observed widget, where selection might happen.
Definition at line 1344 of file qwt_picker.cpp.
References parentWidget().
Referenced by drawRubberBand(), trackerRect(), widgetKeyPressEvent(), widgetMouseMoveEvent(), and widgetWheelEvent().
const QwtPolygon & QwtPicker::selection | ( | ) | const |
Return Selected points.
Definition at line 1271 of file qwt_picker.cpp.
Referenced by QwtPlotZoomer::end(), and QwtPlotPicker::end().
void QwtPicker::drawRubberBand | ( | QPainter * | painter | ) | const [virtual] |
Draw a rubberband , depending on rubberBand() and selectionFlags()
painter | Painter, initialized with clip rect |
Definition at line 654 of file qwt_picker.cpp.
References QwtPainter::drawEllipse(), QwtPainter::drawLine(), QwtPainter::drawRect(), isActive(), pickRect(), rubberBand(), rubberBandPen(), and selectionFlags().
void QwtPicker::drawTracker | ( | QPainter * | painter | ) | const [virtual] |
Draw the tracker
painter | Painter |
Definition at line 749 of file qwt_picker.cpp.
References QwtText::draw(), QwtText::isEmpty(), QwtText::setFont(), trackerRect(), trackerText(), and QwtText::usedFont().
QwtText QwtPicker::trackerText | ( | const QPoint & | pos | ) | const [virtual] |
Return the label for a position.
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 string conversion is "%d".
pos | Position |
Reimplemented in QwtPlotPicker.
Definition at line 628 of file qwt_picker.cpp.
References rubberBand().
Referenced by drawTracker(), and trackerRect().
void QwtPicker::selected | ( | const QwtPolygon & | pa | ) | [signal] |
A signal emitting the selected points, at the end of a selection.
pa | Selected points |
Referenced by end().
void QwtPicker::appended | ( | const QPoint & | pos | ) | [signal] |
void QwtPicker::moved | ( | const QPoint & | pos | ) | [signal] |
A signal emitted whenever the last appended point of the selection has been moved.
pos | Position of the moved last point of the selection. |
Referenced by move().
void QwtPicker::changed | ( | const QwtPolygon & | pa | ) | [signal] |
A signal emitted when the active selection has been changed. This might happen when the observed widget is resized.
pa | Changed selection |
Referenced by stretchSelection().
bool QwtPicker::accept | ( | QwtPolygon & | selection | ) | const [protected, virtual] |
Validate and fixup the selection.
Accepts all selections unmodified
selection | Selection to validate and fixup |
Reimplemented in QwtPlotZoomer.
Definition at line 1256 of file qwt_picker.cpp.
Referenced by end().
void QwtPicker::transition | ( | const QEvent * | e | ) | [protected, virtual] |
Passes an event to the state machine and executes the resulting commands. Append and Move commands use the current position of the cursor (QCursor::pos()).
e | Event |
Definition at line 1079 of file qwt_picker.cpp.
References append(), begin(), end(), move(), and parentWidget().
Referenced by widgetKeyPressEvent(), widgetKeyReleaseEvent(), widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(), widgetMousePressEvent(), widgetMouseReleaseEvent(), and widgetWheelEvent().
void QwtPicker::begin | ( | ) | [protected, virtual] |
Open a selection setting the state to active
Reimplemented in QwtPlotZoomer.
Definition at line 1136 of file qwt_picker.cpp.
References parentWidget(), trackerMode(), and updateDisplay().
Referenced by QwtPlotZoomer::begin(), and transition().
void QwtPicker::append | ( | const QPoint & | pos | ) | [protected, virtual] |
Append a point to the selection and update rubberband and tracker. The appended() signal is emitted.
pos | Additional point |
Reimplemented in QwtPlotPicker.
Definition at line 1215 of file qwt_picker.cpp.
References appended(), and updateDisplay().
Referenced by QwtPlotPicker::append(), and transition().
void QwtPicker::move | ( | const QPoint & | pos | ) | [protected, virtual] |
Move the last point of the selection The moved() signal is emitted.
pos | New position |
Reimplemented in QwtPlotPicker.
Definition at line 1237 of file qwt_picker.cpp.
References moved(), and updateDisplay().
Referenced by QwtPlotPicker::move(), and transition().
bool QwtPicker::end | ( | bool | ok = true |
) | [protected, virtual] |
Close a selection setting the state to inactive.
The selection is validated and maybe fixed by QwtPicker::accept().
ok | If true, complete the selection and emit a selected signal otherwise discard the selection. |
Reimplemented in QwtPlotPicker, and QwtPlotZoomer.
Definition at line 1168 of file qwt_picker.cpp.
References accept(), selected(), trackerMode(), and updateDisplay().
Referenced by QwtPlotPicker::end(), reset(), and transition().
void QwtPicker::reset | ( | ) | [protected, virtual] |
Reset the state machine and terminate (end(false)) the selection
Definition at line 1198 of file qwt_picker.cpp.
References end(), and isActive().
Referenced by widgetKeyPressEvent().
void QwtPicker::widgetMousePressEvent | ( | QMouseEvent * | e | ) | [protected, virtual] |
Handle a mouse press event for the observed widget.
Begin and/or end a selection depending on the selection flags.
eventFilter(), widgetMouseReleaseEvent(), widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
Definition at line 912 of file qwt_picker.cpp.
References transition().
Referenced by eventFilter().
void QwtPicker::widgetMouseReleaseEvent | ( | QMouseEvent * | e | ) | [protected, virtual] |
Handle a mouse relase event for the observed widget.
End a selection depending on the selection flags.
eventFilter(), widgetMousePressEvent(), widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyPressEvent(), widgetKeyReleaseEvent()
Reimplemented in QwtPlotZoomer.
Definition at line 963 of file qwt_picker.cpp.
References transition().
Referenced by eventFilter(), and QwtPlotZoomer::widgetMouseReleaseEvent().
void QwtPicker::widgetMouseDoubleClickEvent | ( | QMouseEvent * | me | ) | [protected, virtual] |
Handle mouse double click event for the observed widget.
Empty implementation, does nothing.
Definition at line 977 of file qwt_picker.cpp.
References transition().
Referenced by eventFilter().
void QwtPicker::widgetMouseMoveEvent | ( | QMouseEvent * | e | ) | [protected, virtual] |
Handle a mouse move event for the observed widget.
Move the last point of the selection in case of isActive() == true
Definition at line 926 of file qwt_picker.cpp.
References isActive(), pickRect(), transition(), and updateDisplay().
Referenced by eventFilter().
void QwtPicker::widgetWheelEvent | ( | QWheelEvent * | e | ) | [protected, virtual] |
Handle a wheel event for the observed widget.
Move the last point of the selection in case of isActive() == true
Definition at line 992 of file qwt_picker.cpp.
References pickRect(), transition(), and updateDisplay().
Referenced by eventFilter().
void QwtPicker::widgetKeyPressEvent | ( | QKeyEvent * | ke | ) | [protected, virtual] |
Handle a key press event for the observed widget.
Selections can be completely done by the keyboard. The arrow keys move the cursor, the abort key aborts a selection. All other keys are handled by the current state machine.
eventFilter(), widgetMousePressEvent(), widgetMouseReleaseEvent(), widgetMouseDoubleClickEvent(), widgetMouseMoveEvent(), widgetWheelEvent(), widgetKeyReleaseEvent(), stateMachine(), QwtEventPattern::KeyPatternCode
Reimplemented in QwtPlotZoomer.
Definition at line 1017 of file qwt_picker.cpp.
References QwtEventPattern::keyMatch(), parentWidget(), pickRect(), reset(), and transition().
Referenced by eventFilter(), and QwtPlotZoomer::widgetKeyPressEvent().
void QwtPicker::widgetKeyReleaseEvent | ( | QKeyEvent * | ke | ) | [protected, virtual] |
Handle a key release event for the observed widget.
Passes the event to the state machine.
Definition at line 1067 of file qwt_picker.cpp.
References transition().
Referenced by eventFilter().
void QwtPicker::widgetLeaveEvent | ( | QEvent * | ) | [protected, virtual] |
Handle a leave event for the observed widget.
Definition at line 946 of file qwt_picker.cpp.
References isActive(), and updateDisplay().
Referenced by eventFilter().
void QwtPicker::stretchSelection | ( | const QSize & | oldSize, | |
const QSize & | newSize | |||
) | [protected, virtual] |
Scale the selection by the ratios of oldSize and newSize The changed() signal is emitted.
oldSize | Previous size | |
newSize | Current size |
Definition at line 1285 of file qwt_picker.cpp.
References changed().
Referenced by eventFilter().
QwtPickerMachine * QwtPicker::stateMachine | ( | int | flags | ) | const [protected, virtual] |
Create a state machine depending on the selection flags.
Definition at line 351 of file qwt_picker.cpp.
Referenced by setSelectionFlags().