aboutsummaryrefslogtreecommitdiffstats
path: root/src/quicktemplates2/qquickslider.cpp
diff options
context:
space:
mode:
authorJ-P Nurmi <jpnurmi@qt.io>2017-01-02 17:39:44 +0100
committerJ-P Nurmi <jpnurmi@qt.io>2017-01-03 13:36:48 +0000
commita11ae74d23890fc3ba1782a286bc226e2db21439 (patch)
treee8ba21a0fb7dc58e7b57d113dbddb7a121249cf7 /src/quicktemplates2/qquickslider.cpp
parent7c5920885ccefd999ed55245c1a500e110a5acb7 (diff)
QQuickSlider: handle touch events
In comparison to handling synthesized mouse events, handling touch events has the advantage that it gives multi-touch support. That is, it is possible to move multiple sliders at the same time, each handling its own touch point. Change-Id: I713307b0e6b5ee777496fc9ba68a5180d13a6aca Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'src/quicktemplates2/qquickslider.cpp')
-rw-r--r--src/quicktemplates2/qquickslider.cpp60
1 files changed, 59 insertions, 1 deletions
diff --git a/src/quicktemplates2/qquickslider.cpp b/src/quicktemplates2/qquickslider.cpp
index bb3c0158..2398b1e1 100644
--- a/src/quicktemplates2/qquickslider.cpp
+++ b/src/quicktemplates2/qquickslider.cpp
@@ -89,7 +89,7 @@ class QQuickSliderPrivate : public QQuickControlPrivate
public:
QQuickSliderPrivate() : from(0), to(1), value(0), position(0), stepSize(0), live(false), pressed(false),
- orientation(Qt::Horizontal), snapMode(QQuickSlider::NoSnap),
+ touchId(-1), orientation(Qt::Horizontal), snapMode(QQuickSlider::NoSnap),
handle(nullptr)
{
}
@@ -111,6 +111,7 @@ public:
qreal stepSize;
bool live;
bool pressed;
+ int touchId;
QPointF pressPoint;
Qt::Orientation orientation;
QQuickSlider::SnapMode snapMode;
@@ -201,6 +202,7 @@ void QQuickSliderPrivate::handleMove(const QPointF &point)
void QQuickSliderPrivate::handleRelease(const QPointF &point)
{
Q_Q(QQuickSlider);
+ touchId = -1;
pressPoint = QPointF();
const qreal oldPos = position;
qreal pos = positionAt(point);
@@ -220,6 +222,7 @@ void QQuickSliderPrivate::handleRelease(const QPointF &point)
void QQuickSliderPrivate::handleUngrab()
{
Q_Q(QQuickSlider);
+ touchId = -1;
pressPoint = QPointF();
q->setPressed(false);
}
@@ -645,6 +648,61 @@ void QQuickSlider::mouseUngrabEvent()
d->handleUngrab();
}
+void QQuickSlider::touchEvent(QTouchEvent *event)
+{
+ Q_D(QQuickSlider);
+ switch (event->type()) {
+ case QEvent::TouchBegin:
+ if (d->touchId == -1) {
+ const QTouchEvent::TouchPoint point = event->touchPoints().first();
+ d->touchId = point.id();
+ d->handlePress(point.pos());
+ } else {
+ event->ignore();
+ }
+ break;
+
+ case QEvent::TouchUpdate:
+ for (const QTouchEvent::TouchPoint &point : event->touchPoints()) {
+ if (point.id() != d->touchId)
+ continue;
+
+ if (!keepMouseGrab()) {
+ if (d->orientation == Qt::Horizontal)
+ setKeepMouseGrab(QQuickWindowPrivate::dragOverThreshold(point.pos().x() - d->pressPoint.x(), Qt::XAxis, &point));
+ else
+ setKeepMouseGrab(QQuickWindowPrivate::dragOverThreshold(point.pos().y() - d->pressPoint.y(), Qt::YAxis, &point));
+ }
+ d->handleMove(point.pos());
+ }
+ break;
+
+ case QEvent::TouchEnd:
+ for (const QTouchEvent::TouchPoint &point : event->touchPoints()) {
+ if (point.id() != d->touchId)
+ continue;
+
+ d->handleRelease(point.pos());
+ }
+ break;
+
+ case QEvent::TouchCancel:
+ d->handleUngrab();
+ break;
+
+ default:
+ QQuickControl::touchEvent(event);
+ break;
+ }
+}
+
+void QQuickSlider::touchUngrabEvent()
+{
+ Q_D(QQuickSlider);
+ QQuickControl::touchUngrabEvent();
+ d->handleUngrab();
+}
+
void QQuickSlider::wheelEvent(QWheelEvent *event)
{
Q_D(QQuickSlider);