Qwt supports image pickup.
Qwteventpattern class: a set of qwt event patterns. Qwteventpattern introduces an indirect layer for mouse and keyboard input. They can be represented by different symbol names, so the application code can be configured by setting the ing relationship separately. The qwteventpattern class defines the mouse key during initialization. The keyboard key corresponds to the ing between the mouse and the keyboard enumeration variables in QT. Of course, we can also re-specify their mappings through the Property setting interface.
Qwtpicker class: inherits from qwteventpattern and provides the option (pick up) function on a component (qwidget.
Qwtplotpicker class: inherits from qwtpicker and is a qwtpicker customized on qwtplotcanvas.
Qwtplotzoomer class: inherited from qwtplotpicker. The qwtplotzoomer class provides the scaling function for a plot component and saves each scaling state to the stack. Therefore, it can be undone and restored at any time.
Code Analysis:
I. qwteventpattern class:
The qwteventpattern class defines two public Nested classes (also known as the nested type, nested class ):
//! A pattern for mouse events class MousePattern { public: //! Constructor MousePattern( int btn = Qt::NoButton, int st = Qt::NoButton ) { button = btn; state = st; } //! Button code int button; //! State int state; }; //! A pattern for key events class KeyPattern { public: //! Constructor KeyPattern( int k = 0, int st = Qt::NoButton ) { key = k; state = st; } //! Key code int key; //! State int state; };
This type is used only in the qwteventpattern class, so it is defined as a nested class of qwteventpattern. However, since the two nested classes need to define global equal comparison operator functions, they are defined as public access permissions. In this way, the global scope can be referenced by the scope qualifier qwteventpattern. See the following code:
//! Compare operatorinline bool operator==( QwtEventPattern::MousePattern b1, QwtEventPattern::MousePattern b2 ){ return b1.button == b2.button && b1.state == b2.state;}//! Compare operatorinline bool operator==( QwtEventPattern::KeyPattern b1, QwtEventPattern::KeyPattern b2 ){ return b1.key == b2.key && b1.state == b2.state;}
Ii. qwtpicker class:
A very important class in the qwt picking mechanism of the qwtpicker class. The qwt document describes the qwtpicker class as follows:
Qwtpicker provides
Selections onA widget.
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:
- Nothing
Qwtpickertrackermachine
- Single points
Qwtpickerclickpointmachine, qwtpickerdragpointmachine
- Rectangles
Qwtpickerclickrectmachine, qwtpickerdragrectmachine
- Polygons
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 fixup 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 rubberband and tracker displaying a text for the current mouse position.
-
Example
-
#include <qwt_picker.h>#include <qwt_picker_machine.h>QwtPicker *picker = new QwtPicker(widget);picker->setStateMachine(new QwtPickerDragRectMachine);picker->setTrackerMode(QwtPicker::ActiveOnly);picker->setRubberBand(QwtPicker::RectRubberBand);
The State Machine Triggers the following commands:
- Begin ()
Activate/initialize the selection.
- Append ()
Add a new point
- Move ()
Change the position of the last point.
- Remove ()
Remove the last point.
- End ()
Terminate the selection and call accept to validate the picked points.
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 while the picker is active, or if trackermode () is
Alwayon.
Code Analysis:
1. constructor:
/*! Constructor Creates an picker that is enabled, but without a state machine. rubberband and tracker are disabled. \param parent Parent widget, that will be observed */QwtPicker::QwtPicker( QWidget *parent ): QObject( parent ){ init( parent, NoRubberBand, AlwaysOff );}
Note: 1) There is no state machine. Therefore, you need to set it through setstatemachine (qwtpickermachine * sm.
2) rubberband and tracker are disabled.
2. Set the state machine:
/*! Set a state machine and delete the previous one \param stateMachine State machine \sa stateMachine()*/void QwtPicker::setStateMachine( QwtPickerMachine *stateMachine ){ if ( d_data->stateMachine != stateMachine ) { reset(); delete d_data->stateMachine; d_data->stateMachine = stateMachine; if ( d_data->stateMachine ) d_data->stateMachine->reset(); }}
Note: 1) The qwtpicker class and qwtplotpicker class do not initialize the state machine during instantiation. Therefore, you must set the state machine separately.
2) Note that the parameter type qwtpickermachine * Sm in the setstatemachine (qwtpickermachine * sm) function is an interface class and an abstract base class for all other State Machine types. In this way, we can use this interface to set any type of state machine, including the custom state machine types (as long as they are derived from qwtpickermachine ). This design also fully demonstrates a principle of object-oriented design: Dependency inversion principle, that is, interface-oriented programming, rather than implementation programming.
3. Several Important Function interfaces:
Virtual qrect pickrect () const; virtual void drawrubberband (qpainter *) const; // draw rubberband, which can be horizontal, vertical, cross, rectangular, elliptical, virtual void drawtracker (qpainter *) const with multiple shapes such as polygon; // draw virtual qwttext trackertext (const qpoint & Pos) Labels (track point text) that display the cursor position) const; // return the text tag qwttext of the mouse point. The text tag qwttext qpoint trackerposition () const and virtual qrect trackerrect (const qfont &) const of different styles can be implemented through re-implementation; qpolygon selection () const; // polygon of the selected area
4. The qwtpicker class provides a variety of attribute setting interfaces. You can set different attributes to get a nice style. In addition, the qwtpicker class also provides several important signals, which makes it easy for us to capture relevant feature parameters during the pick-up process.