qdeclarativepincharea.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtDeclarative module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** No Commercial Usage
  11. ** This file contains pre-release code and may not be distributed.
  12. ** You may use this file in accordance with the terms and conditions
  13. ** contained in the Technology Preview License Agreement accompanying
  14. ** this package.
  15. **
  16. ** GNU Lesser General Public License Usage
  17. ** Alternatively, this file may be used under the terms of the GNU Lesser
  18. ** General Public License version 2.1 as published by the Free Software
  19. ** Foundation and appearing in the file LICENSE.LGPL included in the
  20. ** packaging of this file. Please review the following information to
  21. ** ensure the GNU Lesser General Public License version 2.1 requirements
  22. ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  23. **
  24. ** In addition, as a special exception, Nokia gives you certain additional
  25. ** rights. These rights are described in the Nokia Qt LGPL Exception
  26. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  27. **
  28. ** If you have questions regarding the use of this file, please contact
  29. ** Nokia at qt-info@nokia.com.
  30. **
  31. **
  32. **
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #ifndef QDECLARATIVEPINCHAREA_H
  42. #define QDECLARATIVEPINCHAREA_H
  43. #include <QDeclarativeItem>
  44. #include <QTouchEvent>
  45. QT_BEGIN_HEADER
  46. QT_BEGIN_NAMESPACE
  47. QT_MODULE(Declarative)
  48. class QDeclarativePinch : public QObject
  49. {
  50. Q_OBJECT
  51. Q_ENUMS(Axis)
  52. Q_PROPERTY(QGraphicsObject *target READ target WRITE setTarget RESET resetTarget)
  53. Q_PROPERTY(qreal minimumScale READ minimumScale WRITE setMinimumScale NOTIFY minimumScaleChanged)
  54. Q_PROPERTY(qreal maximumScale READ maximumScale WRITE setMaximumScale NOTIFY maximumScaleChanged)
  55. Q_PROPERTY(qreal minimumRotation READ minimumRotation WRITE setMinimumRotation NOTIFY minimumRotationChanged)
  56. Q_PROPERTY(qreal maximumRotation READ maximumRotation WRITE setMaximumRotation NOTIFY maximumRotationChanged)
  57. Q_PROPERTY(Axis dragAxis READ axis WRITE setAxis NOTIFY dragAxisChanged)
  58. Q_PROPERTY(qreal minimumX READ xmin WRITE setXmin NOTIFY minimumXChanged)
  59. Q_PROPERTY(qreal maximumX READ xmax WRITE setXmax NOTIFY maximumXChanged)
  60. Q_PROPERTY(qreal minimumY READ ymin WRITE setYmin NOTIFY minimumYChanged)
  61. Q_PROPERTY(qreal maximumY READ ymax WRITE setYmax NOTIFY maximumYChanged)
  62. Q_PROPERTY(bool active READ active NOTIFY activeChanged)
  63. public:
  64. QDeclarativePinch();
  65. QGraphicsObject *target() const { return m_target; }
  66. void setTarget(QGraphicsObject *target) {
  67. if (target == m_target)
  68. return;
  69. m_target = target;
  70. emit targetChanged();
  71. }
  72. void resetTarget() {
  73. if (!m_target)
  74. return;
  75. m_target = 0;
  76. emit targetChanged();
  77. }
  78. qreal minimumScale() const { return m_minScale; }
  79. void setMinimumScale(qreal s) {
  80. if (s == m_minScale)
  81. return;
  82. m_minScale = s;
  83. emit minimumScaleChanged();
  84. }
  85. qreal maximumScale() const { return m_maxScale; }
  86. void setMaximumScale(qreal s) {
  87. if (s == m_maxScale)
  88. return;
  89. m_maxScale = s;
  90. emit maximumScaleChanged();
  91. }
  92. qreal minimumRotation() const { return m_minRotation; }
  93. void setMinimumRotation(qreal r) {
  94. if (r == m_minRotation)
  95. return;
  96. m_minRotation = r;
  97. emit minimumRotationChanged();
  98. }
  99. qreal maximumRotation() const { return m_maxRotation; }
  100. void setMaximumRotation(qreal r) {
  101. if (r == m_maxRotation)
  102. return;
  103. m_maxRotation = r;
  104. emit maximumRotationChanged();
  105. }
  106. enum Axis { NoDrag=0x00, XAxis=0x01, YAxis=0x02, XandYAxis=0x03 };
  107. Axis axis() const { return m_axis; }
  108. void setAxis(Axis a) {
  109. if (a == m_axis)
  110. return;
  111. m_axis = a;
  112. emit dragAxisChanged();
  113. }
  114. qreal xmin() const { return m_xmin; }
  115. void setXmin(qreal x) {
  116. if (x == m_xmin)
  117. return;
  118. m_xmin = x;
  119. emit minimumXChanged();
  120. }
  121. qreal xmax() const { return m_xmax; }
  122. void setXmax(qreal x) {
  123. if (x == m_xmax)
  124. return;
  125. m_xmax = x;
  126. emit maximumXChanged();
  127. }
  128. qreal ymin() const { return m_ymin; }
  129. void setYmin(qreal y) {
  130. if (y == m_ymin)
  131. return;
  132. m_ymin = y;
  133. emit minimumYChanged();
  134. }
  135. qreal ymax() const { return m_ymax; }
  136. void setYmax(qreal y) {
  137. if (y == m_ymax)
  138. return;
  139. m_ymax = y;
  140. emit maximumYChanged();
  141. }
  142. bool active() const { return m_active; }
  143. void setActive(bool a) {
  144. if (a == m_active)
  145. return;
  146. m_active = a;
  147. emit activeChanged();
  148. }
  149. signals:
  150. void targetChanged();
  151. void minimumScaleChanged();
  152. void maximumScaleChanged();
  153. void minimumRotationChanged();
  154. void maximumRotationChanged();
  155. void dragAxisChanged();
  156. void minimumXChanged();
  157. void maximumXChanged();
  158. void minimumYChanged();
  159. void maximumYChanged();
  160. void activeChanged();
  161. private:
  162. QGraphicsObject *m_target;
  163. qreal m_minScale;
  164. qreal m_maxScale;
  165. qreal m_minRotation;
  166. qreal m_maxRotation;
  167. Axis m_axis;
  168. qreal m_xmin;
  169. qreal m_xmax;
  170. qreal m_ymin;
  171. qreal m_ymax;
  172. bool m_active;
  173. };
  174. class QDeclarativePinchEvent : public QObject
  175. {
  176. Q_OBJECT
  177. Q_PROPERTY(QPointF center READ center)
  178. Q_PROPERTY(QPointF startCenter READ startCenter)
  179. Q_PROPERTY(QPointF lastCenter READ lastCenter)
  180. Q_PROPERTY(qreal scale READ scale)
  181. Q_PROPERTY(qreal lastScale READ lastScale)
  182. Q_PROPERTY(qreal angle READ angle)
  183. Q_PROPERTY(qreal lastAngle READ lastAngle)
  184. Q_PROPERTY(qreal rotation READ rotation)
  185. Q_PROPERTY(QPointF point1 READ point1)
  186. Q_PROPERTY(QPointF startPoint1 READ startPoint1)
  187. Q_PROPERTY(QPointF point2 READ point2)
  188. Q_PROPERTY(QPointF startPoint2 READ startPoint2)
  189. Q_PROPERTY(bool accepted READ accepted WRITE setAccepted)
  190. public:
  191. QDeclarativePinchEvent()
  192. : QObject(), m_center(QPointF()), m_scale(0), m_angle(0), m_rotation(0) {}
  193. QDeclarativePinchEvent(QPointF c, qreal s, qreal a, qreal r)
  194. : QObject(), m_center(c), m_scale(s), m_angle(a), m_rotation(r) {}
  195. QPointF center() const { return m_center; }
  196. QPointF startCenter() const { return m_startCenter; }
  197. void setStartCenter(QPointF c) { m_startCenter = c; }
  198. QPointF lastCenter() const { return m_lastCenter; }
  199. void setLastCenter(QPointF c) { m_lastCenter = c; }
  200. qreal scale() const { return m_scale; }
  201. qreal lastScale() const { return m_lastScale; }
  202. void setLastScale(qreal s) { m_lastScale = s; }
  203. qreal angle() const { return m_angle; }
  204. qreal lastAngle() const { return m_lastAngle; }
  205. void setLastAngle(qreal a) { m_lastAngle = a; }
  206. qreal rotation() const { return m_rotation; }
  207. QPointF point1() const { return m_point1; }
  208. void setPoint1(QPointF p) { m_point1 = p; }
  209. QPointF startPoint1() const { return m_startPoint1; }
  210. void setStartPoint1(QPointF p) { m_startPoint1 = p; }
  211. QPointF point2() const { return m_point2; }
  212. void setPoint2(QPointF p) { m_point2 = p; }
  213. QPointF startPoint2() const { return m_startPoint2; }
  214. void setStartPoint2(QPointF p) { m_startPoint2 = p; }
  215. bool accepted() const { return m_accepted; }
  216. void setAccepted(bool a) { m_accepted = a; }
  217. private:
  218. QPointF m_center;
  219. QPointF m_startCenter;
  220. QPointF m_lastCenter;
  221. qreal m_scale;
  222. qreal m_lastScale;
  223. qreal m_angle;
  224. qreal m_lastAngle;
  225. qreal m_rotation;
  226. QPointF m_point1;
  227. QPointF m_point2;
  228. QPointF m_startPoint1;
  229. QPointF m_startPoint2;
  230. bool m_accepted;
  231. };
  232. class QDeclarativeMouseEvent;
  233. class QDeclarativePinchArea : public QDeclarativeItem
  234. {
  235. Q_OBJECT
  236. Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
  237. Q_PROPERTY(QDeclarativePinch *pinch READ pinch CONSTANT)
  238. public:
  239. QDeclarativePinchArea(QDeclarativeItem *parent=0);
  240. ~QDeclarativePinchArea();
  241. bool isEnabled() const;
  242. void setEnabled(bool);
  243. QDeclarativePinch *pinch();
  244. Q_SIGNALS:
  245. void enabledChanged();
  246. void pinchStarted(QDeclarativePinchEvent *pinch);
  247. void pinchChanged(QDeclarativePinchEvent *pinch);
  248. void pinchFinished(QDeclarativePinchEvent *pinch);
  249. protected:
  250. void mousePressEvent(QGraphicsSceneMouseEvent *event);
  251. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event);
  252. void mouseMoveEvent(QGraphicsSceneMouseEvent *event);
  253. bool sceneEvent(QEvent *);
  254. bool sendMouseEvent(QGraphicsSceneMouseEvent *event);
  255. bool sceneEventFilter(QGraphicsItem *i, QEvent *e);
  256. bool event(QEvent *);
  257. virtual void geometryChanged(const QRectF &newGeometry,
  258. const QRectF &oldGeometry);
  259. virtual QVariant itemChange(GraphicsItemChange change, const QVariant& value);
  260. private:
  261. void updatePinch();
  262. void handlePress();
  263. void handleRelease();
  264. private:
  265. Q_DISABLE_COPY(QDeclarativePinchArea)
  266. protected:
  267. bool absorb : 1;
  268. bool stealMouse : 1;
  269. bool inPinch : 1;
  270. bool pinchRejected : 1;
  271. QDeclarativePinch *pinch_;
  272. QPointF sceneStartPoint1;
  273. QPointF sceneStartPoint2;
  274. QPointF lastPoint1;
  275. QPointF lastPoint2;
  276. qreal pinchStartDist;
  277. qreal pinchStartScale;
  278. qreal pinchLastScale;
  279. qreal pinchStartRotation;
  280. qreal pinchStartAngle;
  281. qreal pinchLastAngle;
  282. QPointF sceneStartCenter;
  283. QPointF pinchStartCenter;
  284. QPointF sceneLastCenter;
  285. QPointF pinchStartPos;
  286. QList<QTouchEvent::TouchPoint> touchPoints;
  287. };
  288. QT_END_NAMESPACE
  289. QML_DECLARE_TYPE(QDeclarativePinch)
  290. QML_DECLARE_TYPE(QDeclarativePinchEvent)
  291. QML_DECLARE_TYPE(QDeclarativePinchArea)
  292. QT_END_HEADER
  293. #endif // QDECLARATIVEPINCHAREA_H