From d72701a220e9b081085b50a77ca23961e096b42b Mon Sep 17 00:00:00 2001 From: Sol Williams <7689429+solwllms@users.noreply.github.com> Date: Tue, 20 Aug 2024 11:18:39 +0100 Subject: [PATCH] Unify GraphicsView point/pixel-based intersection tests, incl. for transformed views --- .../widgets/graphicsview/qgraphicsscene.cpp | 13 +++++------ .../widgets/graphicsview/qgraphicsview.cpp | 22 +++++-------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/valve/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp b/valve/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp index a428329a45..f92b43f285 100644 --- a/valve/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp +++ b/valve/qtbase/src/widgets/graphicsview/qgraphicsscene.cpp @@ -1130,16 +1130,15 @@ QList QGraphicsScenePrivate::itemsAtPosition(const QPoint &scre if (!view) return q->items(scenePos, Qt::IntersectsItemShape, Qt::DescendingOrder, QTransform()); - const QRectF pointRect(QPointF(widget->mapFromGlobal(screenPos)), QSizeF(1, 1)); + // Sol: we're testing a point so use the pixel intersection test in all cases (prev. would do rect tests?) + if (!view->isTransformed()) - return q->items(pointRect, Qt::IntersectsItemShape, Qt::DescendingOrder); + return q->items(widget->mapFromGlobal(screenPos), Qt::IntersectsItemShape, + Qt::DescendingOrder); const QTransform viewTransform = view->viewportTransform(); - if (viewTransform.type() <= QTransform::TxScale) { - return q->items(viewTransform.inverted().mapRect(pointRect), Qt::IntersectsItemShape, - Qt::DescendingOrder, viewTransform); - } - return q->items(viewTransform.inverted().map(pointRect), Qt::IntersectsItemShape, + return q->items(viewTransform.inverted().map(widget->mapFromGlobal(screenPos)), + Qt::IntersectsItemShape, Qt::DescendingOrder, viewTransform); } diff --git a/valve/qtbase/src/widgets/graphicsview/qgraphicsview.cpp b/valve/qtbase/src/widgets/graphicsview/qgraphicsview.cpp index 90bddf6e84..0237490a66 100644 --- a/valve/qtbase/src/widgets/graphicsview/qgraphicsview.cpp +++ b/valve/qtbase/src/widgets/graphicsview/qgraphicsview.cpp @@ -2306,22 +2306,12 @@ QList QGraphicsView::items(const QPoint &pos) const Q_D(const QGraphicsView); if (!d->scene) return QList(); - // ### Unify these two, and use the items(QPointF) version in - // QGraphicsScene instead. The scene items function could use the viewport - // transform to map the point to a rect/polygon. - if ((d->identityMatrix || d->matrix.type() <= QTransform::TxScale)) { - // Use the rect version - QTransform xinv = viewportTransform().inverted(); - return d->scene->items(xinv.mapRect(QRectF(pos.x(), pos.y(), 1, 1)), - Qt::IntersectsItemShape, - Qt::DescendingOrder, - viewportTransform()); - } - // Use the polygon version - return d->scene->items(mapToScene(pos.x(), pos.y(), 1, 1), - Qt::IntersectsItemShape, - Qt::DescendingOrder, - viewportTransform()); + + QTransform xinv = viewportTransform().inverted(); + return d->scene->items(xinv.map(pos), + Qt::IntersectsItemShape, + Qt::DescendingOrder, + viewportTransform()); } /*!