aboutsummaryrefslogtreecommitdiffstats
path: root/src/quick/handlers/qquicktaphandler.cpp
blob: 306eec65b8a33dc20ec0a230436abb4d12e37ff3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
// Copyright (C) 2019 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

#include "qquicktaphandler_p.h"
#include "qquicksinglepointhandler_p_p.h"
#include <QtQuick/private/qquickdeliveryagent_p_p.h>
#include <QtQuick/qquickwindow.h>
#include <qpa/qplatformtheme.h>
#include <private/qguiapplication_p.h>
#include <QtGui/qstylehints.h>

QT_BEGIN_NAMESPACE

Q_LOGGING_CATEGORY(lcTapHandler, "qt.quick.handler.tap")

qreal QQuickTapHandler::m_multiTapInterval(0.0);
// single tap distance is the same as the drag threshold
int QQuickTapHandler::m_mouseMultiClickDistanceSquared(-1);
int QQuickTapHandler::m_touchMultiTapDistanceSquared(-1);

/*!
    \qmltype TapHandler
    \instantiates QQuickTapHandler
    \inherits SinglePointHandler
    \inqmlmodule QtQuick
    \ingroup qtquick-input-handlers
    \brief Handler for taps and clicks.

    TapHandler is a handler for taps on a touchscreen or clicks on a mouse.

    Detection of a valid tap gesture depends on \l gesturePolicy.  The default
    value is DragThreshold, which requires the press and release to be close
    together in both space and time.  In this case, DragHandler is able to
    function using only a passive grab, and therefore does not interfere with
    event delivery to any other Items or Input Handlers.  So the default
    gesturePolicy is useful when you want to modify behavior of an existing
    control or Item by adding a TapHandler with bindings and/or JavaScript
    callbacks.

    Note that buttons (such as QPushButton) are often implemented not to care
    whether the press and release occur close together: if you press the button
    and then change your mind, you need to drag all the way off the edge of the
    button in order to cancel the click.  For this use case, set the
    \l gesturePolicy to \c TapHandler.ReleaseWithinBounds.

    \snippet pointerHandlers/tapHandlerButton.qml 0

    For multi-tap gestures (double-tap, triple-tap etc.), the distance moved
    must not exceed QStyleHints::mouseDoubleClickDistance() with mouse and
    QStyleHints::touchDoubleTapDistance() with touch, and the time between
    taps must not exceed QStyleHints::mouseDoubleClickInterval().

    \sa MouseArea
*/

QQuickTapHandler::QQuickTapHandler(QQuickItem *parent)
    : QQuickSinglePointHandler(parent)
{
    if (m_mouseMultiClickDistanceSquared < 0) {
        m_multiTapInterval = qApp->styleHints()->mouseDoubleClickInterval() / 1000.0;
        m_mouseMultiClickDistanceSquared = qApp->styleHints()->mouseDoubleClickDistance();
        m_mouseMultiClickDistanceSquared *= m_mouseMultiClickDistanceSquared;
        m_touchMultiTapDistanceSquared = qApp->styleHints()->touchDoubleTapDistance();
        m_touchMultiTapDistanceSquared *= m_touchMultiTapDistanceSquared;
    }
}

bool QQuickTapHandler::wantsEventPoint(const QPointerEvent *event, const QEventPoint &point)
{
    if (!QQuickDeliveryAgentPrivate::isMouseEvent(event) &&
            !QQuickDeliveryAgentPrivate::isTouchEvent(event) &&
            !QQuickDeliveryAgentPrivate::isTabletEvent(event))
        return false;
    // If the user has not violated any constraint, it could be a tap.
    // Otherwise we want to give up the grab so that a competing handler
    // (e.g. DragHandler) gets a chance to take over.
    // Don't forget to emit released in case of a cancel.
    bool ret = false;
    bool overThreshold = d_func()->dragOverThreshold(point);
    if (overThreshold && m_gesturePolicy != DragWithinBounds) {
        m_longPressTimer.stop();
        m_holdTimer.invalidate();
    }
    switch (point.state()) {
    case QEventPoint::Pressed:
    case QEventPoint::Released:
        ret = parentContains(point);
        break;
    case QEventPoint::Updated:
        switch (m_gesturePolicy) {
        case DragThreshold:
            ret = !overThreshold && parentContains(point);
            break;
        case WithinBounds:
        case DragWithinBounds:
            ret = parentContains(point);
            break;
        case ReleaseWithinBounds:
            ret = point.id() == this->point().id();
            break;
        }
        break;
    case QEventPoint::Stationary:
        // If the point hasn't moved since last time, the return value should be the same as last time.
        // If we return false here, QQuickPointerHandler::handlePointerEvent() will call setActive(false).
        ret = point.id() == this->point().id();
        break;
    case QEventPoint::Unknown:
        break;
    }
    // If this is the grabber, returning false from this function will cancel the grab,
    // so onGrabChanged(this, CancelGrabExclusive, point) and setPressed(false) will be called.
    // But when m_gesturePolicy is DragThreshold, we don't get an exclusive grab, but
    // we still don't want to be pressed anymore.
    if (!ret && point.id() == this->point().id())
        setPressed(false, true, const_cast<QPointerEvent *>(event), const_cast<QEventPoint &>(point));
    return ret;
}

