aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-01-24 10:48:46 +0100
committerJ-P Nurmi <jpnurmi@qt.io>2017-01-24 11:53:02 +0000
commitdc5f909fe1337aa6d7adf2c2445adaec547c5c6f (patch)
tree00a4691b9ca2ae294ff3c06e119d33df6d249ce6 /src
parent3ac5f0c795a91efa975935d889c99052b8bc2137 (diff)
QQuickSwitch: handle touch events
This makes it possible to interact with multiple switches at the same time. Change-Id: I1a8c2ff6ddb7e867b3d9d66f15c49ae5b70231e7 Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/quicktemplates2/qquickswitch.cpp66
-rw-r--r--src/quicktemplates2/qquickswitch_p.h3
2 files changed, 46 insertions, 23 deletions
diff --git a/src/quicktemplates2/qquickswitch.cpp b/src/quicktemplates2/qquickswitch.cpp
index f96fa391..ece9802d 100644
--- a/src/quicktemplates2/qquickswitch.cpp
+++ b/src/quicktemplates2/qquickswitch.cpp
@@ -86,6 +86,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;
};
@@ -100,6 +104,32 @@ qreal QQuickSwitchPrivate::positionAt(const QPointF &point) const
return pos;
}
+bool QQuickSwitchPrivate::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 QQuickSwitchPrivate::handleMove(const QPointF &point)
+{
+ Q_Q(QQuickSwitch);
+ QQuickAbstractButtonPrivate::handleMove(point);
+ if (q->keepMouseGrab() || q->keepTouchGrab())
+ q->setPosition(positionAt(point));
+}
+
+void QQuickSwitchPrivate::handleRelease(const QPointF &point)
+{
+ Q_Q(QQuickSwitch);
+ QQuickAbstractButtonPrivate::handleRelease(point);
+ q->setKeepMouseGrab(false);
+ q->setKeepTouchGrab(false);
+}
+
QQuickSwitch::QQuickSwitch(QQuickItem *parent)
: QQuickAbstractButton(*(new QQuickSwitchPrivate), parent)
{
@@ -146,35 +176,29 @@ qreal QQuickSwitch::visualPosition() const
return d->position;
}
-void QQuickSwitch::mousePressEvent(QMouseEvent *event)
-{
- QQuickAbstractButton::mousePressEvent(event);
-}
-
void QQuickSwitch::mouseMoveEvent(QMouseEvent *event)
{
Q_D(QQuickSwitch);
- QQuickAbstractButton::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));
+ QQuickAbstractButton::mouseMoveEvent(event);
}
-void QQuickSwitch::mouseReleaseEvent(QMouseEvent *event)
+void QQuickSwitch::touchEvent(QTouchEvent *event)
{
- QQuickAbstractButton::mouseReleaseEvent(event);
- setKeepMouseGrab(false);
+ Q_D(QQuickSwitch);
+ 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));
+ }
+ }
+ QQuickAbstractButton::touchEvent(event);
}
void QQuickSwitch::mirrorChange()
@@ -186,7 +210,7 @@ void QQuickSwitch::mirrorChange()
void QQuickSwitch::nextCheckState()
{
Q_D(QQuickSwitch);
- 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/qquickswitch_p.h b/src/quicktemplates2/qquickswitch_p.h
index ccdb23a8..5b93cd32 100644
--- a/src/quicktemplates2/qquickswitch_p.h
+++ b/src/quicktemplates2/qquickswitch_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;
void mirrorChange() override;