diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-04-25 00:06:39 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-04-26 17:24:30 +0000 |
commit | 763b247911cb58663c749e44064e2ab6fc3396f8 (patch) | |
tree | 1cfef065718e8842bc22ac2b22da249dfbb4b81a | |
parent | 3326439361d3ec4e60bcdb189e6bfb75189136ca (diff) |
Enable touch events for popups and drawers
Change-Id: Icc0a496facdc2ada1c87b4b02c49a58cb9a4daec
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
-rw-r--r-- | src/quicktemplates2/qquickdrawer.cpp | 65 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdrawer_p.h | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdrawer_p_p.h | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickpopup.cpp | 3 |
4 files changed, 58 insertions, 12 deletions
diff --git a/src/quicktemplates2/qquickdrawer.cpp b/src/quicktemplates2/qquickdrawer.cpp index 66ba74a2..3275a0b2 100644 --- a/src/quicktemplates2/qquickdrawer.cpp +++ b/src/quicktemplates2/qquickdrawer.cpp @@ -328,6 +328,53 @@ bool QQuickDrawerPrivate::grabMouse(QQuickItem *item, QMouseEvent *event) return overThreshold; } +bool QQuickDrawerPrivate::grabTouch(QQuickItem *item, QTouchEvent *event) +{ + Q_Q(QQuickDrawer); + handleTouchEvent(item, event); + + if (!window || !interactive || popupItem->keepTouchGrab() || !event->touchPointStates().testFlag(Qt::TouchPointMoved)) + return false; + + bool overThreshold = false; + for (const QTouchEvent::TouchPoint &point : event->touchPoints()) { + if (!acceptTouch(point) || point.state() != Qt::TouchPointMoved) + continue; + + const QPointF movePoint = point.scenePos(); + + // Flickable uses a hard-coded threshold of 15 for flicking, and + // QStyleHints::startDragDistance for dragging. Drawer uses a bit + // larger threshold to avoid being too eager to steal touch (QTBUG-50045) + const int threshold = qMax(20, QGuiApplication::styleHints()->startDragDistance() + 5); + if (position > 0 || dragMargin > 0) { + if (edge == Qt::LeftEdge || edge == Qt::RightEdge) + overThreshold = QQuickWindowPrivate::dragOverThreshold(movePoint.x() - pressPoint.x(), Qt::XAxis, &point, threshold); + else + overThreshold = QQuickWindowPrivate::dragOverThreshold(movePoint.y() - pressPoint.y(), Qt::YAxis, &point, threshold); + } + + // Don't be too eager to steal presses outside the drawer (QTBUG-53929) + if (overThreshold && qFuzzyCompare(position, qreal(1.0)) && !popupItem->contains(popupItem->mapFromScene(movePoint))) { + if (edge == Qt::LeftEdge || edge == Qt::RightEdge) + overThreshold = qAbs(movePoint.x() - q->width()) < dragMargin; + else + overThreshold = qAbs(movePoint.y() - q->height()) < dragMargin; + } + + if (overThreshold) { + popupItem->setKeepTouchGrab(true); + offset = positionAt(movePoint) - position; + + // don't jump when dragged open + if (offset > 0 && position > 0 && !popupItem->contains(popupItem->mapFromScene(movePoint))) + offset = 0; + } + } + + return overThreshold; +} + static const qreal openCloseVelocityThreshold = 300; bool QQuickDrawerPrivate::handlePress(QQuickItem *item, const QPointF &point, ulong timestamp) @@ -615,6 +662,8 @@ bool QQuickDrawer::childMouseEventFilter(QQuickItem *child, QEvent *event) { Q_D(QQuickDrawer); switch (event->type()) { + case QEvent::TouchUpdate: + return d->grabTouch(child, static_cast<QTouchEvent *>(event)); case QEvent::MouseMove: return d->grabMouse(child, static_cast<QMouseEvent *>(event)); case QEvent::MouseButtonPress: @@ -636,24 +685,22 @@ bool QQuickDrawer::overlayEvent(QQuickItem *item, QEvent *event) { Q_D(QQuickDrawer); switch (event->type()) { - case QEvent::TouchBegin: case QEvent::TouchUpdate: - case QEvent::TouchEnd: - case QEvent::TouchCancel: - // TODO: QQuickDrawer still relies on synthesized mouse events - event->ignore(); - return false; - + return d->grabTouch(item, static_cast<QTouchEvent *>(event)); case QEvent::MouseMove: return d->grabMouse(item, static_cast<QMouseEvent *>(event)); - default: break; } - return QQuickPopup::overlayEvent(item, event); } +void QQuickDrawer::touchEvent(QTouchEvent *event) +{ + Q_D(QQuickDrawer); + d->grabTouch(d->popupItem, event); +} + void QQuickDrawer::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) { Q_D(QQuickDrawer); diff --git a/src/quicktemplates2/qquickdrawer_p.h b/src/quicktemplates2/qquickdrawer_p.h index 38b425be..9ab1c4ef 100644 --- a/src/quicktemplates2/qquickdrawer_p.h +++ b/src/quicktemplates2/qquickdrawer_p.h @@ -88,6 +88,7 @@ protected: bool childMouseEventFilter(QQuickItem *child, QEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; bool overlayEvent(QQuickItem *item, QEvent *event) override; + void touchEvent(QTouchEvent *event) override; void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; diff --git a/src/quicktemplates2/qquickdrawer_p_p.h b/src/quicktemplates2/qquickdrawer_p_p.h index bcfd7206..8956acef 100644 --- a/src/quicktemplates2/qquickdrawer_p_p.h +++ b/src/quicktemplates2/qquickdrawer_p_p.h @@ -72,6 +72,7 @@ public: bool startDrag(QEvent *event); bool grabMouse(QQuickItem *item, QMouseEvent *event); + bool grabTouch(QQuickItem *item, QTouchEvent *event); bool handlePress(QQuickItem* item, const QPointF &point, ulong timestamp) override; bool handleMove(QQuickItem* item, const QPointF &point, ulong timestamp) override; diff --git a/src/quicktemplates2/qquickpopup.cpp b/src/quicktemplates2/qquickpopup.cpp index ccbfd3c8..68a82204 100644 --- a/src/quicktemplates2/qquickpopup.cpp +++ b/src/quicktemplates2/qquickpopup.cpp @@ -1982,9 +1982,6 @@ void QQuickPopup::touchEvent(QTouchEvent *event) { Q_D(QQuickPopup); d->handleTouchEvent(d->popupItem, event); - - // TODO: QQuickPopup still relies on synthesized mouse events - event->ignore(); } void QQuickPopup::touchUngrabEvent() |