tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 22 79de32ba3296
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
   382     void nestedClippingTransforms();
   382     void nestedClippingTransforms();
   383     void sceneTransformCache();
   383     void sceneTransformCache();
   384     void tabChangesFocus();
   384     void tabChangesFocus();
   385     void tabChangesFocus_data();
   385     void tabChangesFocus_data();
   386     void cacheMode();
   386     void cacheMode();
       
   387     void cacheMode2();
   387     void updateCachedItemAfterMove();
   388     void updateCachedItemAfterMove();
   388     void deviceTransform_data();
   389     void deviceTransform_data();
   389     void deviceTransform();
   390     void deviceTransform();
   390     void update();
   391     void update();
   391     void setTransformProperties_data();
   392     void setTransformProperties_data();
   436     void QTBUG_4233_updateCachedWithSceneRect();
   437     void QTBUG_4233_updateCachedWithSceneRect();
   437     void QTBUG_5418_textItemSetDefaultColor();
   438     void QTBUG_5418_textItemSetDefaultColor();
   438     void QTBUG_6738_missingUpdateWithSetParent();
   439     void QTBUG_6738_missingUpdateWithSetParent();
   439     void QTBUG_7714_fullUpdateDiscardingOpacityUpdate2();
   440     void QTBUG_7714_fullUpdateDiscardingOpacityUpdate2();
   440     void QT_2653_fullUpdateDiscardingOpacityUpdate();
   441     void QT_2653_fullUpdateDiscardingOpacityUpdate();
       
   442     void QT_2649_focusScope();
       
   443     void sortItemsWhileAdding();
   441 
   444 
   442 private:
   445 private:
   443     QList<QGraphicsItem *> paintedItems;
   446     QList<QGraphicsItem *> paintedItems;
   444 };
   447 };
   445 
   448 
  6820     testerChild2->rotate(22);
  6823     testerChild2->rotate(22);
  6821     QTest::qWait(25);
  6824     QTest::qWait(25);
  6822     QTRY_COMPARE(tester->repaints, 4);
  6825     QTRY_COMPARE(tester->repaints, 4);
  6823     QCOMPARE(testerChild->repaints, 4);
  6826     QCOMPARE(testerChild->repaints, 4);
  6824     QCOMPARE(testerChild2->repaints, 3);
  6827     QCOMPARE(testerChild2->repaints, 3);
       
  6828     tester->resetTransform();
       
  6829     testerChild->resetTransform();
       
  6830     testerChild2->resetTransform();
  6825 
  6831 
  6826     // Explicit update causes a repaint.
  6832     // Explicit update causes a repaint.
  6827     tester->update(0, 0, 5, 5);
  6833     tester->update(0, 0, 5, 5);
  6828     QTest::qWait(25);
  6834     QTest::qWait(25);
  6829     QTRY_COMPARE(tester->repaints, 5);
  6835     QTRY_COMPARE(tester->repaints, 5);
  6893 
  6899 
  6894     // Moving the middle item should case a repaint even if it's a move,
  6900     // Moving the middle item should case a repaint even if it's a move,
  6895     // because the parent is rotated with a perspective.
  6901     // because the parent is rotated with a perspective.
  6896     testerChild->setPos(1, 1);
  6902     testerChild->setPos(1, 1);
  6897     QTest::qWait(25);
  6903     QTest::qWait(25);
  6898     QTRY_COMPARE(tester->repaints, 10);
  6904     QTRY_COMPARE(tester->repaints, 11);
  6899     QCOMPARE(testerChild->repaints, 10);
  6905     QCOMPARE(testerChild->repaints, 10);
  6900     QCOMPARE(testerChild2->repaints, 5);
  6906     QCOMPARE(testerChild2->repaints, 5);
       
  6907     tester->resetTransform();
  6901 
  6908 
  6902     // Make a huge item
  6909     // Make a huge item
  6903     tester->setGeometry(QRectF(-4000, -4000, 8000, 8000));
  6910     tester->setGeometry(QRectF(-4000, -4000, 8000, 8000));
  6904     QTest::qWait(25);
  6911     QTest::qWait(25);
  6905     QTRY_COMPARE(tester->repaints, 11);
  6912     QTRY_COMPARE(tester->repaints, 12);
  6906     QCOMPARE(testerChild->repaints, 10);
  6913     QCOMPARE(testerChild->repaints, 11);
  6907     QCOMPARE(testerChild2->repaints, 5);
  6914     QCOMPARE(testerChild2->repaints, 5);
  6908 
  6915 
  6909     // Move the large item - will cause a repaint as the
  6916     // Move the large item - will cause a repaint as the
  6910     // cache is clipped.
  6917     // cache is clipped.
  6911     tester->setPos(5, 0);
  6918     tester->setPos(5, 0);
  6912     QTest::qWait(25);
  6919     QTest::qWait(25);
  6913     QTRY_COMPARE(tester->repaints, 12);
  6920     QTRY_COMPARE(tester->repaints, 13);
  6914     QCOMPARE(testerChild->repaints, 10);
  6921     QCOMPARE(testerChild->repaints, 11);
  6915     QCOMPARE(testerChild2->repaints, 5);
  6922     QCOMPARE(testerChild2->repaints, 5);
  6916 
  6923 
  6917     // Hiding and showing should invalidate the cache
  6924     // Hiding and showing should invalidate the cache
  6918     tester->hide();
  6925     tester->hide();
  6919     QTest::qWait(25);
  6926     QTest::qWait(25);
  6920     tester->show();
  6927     tester->show();
  6921     QTest::qWait(25);
  6928     QTest::qWait(25);
  6922     QTRY_COMPARE(tester->repaints, 13);
  6929     QTRY_COMPARE(tester->repaints, 14);
  6923     QCOMPARE(testerChild->repaints, 11);
  6930     QCOMPARE(testerChild->repaints, 12);
  6924     QCOMPARE(testerChild2->repaints, 6);
  6931     QCOMPARE(testerChild2->repaints, 6);
       
  6932 }
       
  6933 
       
  6934 void tst_QGraphicsItem::cacheMode2()
       
  6935 {
       
  6936     QGraphicsScene scene(0, 0, 100, 100);
       
  6937     QGraphicsView view(&scene);
       
  6938     view.resize(150, 150);
       
  6939     view.show();
       
  6940     QApplication::setActiveWindow(&view);
       
  6941     QTest::qWaitForWindowShown(&view);
       
  6942 
       
  6943     // Increase the probability of window activation
       
  6944     // not causing another repaint of test items.
       
  6945     QTest::qWait(50);
       
  6946 
       
  6947     EventTester *tester = new EventTester;
       
  6948     scene.addItem(tester);
       
  6949     QTest::qWait(10);
       
  6950     QTRY_COMPARE(tester->repaints, 1);
       
  6951 
       
  6952     // Switching from NoCache to NoCache (no repaint)
       
  6953     tester->setCacheMode(QGraphicsItem::NoCache);
       
  6954     QTest::qWait(50);
       
  6955     QTRY_COMPARE(tester->repaints, 1);
       
  6956 
       
  6957     // Switching from NoCache to DeviceCoordinateCache (no repaint)
       
  6958     tester->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
       
  6959     QTest::qWait(50);
       
  6960     QTRY_COMPARE(tester->repaints, 1);
       
  6961 
       
  6962     // Switching from DeviceCoordinateCache to DeviceCoordinateCache (no repaint)
       
  6963     tester->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
       
  6964     QTest::qWait(50);
       
  6965     QTRY_COMPARE(tester->repaints, 1);
       
  6966 
       
  6967     // Switching from DeviceCoordinateCache to NoCache (no repaint)
       
  6968     tester->setCacheMode(QGraphicsItem::NoCache);
       
  6969     QTest::qWait(50);
       
  6970     QTRY_COMPARE(tester->repaints, 1);
       
  6971 
       
  6972     // Switching from NoCache to ItemCoordinateCache (repaint)
       
  6973     tester->setCacheMode(QGraphicsItem::ItemCoordinateCache);
       
  6974     QTest::qWait(50);
       
  6975     QTRY_COMPARE(tester->repaints, 2);
       
  6976 
       
  6977     // Switching from ItemCoordinateCache to ItemCoordinateCache (no repaint)
       
  6978     tester->setCacheMode(QGraphicsItem::ItemCoordinateCache);
       
  6979     QTest::qWait(50);
       
  6980     QTRY_COMPARE(tester->repaints, 2);
       
  6981 
       
  6982     // Switching from ItemCoordinateCache to ItemCoordinateCache with different size (repaint)
       
  6983     tester->setCacheMode(QGraphicsItem::ItemCoordinateCache, QSize(100, 100));
       
  6984     QTest::qWait(50);
       
  6985     QTRY_COMPARE(tester->repaints, 3);
       
  6986 
       
  6987     // Switching from ItemCoordinateCache to NoCache (repaint)
       
  6988     tester->setCacheMode(QGraphicsItem::NoCache);
       
  6989     QTest::qWait(50);
       
  6990     QTRY_COMPARE(tester->repaints, 4);
       
  6991 
       
  6992     // Switching from DeviceCoordinateCache to ItemCoordinateCache (repaint)
       
  6993     tester->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
       
  6994     QTest::qWait(50);
       
  6995     QTRY_COMPARE(tester->repaints, 4);
       
  6996     tester->setCacheMode(QGraphicsItem::ItemCoordinateCache);
       
  6997     QTest::qWait(50);
       
  6998     QTRY_COMPARE(tester->repaints, 5);
       
  6999 
       
  7000     // Switching from ItemCoordinateCache to DeviceCoordinateCache (repaint)
       
  7001     tester->setCacheMode(QGraphicsItem::DeviceCoordinateCache);
       
  7002     QTest::qWait(50);
       
  7003     QTRY_COMPARE(tester->repaints, 6);
  6925 }
  7004 }
  6926 
  7005 
  6927 void tst_QGraphicsItem::updateCachedItemAfterMove()
  7006 void tst_QGraphicsItem::updateCachedItemAfterMove()
  6928 {
  7007 {
  6929     // A simple item that uses ItemCoordinateCache
  7008     // A simple item that uses ItemCoordinateCache
 10000 
 10079 
 10001     QTRY_COMPARE(origView.repaints, 1);
 10080     QTRY_COMPARE(origView.repaints, 1);
 10002     QTRY_COMPARE(view.repaints, 1);
 10081     QTRY_COMPARE(view.repaints, 1);
 10003 }
 10082 }
 10004 
 10083 
       
 10084 void tst_QGraphicsItem::QT_2649_focusScope()
       
 10085 {
       
 10086     QGraphicsScene *scene = new QGraphicsScene;
       
 10087 
       
 10088     QGraphicsRectItem *subFocusItem = new QGraphicsRectItem;
       
 10089     subFocusItem->setFlags(QGraphicsItem::ItemIsFocusable);
       
 10090     subFocusItem->setFocus();
       
 10091     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10092 
       
 10093     QGraphicsRectItem *scope = new QGraphicsRectItem;
       
 10094     scope->setFlags(QGraphicsItem::ItemIsFocusable | QGraphicsItem::ItemIsFocusScope);
       
 10095     scope->setFocus();
       
 10096     subFocusItem->setParentItem(scope);
       
 10097     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10098     QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10099     QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10100     QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
       
 10101 
       
 10102     QGraphicsRectItem *rootItem = new QGraphicsRectItem;
       
 10103     rootItem->setFlags(QGraphicsItem::ItemIsFocusable);
       
 10104     scope->setParentItem(rootItem);
       
 10105     QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10106     QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10107     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10108     QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10109     QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10110     QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
       
 10111 
       
 10112     scene->addItem(rootItem);
       
 10113 
       
 10114     QEvent windowActivate(QEvent::WindowActivate);
       
 10115     qApp->sendEvent(scene, &windowActivate);
       
 10116     scene->setFocus();
       
 10117 
       
 10118     QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10119     QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10120     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10121     QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10122     QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
       
 10123     QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10124     QVERIFY(subFocusItem->hasFocus());
       
 10125 
       
 10126     scope->hide();
       
 10127 
       
 10128     QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)0);
       
 10129     QCOMPARE(scope->focusItem(), (QGraphicsItem *)0);
       
 10130     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)0);
       
 10131     QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10132     QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
       
 10133     QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10134     QVERIFY(!subFocusItem->hasFocus());
       
 10135 
       
 10136     scope->show();
       
 10137 
       
 10138     QCOMPARE(rootItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10139     QCOMPARE(scope->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10140     QCOMPARE(subFocusItem->focusItem(), (QGraphicsItem *)subFocusItem);
       
 10141     QCOMPARE(rootItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10142     QCOMPARE(scope->focusScopeItem(), (QGraphicsItem *)subFocusItem);
       
 10143     QCOMPARE(subFocusItem->focusScopeItem(), (QGraphicsItem *)0);
       
 10144     QVERIFY(subFocusItem->hasFocus());
       
 10145 
       
 10146     // This should not crash
       
 10147     scope->hide();
       
 10148     delete scene;
       
 10149 }
       
 10150 
       
 10151 class MyGraphicsItemWithItemChange : public QGraphicsWidget
       
 10152 {
       
 10153 public:
       
 10154     MyGraphicsItemWithItemChange(QGraphicsItem *parent = 0) : QGraphicsWidget(parent)
       
 10155     {}
       
 10156 
       
 10157     QVariant itemChange(GraphicsItemChange change, const QVariant &value)
       
 10158     {
       
 10159         if (change == QGraphicsItem::ItemSceneHasChanged) {
       
 10160             foreach (QGraphicsView *view, scene()->views()) {
       
 10161                 //We trigger a sort of unindexed items in the BSP
       
 10162                 view->sceneRect();
       
 10163             }
       
 10164         }
       
 10165         return QGraphicsWidget::itemChange(change, value);
       
 10166     }
       
 10167 };
       
 10168 
       
 10169 void tst_QGraphicsItem::sortItemsWhileAdding()
       
 10170 {
       
 10171     QGraphicsScene scene;
       
 10172     QGraphicsView view(&scene);
       
 10173     QGraphicsWidget grandGrandParent;
       
 10174     grandGrandParent.resize(200, 200);
       
 10175     scene.addItem(&grandGrandParent);
       
 10176     QGraphicsWidget grandParent;
       
 10177     grandParent.resize(200, 200);
       
 10178     QGraphicsWidget parent(&grandParent);
       
 10179     parent.resize(200, 200);
       
 10180     MyGraphicsItemWithItemChange item(&parent);
       
 10181     grandParent.setParentItem(&grandGrandParent);
       
 10182 }
       
 10183 
 10005 QTEST_MAIN(tst_QGraphicsItem)
 10184 QTEST_MAIN(tst_QGraphicsItem)
 10006 #include "tst_qgraphicsitem.moc"
 10185 #include "tst_qgraphicsitem.moc"