void QQuickTapHandler::handleEventPoint(QPointerEvent *event, QEventPoint &point)
{
    const bool isTouch = QQuickDeliveryAgentPrivate::isTouchEvent(event);
    switch (point.state()) {
    case QEventPoint::Pressed:
        setPressed(true, false, event, point);
        break;
    case QEventPoint::Released: {
        if (isTouch || (static_cast<const QSinglePointEvent *>(event)->buttons() & acceptedButtons()) == Qt::NoButton)
            setPressed(false, false, event, point);
        break;
    }
    default:
        break;
    }

    QQuickSinglePointHandler::handleEventPoint(event, point);

    // If TapHandler only needs a passive grab, it should not block other items and handlers from reacting.
    // If the point is accepted, QQuickItemPrivate::localizedTouchEvent() would skip it.
    if (isTouch && m_gesturePolicy == DragThreshold)
        point.setAccepted(false);
}

/*!
    \qmlproperty real QtQuick::TapHandler::longPressThreshold

    The time in seconds that an event point must be pressed in order to
    trigger a long press gesture and emit the \l longPressed() signal.
    If the point is released before this time limit, a tap can be detected
    if the \l gesturePolicy constraint is satisfied. The default value is
    QStyleHints::mousePressAndHoldInterval() converted to seconds.
*/
qreal QQuickTapHandler::longPressThreshold() const
{
    return longPressThresholdMilliseconds() / 1000.0;
}

void QQuickTapHandler::setLongPressThreshold(qreal longPressThreshold)
{
    int ms = qRound(longPressThreshold * 1000);
    if (m_longPressThreshold == ms)
        return;

    m_longPressThreshold = ms;
    emit longPressThresholdChanged();
}

int QQuickTapHandler::longPressThresholdMilliseconds() const
{
    return (m_longPressThreshold < 0 ? QGuiApplication::styleHints()->mousePressAndHoldInterval() : m_longPressThreshold);
}

void QQuickTapHandler::timerEvent(QTimerEvent *event)
{
    if (event->timerId() == m_longPressTimer.timerId()) {
        m_longPressTimer.stop();
        qCDebug(lcTapHandler) << objectName() << "longPressed";
        emit longPressed();
    } else if (event->timerId() == m_doubleTapTimer.timerId()) {
        m_doubleTapTimer.stop();
        qCDebug(lcTapHandler) << objectName() << "double-tap timer expired; taps:" << m_tapCount;
        Q_ASSERT(m_exclusiveSignals == (SingleTap | DoubleTap));
        if (m_tapCount == 1)
            emit singleTapped(m_singleTapReleasedPoint, m_singleTapReleasedButton);
        else if (m_tapCount == 2)
            emit doubleTapped(m_singleTapReleasedPoint, m_singleTapReleasedButton);
    }
}

