From 9911550fa7fa58cfef66a33ccd8b1d0c531f848e Mon Sep 17 00:00:00 2001 From: aavit Date: Mon, 20 Oct 2014 14:09:58 +0200 Subject: Fix rubberband selection in rotated GraphicsView MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Selection rectangle was incorrectly mapped to scene space when the view was rotated. It became a rotated rectangle instead of the correct polygon (rhombus). Task-number: QTBUG-42008 Change-Id: Ib7b366bec7e1f83109e03c434268ad6897138f30 Reviewed-by: Thorbjørn Lund Martsum --- src/widgets/graphicsview/qgraphicsview.cpp | 2 +- .../qgraphicsview/tst_qgraphicsview.cpp | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/widgets/graphicsview/qgraphicsview.cpp b/src/widgets/graphicsview/qgraphicsview.cpp index 88d5e52204..5484ecb96e 100644 --- a/src/widgets/graphicsview/qgraphicsview.cpp +++ b/src/widgets/graphicsview/qgraphicsview.cpp @@ -765,7 +765,7 @@ void QGraphicsViewPrivate::updateRubberBand(const QMouseEvent *event) } // Set the new selection area QPainterPath selectionArea; - selectionArea.addPolygon(mapToScene(rubberBandRect)); + selectionArea.addPolygon(q->mapToScene(rubberBandRect)); selectionArea.closeSubpath(); if (scene) scene->setSelectionArea(selectionArea, rubberBandSelectionMode, q->viewportTransform()); diff --git a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp index d38bb86487..afd8be71e8 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsview/tst_qgraphicsview.cpp @@ -160,6 +160,7 @@ private slots: void dragMode_scrollHand(); void dragMode_rubberBand(); void rubberBandSelectionMode(); + void rotated_rubberBand(); void backgroundBrush(); void foregroundBrush(); void matrix(); @@ -935,6 +936,48 @@ void tst_QGraphicsView::rubberBandSelectionMode() QCOMPARE(scene.selectedItems(), QList() << rect); } +void tst_QGraphicsView::rotated_rubberBand() +{ + QWidget toplevel; + setFrameless(&toplevel); + + QGraphicsScene scene; + const int dim = 3; + for (int i = 0; i < dim; i++) { + for (int j = 0; j < dim; j ++) { + QGraphicsRectItem *rect = new QGraphicsRectItem(i * 20, j * 20, 10, 10); + rect->setFlag(QGraphicsItem::ItemIsSelectable); + rect->setData(0, (i == j)); + scene.addItem(rect); + } + } + + QGraphicsView view(&scene, &toplevel); + QCOMPARE(view.rubberBandSelectionMode(), Qt::IntersectsItemShape); + view.setDragMode(QGraphicsView::RubberBandDrag); + view.resize(120, 120); + view.rotate(45); + toplevel.show(); + QVERIFY(QTest::qWaitForWindowExposed(&toplevel)); + + // Disable mouse tracking to prevent the window system from sending mouse + // move events to the viewport while we are synthesizing events. If + // QGraphicsView gets a mouse move event with no buttons down, it'll + // terminate the rubber band. + view.viewport()->setMouseTracking(false); + + QCOMPARE(scene.selectedItems(), QList()); + int midWidth = view.viewport()->width() / 2; + sendMousePress(view.viewport(), QPoint(midWidth - 2, 0), Qt::LeftButton); + sendMouseMove(view.viewport(), QPoint(midWidth + 2, view.viewport()->height()), + Qt::LeftButton, Qt::LeftButton); + QCOMPARE(scene.selectedItems().count(), dim); + foreach (const QGraphicsItem *item, scene.items()) { + QCOMPARE(item->isSelected(), item->data(0).toBool()); + } + sendMouseRelease(view.viewport(), QPoint(), Qt::LeftButton); +} + void tst_QGraphicsView::backgroundBrush() { QGraphicsScene scene; -- cgit v1.2.3