qtesttouch.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the QtTest module of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #ifndef QTESTTOUCH_H
  40. #define QTESTTOUCH_H
  41. #if 0
  42. // inform syncqt
  43. #pragma qt_no_master_include
  44. #endif
  45. #include <QtTest/qtest_global.h>
  46. #include <QtTest/qtestassert.h>
  47. #include <QtTest/qtestsystem.h>
  48. #include <QtTest/qtestspontaneevent.h>
  49. #include <QtCore/qmap.h>
  50. #include <QtGui/qevent.h>
  51. #include <QtGui/qwindow.h>
  52. #ifdef QT_WIDGETS_LIB
  53. #include <QtWidgets/qwidget.h>
  54. #endif
  55. QT_BEGIN_NAMESPACE
  56. Q_GUI_EXPORT void qt_handleTouchEvent(QWindow *w, QTouchDevice *device,
  57. const QList<QTouchEvent::TouchPoint> &points,
  58. Qt::KeyboardModifiers mods = Qt::NoModifier);
  59. namespace QTest
  60. {
  61. class QTouchEventSequence
  62. {
  63. public:
  64. ~QTouchEventSequence()
  65. {
  66. if (commitWhenDestroyed)
  67. commit();
  68. }
  69. QTouchEventSequence& press(int touchId, const QPoint &pt, QWindow *window = Q_NULLPTR)
  70. {
  71. QTouchEvent::TouchPoint &p = point(touchId);
  72. p.setScreenPos(mapToScreen(window, pt));
  73. p.setState(Qt::TouchPointPressed);
  74. return *this;
  75. }
  76. QTouchEventSequence& move(int touchId, const QPoint &pt, QWindow *window = Q_NULLPTR)
  77. {
  78. QTouchEvent::TouchPoint &p = point(touchId);
  79. p.setScreenPos(mapToScreen(window, pt));
  80. p.setState(Qt::TouchPointMoved);
  81. return *this;
  82. }
  83. QTouchEventSequence& release(int touchId, const QPoint &pt, QWindow *window = Q_NULLPTR)
  84. {
  85. QTouchEvent::TouchPoint &p = point(touchId);
  86. p.setScreenPos(mapToScreen(window, pt));
  87. p.setState(Qt::TouchPointReleased);
  88. return *this;
  89. }
  90. QTouchEventSequence& stationary(int touchId)
  91. {
  92. QTouchEvent::TouchPoint &p = pointOrPreviousPoint(touchId);
  93. p.setState(Qt::TouchPointStationary);
  94. return *this;
  95. }
  96. #ifdef QT_WIDGETS_LIB
  97. QTouchEventSequence& press(int touchId, const QPoint &pt, QWidget *widget = Q_NULLPTR)
  98. {
  99. QTouchEvent::TouchPoint &p = point(touchId);
  100. p.setScreenPos(mapToScreen(widget, pt));
  101. p.setState(Qt::TouchPointPressed);
  102. return *this;
  103. }
  104. QTouchEventSequence& move(int touchId, const QPoint &pt, QWidget *widget = Q_NULLPTR)
  105. {
  106. QTouchEvent::TouchPoint &p = point(touchId);
  107. p.setScreenPos(mapToScreen(widget, pt));
  108. p.setState(Qt::TouchPointMoved);
  109. return *this;
  110. }
  111. QTouchEventSequence& release(int touchId, const QPoint &pt, QWidget *widget = Q_NULLPTR)
  112. {
  113. QTouchEvent::TouchPoint &p = point(touchId);
  114. p.setScreenPos(mapToScreen(widget, pt));
  115. p.setState(Qt::TouchPointReleased);
  116. return *this;
  117. }
  118. #endif
  119. void commit(bool processEvents = true)
  120. {
  121. if (!points.isEmpty()) {
  122. qSleep(1);
  123. if (targetWindow)
  124. {
  125. qt_handleTouchEvent(targetWindow, device, points.values());
  126. }
  127. #ifdef QT_WIDGETS_LIB
  128. else if (targetWidget)
  129. {
  130. qt_handleTouchEvent(targetWidget->windowHandle(), device, points.values());
  131. }
  132. #endif
  133. }
  134. if (processEvents)
  135. QCoreApplication::processEvents();
  136. previousPoints = points;
  137. points.clear();
  138. }
  139. private:
  140. #ifdef QT_WIDGETS_LIB
  141. QTouchEventSequence(QWidget *widget, QTouchDevice *aDevice, bool autoCommit)
  142. : targetWidget(widget), targetWindow(Q_NULLPTR), device(aDevice), commitWhenDestroyed(autoCommit)
  143. {
  144. }
  145. #endif
  146. QTouchEventSequence(QWindow *window, QTouchDevice *aDevice, bool autoCommit)
  147. :
  148. #ifdef QT_WIDGETS_LIB
  149. targetWidget(Q_NULLPTR),
  150. #endif
  151. targetWindow(window), device(aDevice), commitWhenDestroyed(autoCommit)
  152. {
  153. }
  154. QTouchEvent::TouchPoint &point(int touchId)
  155. {
  156. if (!points.contains(touchId))
  157. points[touchId] = QTouchEvent::TouchPoint(touchId);
  158. return points[touchId];
  159. }
  160. QTouchEvent::TouchPoint &pointOrPreviousPoint(int touchId)
  161. {
  162. if (!points.contains(touchId)) {
  163. if (previousPoints.contains(touchId))
  164. points[touchId] = previousPoints.value(touchId);
  165. else
  166. points[touchId] = QTouchEvent::TouchPoint(touchId);
  167. }
  168. return points[touchId];
  169. }
  170. #ifdef QT_WIDGETS_LIB
  171. QPoint mapToScreen(QWidget *widget, const QPoint &pt)
  172. {
  173. if (widget)
  174. return widget->mapToGlobal(pt);
  175. return targetWidget ? targetWidget->mapToGlobal(pt) : pt;
  176. }
  177. #endif
  178. QPoint mapToScreen(QWindow *window, const QPoint &pt)
  179. {
  180. if(window)
  181. return window->mapToGlobal(pt);
  182. return targetWindow ? targetWindow->mapToGlobal(pt) : pt;
  183. }
  184. QMap<int, QTouchEvent::TouchPoint> previousPoints;
  185. QMap<int, QTouchEvent::TouchPoint> points;
  186. #ifdef QT_WIDGETS_LIB
  187. QWidget *targetWidget;
  188. #endif
  189. QWindow *targetWindow;
  190. QTouchDevice *device;
  191. bool commitWhenDestroyed;
  192. #ifdef QT_WIDGETS_LIB
  193. friend QTouchEventSequence touchEvent(QWidget *, QTouchDevice*, bool);
  194. #endif
  195. friend QTouchEventSequence touchEvent(QWindow *, QTouchDevice*, bool);
  196. };
  197. #ifdef QT_WIDGETS_LIB
  198. inline
  199. QTouchEventSequence touchEvent(QWidget *widget,
  200. QTouchDevice *device,
  201. bool autoCommit = true)
  202. {
  203. return QTouchEventSequence(widget, device, autoCommit);
  204. }
  205. #endif
  206. inline
  207. QTouchEventSequence touchEvent(QWindow *window,
  208. QTouchDevice *device,
  209. bool autoCommit = true)
  210. {
  211. return QTouchEventSequence(window, device, autoCommit);
  212. }
  213. }
  214. QT_END_NAMESPACE
  215. #endif // QTESTTOUCH_H