PointHandler: distribute simultaneous touch presses properly

If multiple touchpoints are pressed simultaneously, each point can
be grabbed by one PointHandler instance. Each PointHandler instance
cannot grab more than one point, nor grab a point that is already
chosen by another PointHandler.  This was always the intention, but
got broken (perhaps by 3523b67638),
and there was no test coverage of this case until now.

Fixes: QTBUG-71431
Change-Id: I6d7614eb4767c677d929291f917cf62d9c03bd93
Reviewed-by: Andre de la Rocha <andre.rocha@qt.io>
This commit is contained in:
Shawn Rutledge 2018-12-12 22:41:13 +01:00
parent b1494ffa7d
commit 358a72b6ec
3 changed files with 166 additions and 1 deletions

View File

@ -108,7 +108,7 @@ bool QQuickSinglePointHandler::wantsPointerEvent(QQuickPointerEvent *event)
int candidatePointCount = 0;
int c = event->pointCount();
QQuickEventPoint *chosen = nullptr;
for (int i = 0; i < c; ++i) {
for (int i = 0; i < c && !chosen; ++i) {
QQuickEventPoint *p = event->point(i);
if (!p->exclusiveGrabber() && wantsEventPoint(p)) {
if (!chosen)

View File

@ -0,0 +1,86 @@
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.12
Item {
id: root
width: 400; height: 400
PointHandler {
id: ph1
objectName: "ph1"
target: Rectangle {
parent: root
visible: ph1.active
x: ph1.point.position.x - width / 2
y: ph1.point.position.y - height / 2
width: 140
height: width
radius: width / 2
color: "orange"
opacity: 0.3
}
}
PointHandler {
id: ph2
objectName: "ph2"
target: Rectangle {
parent: root
visible: ph2.active
x: ph2.point.position.x - width / 2
y: ph2.point.position.y - height / 2
width: 140
height: width
radius: width / 2
color: "orange"
opacity: 0.3
}
}
PointHandler {
id: ph3
objectName: "ph3"
target: Rectangle {
parent: root
visible: ph3.active
x: ph3.point.position.x - width / 2
y: ph3.point.position.y - height / 2
width: 140
height: width
radius: width / 2
color: "orange"
opacity: 0.3
}
}
}

View File

@ -55,6 +55,7 @@ private slots:
void initTestCase();
void singleTouch();
void simultaneousMultiTouch();
void pressedMultipleButtons_data();
void pressedMultipleButtons();
@ -135,6 +136,84 @@ void tst_PointHandler::singleTouch()
QCOMPARE(translationSpy.count(), 3);
}
void tst_PointHandler::simultaneousMultiTouch()
{
QScopedPointer<QQuickView> windowPtr;
createView(windowPtr, "multiPointTracker.qml");
QQuickView * window = windowPtr.data();
QList<QQuickPointHandler *> handlers = window->rootObject()->findChildren<QQuickPointHandler *>();
QCOMPARE(handlers.count(), 3);
QVector<QSignalSpy*> activeSpies;
QVector<QSignalSpy*> pointSpies;
QVector<QSignalSpy*> translationSpies;
QVector<QPoint> points{{100,100}, {200, 200}, {100, 300}};
QVector<QPoint> pressPoints = points;
for (auto h : handlers) {
activeSpies << new QSignalSpy(h, SIGNAL(activeChanged()));
pointSpies << new QSignalSpy(h, SIGNAL(pointChanged()));
translationSpies << new QSignalSpy(h, SIGNAL(translationChanged()));
}
QTest::touchEvent(window, touchDevice).press(1, points[0], window).press(2, points[1], window).press(3, points[2], window);
QQuickTouchUtils::flush(window);
QVector<int> pointIndexPerHandler;
int i = 0;
for (auto h : handlers) {
QTRY_COMPARE(h->active(), true);
QCOMPARE(activeSpies[i]->count(), 1);
QCOMPARE(pointSpies[i]->count(), 1);
int chosenPointIndex = points.indexOf(h->point().position().toPoint());
QVERIFY(chosenPointIndex != -1);
// Verify that each handler chose a unique point
QVERIFY(!pointIndexPerHandler.contains(chosenPointIndex));
pointIndexPerHandler.append(chosenPointIndex);
QPoint point = points[chosenPointIndex];
QCOMPARE(h->point().scenePosition().toPoint(), point);
QCOMPARE(h->point().pressedButtons(), Qt::NoButton);
QCOMPARE(h->translation(), QVector2D());
QCOMPARE(translationSpies[i]->count(), 1);
++i;
}
for (int i = 0; i < 3; ++i)
points[i] += QPoint(10 + 10 * i, 10 + 10 * i % 2);
QTest::touchEvent(window, touchDevice).move(1, points[0], window).move(2, points[1], window).move(3, points[2], window);
QQuickTouchUtils::flush(window);
i = 0;
for (auto h : handlers) {
QCOMPARE(h->active(), true);
QCOMPARE(activeSpies[i]->count(), 1);
QCOMPARE(pointSpies[i]->count(), 2);
QCOMPARE(h->point().position().toPoint(), points[pointIndexPerHandler[i]]);
QCOMPARE(h->point().scenePosition().toPoint(), points[pointIndexPerHandler[i]]);
QCOMPARE(h->point().pressPosition().toPoint(), pressPoints[pointIndexPerHandler[i]]);
QCOMPARE(h->point().scenePressPosition().toPoint(), pressPoints[pointIndexPerHandler[i]]);
QCOMPARE(h->point().pressedButtons(), Qt::NoButton);
QVERIFY(h->point().velocity().x() > 0);
QVERIFY(h->point().velocity().y() > 0);
QCOMPARE(h->translation(), QVector2D(10 + 10 * pointIndexPerHandler[i], 10 + 10 * pointIndexPerHandler[i] % 2));
QCOMPARE(translationSpies[i]->count(), 2);
++i;
}
QTest::touchEvent(window, touchDevice).release(1, points[0], window).release(2, points[1], window).release(3, points[2], window);
QQuickTouchUtils::flush(window);
i = 0;
for (auto h : handlers) {
QTRY_COMPARE(h->active(), false);
QCOMPARE(activeSpies[i]->count(), 2);
QCOMPARE(pointSpies[i]->count(), 3);
QCOMPARE(h->translation(), QVector2D());
QCOMPARE(translationSpies[i]->count(), 3);
++i;
}
qDeleteAll(activeSpies);
qDeleteAll(pointSpies);
qDeleteAll(translationSpies);
}
void tst_PointHandler::pressedMultipleButtons_data()
{
QTest::addColumn<Qt::MouseButtons>("accepted");