diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-01-24 11:10:15 +0100 |
---|---|---|
committer | J-P Nurmi <jpnurmi@qt.io> | 2017-01-24 11:53:05 +0000 |
commit | 2b60b354e6a9a131ed79190315c7d9e0173bb004 (patch) | |
tree | 925e479fd6856cf4392d1b707780ff3522ee4f1c /src | |
parent | dc5f909fe1337aa6d7adf2c2445adaec547c5c6f (diff) |
QQuickSwitchDelegate: handle touch events
This makes it possible to interact with multiple switch delegates at
the same time.
Change-Id: I40d1f31d2e361665b2e09b3bb071832f3efcd75b
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/quicktemplates2/qquickswitchdelegate.cpp | 66 | ||||
-rw-r--r-- | src/quicktemplates2/qquickswitchdelegate_p.h | 3 |
2 files changed, 46 insertions, 23 deletions
diff --git a/src/quicktemplates2/qquickswitchdelegate.cpp b/src/quicktemplates2/qquickswitchdelegate.cpp index 82c9594c..9bce76db 100644 --- a/src/quicktemplates2/qquickswitchdelegate.cpp +++ b/src/quicktemplates2/qquickswitchdelegate.cpp @@ -83,6 +83,10 @@ public: qreal positionAt(const QPointF &point) const; + bool canDrag(const QPointF &movePoint) const; + void handleMove(const QPointF &point) override; + void handleRelease(const QPointF &point) override; + qreal position; }; @@ -97,6 +101,32 @@ qreal QQuickSwitchDelegatePrivate::positionAt(const QPointF &point) const return pos; } +bool QQuickSwitchDelegatePrivate::canDrag(const QPointF &movePoint) const +{ + // don't start dragging the handle unless the initial press was at the indicator, + // or the drag has reached the indicator area. this prevents unnatural jumps when + // dragging far outside the indicator. + const qreal pressPos = positionAt(pressPoint); + const qreal movePos = positionAt(movePoint); + return (pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0); +} + +void QQuickSwitchDelegatePrivate::handleMove(const QPointF &point) +{ + Q_Q(QQuickSwitchDelegate); + QQuickItemDelegatePrivate::handleMove(point); + if (q->keepMouseGrab() || q->keepTouchGrab()) + q->setPosition(positionAt(point)); +} + +void QQuickSwitchDelegatePrivate::handleRelease(const QPointF &point) +{ + Q_Q(QQuickSwitchDelegate); + QQuickItemDelegatePrivate::handleRelease(point); + q->setKeepMouseGrab(false); + q->setKeepTouchGrab(false); +} + QQuickSwitchDelegate::QQuickSwitchDelegate(QQuickItem *parent) : QQuickItemDelegate(*(new QQuickSwitchDelegatePrivate), parent) { @@ -143,35 +173,29 @@ qreal QQuickSwitchDelegate::visualPosition() const return d->position; } -void QQuickSwitchDelegate::mousePressEvent(QMouseEvent *event) -{ - QQuickItemDelegate::mousePressEvent(event); -} - void QQuickSwitchDelegate::mouseMoveEvent(QMouseEvent *event) { Q_D(QQuickSwitchDelegate); - QQuickItemDelegate::mouseMoveEvent(event); - - const QPointF movePoint = event->localPos(); if (!keepMouseGrab()) { - // don't start dragging the handle unless the initial press was at the indicator, - // or the drag has reached the indicator area. this prevents unnatural jumps when - // dragging far outside the indicator. - const qreal pressPos = d->positionAt(d->pressPoint); - const qreal movePos = d->positionAt(movePoint); - if ((pressPos >= 0.0 && pressPos <= 1.0) || (movePos >= 0.0 && movePos <= 1.0)) + const QPointF movePoint = event->localPos(); + if (d->canDrag(movePoint)) setKeepMouseGrab(QQuickWindowPrivate::dragOverThreshold(movePoint.x() - d->pressPoint.x(), Qt::XAxis, event)); } - - if (keepMouseGrab()) - setPosition(d->positionAt(movePoint)); + QQuickItemDelegate::mouseMoveEvent(event); } -void QQuickSwitchDelegate::mouseReleaseEvent(QMouseEvent *event) +void QQuickSwitchDelegate::touchEvent(QTouchEvent *event) { - QQuickItemDelegate::mouseReleaseEvent(event); - setKeepMouseGrab(false); + Q_D(QQuickSwitchDelegate); + if (!keepTouchGrab() && event->type() == QEvent::TouchUpdate) { + for (const QTouchEvent::TouchPoint &point : event->touchPoints()) { + if (point.id() != d->touchId || point.state() != Qt::TouchPointMoved) + continue; + if (d->canDrag(point.pos())) + setKeepTouchGrab(QQuickWindowPrivate::dragOverThreshold(point.pos().x() - d->pressPoint.x(), Qt::XAxis, &point)); + } + } + QQuickItemDelegate::touchEvent(event); } QFont QQuickSwitchDelegate::defaultFont() const @@ -188,7 +212,7 @@ void QQuickSwitchDelegate::mirrorChange() void QQuickSwitchDelegate::nextCheckState() { Q_D(QQuickSwitchDelegate); - if (keepMouseGrab()) { + if (keepMouseGrab() || keepTouchGrab()) { d->toggle(d->position > 0.5); // the checked state might not change => force a position update to // avoid that the handle is left somewhere in the middle (QTBUG-57944) diff --git a/src/quicktemplates2/qquickswitchdelegate_p.h b/src/quicktemplates2/qquickswitchdelegate_p.h index f8cb33d8..e3e0804a 100644 --- a/src/quicktemplates2/qquickswitchdelegate_p.h +++ b/src/quicktemplates2/qquickswitchdelegate_p.h @@ -73,9 +73,8 @@ Q_SIGNALS: void visualPositionChanged(); protected: - void mousePressEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override; - void mouseReleaseEvent(QMouseEvent *event) override; + void touchEvent(QTouchEvent *event) override; QFont defaultFont() const override; void mirrorChange() override; |