Fix double transform for items ignoring parent transformations.

Previously, the topmost untransformable's scene transform, which
includes the item's position and local transformation, was used to
determine the item's anchoring position. This position was then
passed on to be multiplied by the item's transform again. This
works fine for toplevel untransformable items that don't have any
transform set at all, but those who do would have their transforms
applied twice - one to determine the anchoring position, and again
to transform the item itself. Since only translation transformations
can affect the first operation (the anchoring pos), this bug only
applies to items that set ItemIgnoresTransformations and use a
local transform that includes translation.

Task-number: QTBUG-21618
Change-Id: I772d52d59dfd9f242d0140632a87e9c68dfe0ea1
Reviewed-by: Jan Arve Sæther <jan-arve.saether@digia.com>
This commit is contained in:
Andreas Aardal Hanssen 2013-07-30 18:08:10 +02:00 committed by The Qt Project
parent 12571cc095
commit 8fce4e97ba
2 changed files with 69 additions and 3 deletions

View File

@ -4199,9 +4199,14 @@ QTransform QGraphicsItem::deviceTransform(const QTransform &viewportTransform) c
return QTransform();
}
// First translate the base untransformable item.
untransformedAncestor->d_ptr->ensureSceneTransform();
QPointF mappedPoint = (untransformedAncestor->d_ptr->sceneTransform * viewportTransform).map(QPointF(0, 0));
// Determine the inherited origin. Find the parent of the topmost untransformable.
// Use its scene transform to map the position of the untransformable. Then use
// that viewport position as the anchoring point for the untransformable subtree.
QGraphicsItem *parentOfUntransformedAncestor = untransformedAncestor->parentItem();
QTransform inheritedMatrix;
if (parentOfUntransformedAncestor)
inheritedMatrix = parentOfUntransformedAncestor->sceneTransform();
QPointF mappedPoint = (inheritedMatrix * viewportTransform).map(untransformedAncestor->pos());
// COMBINE
QTransform matrix = QTransform::fromTranslate(mappedPoint.x(), mappedPoint.y());

View File

@ -471,6 +471,7 @@ private slots:
void QTBUG_16374_crashInDestructor();
void QTBUG_20699_focusScopeCrash();
void QTBUG_30990_rightClickSelection();
void QTBUG_21618_untransformable_sceneTransform();
private:
QList<QGraphicsItem *> paintedItems;
@ -11496,5 +11497,65 @@ void tst_QGraphicsItem::QTBUG_30990_rightClickSelection()
QVERIFY(!item2->isSelected());
}
void tst_QGraphicsItem::QTBUG_21618_untransformable_sceneTransform()
{
QGraphicsScene scene(0, 0, 150, 150);
scene.addRect(-2, -2, 4, 4);
QGraphicsItem *item1 = scene.addRect(0, 0, 100, 100, QPen(), Qt::red);
item1->setPos(50, 50);
item1->translate(50, 50);
item1->rotate(90);
QGraphicsItem *item2 = scene.addRect(0, 0, 100, 100, QPen(), Qt::green);
item2->setPos(50, 50);
item2->translate(50, 50);
item2->rotate(90);
item2->setFlags(QGraphicsItem::ItemIgnoresTransformations);
QGraphicsRectItem *item1_topleft = new QGraphicsRectItem(QRectF(-2, -2, 4, 4));
item1_topleft->setParentItem(item1);
item1_topleft->setBrush(Qt::black);
QGraphicsRectItem *item1_bottomright = new QGraphicsRectItem(QRectF(-2, -2, 4, 4));
item1_bottomright->setParentItem(item1);
item1_bottomright->setPos(100, 100);
item1_bottomright->setBrush(Qt::yellow);
QGraphicsRectItem *item2_topleft = new QGraphicsRectItem(QRectF(-2, -2, 4, 4));
item2_topleft->setParentItem(item2);
item2_topleft->setBrush(Qt::black);
QGraphicsRectItem *item2_bottomright = new QGraphicsRectItem(QRectF(-2, -2, 4, 4));
item2_bottomright->setParentItem(item2);
item2_bottomright->setPos(100, 100);
item2_bottomright->setBrush(Qt::yellow);
QCOMPARE(item1->sceneTransform(), item2->sceneTransform());
QCOMPARE(item1_topleft->sceneTransform(), item2_topleft->sceneTransform());
QCOMPARE(item1_bottomright->sceneTransform(), item2_bottomright->sceneTransform());
QCOMPARE(item1->deviceTransform(QTransform()), item2->deviceTransform(QTransform()));
QCOMPARE(item1->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 100));
QCOMPARE(item2->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 100));
QCOMPARE(item1->deviceTransform(QTransform()).map(QPointF(100, 100)), QPointF(0, 200));
QCOMPARE(item2->deviceTransform(QTransform()).map(QPointF(100, 100)), QPointF(0, 200));
QCOMPARE(item1_topleft->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 100));
QCOMPARE(item2_topleft->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 100));
QCOMPARE(item1_bottomright->deviceTransform(QTransform()).map(QPointF()), QPointF(0, 200));
QCOMPARE(item2_bottomright->deviceTransform(QTransform()).map(QPointF()), QPointF(0, 200));
item2->setParentItem(item1);
QCOMPARE(item2->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 200));
QCOMPARE(item2->deviceTransform(QTransform()).map(QPointF(100, 100)), QPointF(0, 300));
QCOMPARE(item2_topleft->deviceTransform(QTransform()).map(QPointF()), QPointF(100, 200));
QCOMPARE(item2_bottomright->deviceTransform(QTransform()).map(QPointF()), QPointF(0, 300));
QTransform tx = QTransform::fromTranslate(100, 0);
QCOMPARE(item1->deviceTransform(tx).map(QPointF()), QPointF(200, 100));
QCOMPARE(item1->deviceTransform(tx).map(QPointF(100, 100)), QPointF(100, 200));
QCOMPARE(item2->deviceTransform(tx).map(QPointF()), QPointF(200, 200));
QCOMPARE(item2->deviceTransform(tx).map(QPointF(100, 100)), QPointF(100, 300));
QCOMPARE(item2_topleft->deviceTransform(tx).map(QPointF()), QPointF(200, 200));
QCOMPARE(item2_bottomright->deviceTransform(tx).map(QPointF()), QPointF(100, 300));
}
QTEST_MAIN(tst_QGraphicsItem)
#include "tst_qgraphicsitem.moc"