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
40#ifndef QTESTTOUCH_H
41#define QTESTTOUCH_H
42
43#if 0
44// inform syncqt
45#pragma qt_no_master_include
46#endif
47
48#include <QtTest/qttestglobal.h>
49#include <QtTest/qtestassert.h>
50#include <QtTest/qtestsystem.h>
51#include <QtTest/qtestspontaneevent.h>
52#include <QtCore/qmap.h>
53#include <QtGui/qevent.h>
54#include <QtGui/qwindow.h>
55#ifdef QT_WIDGETS_LIB
56#include <QtWidgets/qwidget.h>
57#endif
58
59QT_BEGIN_NAMESPACE
60
61Q_GUI_EXPORT void qt_handleTouchEvent(QWindow *w, QTouchDevice *device,
62 const QList<QTouchEvent::TouchPoint> &points,
63 Qt::KeyboardModifiers mods = Qt::NoModifier);
64
65
66namespace QTest
67{
68 Q_GUI_EXPORT QTouchDevice * createTouchDevice(QTouchDevice::DeviceType devType = QTouchDevice::TouchScreen);
69
70 class QTouchEventSequence
71 {
72 public:
73 ~QTouchEventSequence()
74 {
75 if (commitWhenDestroyed)
76 commit();
77 }
78 QTouchEventSequence& press(int touchId, const QPoint &pt, QWindow *window = nullptr)
79 {
80 QTouchEvent::TouchPoint &p = point(touchId);
81 p.setScreenPos(mapToScreen(window, pt));
82 p.setState(Qt::TouchPointPressed);
83 return *this;
84 }
85 QTouchEventSequence& move(int touchId, const QPoint &pt, QWindow *window = nullptr)
86 {
87 QTouchEvent::TouchPoint &p = point(touchId);
88 p.setScreenPos(mapToScreen(window, pt));
89 p.setState(Qt::TouchPointMoved);
90 return *this;
91 }
92 QTouchEventSequence& release(int touchId, const QPoint &pt, QWindow *window = nullptr)
93 {
94 QTouchEvent::TouchPoint &p = point(touchId);
95 p.setScreenPos(mapToScreen(window, pt));
96 p.setState(Qt::TouchPointReleased);
97 return *this;
98 }
99 QTouchEventSequence& stationary(int touchId)
100 {
101 QTouchEvent::TouchPoint &p = pointOrPreviousPoint(touchId);
102 p.setState(Qt::TouchPointStationary);
103 return *this;
104 }
105
106#ifdef QT_WIDGETS_LIB
107 QTouchEventSequence& press(int touchId, const QPoint &pt, QWidget *widget = nullptr)
108 {
109 QTouchEvent::TouchPoint &p = point(touchId);
110 p.setScreenPos(mapToScreen(widget, pt));
111 p.setState(Qt::TouchPointPressed);
112 return *this;
113 }
114 QTouchEventSequence& move(int touchId, const QPoint &pt, QWidget *widget = nullptr)
115 {
116 QTouchEvent::TouchPoint &p = point(touchId);
117 p.setScreenPos(mapToScreen(widget, pt));
118 p.setState(Qt::TouchPointMoved);
119 return *this;
120 }
121 QTouchEventSequence& release(int touchId, const QPoint &pt, QWidget *widget = nullptr)
122 {
123 QTouchEvent::TouchPoint &p = point(touchId);
124 p.setScreenPos(mapToScreen(widget, pt));
125 p.setState(Qt::TouchPointReleased);
126 return *this;
127 }
128#endif
129
130 void commit(bool processEvents = true)
131 {
132 if (!points.isEmpty()) {
133 qSleep(1);
134 if (targetWindow)
135 {
136 qt_handleTouchEvent(targetWindow, device, points.values());
137 }
138#ifdef QT_WIDGETS_LIB
139 else if (targetWidget)
140 {
141 qt_handleTouchEvent(targetWidget->windowHandle(), device, points.values());
142 }
143#endif
144 }
145 if (processEvents)
146 QCoreApplication::processEvents();
147 previousPoints = points;
148 points.clear();
149 }
150
151private:
152#ifdef QT_WIDGETS_LIB
153 QTouchEventSequence(QWidget *widget, QTouchDevice *aDevice, bool autoCommit)
154 : targetWidget(widget), targetWindow(nullptr), device(aDevice), commitWhenDestroyed(autoCommit)
155 {
156 }
157#endif
158 QTouchEventSequence(QWindow *window, QTouchDevice *aDevice, bool autoCommit)
159 :
160#ifdef QT_WIDGETS_LIB
161 targetWidget(nullptr),
162#endif
163 targetWindow(window), device(aDevice), commitWhenDestroyed(autoCommit)
164 {
165 }
166
167 QTouchEvent::TouchPoint &point(int touchId)
168 {
169 if (!points.contains(touchId))
170 points[touchId] = QTouchEvent::TouchPoint(touchId);
171 return points[touchId];
172 }
173
174 QTouchEvent::TouchPoint &pointOrPreviousPoint(int touchId)
175 {
176 if (!points.contains(touchId)) {
177 if (previousPoints.contains(touchId))
178 points[touchId] = previousPoints.value(touchId);
179 else
180 points[touchId] = QTouchEvent::TouchPoint(touchId);
181 }
182 return points[touchId];
183 }
184
185#ifdef QT_WIDGETS_LIB
186 QPoint mapToScreen(QWidget *widget, const QPoint &pt)
187 {
188 if (widget)
189 return widget->mapToGlobal(pt);
190 return targetWidget ? targetWidget->mapToGlobal(pt) : pt;
191 }
192#endif
193 QPoint mapToScreen(QWindow *window, const QPoint &pt)
194 {
195 if(window)
196 return window->mapToGlobal(pt);
197 return targetWindow ? targetWindow->mapToGlobal(pt) : pt;
198 }
199
200 QMap<int, QTouchEvent::TouchPoint> previousPoints;
201 QMap<int, QTouchEvent::TouchPoint> points;
202#ifdef QT_WIDGETS_LIB
203 QWidget *targetWidget;
204#endif
205 QWindow *targetWindow;
206 QTouchDevice *device;
207 bool commitWhenDestroyed;
208#if defined(QT_WIDGETS_LIB) || defined(Q_CLANG_QDOC)
209 friend QTouchEventSequence touchEvent(QWidget *widget, QTouchDevice *device, bool autoCommit);
210#endif
211 friend QTouchEventSequence touchEvent(QWindow *window, QTouchDevice *device, bool autoCommit);
212 };
213
214#if defined(QT_WIDGETS_LIB) || defined(Q_CLANG_QDOC)
215 inline
216 QTouchEventSequence touchEvent(QWidget *widget,
217 QTouchDevice *device,
218 bool autoCommit = true)
219 {
220 return QTouchEventSequence(widget, device, autoCommit);
221 }
222#endif
223 inline
224 QTouchEventSequence touchEvent(QWindow *window,
225 QTouchDevice *device,
226 bool autoCommit = true)
227 {
228 return QTouchEventSequence(window, device, autoCommit);
229 }
230
231}
232
233QT_END_NAMESPACE
234
235#endif // QTESTTOUCH_H
236