qwt_picker.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_PICKER
  10. #define QWT_PICKER 1
  11. #include "qwt_global.h"
  12. #include "qwt_text.h"
  13. #include "qwt_event_pattern.h"
  14. #include <qobject.h>
  15. #include <qpen.h>
  16. #include <qfont.h>
  17. #include <qrect.h>
  18. #include <qpainterpath.h>
  19. class QWidget;
  20. class QMouseEvent;
  21. class QWheelEvent;
  22. class QKeyEvent;
  23. class QwtPickerMachine;
  24. class QwtWidgetOverlay;
  25. /*!
  26. \brief QwtPicker provides selections on a widget
  27. QwtPicker filters all enter, leave, mouse and keyboard events of a widget
  28. and translates them into an array of selected points.
  29. The way how the points are collected depends on type of state machine
  30. that is connected to the picker. Qwt offers a couple of predefined
  31. state machines for selecting:
  32. - Nothing\n
  33. QwtPickerTrackerMachine
  34. - Single points\n
  35. QwtPickerClickPointMachine, QwtPickerDragPointMachine
  36. - Rectangles\n
  37. QwtPickerClickRectMachine, QwtPickerDragRectMachine
  38. - Polygons\n
  39. QwtPickerPolygonMachine
  40. While these state machines cover the most common ways to collect points
  41. it is also possible to implement individual machines as well.
  42. QwtPicker translates the picked points into a selection using the
  43. adjustedPoints() method. adjustedPoints() is intended to be reimplemented
  44. to fix up the selection according to application specific requirements.
  45. (F.e. when an application accepts rectangles of a fixed aspect ratio only.)
  46. Optionally QwtPicker support the process of collecting points by a
  47. rubber band and tracker displaying a text for the current mouse
  48. position.
  49. \par Example
  50. \code
  51. #include <qwt_picker.h>
  52. #include <qwt_picker_machine.h>
  53. QwtPicker *picker = new QwtPicker(widget);
  54. picker->setStateMachine(new QwtPickerDragRectMachine);
  55. picker->setTrackerMode(QwtPicker::ActiveOnly);
  56. picker->setRubberBand(QwtPicker::RectRubberBand);
  57. \endcode
  58. \endpar
  59. The state machine triggers the following commands:
  60. - begin()\n
  61. Activate/Initialize the selection.
  62. - append()\n
  63. Add a new point
  64. - move() \n
  65. Change the position of the last point.
  66. - remove()\n
  67. Remove the last point.
  68. - end()\n
  69. Terminate the selection and call accept to validate the picked points.
  70. The picker is active (isActive()), between begin() and end().
  71. In active state the rubber band is displayed, and the tracker is visible
  72. in case of trackerMode is ActiveOnly or AlwaysOn.
  73. The cursor can be moved using the arrow keys. All selections can be aborted
  74. using the abort key. (QwtEventPattern::KeyPatternCode)
  75. \warning In case of QWidget::NoFocus the focus policy of the observed
  76. widget is set to QWidget::WheelFocus and mouse tracking
  77. will be manipulated while the picker is active,
  78. or if trackerMode() is AlwayOn.
  79. */
  80. class QWT_EXPORT QwtPicker: public QObject, public QwtEventPattern
  81. {
  82. Q_OBJECT
  83. Q_ENUMS( RubberBand DisplayMode ResizeMode )
  84. Q_PROPERTY( bool isEnabled READ isEnabled WRITE setEnabled )
  85. Q_PROPERTY( ResizeMode resizeMode READ resizeMode WRITE setResizeMode )
  86. Q_PROPERTY( DisplayMode trackerMode READ trackerMode WRITE setTrackerMode )
  87. Q_PROPERTY( QPen trackerPen READ trackerPen WRITE setTrackerPen )
  88. Q_PROPERTY( QFont trackerFont READ trackerFont WRITE setTrackerFont )
  89. Q_PROPERTY( RubberBand rubberBand READ rubberBand WRITE setRubberBand )
  90. Q_PROPERTY( QPen rubberBandPen READ rubberBandPen WRITE setRubberBandPen )
  91. public:
  92. /*!
  93. Rubber band style
  94. The default value is QwtPicker::NoRubberBand.
  95. \sa setRubberBand(), rubberBand()
  96. */
  97. enum RubberBand
  98. {
  99. //! No rubberband.
  100. NoRubberBand = 0,
  101. //! A horizontal line ( only for QwtPickerMachine::PointSelection )
  102. HLineRubberBand,
  103. //! A vertical line ( only for QwtPickerMachine::PointSelection )
  104. VLineRubberBand,
  105. //! A crosshair ( only for QwtPickerMachine::PointSelection )
  106. CrossRubberBand,
  107. //! A rectangle ( only for QwtPickerMachine::RectSelection )
  108. RectRubberBand,
  109. //! An ellipse ( only for QwtPickerMachine::RectSelection )
  110. EllipseRubberBand,
  111. //! A polygon ( only for QwtPickerMachine::PolygonSelection )
  112. PolygonRubberBand,
  113. /*!
  114. Values >= UserRubberBand can be used to define additional
  115. rubber bands.
  116. */
  117. UserRubberBand = 100
  118. };
  119. /*!
  120. \brief Display mode
  121. \sa setTrackerMode(), trackerMode(), isActive()
  122. */
  123. enum DisplayMode
  124. {
  125. //! Display never
  126. AlwaysOff,
  127. //! Display always
  128. AlwaysOn,
  129. //! Display only when the selection is active
  130. ActiveOnly
  131. };
  132. /*!
  133. Controls what to do with the selected points of an active
  134. selection when the observed widget is resized.
  135. The default value is QwtPicker::Stretch.
  136. \sa setResizeMode()
  137. */
  138. enum ResizeMode
  139. {
  140. //! All points are scaled according to the new size,
  141. Stretch,
  142. //! All points remain unchanged.
  143. KeepSize
  144. };
  145. explicit QwtPicker( QWidget *parent );
  146. explicit QwtPicker( RubberBand rubberBand,
  147. DisplayMode trackerMode, QWidget * );
  148. virtual ~QwtPicker();
  149. void setStateMachine( QwtPickerMachine * );
  150. const QwtPickerMachine *stateMachine() const;
  151. QwtPickerMachine *stateMachine();
  152. void setRubberBand( RubberBand );
  153. RubberBand rubberBand() const;
  154. void setTrackerMode( DisplayMode );
  155. DisplayMode trackerMode() const;
  156. void setResizeMode( ResizeMode );
  157. ResizeMode resizeMode() const;
  158. void setRubberBandPen( const QPen & );
  159. QPen rubberBandPen() const;
  160. void setTrackerPen( const QPen & );
  161. QPen trackerPen() const;
  162. void setTrackerFont( const QFont & );
  163. QFont trackerFont() const;
  164. bool isEnabled() const;
  165. bool isActive() const;
  166. virtual bool eventFilter( QObject *, QEvent * );
  167. QWidget *parentWidget();
  168. const QWidget *parentWidget() const;
  169. virtual QPainterPath pickArea() const;
  170. virtual void drawRubberBand( QPainter * ) const;
  171. virtual void drawTracker( QPainter * ) const;
  172. virtual QRegion rubberBandMask() const;
  173. virtual QwtText trackerText( const QPoint &pos ) const;
  174. QPoint trackerPosition() const;
  175. virtual QRect trackerRect( const QFont & ) const;
  176. QPolygon selection() const;
  177. public Q_SLOTS:
  178. void setEnabled( bool );
  179. Q_SIGNALS:
  180. /*!
  181. A signal indicating, when the picker has been activated.
  182. Together with setEnabled() it can be used to implement
  183. selections with more than one picker.
  184. \param on True, when the picker has been activated
  185. */
  186. void activated( bool on );
  187. /*!
  188. A signal emitting the selected points,
  189. at the end of a selection.
  190. \param polygon Selected points
  191. */
  192. void selected( const QPolygon &polygon );
  193. /*!
  194. A signal emitted when a point has been appended to the selection
  195. \param pos Position of the appended point.
  196. \sa append(). moved()
  197. */
  198. void appended( const QPoint &pos );
  199. /*!
  200. A signal emitted whenever the last appended point of the
  201. selection has been moved.
  202. \param pos Position of the moved last point of the selection.
  203. \sa move(), appended()
  204. */
  205. void moved( const QPoint &pos );
  206. /*!
  207. A signal emitted whenever the last appended point of the
  208. selection has been removed.
  209. \param pos Position of the point, that has been removed
  210. \sa remove(), appended()
  211. */
  212. void removed( const QPoint &pos );
  213. /*!
  214. A signal emitted when the active selection has been changed.
  215. This might happen when the observed widget is resized.
  216. \param selection Changed selection
  217. \sa stretchSelection()
  218. */
  219. void changed( const QPolygon &selection );
  220. protected:
  221. virtual QPolygon adjustedPoints( const QPolygon & ) const;
  222. virtual void transition( const QEvent * );
  223. virtual void begin();
  224. virtual void append( const QPoint & );
  225. virtual void move( const QPoint & );
  226. virtual void remove();
  227. virtual bool end( bool ok = true );
  228. virtual bool accept( QPolygon & ) const;
  229. virtual void reset();
  230. virtual void widgetMousePressEvent( QMouseEvent * );
  231. virtual void widgetMouseReleaseEvent( QMouseEvent * );
  232. virtual void widgetMouseDoubleClickEvent( QMouseEvent * );
  233. virtual void widgetMouseMoveEvent( QMouseEvent * );
  234. virtual void widgetWheelEvent( QWheelEvent * );
  235. virtual void widgetKeyPressEvent( QKeyEvent * );
  236. virtual void widgetKeyReleaseEvent( QKeyEvent * );
  237. virtual void widgetEnterEvent( QEvent * );
  238. virtual void widgetLeaveEvent( QEvent * );
  239. virtual void stretchSelection(
  240. const QSize &oldSize, const QSize &newSize );
  241. virtual void updateDisplay();
  242. const QwtWidgetOverlay *rubberBandOverlay() const;
  243. const QwtWidgetOverlay *trackerOverlay() const;
  244. const QPolygon &pickedPoints() const;
  245. private:
  246. void init( QWidget *, RubberBand rubberBand, DisplayMode trackerMode );
  247. void setMouseTracking( bool );
  248. class PrivateData;
  249. PrivateData *d_data;
  250. };
  251. #endif