/*!
    \qmlproperty enumeration QtQuick::TapHandler::gesturePolicy

    The spatial constraint for a tap or long press gesture to be recognized,
    in addition to the constraint that the release must occur before
    \l longPressThreshold has elapsed. If these constraints are not satisfied,
    the \l tapped signal is not emitted, and \l tapCount is not incremented.
    If the spatial constraint is violated, \l pressed transitions immediately
    from true to false, regardless of the time held.

    The \c gesturePolicy also affects grab behavior as described below.

    \value TapHandler.DragThreshold
           (the default value) The event point must not move significantly.
           If the mouse, finger or stylus moves past the system-wide drag
           threshold (QStyleHints::startDragDistance), the tap gesture is
           canceled, even if the button or finger is still pressed. This policy
           can be useful whenever TapHandler needs to cooperate with other
           input handlers (for example \l DragHandler) or event-handling Items
           (for example QtQuick Controls), because in this case TapHandler
           will not take the exclusive grab, but merely a
           \l {QPointerEvent::addPassiveGrabber()}{passive grab}.

    \value TapHandler.WithinBounds
           If the event point leaves the bounds of the \c parent Item, the tap
           gesture is canceled. The TapHandler will take the
           \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on
           press, but will release the grab as soon as the boundary constraint
           is no longer satisfied.

    \value TapHandler.ReleaseWithinBounds
           At the time of release (the mouse button is released or the finger
           is lifted), if the event point is outside the bounds of the
           \c parent Item, a tap gesture is not recognized. This corresponds to
           typical behavior for button widgets: you can cancel a click by
           dragging outside the button, and you can also change your mind by
           dragging back inside the button before release. Note that it's
           necessary for TapHandler to take the
           \l {QPointerEvent::setExclusiveGrabber}{exclusive grab} on press
           and retain it until release in order to detect this gesture.

    \value TapHandler.DragWithinBounds
           On press, TapHandler takes the
           \l {QPointerEvent::setExclusiveGrabber}{exclusive grab}; after that,
           the event point can be dragged within the bounds of the \c parent
           item, while the \l timeHeld property keeps counting, and the
           \l longPressed() signal will be emitted regardless of drag distance.
           However, like \c WithinBounds, if the point leaves the bounds,
           the tap gesture is \l {PointerHandler::}{canceled()}, \l active()
           becomes \c false, and \l timeHeld stops counting. This is suitable
           for implementing press-drag-release components, such as menus, in
           which a single TapHandler detects press, \c timeHeld drives an
           "opening" animation, and then the user can drag to a menu item and
           release, while never leaving the bounds of the parent scene containing
           the menu. This value was added in Qt 6.3.
*/
void QQuickTapHandler::setGesturePolicy(QQuickTapHandler::GesturePolicy gesturePolicy)
{
    if (m_gesturePolicy == gesturePolicy)
        return;

    m_gesturePolicy = gesturePolicy;
    emit gesturePolicyChanged();
}

/*!
    \qmlproperty enumeration QtQuick::TapHandler::exclusiveSignals
    \since 6.5

    Determines the exclusivity of the singleTapped() and doubleTapped() signals.

    \value NotExclusive (the default) singleTapped() and doubleTapped() are
    emitted immediately when the user taps once or twice, respectively.

    \value SingleTap singleTapped() is emitted immediately when the user taps
    once, and doubleTapped() is never emitted.

    \value DoubleTap doubleTapped() is emitted immediately when the user taps
    twice, and singleTapped() is never emitted.

    \value (SingleTap | DoubleTap) Both signals are delayed until
    QStyleHints::mouseDoubleClickInterval(), such that either singleTapped()
    or doubleTapped() can be emitted, but not both. But if 3 or more taps
    occur within \c mouseDoubleClickInterval, neither signal is emitted.

    \note The remaining signals such as tapped() and tapCountChanged() are
    always emitted immediately, regardless of this property.
*/
void QQuickTapHandler::setExclusiveSignals(QQuickTapHandler::ExclusiveSignals exc)
{
    if (m_exclusiveSignals == exc)
        return;

    m_exclusiveSignals = exc;
    emit exclusiveSignalsChanged();
}

