diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-04-24 22:35:59 +0200 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-04-25 13:27:14 +0000 |
commit | 279ab48f8bf61a06d5a334924f0b01f45e202cef (patch) | |
tree | 42828f3642625aca32aa30257637ca4f65f10170 /src | |
parent | c6151f84a7bd7cadb5f86d58582fb659ce4fae09 (diff) |
QQuickDrawer: move mouse/touch release handling to handleRelease()
Change-Id: I279fe0a0c77e482a414424f90827e85a91df221a
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickdrawer.cpp | 150 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdrawer_p.h | 1 | ||||
-rw-r--r-- | src/quicktemplates2/qquickdrawer_p_p.h | 2 |
3 files changed, 66 insertions, 87 deletions
diff --git a/src/quicktemplates2/qquickdrawer.cpp b/src/quicktemplates2/qquickdrawer.cpp index 7ecd1558..02281d5e 100644 --- a/src/quicktemplates2/qquickdrawer.cpp +++ b/src/quicktemplates2/qquickdrawer.cpp @@ -333,66 +333,6 @@ bool QQuickDrawerPrivate::grabMouse(QMouseEvent *event) static const qreal openCloseVelocityThreshold = 300; -bool QQuickDrawerPrivate::ungrabMouse(QMouseEvent *event) -{ - bool wasGrabbed = popupItem->keepMouseGrab(); - if (wasGrabbed) { - const QPointF releasePoint = event->windowPos(); - velocityCalculator.stopMeasuring(releasePoint, event->timestamp()); - - qreal velocity = 0; - if (edge == Qt::LeftEdge || edge == Qt::RightEdge) - velocity = velocityCalculator.velocity().x(); - else - velocity = velocityCalculator.velocity().y(); - - // the velocity is calculated so that swipes from left to right - // and top to bottom have positive velocity, and swipes from right - // to left and bottom to top have negative velocity. - // - // - top/left edge: positive velocity opens, negative velocity closes - // - bottom/right edge: negative velocity opens, positive velocity closes - // - // => invert the velocity for bottom and right edges, for the threshold comparison below - if (edge == Qt::RightEdge || edge == Qt::BottomEdge) - velocity = -velocity; - - if (position > 0.7 || velocity > openCloseVelocityThreshold) { - transitionManager.transitionEnter(); - } else if (position < 0.3 || velocity < -openCloseVelocityThreshold) { - transitionManager.transitionExit(); - } else { - switch (edge) { - case Qt::LeftEdge: - if (releasePoint.x() - pressPoint.x() > 0) - transitionManager.transitionEnter(); - else - transitionManager.transitionExit(); - break; - case Qt::RightEdge: - if (releasePoint.x() - pressPoint.x() < 0) - transitionManager.transitionEnter(); - else - transitionManager.transitionExit(); - break; - case Qt::TopEdge: - if (releasePoint.y() - pressPoint.y() > 0) - transitionManager.transitionEnter(); - else - transitionManager.transitionExit(); - break; - case Qt::BottomEdge: - if (releasePoint.y() - pressPoint.y() < 0) - transitionManager.transitionEnter(); - else - transitionManager.transitionExit(); - break; - } - } - } - return wasGrabbed; -} - void QQuickDrawerPrivate::handlePress(const QPointF &point, ulong timestamp) { QQuickPopupPrivate::handlePress(point, timestamp); @@ -409,7 +349,68 @@ void QQuickDrawerPrivate::handleMove(const QPointF &point, ulong timestamp) void QQuickDrawerPrivate::handleRelease(const QPointF &point, ulong timestamp) { - QQuickPopupPrivate::handleRelease(point, timestamp); + pressPoint = QPointF(); + + if (!popupItem->keepMouseGrab() && !popupItem->keepTouchGrab()) { + velocityCalculator.reset(); + QQuickPopupPrivate::handleRelease(point, timestamp); + return; + } + + velocityCalculator.stopMeasuring(point, timestamp); + + qreal velocity = 0; + if (edge == Qt::LeftEdge || edge == Qt::RightEdge) + velocity = velocityCalculator.velocity().x(); + else + velocity = velocityCalculator.velocity().y(); + + // the velocity is calculated so that swipes from left to right + // and top to bottom have positive velocity, and swipes from right + // to left and bottom to top have negative velocity. + // + // - top/left edge: positive velocity opens, negative velocity closes + // - bottom/right edge: negative velocity opens, positive velocity closes + // + // => invert the velocity for bottom and right edges, for the threshold comparison below + if (edge == Qt::RightEdge || edge == Qt::BottomEdge) + velocity = -velocity; + + if (position > 0.7 || velocity > openCloseVelocityThreshold) { + transitionManager.transitionEnter(); + } else if (position < 0.3 || velocity < -openCloseVelocityThreshold) { + transitionManager.transitionExit(); + } else { + switch (edge) { + case Qt::LeftEdge: + if (point.x() - pressPoint.x() > 0) + transitionManager.transitionEnter(); + else + transitionManager.transitionExit(); + break; + case Qt::RightEdge: + if (point.x() - pressPoint.x() < 0) + transitionManager.transitionEnter(); + else + transitionManager.transitionExit(); + break; + case Qt::TopEdge: + if (point.y() - pressPoint.y() > 0) + transitionManager.transitionEnter(); + else + transitionManager.transitionExit(); + break; + case Qt::BottomEdge: + if (point.y() - pressPoint.y() < 0) + transitionManager.transitionEnter(); + else + transitionManager.transitionExit(); + break; + } + } + + popupItem->setKeepMouseGrab(false); + popupItem->setKeepTouchGrab(false); } void QQuickDrawerPrivate::handleUngrab() @@ -469,19 +470,6 @@ bool QQuickDrawerPrivate::handleMouseMoveEvent(QQuickItem *item, QMouseEvent *ev return popupItem->keepMouseGrab(); } -bool QQuickDrawerPrivate::handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event) -{ - const bool wasGrabbed = ungrabMouse(event); - if (!wasGrabbed) - handleRelease(item->mapToScene(event->localPos()), event->timestamp()); - - popupItem->setKeepMouseGrab(false); - pressPoint = QPoint(); - event->accept(); - - return wasGrabbed; -} - static QList<QQuickStateAction> prepareTransition(QQuickDrawer *drawer, QQuickTransition *transition, qreal to) { QList<QQuickStateAction> actions; @@ -667,10 +655,12 @@ bool QQuickDrawer::childMouseEventFilter(QQuickItem *child, QEvent *event) case QEvent::MouseMove: return d->handleMouseMoveEvent(child, static_cast<QMouseEvent *>(event)); case QEvent::MouseButtonRelease: - return d->handleMouseReleaseEvent(child, static_cast<QMouseEvent *>(event)); + d->handleMouseEvent(child, static_cast<QMouseEvent *>(event)); + break; default: - return false; + break; } + return false; } void QQuickDrawer::mousePressEvent(QMouseEvent *event) @@ -685,12 +675,6 @@ void QQuickDrawer::mouseMoveEvent(QMouseEvent *event) d->handleMouseMoveEvent(d->popupItem, event); } -void QQuickDrawer::mouseReleaseEvent(QMouseEvent *event) -{ - Q_D(QQuickDrawer); - d->handleMouseReleaseEvent(d->popupItem, event); -} - bool QQuickDrawer::overlayEvent(QQuickItem *item, QEvent *event) { Q_D(QQuickDrawer); @@ -707,8 +691,6 @@ bool QQuickDrawer::overlayEvent(QQuickItem *item, QEvent *event) return d->handleMousePressEvent(item, static_cast<QMouseEvent *>(event)); case QEvent::MouseMove: return d->handleMouseMoveEvent(item, static_cast<QMouseEvent *>(event)); - case QEvent::MouseButtonRelease: - return d->handleMouseReleaseEvent(item, static_cast<QMouseEvent *>(event)); default: return QQuickPopup::overlayEvent(item, event); } diff --git a/src/quicktemplates2/qquickdrawer_p.h b/src/quicktemplates2/qquickdrawer_p.h index f4987e8b..74b92717 100644 --- a/src/quicktemplates2/qquickdrawer_p.h +++ b/src/quicktemplates2/qquickdrawer_p.h @@ -88,7 +88,6 @@ protected: bool childMouseEventFilter(QQuickItem *child, QEvent *event) override; void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; - void mouseReleaseEvent(QMouseEvent *event) override; bool overlayEvent(QQuickItem *item, QEvent *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 bb341d50..5117ef77 100644 --- a/src/quicktemplates2/qquickdrawer_p_p.h +++ b/src/quicktemplates2/qquickdrawer_p_p.h @@ -72,7 +72,6 @@ public: bool startDrag(QEvent *event); bool grabMouse(QMouseEvent *event); - bool ungrabMouse(QMouseEvent *event); void handlePress(const QPointF &point, ulong timestamp) override; void handleMove(const QPointF &point, ulong timestamp) override; @@ -81,7 +80,6 @@ public: bool handleMousePressEvent(QQuickItem *item, QMouseEvent *event); bool handleMouseMoveEvent(QQuickItem *item, QMouseEvent *event); - bool handleMouseReleaseEvent(QQuickItem *item, QMouseEvent *event); bool prepareEnterTransition() override; bool prepareExitTransition() override; |