From 4e400369c08db251cd489fec1229398c224d02b4 Mon Sep 17 00:00:00 2001 From: Shawn Rutledge Date: Fri, 27 Mar 2020 16:06:11 +0000 Subject: Refactor pointer event hierarchy Some goals that have hopefully been achieved are: - make QPointerEvent and QEventPoint resemble their Qt Quick counterparts to such an extent that we can remove those wrappers and go back to delivering the original events in Qt Quick - make QEventPoint much smaller than QTouchEvent::TouchPoint, with no pimpl - remove most public setters - reduce the usage of complex constructors that take many arguments - don't repeat ourselves: move accessors and storage upwards rather than having redundant ones in subclasses - standardize the set of accessors in QPointerEvent - maintain source compatibility as much as possible: do not require modifying event-handling code in any QWidget subclass To avoid public setters we now introduce a few QMutable* subclasses. This is a bit like the Builder pattern except that it doesn't involve constructing a separate disposable object: the main event type can be cast to the mutable type at any time to enable modifications, iff the code is linked with gui-private. Therefore event classes can have less-"complete" constructors, because internal Qt code can use setters the same way it could use the ones in QTouchEvent before; and the event classes don't need many friends. Even some read-accessors can be kept private unless we are sure we want to expose them. Task-number: QTBUG-46266 Fixes: QTBUG-72173 Change-Id: I740e4e40165b7bc41223d38b200bbc2b403e07b6 Reviewed-by: Volker Hilsheimer --- .../qgraphicsitem/tst_qgraphicsitem.cpp | 65 ++++++++++------------ .../qgraphicsscene/tst_qgraphicsscene.cpp | 15 ++--- 2 files changed, 35 insertions(+), 45 deletions(-) (limited to 'tests/auto/widgets/graphicsview') diff --git a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp index 1bd5b160ea..1c311a27ea 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsitem/tst_qgraphicsitem.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -490,7 +491,7 @@ private slots: private: GraphicsItems paintedItems; - QPointingDevice *m_touchDevice = nullptr; + QPointingDevice *m_touchDevice = QTest::createTouchDevice(); }; void tst_QGraphicsItem::initMain() @@ -10980,7 +10981,7 @@ void tst_QGraphicsItem::focusHandling() class TouchEventTestee : public QGraphicsRectItem { public: - using TouchPoints = QList; + using TouchPoints = QList; TouchEventTestee(const QSizeF &size = QSizeF(100, 100)) : QGraphicsRectItem(QRectF(QPointF(), size)) @@ -11019,25 +11020,21 @@ private: TouchPoints m_touchUpdatePoints; }; -static QList +static QList createTouchPoints(const QGraphicsView &view, const QPointF &scenePos, const QSizeF &ellipseDiameters, - Qt::TouchPointState state = Qt::TouchPointPressed) + QEventPoint::State state = QEventPoint::State::Pressed) { - QTouchEvent::TouchPoint tp(0); - tp.setState(state); - tp.setScenePos(scenePos); - tp.setStartScenePos(scenePos); - tp.setLastScenePos(scenePos); const QPointF screenPos = view.viewport()->mapToGlobal(view.mapFromScene(scenePos)); - tp.setScreenPos(screenPos); - tp.setStartScreenPos(screenPos); - tp.setLastScreenPos(screenPos); + QMutableEventPoint tp(0, state, scenePos, screenPos); + tp.setState(state); + tp.setScenePosition(scenePos); + tp.setGlobalPosition(screenPos); + tp.setGlobalPressPosition(screenPos); + tp.setGlobalLastPosition(screenPos); tp.setEllipseDiameters(ellipseDiameters); - const QSizeF screenSize = view.screen()->geometry().size(); - tp.setNormalizedPos(QPointF(screenPos.x() / screenSize.width(), screenPos.y() / screenSize.height())); - return QList() << tp; + return QList() << tp; } static bool comparePointF(const QPointF &p1, const QPointF &p2) @@ -11104,15 +11101,14 @@ void tst_QGraphicsItem::touchEventPropagation() view.setSceneRect(touchEventReceiver->boundingRect()); view.show(); QVERIFY(QTest::qWaitForWindowExposed(&view)); + QInputDevicePrivate::get(m_touchDevice)->setAvailableVirtualGeometry(view.screen()->geometry()); QCOMPARE(touchEventReceiver->touchBeginEventCount(), 0); const QPointF scenePos = view.sceneRect().center(); sendMousePress(&scene, scenePos); - if (m_touchDevice == nullptr) - m_touchDevice = QTest::createTouchDevice(); - QTouchEvent touchBegin(QEvent::TouchBegin, m_touchDevice, Qt::NoModifier, Qt::TouchPointPressed, - createTouchPoints(view, scenePos, QSizeF(10, 10))); + QMutableTouchEvent touchBegin(QEvent::TouchBegin, m_touchDevice, Qt::NoModifier, + createTouchPoints(view, scenePos, QSizeF(10, 10))); touchBegin.setTarget(view.viewport()); qApp->sendEvent(&scene, &touchBegin); @@ -11165,39 +11161,36 @@ void tst_QGraphicsItem::touchEventTransformation() view.setTransform(viewTransform); view.show(); QVERIFY(QTest::qWaitForWindowExposed(&view)); + QInputDevicePrivate::get(m_touchDevice)->setAvailableVirtualGeometry(view.screen()->geometry()); QCOMPARE(touchEventReceiver->touchBeginEventCount(), 0); - if (m_touchDevice == nullptr) - m_touchDevice = QTest::createTouchDevice(); - QTouchEvent touchBegin(QEvent::TouchBegin, m_touchDevice, Qt::NoModifier, Qt::TouchPointPressed, - createTouchPoints(view, touchScenePos, ellipseDiameters)); + QMutableTouchEvent touchBegin(QEvent::TouchBegin, m_touchDevice, Qt::NoModifier, + createTouchPoints(view, touchScenePos, ellipseDiameters)); touchBegin.setTarget(view.viewport()); - QCoreApplication::sendEvent(&scene, &touchBegin); QCOMPARE(touchEventReceiver->touchBeginEventCount(), 1); - const QTouchEvent::TouchPoint touchBeginPoint = touchEventReceiver->touchBeginPoints().constFirst(); + const QEventPoint touchBeginPoint = touchEventReceiver->touchBeginPoints().constFirst(); - COMPARE_POINTF(touchBeginPoint.scenePos(), touchScenePos); - COMPARE_POINTF(touchBeginPoint.startScenePos(), touchScenePos); - COMPARE_POINTF(touchBeginPoint.lastScenePos(), touchScenePos); - COMPARE_POINTF(touchBeginPoint.pos(), expectedItemPos); + COMPARE_POINTF(touchBeginPoint.scenePosition(), touchScenePos); + COMPARE_POINTF(touchBeginPoint.scenePressPosition(), touchScenePos); + COMPARE_POINTF(touchBeginPoint.sceneLastPosition(), touchScenePos); + COMPARE_POINTF(touchBeginPoint.position(), expectedItemPos); COMPARE_SIZEF(touchBeginPoint.ellipseDiameters(), ellipseDiameters); // Must remain untransformed - QTouchEvent touchUpdate(QEvent::TouchUpdate, m_touchDevice, Qt::NoModifier, Qt::TouchPointMoved, - createTouchPoints(view, touchScenePos, ellipseDiameters, Qt::TouchPointMoved)); + QMutableTouchEvent touchUpdate(QEvent::TouchUpdate, m_touchDevice, Qt::NoModifier, + createTouchPoints(view, touchScenePos, ellipseDiameters, QEventPoint::State::Updated)); touchUpdate.setTarget(view.viewport()); QCoreApplication::sendEvent(&scene, &touchUpdate); QCOMPARE(touchEventReceiver->touchUpdateEventCount(), 1); - const QTouchEvent::TouchPoint touchUpdatePoint = touchEventReceiver->touchUpdatePoints().constFirst(); + const QEventPoint touchUpdatePoint = touchEventReceiver->touchUpdatePoints().constFirst(); - COMPARE_POINTF(touchUpdatePoint.scenePos(), touchScenePos); - COMPARE_POINTF(touchBeginPoint.startScenePos(), touchScenePos); - COMPARE_POINTF(touchUpdatePoint.lastScenePos(), touchScenePos); - COMPARE_POINTF(touchUpdatePoint.pos(), expectedItemPos); + COMPARE_POINTF(touchUpdatePoint.scenePosition(), touchScenePos); + COMPARE_POINTF(touchBeginPoint.scenePressPosition(), touchScenePos); + COMPARE_POINTF(touchUpdatePoint.position(), expectedItemPos); COMPARE_SIZEF(touchUpdatePoint.ellipseDiameters(), ellipseDiameters); // Must remain untransformed } diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp index 8387d6f1dd..3365ccd179 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp @@ -4840,15 +4840,12 @@ void tst_QGraphicsScene::focusOnTouch() scene.setFocusOnTouch(false); - QPointingDevice device; - device.setType(QInputDevice::DeviceType::TouchPad); - QList touchPoints; - QTouchEvent::TouchPoint point; - point.setScenePos(QPointF(10, 10)); - point.setState(Qt::TouchPointPressed); - touchPoints.append(point); - QTouchEvent event(QEvent::TouchBegin, &device, Qt::NoModifier, Qt::TouchPointStates(), - touchPoints); + QPointingDevice device("fake touchpad", 4321, QInputDevice::DeviceType::TouchPad, + QPointingDevice::PointerType::Finger, QInputDevice::Capability::Position, 1, 3); + + auto touchPoints = QList() << + QEventPoint(0, QEventPoint::State::Pressed, {10, 10}, {}); + QTouchEvent event(QEvent::TouchBegin, &device, Qt::NoModifier, touchPoints); QApplication::sendEvent(&scene, &event); -- cgit v1.2.3