summaryrefslogtreecommitdiffstats
path: root/tests/auto/qgraphicsitem
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/qgraphicsitem')
-rw-r--r--tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp167
1 files changed, 89 insertions, 78 deletions
diff --git a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
index 0f2d671..7964c9b 100644
--- a/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
+++ b/tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
@@ -126,6 +126,7 @@ private slots:
void construction();
void constructionWithParent();
void destruction();
+ void deleteChildItem();
void scene();
void parentItem();
void setParentItem();
@@ -513,6 +514,18 @@ void tst_QGraphicsItem::destruction()
QCOMPARE(itemDeleted, 109);
}
+void tst_QGraphicsItem::deleteChildItem()
+{
+ QGraphicsScene scene;
+ QGraphicsItem *rect = scene.addRect(QRectF());
+ QGraphicsItem *child1 = new QGraphicsRectItem(rect);
+ QGraphicsItem *child2 = new QGraphicsRectItem(rect);
+ QGraphicsItem *child3 = new QGraphicsRectItem(rect);
+ delete child1;
+ child2->setParentItem(0);
+ delete child2;
+}
+
void tst_QGraphicsItem::scene()
{
QGraphicsRectItem *item = new QGraphicsRectItem;
@@ -3013,150 +3026,134 @@ protected:
void mouseReleaseEvent(QGraphicsSceneMouseEvent *)
{ ++counter; }
};
+
void tst_QGraphicsItem::handlesChildEvents()
{
- ChildEventTester *item2_level2 = new ChildEventTester(QRectF(0, 0, 25, 25));
- ChildEventTester *item1_level1 = new ChildEventTester(QRectF(0, 0, 50, 50));
- ChildEventTester *item_level0 = new ChildEventTester(QRectF(0, 0, 100, 100));
- ChildEventTester *item1_level2 = new ChildEventTester(QRectF(0, 0, 25, 25));
- ChildEventTester *item2_level1 = new ChildEventTester(QRectF(0, 0, 50, 50));
-
- item_level0->setBrush(QBrush(Qt::blue));
- item1_level1->setBrush(QBrush(Qt::red));
- item2_level1->setBrush(QBrush(Qt::yellow));
- item1_level2->setBrush(QBrush(Qt::green));
- item2_level2->setBrush(QBrush(Qt::gray));
- item1_level1->setPos(50, 0);
- item2_level1->setPos(50, 50);
- item1_level2->setPos(25, 0);
- item2_level2->setPos(25, 25);
- item1_level1->setParentItem(item_level0);
- item2_level1->setParentItem(item_level0);
- item1_level2->setParentItem(item1_level1);
- item2_level2->setParentItem(item1_level1);
+ ChildEventTester *blue = new ChildEventTester(QRectF(0, 0, 100, 100));
+ ChildEventTester *red = new ChildEventTester(QRectF(0, 0, 50, 50));
+ ChildEventTester *green = new ChildEventTester(QRectF(0, 0, 25, 25));
+ ChildEventTester *gray = new ChildEventTester(QRectF(0, 0, 25, 25));
+ ChildEventTester *yellow = new ChildEventTester(QRectF(0, 0, 50, 50));
+
+ blue->setBrush(QBrush(Qt::blue));
+ red->setBrush(QBrush(Qt::red));
+ yellow->setBrush(QBrush(Qt::yellow));
+ green->setBrush(QBrush(Qt::green));
+ gray->setBrush(QBrush(Qt::gray));
+ red->setPos(50, 0);
+ yellow->setPos(50, 50);
+ green->setPos(25, 0);
+ gray->setPos(25, 25);
+ red->setParentItem(blue);
+ yellow->setParentItem(blue);
+ green->setParentItem(red);
+ gray->setParentItem(red);
QGraphicsScene scene;
- scene.addItem(item_level0);
-
- // Pull out the items, closest item first
- QList<QGraphicsItem *> items = scene.items(scene.itemsBoundingRect());
- QCOMPARE(items.at(4), (QGraphicsItem *)item_level0);
- if (item1_level1 < item2_level1) {
- QCOMPARE(items.at(3), (QGraphicsItem *)item1_level1);
- if (item1_level2 < item2_level2) {
- QCOMPARE(items.at(2), (QGraphicsItem *)item1_level2);
- QCOMPARE(items.at(1), (QGraphicsItem *)item2_level2);
- } else {
- QCOMPARE(items.at(2), (QGraphicsItem *)item2_level2);
- QCOMPARE(items.at(1), (QGraphicsItem *)item1_level2);
- }
- QCOMPARE(items.at(0), (QGraphicsItem *)item2_level1);
- } else {
- QCOMPARE(items.at(3), (QGraphicsItem *)item2_level1);
- QCOMPARE(items.at(2), (QGraphicsItem *)item1_level1);
- if (item1_level2 < item2_level2) {
- QCOMPARE(items.at(1), (QGraphicsItem *)item1_level2);
- QCOMPARE(items.at(0), (QGraphicsItem *)item2_level2);
- } else {
- QCOMPARE(items.at(1), (QGraphicsItem *)item2_level2);
- QCOMPARE(items.at(0), (QGraphicsItem *)item1_level2);
- }
- }
+ scene.addItem(blue);
QGraphicsView view(&scene);
view.show();
QTest::qWait(1000);
- QCOMPARE(item_level0->counter, 0);
+ // Pull out the items, closest item first
+ QList<QGraphicsItem *> items = scene.items(scene.itemsBoundingRect());
+ QCOMPARE(items.at(0), (QGraphicsItem *)yellow);
+ QCOMPARE(items.at(1), (QGraphicsItem *)gray);
+ QCOMPARE(items.at(2), (QGraphicsItem *)green);
+ QCOMPARE(items.at(3), (QGraphicsItem *)red);
+ QCOMPARE(items.at(4), (QGraphicsItem *)blue);
+
+ QCOMPARE(blue->counter, 0);
// Send events to the toplevel item
QGraphicsSceneMouseEvent pressEvent(QEvent::GraphicsSceneMousePress);
QGraphicsSceneMouseEvent releaseEvent(QEvent::GraphicsSceneMouseRelease);
pressEvent.setButton(Qt::LeftButton);
- pressEvent.setScenePos(item_level0->mapToScene(5, 5));
+ pressEvent.setScenePos(blue->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
releaseEvent.setButton(Qt::LeftButton);
- releaseEvent.setScenePos(item_level0->mapToScene(5, 5));
+ releaseEvent.setScenePos(blue->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 2);
+ QCOMPARE(blue->counter, 2);
// Send events to a level1 item
- pressEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ pressEvent.setScenePos(red->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ releaseEvent.setScenePos(red->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 2);
- QCOMPARE(item1_level1->counter, 2);
+ QCOMPARE(blue->counter, 2);
+ QCOMPARE(red->counter, 2);
// Send events to a level2 item
- pressEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ pressEvent.setScenePos(green->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ releaseEvent.setScenePos(green->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 2);
- QCOMPARE(item1_level1->counter, 2);
- QCOMPARE(item1_level2->counter, 2);
+ QCOMPARE(blue->counter, 2);
+ QCOMPARE(red->counter, 2);
+ QCOMPARE(green->counter, 2);
- item_level0->setHandlesChildEvents(true);
+ blue->setHandlesChildEvents(true);
// Send events to a level1 item
- pressEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ pressEvent.setScenePos(red->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ releaseEvent.setScenePos(red->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 4);
- QCOMPARE(item1_level1->counter, 2);
+ QCOMPARE(blue->counter, 4);
+ QCOMPARE(red->counter, 2);
// Send events to a level2 item
- pressEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ pressEvent.setScenePos(green->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ releaseEvent.setScenePos(green->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 6);
- QCOMPARE(item1_level1->counter, 2);
- QCOMPARE(item1_level2->counter, 2);
+ QCOMPARE(blue->counter, 6);
+ QCOMPARE(red->counter, 2);
+ QCOMPARE(green->counter, 2);
- item_level0->setHandlesChildEvents(false);
+ blue->setHandlesChildEvents(false);
// Send events to a level1 item
- pressEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ pressEvent.setScenePos(red->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level1->mapToScene(5, 5));
+ releaseEvent.setScenePos(red->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 6);
- QCOMPARE(item1_level1->counter, 4);
+ QCOMPARE(blue->counter, 6);
+ QCOMPARE(red->counter, 4);
// Send events to a level2 item
- pressEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ pressEvent.setScenePos(green->mapToScene(5, 5));
pressEvent.setScreenPos(view.mapFromScene(pressEvent.scenePos()));
- releaseEvent.setScenePos(item1_level2->mapToScene(5, 5));
+ releaseEvent.setScenePos(green->mapToScene(5, 5));
releaseEvent.setScreenPos(view.mapFromScene(releaseEvent.scenePos()));
QApplication::sendEvent(&scene, &pressEvent);
QApplication::sendEvent(&scene, &releaseEvent);
- QCOMPARE(item_level0->counter, 6);
- QCOMPARE(item1_level1->counter, 4);
- QCOMPARE(item1_level2->counter, 4);
+ QCOMPARE(blue->counter, 6);
+ QCOMPARE(red->counter, 4);
+ QCOMPARE(green->counter, 4);
}
void tst_QGraphicsItem::handlesChildEvents2()
@@ -3750,6 +3747,20 @@ void tst_QGraphicsItem::itemChange()
QCOMPARE(tester.pos(), QPointF(42, 0));
}
{
+ // ItemZValueChange / ItemZValueHasChanged
+ tester.itemChangeReturnValue = qreal(2.0);
+ tester.setZValue(1.0);
+ ++changeCount; // notification sent too
+ ++changeCount;
+ QCOMPARE(tester.changes.size(), changeCount);
+ QCOMPARE(tester.changes.at(tester.changes.size() - 2), QGraphicsItem::ItemZValueChange);
+ QCOMPARE(tester.changes.at(tester.changes.size() - 1), QGraphicsItem::ItemZValueHasChanged);
+ QCOMPARE(tester.values.at(tester.changes.size() - 2), QVariant(qreal(1.0)));
+ QCOMPARE(tester.values.at(tester.changes.size() - 1), QVariant(qreal(2.0)));
+ QCOMPARE(tester.oldValues.last(), QVariant(qreal(0.0)));
+ QCOMPARE(tester.zValue(), qreal(2.0));
+ }
+ {
// ItemFlagsChange
tester.itemChangeReturnValue = QGraphicsItem::ItemIsSelectable;
tester.setFlag(QGraphicsItem::ItemIsSelectable, false);