/*!
    \qmlproperty bool QtQuick::TapHandler::pressed
    \readonly

    Holds true whenever the mouse or touch point is pressed,
    and any movement since the press is compliant with the current
    \l gesturePolicy. When the event point is released or the policy is
    violated, \e pressed will change to false.
*/
void QQuickTapHandler::setPressed(bool press, bool cancel, QPointerEvent *event, QEventPoint &point)
{
    if (m_pressed != press) {
        qCDebug(lcTapHandler) << objectName() << "pressed" << m_pressed << "->" << press
                              << (cancel ? "CANCEL" : "") << point << "gp" << m_gesturePolicy;
        m_pressed = press;
        connectPreRenderSignal(press);
        updateTimeHeld();
        if (press) {
            m_longPressTimer.start(longPressThresholdMilliseconds(), this);
            m_holdTimer.start();
        } else {
            m_longPressTimer.stop();
            m_holdTimer.invalidate();
            if (m_exclusiveSignals == (SingleTap | DoubleTap)) {
                if (m_tapCount == 0) {
                    m_singleTapReleasedPoint = point;
                    m_singleTapReleasedButton = event->isSinglePointEvent() ? static_cast<QSinglePointEvent *>(event)->button() : Qt::NoButton;
                    qCDebug(lcTapHandler) << objectName() << "waiting to emit singleTapped:" << qApp->styleHints()->mouseDoubleClickInterval() << "ms";
                    m_doubleTapTimer.start(qApp->styleHints()->mouseDoubleClickInterval(), this);
                } else if (m_doubleTapTimer.isActive()) {
                    qCDebug(lcTapHandler) << objectName() << "tap" << (m_tapCount + 1) << "after" << event->timestamp() / 1000.0 - m_lastTapTimestamp << "sec";
                }
            }
        }
        if (press) {
            // on press, grab before emitting changed signals
            if (m_gesturePolicy == DragThreshold)
                setPassiveGrab(event, point, press);
            else
                setExclusiveGrab(event, point, press);
        }
        if (!cancel && !press && parentContains(point)) {
            if (point.timeHeld() < longPressThreshold()) {
                // Assuming here that pointerEvent()->timestamp() is in ms.
                const qreal ts = event->timestamp() / 1000.0;
                const qreal interval = ts - m_lastTapTimestamp;
                const auto distanceSquared = QVector2D(point.scenePosition() - m_lastTapPos).lengthSquared();
                if (interval < m_multiTapInterval && distanceSquared <
                        (event->device()->type() == QInputDevice::DeviceType::Mouse ?
                         m_mouseMultiClickDistanceSquared : m_touchMultiTapDistanceSquared))
                    ++m_tapCount;
                else
                    m_tapCount = 1;
                qCDebug(lcTapHandler) << objectName() << "tapped" << m_tapCount << "times; interval since last:" << interval
                                      << "sec; distance since last:" << qSqrt(distanceSquared);
                auto button = event->isSinglePointEvent() ? static_cast<QSinglePointEvent *>(event)->button() : Qt::NoButton;
                emit tapped(point, button);
                emit tapCountChanged();
                if (m_tapCount == 1 && !m_exclusiveSignals.testFlag(DoubleTap))
                    emit singleTapped(point, button);
                else if (m_tapCount == 2 && !m_exclusiveSignals.testFlag(SingleTap)) {
                    emit doubleTapped(point, button);
                }
                m_lastTapTimestamp = ts;
                m_lastTapPos = point.scenePosition();
            } else {
                qCDebug(lcTapHandler) << objectName() << "tap threshold" << longPressThreshold() << "exceeded:" << point.timeHeld();
            }
        }
        emit pressedChanged();
        if (!press && m_gesturePolicy != DragThreshold) {
            // on release, ungrab after emitting changed signals
            setExclusiveGrab(event, point, press);
        }
        if (cancel) {
            emit canceled(point);
            setExclusiveGrab(event, point, false);
            // In case there is a filtering parent (Flickable), we should not give up the passive grab,
            // so that it can continue to filter future events.
            d_func()->reset();
            emit pointChanged();
        }
    }
}

void QQuickTapHandler::onGrabChanged(QQuickPointerHandler *grabber, QPointingDevice::GrabTransition transition,
                                     QPointerEvent *ev, QEventPoint &point)
{
    QQuickSinglePointHandler::onGrabChanged(grabber, transition, ev, point);
    bool isCanceled = transition == QPointingDevice::CancelGrabExclusive || transition == QPointingDevice::CancelGrabPassive;
    if (grabber == this && (isCanceled || point.state() == QEventPoint::Released))
        setPressed(false, isCanceled, ev, point);
}

