17 #include "TouchDispatcher.h"
19 #include <QGuiApplication>
20 #include <QScopedPointer>
21 #include <QStyleHints>
23 #pragma GCC diagnostic push
24 #pragma GCC diagnostic ignored "-pedantic"
25 #include <private/qquickitem_p.h>
26 #pragma GCC diagnostic pop
28 #define TOUCHDISPATCHER_DEBUG 0
30 #if TOUCHDISPATCHER_DEBUG
31 #define ugDebug(params) qDebug().nospace() << "[TouchDispatcher(" << this << ")] " << params
32 #include <DebugHelpers.h>
33 #else // TOUCHDISPATCHER_DEBUG
34 #define ugDebug(params) ((void)0)
35 #endif // TOUCHDISPATCHER_DEBUG
37 TouchDispatcher::TouchDispatcher()
38 : m_status(NoActiveTouch)
40 , m_touchMousePressTimestamp(0)
44 void TouchDispatcher::setTargetItem(QQuickItem *target)
46 if (target != m_targetItem) {
47 m_targetItem = target;
48 if (m_status != NoActiveTouch) {
49 qWarning(
"[TouchDispatcher] Changing target item in the middle of a touch stream");
50 setStatus(TargetRejectedTouches);
55 void TouchDispatcher::dispatch(QTouchDevice *device,
56 Qt::KeyboardModifiers modifiers,
57 const QList<QTouchEvent::TouchPoint> &touchPoints,
61 if (m_targetItem.isNull()) {
62 qWarning(
"[TouchDispatcher] Cannot dispatch touch event because target item is null");
66 QEvent::Type eventType = resolveEventType(touchPoints);
68 if (eventType == QEvent::TouchBegin) {
69 dispatchTouchBegin(device, modifiers, touchPoints, window, timestamp);
71 }
else if (eventType == QEvent::TouchUpdate || eventType == QEvent::TouchEnd) {
73 if (m_status == DeliveringTouchEvents) {
74 dispatchAsTouch(eventType, device, modifiers, touchPoints, window, timestamp);
75 }
else if (m_status == DeliveringMouseEvents) {
76 dispatchAsMouse(device, modifiers, touchPoints, timestamp);
78 Q_ASSERT(m_status == TargetRejectedTouches);
79 ugDebug(
"Not dispatching touch event to " << m_targetItem.data()
80 <<
"because it already rejected the touch stream.");
84 if (eventType == QEvent::TouchEnd) {
85 setStatus(NoActiveTouch);
91 qCritical() <<
"[TouchDispatcher] Unexpected event type" << eventType;
97 void TouchDispatcher::dispatchTouchBegin(
99 Qt::KeyboardModifiers modifiers,
100 const QList<QTouchEvent::TouchPoint> &touchPoints,
104 Q_ASSERT(m_status == NoActiveTouch);
105 QQuickItem *targetItem = m_targetItem.data();
107 if (!targetItem->isEnabled() || !targetItem->isVisible()) {
108 ugDebug(
"Cannot dispatch touch event to " << targetItem <<
" because it's disabled or invisible.");
113 QList<QTouchEvent::TouchPoint> targetTouchPoints = touchPoints;
114 transformTouchPoints(targetTouchPoints, QQuickItemPrivate::get(targetItem)->windowToItemTransform());
116 QScopedPointer<QTouchEvent> touchEvent(
117 createQTouchEvent(QEvent::TouchBegin, device, modifiers, targetTouchPoints, window, timestamp));
120 ugDebug(
"dispatching " << qPrintable(touchEventToString(touchEvent.data()))
121 <<
" to " << targetItem);
122 QCoreApplication::sendEvent(targetItem, touchEvent.data());
125 if (touchEvent->isAccepted()) {
126 ugDebug(
"Item accepted the touch event.");
127 setStatus(DeliveringTouchEvents);
128 }
else if (targetItem->acceptedMouseButtons() & Qt::LeftButton) {
129 ugDebug(
"Item rejected the touch event. Trying a QMouseEvent");
131 QScopedPointer<QMouseEvent> mouseEvent(
132 touchToMouseEvent(QEvent::MouseButtonPress, targetTouchPoints.at(0), timestamp,
134 Q_ASSERT(targetTouchPoints.at(0).state() == Qt::TouchPointPressed);
136 ugDebug(
"dispatching " << qPrintable(mouseEventToString(mouseEvent.data()))
137 <<
" to " << m_targetItem.data());
138 QCoreApplication::sendEvent(targetItem, mouseEvent.data());
139 if (mouseEvent->isAccepted()) {
140 ugDebug(
"Item accepted the QMouseEvent.");
141 setStatus(DeliveringMouseEvents);
142 m_touchMouseId = targetTouchPoints.at(0).id();
144 if (checkIfDoubleClicked(timestamp)) {
145 QScopedPointer<QMouseEvent> doubleClickEvent(
146 touchToMouseEvent(QEvent::MouseButtonDblClick, targetTouchPoints.at(0), timestamp,
148 ugDebug(
"dispatching " << qPrintable(mouseEventToString(doubleClickEvent.data()))
149 <<
" to " << m_targetItem.data());
150 QCoreApplication::sendEvent(targetItem, doubleClickEvent.data());
154 ugDebug(
"Item rejected the QMouseEvent.");
155 setStatus(TargetRejectedTouches);
158 ugDebug(
"Item rejected the touch event and does not accept mouse buttons.");
159 setStatus(TargetRejectedTouches);
163 void TouchDispatcher::dispatchAsTouch(QEvent::Type eventType,
164 QTouchDevice *device,
165 Qt::KeyboardModifiers modifiers,
166 const QList<QTouchEvent::TouchPoint> &touchPoints,
170 QQuickItem *targetItem = m_targetItem.data();
173 QList<QTouchEvent::TouchPoint> targetTouchPoints = touchPoints;
174 transformTouchPoints(targetTouchPoints, QQuickItemPrivate::get(targetItem)->windowToItemTransform());
176 QScopedPointer<QTouchEvent> eventForTargetItem(
177 createQTouchEvent(eventType, device, modifiers, targetTouchPoints, window, timestamp));
180 ugDebug(
"dispatching " << qPrintable(touchEventToString(eventForTargetItem.data()))
181 <<
" to " << targetItem);
182 QCoreApplication::sendEvent(targetItem, eventForTargetItem.data());
185 void TouchDispatcher::dispatchAsMouse(
187 Qt::KeyboardModifiers modifiers,
188 const QList<QTouchEvent::TouchPoint> &touchPoints,
193 Q_ASSERT(!touchPoints.isEmpty());
195 const QTouchEvent::TouchPoint *touchMouse =
nullptr;
197 if (m_touchMouseId != -1) {
198 for (
int i = 0; i < touchPoints.count() && !touchMouse; ++i) {
199 const auto &touchPoint = touchPoints.at(i);
200 if (touchPoint.id() == m_touchMouseId) {
201 touchMouse = &touchPoint;
205 Q_ASSERT(touchMouse);
208 qWarning(
"[TouchDispatcher] Didn't find touch with id %d, used for mouse pointer emulation.",
210 m_touchMouseId = touchPoints.at(0).id();
211 touchMouse = &touchPoints.at(0);
215 for (
int i = 0; i < touchPoints.count() && !touchMouse; ++i) {
216 const auto &touchPoint = touchPoints.at(i);
217 if (touchPoint.state() == Qt::TouchPointPressed) {
218 touchMouse = &touchPoint;
219 m_touchMouseId = touchMouse->id();
225 QEvent::Type eventType;
226 if (touchMouse->state() == Qt::TouchPointPressed) {
227 eventType = QEvent::MouseButtonPress;
228 }
if (touchMouse->state() == Qt::TouchPointReleased) {
229 eventType = QEvent::MouseButtonRelease;
232 eventType = QEvent::MouseMove;
235 QScopedPointer<QMouseEvent> mouseEvent(touchToMouseEvent(eventType, *touchMouse, timestamp, modifiers,
238 ugDebug(
"dispatching " << qPrintable(mouseEventToString(mouseEvent.data()))
239 <<
" to " << m_targetItem.data());
240 QCoreApplication::sendEvent(m_targetItem.data(), mouseEvent.data());
244 QTouchEvent *TouchDispatcher::createQTouchEvent(QEvent::Type eventType,
245 QTouchDevice *device,
246 Qt::KeyboardModifiers modifiers,
247 const QList<QTouchEvent::TouchPoint> &touchPoints,
251 Qt::TouchPointStates eventStates = 0;
252 for (
int i = 0; i < touchPoints.count(); i++)
253 eventStates |= touchPoints[i].state();
255 switch (eventStates) {
256 case Qt::TouchPointPressed:
257 eventType = QEvent::TouchBegin;
259 case Qt::TouchPointReleased:
260 eventType = QEvent::TouchEnd;
263 eventType = QEvent::TouchUpdate;
267 QTouchEvent *touchEvent =
new QTouchEvent(eventType);
268 touchEvent->setWindow(window);
269 touchEvent->setTarget(m_targetItem.data());
270 touchEvent->setDevice(device);
271 touchEvent->setModifiers(modifiers);
272 touchEvent->setTouchPoints(touchPoints);
273 touchEvent->setTouchPointStates(eventStates);
274 touchEvent->setTimestamp(timestamp);
275 touchEvent->accept();
280 void TouchDispatcher::transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints,
const QTransform &transform)
282 QMatrix4x4 transformMatrix(transform);
283 for (
int i=0; i<touchPoints.count(); i++) {
284 QTouchEvent::TouchPoint &touchPoint = touchPoints[i];
285 touchPoint.setRect(transform.mapRect(touchPoint.sceneRect()));
286 touchPoint.setStartPos(transform.map(touchPoint.startScenePos()));
287 touchPoint.setLastPos(transform.map(touchPoint.lastScenePos()));
288 touchPoint.setVelocity(transformMatrix.mapVector(touchPoint.velocity()).toVector2D());
293 QMouseEvent *TouchDispatcher::touchToMouseEvent(
294 QEvent::Type type,
const QTouchEvent::TouchPoint &p,
295 ulong timestamp, Qt::KeyboardModifiers modifiers,
296 bool transformNeeded)
298 QQuickItem *item = m_targetItem.data();
301 QMouseEvent *me =
new QMouseEvent(type, transformNeeded ? item->mapFromScene(p.scenePos()) : p.pos(),
302 p.scenePos(), p.screenPos(), Qt::LeftButton,
303 (type == QEvent::MouseButtonRelease ? Qt::NoButton : Qt::LeftButton),
305 me->setAccepted(
true);
306 me->setTimestamp(timestamp);
307 QVector2D transformedVelocity = p.velocity();
308 if (transformNeeded) {
309 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
310 QMatrix4x4 transformMatrix(itemPrivate->windowToItemTransform());
311 transformedVelocity = transformMatrix.mapVector(p.velocity()).toVector2D();
325 bool TouchDispatcher::checkIfDoubleClicked(ulong newPressEventTimestamp)
329 if (m_touchMousePressTimestamp == 0) {
331 m_touchMousePressTimestamp = newPressEventTimestamp;
332 doubleClicked =
false;
334 ulong timeBetweenPresses = newPressEventTimestamp - m_touchMousePressTimestamp;
335 ulong doubleClickInterval =
static_cast<ulong
>(qApp->styleHints()->
336 mouseDoubleClickInterval());
337 doubleClicked = timeBetweenPresses < doubleClickInterval;
339 m_touchMousePressTimestamp = 0;
341 m_touchMousePressTimestamp = newPressEventTimestamp;
345 return doubleClicked;
348 void TouchDispatcher::setStatus(Status status)
350 if (status != m_status) {
351 #if TOUCHDISPATCHER_DEBUG
354 ugDebug(
"status = NoActiveTouch");
356 case DeliveringTouchEvents:
357 ugDebug(
"status = DeliveringTouchEvents");
359 case DeliveringMouseEvents:
360 ugDebug(
"status = DeliveringMouseEvents");
362 case TargetRejectedTouches:
363 ugDebug(
"status = TargetRejectedTouches");
366 ugDebug(
"status = " << status);
374 void TouchDispatcher::reset()
376 setStatus(NoActiveTouch);
378 m_touchMousePressTimestamp =0;
381 QEvent::Type TouchDispatcher::resolveEventType(
const QList<QTouchEvent::TouchPoint> &touchPoints)
383 QEvent::Type eventType;
385 Qt::TouchPointStates eventStates = 0;
386 for (
int i = 0; i < touchPoints.count(); i++)
387 eventStates |= touchPoints[i].state();
389 switch (eventStates) {
390 case Qt::TouchPointPressed:
391 eventType = QEvent::TouchBegin;
393 case Qt::TouchPointReleased:
394 eventType = QEvent::TouchEnd;
397 eventType = QEvent::TouchUpdate;