7#include <QtQuick/private/qquickdeliveryagent_p_p.h>
8#include <QtQuick/qquickwindow.h>
9#include <qpa/qplatformtheme.h>
10#include <private/qguiapplication_p.h>
11#include <QtGui/qstylehints.h>
15Q_STATIC_LOGGING_CATEGORY(lcTapHandler,
"qt.quick.handler.tap")
17quint64 QQuickTapHandler::m_multiTapInterval(0);
19int QQuickTapHandler::m_mouseMultiClickDistanceSquared(-1);
20int QQuickTapHandler::m_touchMultiTapDistanceSquared(-1);
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
57QQuickTapHandler::QQuickTapHandler(QQuickItem *parent)
58 : QQuickSinglePointHandler(parent)
59 , m_longPressThreshold(QGuiApplication::styleHints()->mousePressAndHoldInterval())
61 if (m_mouseMultiClickDistanceSquared < 0) {
62 m_multiTapInterval = qApp->styleHints()->mouseDoubleClickInterval();
63 m_mouseMultiClickDistanceSquared = qApp->styleHints()->mouseDoubleClickDistance();
64 m_mouseMultiClickDistanceSquared *= m_mouseMultiClickDistanceSquared;
65 m_touchMultiTapDistanceSquared = qApp->styleHints()->touchDoubleTapDistance();
66 m_touchMultiTapDistanceSquared *= m_touchMultiTapDistanceSquared;
70bool QQuickTapHandler::wantsEventPoint(
const QPointerEvent *event,
const QEventPoint &point)
72 if (!QQuickDeliveryAgentPrivate::isMouseEvent(event) &&
73 !QQuickDeliveryAgentPrivate::isTouchEvent(event) &&
74 !QQuickDeliveryAgentPrivate::isTabletEvent(event))
81 bool overThreshold = d_func()->dragOverThreshold(point);
82 if (overThreshold && m_gesturePolicy != DragWithinBounds) {
83 if (m_longPressTimer.isActive())
84 qCDebug(lcTapHandler) << objectName() <<
"drag threshold exceeded";
85 m_longPressTimer.stop();
86 m_holdTimer.invalidate();
88 switch (point.state()) {
89 case QEventPoint::Pressed:
90 case QEventPoint::Released:
91 ret = parentContains(point);
93 case QEventPoint::Updated:
94 ret = point.id() ==
this->point().id();
95 switch (m_gesturePolicy) {
97 ret = ret && !overThreshold && parentContains(point);
100 case DragWithinBounds:
101 ret = ret && parentContains(point);
103 case ReleaseWithinBounds:
108 case QEventPoint::Stationary:
111 ret = point.id() ==
this->point().id();
113 case QEventPoint::Unknown:
120 if (!ret && point.id() ==
this->point().id())
121 setPressed(
false,
true,
const_cast<QPointerEvent *>(event),
const_cast<QEventPoint &>(point));
125void QQuickTapHandler::handleEventPoint(QPointerEvent *event, QEventPoint &point)
127 const bool isTouch = QQuickDeliveryAgentPrivate::isTouchEvent(event);
128 switch (point.state()) {
129 case QEventPoint::Pressed:
130 setPressed(
true,
false, event, point);
132 case QEventPoint::Released: {
133 if (isTouch || (
static_cast<
const QSinglePointEvent *>(event)->buttons() & acceptedButtons()) == Qt::NoButton)
134 setPressed(
false,
false, event, point);
141 QQuickSinglePointHandler::handleEventPoint(event, point);
147 if (isTouch && m_gesturePolicy == DragThreshold)
148 point.setAccepted(
false);
152
153
154
155
156
157
158
159
160
161
162
163
164
165qreal QQuickTapHandler::longPressThreshold()
const
167 return m_longPressThreshold / qreal(1000);
170void QQuickTapHandler::setLongPressThreshold(qreal longPressThreshold)
172 if (longPressThreshold < 0) {
173 resetLongPressThreshold();
176 int ms = qRound(longPressThreshold * 1000);
177 if (m_longPressThreshold == ms)
180 m_longPressThreshold = ms;
181 emit longPressThresholdChanged();
184void QQuickTapHandler::resetLongPressThreshold()
186 int ms = QGuiApplication::styleHints()->mousePressAndHoldInterval();
187 if (m_longPressThreshold == ms)
190 m_longPressThreshold = ms;
191 emit longPressThresholdChanged();
194void QQuickTapHandler::timerEvent(QTimerEvent *event)
196 if (event->timerId() == m_longPressTimer.timerId()) {
197 m_longPressTimer.stop();
198 qCDebug(lcTapHandler) << objectName() <<
"longPressed";
199 m_longPressed =
true;
201 }
else if (event->timerId() == m_doubleTapTimer.timerId()) {
202 m_doubleTapTimer.stop();
203 qCDebug(lcTapHandler) << objectName() <<
"double-tap timer expired; taps:" << m_tapCount;
204 Q_ASSERT(m_exclusiveSignals == (SingleTap | DoubleTap));
206 emit singleTapped(m_singleTapReleasedPoint, m_singleTapReleasedButton);
207 else if (m_tapCount == 2)
208 emit doubleTapped(m_singleTapReleasedPoint, m_singleTapReleasedButton);
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315void QQuickTapHandler::setGesturePolicy(QQuickTapHandler::GesturePolicy gesturePolicy)
317 if (m_gesturePolicy == gesturePolicy)
320 m_gesturePolicy = gesturePolicy;
321 emit gesturePolicyChanged();
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347void QQuickTapHandler::setExclusiveSignals(QQuickTapHandler::ExclusiveSignals exc)
349 if (m_exclusiveSignals == exc)
352 m_exclusiveSignals = exc;
353 emit exclusiveSignalsChanged();
357
358
359
360
361
362
363
364
365void QQuickTapHandler::setPressed(
bool press,
bool cancel, QPointerEvent *event, QEventPoint &point)
367 if (m_pressed != press) {
368 qCDebug(lcTapHandler) << objectName() <<
"pressed" << m_pressed <<
"->" << press
369 << (cancel ?
"CANCEL" :
"") << point <<
"gp" << m_gesturePolicy;
371 connectPreRenderSignal(press);
374 if (m_longPressThreshold > 0)
375 m_longPressTimer.start(m_longPressThreshold,
this);
378 m_longPressTimer.stop();
379 m_holdTimer.invalidate();
383 if (m_gesturePolicy == DragThreshold)
384 setPassiveGrab(event, point, press);
386 setExclusiveGrab(event, point, press);
388 if (!cancel && !press && parentContains(point)) {
390 qCDebug(lcTapHandler) << objectName() <<
"long press threshold" << longPressThreshold() <<
"exceeded:" << point.timeHeld();
393 const quint64 ts = event->timestamp();
394 const quint64 interval = ts - m_lastTapTimestamp;
395 const auto distanceSquared = QVector2D(point.scenePosition() - m_lastTapPos).lengthSquared();
396 const auto singleTapReleasedButton = event->isSinglePointEvent() ?
static_cast<QSinglePointEvent *>(event)->button() : Qt::NoButton;
397 if ((interval < m_multiTapInterval && distanceSquared <
398 (event->device()->type() == QInputDevice::DeviceType::Mouse ?
399 m_mouseMultiClickDistanceSquared : m_touchMultiTapDistanceSquared))
400 && m_singleTapReleasedButton == singleTapReleasedButton) {
403 m_singleTapReleasedButton = singleTapReleasedButton;
404 m_singleTapReleasedPoint = point;
407 qCDebug(lcTapHandler) << objectName() <<
"tapped" << m_tapCount <<
"times; interval since last:" << interval
408 <<
"sec; distance since last:" << qSqrt(distanceSquared);
409 auto button = event->isSinglePointEvent() ?
static_cast<QSinglePointEvent *>(event)->button() : Qt::NoButton;
410 emit tapped(point, button);
411 emit tapCountChanged();
412 switch (m_exclusiveSignals) {
415 emit singleTapped(point, button);
416 else if (m_tapCount == 2)
417 emit doubleTapped(point, button);
421 emit singleTapped(point, button);
425 emit doubleTapped(point, button);
427 case (SingleTap | DoubleTap):
428 if (m_tapCount == 1) {
429 qCDebug(lcTapHandler) << objectName() <<
"waiting to emit singleTapped:" << m_multiTapInterval <<
"ms";
430 m_doubleTapTimer.start(m_multiTapInterval,
this);
433 qCDebug(lcTapHandler) << objectName() <<
"tap" << m_tapCount <<
"after" << event->timestamp() - m_lastTapTimestamp <<
"ms";
435 m_lastTapTimestamp = ts;
436 m_lastTapPos = point.scenePosition();
439 m_longPressed =
false;
440 emit pressedChanged();
441 if (!press && m_gesturePolicy != DragThreshold) {
443 setExclusiveGrab(event, point, press);
446 emit canceled(point);
448 setExclusiveGrab(event, point,
false);
457void QQuickTapHandler::onGrabChanged(QQuickPointerHandler *grabber, QPointingDevice::GrabTransition transition,
458 QPointerEvent *ev, QEventPoint &point)
461 QQuickSinglePointHandler::onGrabChanged(grabber, transition, ev, point);
464 const bool isCanceled = transition == QPointingDevice::CancelGrabExclusive || transition == QPointingDevice::CancelGrabPassive;
467 const bool passiveGrab = transition == QPointingDevice::GrabPassive || transition == QPointingDevice::UngrabPassive;
468 if (grabber ==
this && (isCanceled || point.state() == QEventPoint::Released || (!active() && !passiveGrab)))
469 setPressed(
false, isCanceled, ev, point);
472void QQuickTapHandler::connectPreRenderSignal(
bool conn)
475 disconnect(m_preRenderSignalConnection);
477 auto par = parentItem();
478 if (!par || !par->window())
482
483
484
485
486
487
488
490 m_preRenderSignalConnection = connect(par->window(), &QQuickWindow::beforeSynchronizing,
491 this, &QQuickTapHandler::updateTimeHeld);
495void QQuickTapHandler::updateTimeHeld()
497 emit timeHeldChanged();
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
541
542
543
544
545
546
547
548
549
550
551
552
553
554
557
558
559
560
561
562
563
564
565
566
567
570
571
572
573
574
575
576
577
578
579
580
581
582
585
586
587
588
589
590
591
592
595
596
597
598
599
600
603#include "moc_qquicktaphandler_p.cpp"
Combined button and popup list for selecting options.