void QQuickTapHandler::connectPreRenderSignal(bool conn)
{
    auto par = parentItem();
    if (!par)
        return;
    if (conn)
        connect(par->window(), &QQuickWindow::beforeSynchronizing, this, &QQuickTapHandler::updateTimeHeld);
    else
        disconnect(par->window(), &QQuickWindow::beforeSynchronizing, this, &QQuickTapHandler::updateTimeHeld);
}

void QQuickTapHandler::updateTimeHeld()
{
    emit timeHeldChanged();
}

/*!
    \qmlproperty int QtQuick::TapHandler::tapCount
    \readonly

    The number of taps which have occurred within the time and space
    constraints to be considered a single gesture.  For example, to detect
    a triple-tap, you can write:

    \qml
    Rectangle {
        width: 100; height: 30
        signal tripleTap
        TapHandler {
            acceptedButtons: Qt.AllButtons
            onTapped: if (tapCount == 3) tripleTap()
        }
    }
    \endqml
*/

/*!
    \qmlproperty real QtQuick::TapHandler::timeHeld
    \readonly

    The amount of time in seconds that a pressed point has been held, without
    moving beyond the drag threshold. It will be updated at least once per
    frame rendered, which enables rendering an animation showing the progress
    towards an action which will be triggered by a long-press. It is also
    possible to trigger one of a series of actions depending on how long the
    press is held.

    A value of less than zero means no point is being held within this
    handler's \l [QML] Item.

    \note If \l gesturePolicy is set to \c TapHandler.DragWithinBounds,
    \c timeHeld does not stop counting even when the pressed point is moved
    beyond the drag threshold, but only when the point leaves the \l {Item::}
    {parent} item's \l {QtQuick::Item::contains()}{bounds}.
*/

/*!
    \qmlsignal QtQuick::TapHandler::tapped(eventPoint eventPoint, Qt::MouseButton button)

    This signal is emitted each time the \c parent Item is tapped.

    That is, if you press and release a touchpoint or button within a time
    period less than \l longPressThreshold, while any movement does not exceed
    the drag threshold, then the \c tapped signal will be emitted at the time
    of release.  The \a eventPoint signal parameter contains information
    from the release event about the point that was tapped, and \a button
    is the \l {Qt::MouseButton}{mouse button} that was clicked, or \c NoButton
    on a touchscreen.

    \snippet pointerHandlers/tapHandlerOnTapped.qml 0
*/

/*!
    \qmlsignal QtQuick::TapHandler::singleTapped(eventPoint eventPoint, Qt::MouseButton button)
    \since 5.11

    This signal is emitted when the \c parent Item is tapped once.
    After an amount of time greater than QStyleHints::mouseDoubleClickInterval,
    it can be tapped again; but if the time until the next tap is less,
    \l tapCount will increase. The \a eventPoint signal parameter contains
    information from the release event about the point that was tapped, and
    \a button is the \l {Qt::MouseButton}{mouse button} that was clicked, or
    \c NoButton on a touchscreen.
*/

/*!
    \qmlsignal QtQuick::TapHandler::doubleTapped(eventPoint eventPoint, Qt::MouseButton button)
    \since 5.11

    This signal is emitted when the \c parent Item is tapped twice within a
    short span of time (QStyleHints::mouseDoubleClickInterval()) and distance
    (QStyleHints::mouseDoubleClickDistance() or
    QStyleHints::touchDoubleTapDistance()). This signal always occurs after
    \l singleTapped, \l tapped, and \l tapCountChanged. The \a eventPoint
    signal parameter contains information from the release event about the
    point that was tapped, and \a button is the
    \l {Qt::MouseButton}{mouse button} that was clicked, or \c NoButton
    on a touchscreen.
*/

/*!
    \qmlsignal QtQuick::TapHandler::longPressed()

    This signal is emitted when the \c parent Item is pressed and held for a
    time period greater than \l longPressThreshold. That is, if you press and
    hold a touchpoint or button, while any movement does not exceed the drag
    threshold, then the \c longPressed signal will be emitted at the time that
    \l timeHeld exceeds \l longPressThreshold.
*/

/*!
    \qmlsignal QtQuick::TapHandler::tapCountChanged()

    This signal is emitted when the \c parent Item is tapped once or more (within
    a specified time and distance span) and when the present \c tapCount differs
    from the previous \c tapCount.
*/
QT_END_NAMESPACE

#include "moc_qquicktaphandler_p.cpp"