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 #include <DebugHelpers.h>
34 TouchDispatcher::TouchDispatcher()
35 : m_status(NoActiveTouch)
37 , m_touchMousePressTimestamp(0)
41 void TouchDispatcher::setTargetItem(QQuickItem *target)
43 if (target != m_targetItem) {
44 m_targetItem = target;
45 if (m_status != NoActiveTouch) {
46 qWarning(
"[TouchDispatcher] Changing target item in the middle of a touch stream");
47 m_status = TargetRejectedTouches;
52 void TouchDispatcher::dispatch(QEvent::Type eventType,
54 Qt::KeyboardModifiers modifiers,
55 const QList<QTouchEvent::TouchPoint> &touchPoints,
59 if (m_targetItem.isNull()) {
60 qWarning(
"[TouchDispatcher] Cannot dispatch touch event because target item is null");
64 if (eventType == QEvent::TouchBegin) {
65 dispatchTouchBegin(device, modifiers, touchPoints, window, timestamp);
67 }
else if (eventType == QEvent::TouchUpdate || eventType == QEvent::TouchEnd) {
69 if (m_status == DeliveringTouchEvents) {
70 dispatchAsTouch(eventType, device, modifiers, touchPoints, window, timestamp);
71 }
else if (m_status == DeliveringMouseEvents) {
72 dispatchAsMouse(device, modifiers, touchPoints, timestamp);
74 Q_ASSERT(m_status == TargetRejectedTouches);
75 #if TOUCHDISPATCHER_DEBUG
76 qDebug() <<
"[TouchDispatcher] Not dispatching touch event to" << m_targetItem.data()
77 <<
"because it already rejected the touch stream.";
82 if (eventType == QEvent::TouchEnd) {
83 m_status = NoActiveTouch;
89 qCritical() <<
"[TouchDispatcher] Unexpected event type" << eventType;
95 void TouchDispatcher::dispatchTouchBegin(
97 Qt::KeyboardModifiers modifiers,
98 const QList<QTouchEvent::TouchPoint> &touchPoints,
102 Q_ASSERT(m_status == NoActiveTouch);
103 QQuickItem *targetItem = m_targetItem.data();
105 if (!targetItem->isEnabled() || !targetItem->isVisible()) {
106 #if TOUCHDISPATCHER_DEBUG
107 qDebug() <<
"[TouchDispatcher] Cannot dispatch touch event to" << targetItem
108 <<
"because it's disabled or invisible.";
114 QList<QTouchEvent::TouchPoint> targetTouchPoints = touchPoints;
115 transformTouchPoints(targetTouchPoints, QQuickItemPrivate::get(targetItem)->windowToItemTransform());
117 QScopedPointer<QTouchEvent> touchEvent(
118 createQTouchEvent(QEvent::TouchBegin, device, modifiers, targetTouchPoints, window, timestamp));
121 #if TOUCHDISPATCHER_DEBUG
122 qDebug() <<
"[TouchDispatcher] dispatching" << qPrintable(touchEventToString(touchEvent.data()))
123 <<
"to" << targetItem;
125 QCoreApplication::sendEvent(targetItem, touchEvent.data());
128 if (touchEvent->isAccepted()) {
129 #if TOUCHDISPATCHER_DEBUG
130 qDebug() <<
"[TouchDispatcher] Item accepted the touch event.";
132 m_status = DeliveringTouchEvents;
133 }
else if (targetItem->acceptedMouseButtons() & Qt::LeftButton) {
134 #if TOUCHDISPATCHER_DEBUG
135 qDebug() <<
"[TouchDispatcher] Item rejected the touch event. Trying a QMouseEvent";
138 QScopedPointer<QMouseEvent> mouseEvent(
139 touchToMouseEvent(QEvent::MouseButtonPress, targetTouchPoints.at(0), timestamp,
141 Q_ASSERT(targetTouchPoints.at(0).state() == Qt::TouchPointPressed);
143 #if TOUCHDISPATCHER_DEBUG
144 qDebug() <<
"[TouchDispatcher] dispatching" << qPrintable(mouseEventToString(mouseEvent.data()))
145 <<
"to" << m_targetItem.data();
147 QCoreApplication::sendEvent(targetItem, mouseEvent.data());
148 if (mouseEvent->isAccepted()) {
149 #if TOUCHDISPATCHER_DEBUG
150 qDebug() <<
"[TouchDispatcher] Item accepted the QMouseEvent.";
152 m_status = DeliveringMouseEvents;
153 m_touchMouseId = targetTouchPoints.at(0).id();
155 if (checkIfDoubleClicked(timestamp)) {
156 QScopedPointer<QMouseEvent> doubleClickEvent(
157 touchToMouseEvent(QEvent::MouseButtonDblClick, targetTouchPoints.at(0), timestamp,
159 #if TOUCHDISPATCHER_DEBUG
160 qDebug() <<
"[TouchDispatcher] dispatching" << qPrintable(mouseEventToString(doubleClickEvent.data()))
161 <<
"to" << m_targetItem.data();
163 QCoreApplication::sendEvent(targetItem, doubleClickEvent.data());
167 #if TOUCHDISPATCHER_DEBUG
168 qDebug() <<
"[TouchDispatcher] Item rejected the QMouseEvent.";
170 m_status = TargetRejectedTouches;
173 #if TOUCHDISPATCHER_DEBUG
174 qDebug() <<
"[TouchDispatcher] Item rejected the touch event and does not accept mouse buttons.";
176 m_status = TargetRejectedTouches;
180 void TouchDispatcher::dispatchAsTouch(QEvent::Type eventType,
181 QTouchDevice *device,
182 Qt::KeyboardModifiers modifiers,
183 const QList<QTouchEvent::TouchPoint> &touchPoints,
187 QQuickItem *targetItem = m_targetItem.data();
190 QList<QTouchEvent::TouchPoint> targetTouchPoints = touchPoints;
191 transformTouchPoints(targetTouchPoints, QQuickItemPrivate::get(targetItem)->windowToItemTransform());
193 QScopedPointer<QTouchEvent> eventForTargetItem(
194 createQTouchEvent(eventType, device, modifiers, targetTouchPoints, window, timestamp));
197 #if TOUCHDISPATCHER_DEBUG
198 qDebug() <<
"[TouchDispatcher] dispatching" << qPrintable(touchEventToString(eventForTargetItem.data()))
199 <<
"to" << targetItem;
201 QCoreApplication::sendEvent(targetItem, eventForTargetItem.data());
204 void TouchDispatcher::dispatchAsMouse(
206 Qt::KeyboardModifiers modifiers,
207 const QList<QTouchEvent::TouchPoint> &touchPoints,
212 Q_ASSERT(!touchPoints.isEmpty());
214 const QTouchEvent::TouchPoint *touchMouse =
nullptr;
216 if (m_touchMouseId != -1) {
217 for (
int i = 0; i < touchPoints.count() && !touchMouse; ++i) {
218 const auto &touchPoint = touchPoints.at(i);
219 if (touchPoint.id() == m_touchMouseId) {
220 touchMouse = &touchPoint;
224 Q_ASSERT(touchMouse);
227 qWarning(
"[TouchDispatcher] Didn't find touch with id %d, used for mouse pointer emulation.",
229 m_touchMouseId = touchPoints.at(0).id();
230 touchMouse = &touchPoints.at(0);
234 for (
int i = 0; i < touchPoints.count() && !touchMouse; ++i) {
235 const auto &touchPoint = touchPoints.at(i);
236 if (touchPoint.state() == Qt::TouchPointPressed) {
237 touchMouse = &touchPoint;
238 m_touchMouseId = touchMouse->id();
244 QEvent::Type eventType;
245 if (touchMouse->state() == Qt::TouchPointPressed) {
246 eventType = QEvent::MouseButtonPress;
247 }
if (touchMouse->state() == Qt::TouchPointReleased) {
248 eventType = QEvent::MouseButtonRelease;
251 eventType = QEvent::MouseMove;
254 QScopedPointer<QMouseEvent> mouseEvent(touchToMouseEvent(eventType, *touchMouse, timestamp, modifiers,
257 #if TOUCHDISPATCHER_DEBUG
258 qDebug() <<
"[TouchDispatcher] dispatching" << qPrintable(mouseEventToString(mouseEvent.data()))
259 <<
"to" << m_targetItem.data();
261 QCoreApplication::sendEvent(m_targetItem.data(), mouseEvent.data());
265 QTouchEvent *TouchDispatcher::createQTouchEvent(QEvent::Type eventType,
266 QTouchDevice *device,
267 Qt::KeyboardModifiers modifiers,
268 const QList<QTouchEvent::TouchPoint> &touchPoints,
272 Qt::TouchPointStates eventStates = 0;
273 for (
int i = 0; i < touchPoints.count(); i++)
274 eventStates |= touchPoints[i].state();
276 switch (eventStates) {
277 case Qt::TouchPointPressed:
278 eventType = QEvent::TouchBegin;
280 case Qt::TouchPointReleased:
281 eventType = QEvent::TouchEnd;
284 eventType = QEvent::TouchUpdate;
288 QTouchEvent *touchEvent =
new QTouchEvent(eventType);
289 touchEvent->setWindow(window);
290 touchEvent->setTarget(m_targetItem.data());
291 touchEvent->setDevice(device);
292 touchEvent->setModifiers(modifiers);
293 touchEvent->setTouchPoints(touchPoints);
294 touchEvent->setTouchPointStates(eventStates);
295 touchEvent->setTimestamp(timestamp);
296 touchEvent->accept();
301 void TouchDispatcher::transformTouchPoints(QList<QTouchEvent::TouchPoint> &touchPoints,
const QTransform &transform)
303 QMatrix4x4 transformMatrix(transform);
304 for (
int i=0; i<touchPoints.count(); i++) {
305 QTouchEvent::TouchPoint &touchPoint = touchPoints[i];
306 touchPoint.setRect(transform.mapRect(touchPoint.sceneRect()));
307 touchPoint.setStartPos(transform.map(touchPoint.startScenePos()));
308 touchPoint.setLastPos(transform.map(touchPoint.lastScenePos()));
309 touchPoint.setVelocity(transformMatrix.mapVector(touchPoint.velocity()).toVector2D());
314 QMouseEvent *TouchDispatcher::touchToMouseEvent(
315 QEvent::Type type,
const QTouchEvent::TouchPoint &p,
316 ulong timestamp, Qt::KeyboardModifiers modifiers,
317 bool transformNeeded)
319 QQuickItem *item = m_targetItem.data();
322 QMouseEvent *me =
new QMouseEvent(type, transformNeeded ? item->mapFromScene(p.scenePos()) : p.pos(),
323 p.scenePos(), p.screenPos(), Qt::LeftButton,
324 (type == QEvent::MouseButtonRelease ? Qt::NoButton : Qt::LeftButton),
326 me->setAccepted(
true);
327 me->setTimestamp(timestamp);
328 QVector2D transformedVelocity = p.velocity();
329 if (transformNeeded) {
330 QQuickItemPrivate *itemPrivate = QQuickItemPrivate::get(item);
331 QMatrix4x4 transformMatrix(itemPrivate->windowToItemTransform());
332 transformedVelocity = transformMatrix.mapVector(p.velocity()).toVector2D();
346 bool TouchDispatcher::checkIfDoubleClicked(ulong newPressEventTimestamp)
350 if (m_touchMousePressTimestamp == 0) {
352 m_touchMousePressTimestamp = newPressEventTimestamp;
353 doubleClicked =
false;
355 ulong timeBetweenPresses = newPressEventTimestamp - m_touchMousePressTimestamp;
356 ulong doubleClickInterval =
static_cast<ulong
>(qApp->styleHints()->
357 mouseDoubleClickInterval());
358 doubleClicked = timeBetweenPresses < doubleClickInterval;
360 m_touchMousePressTimestamp = 0;
362 m_touchMousePressTimestamp = newPressEventTimestamp;
366 return doubleClicked;