tests/auto/qgraphicsitem/tst_qgraphicsitem.cpp
branchRCL_3
changeset 7 3f74d0d4af4c
parent 5 d3bac044e0f0
child 13 c0432d11811c
equal deleted inserted replaced
6:dee5afe5301f 7:3f74d0d4af4c
    88 #define COMPARE_REGIONS(ACTUAL, EXPECTED) QVERIFY((EXPECTED).subtracted(ACTUAL).isEmpty())
    88 #define COMPARE_REGIONS(ACTUAL, EXPECTED) QVERIFY((EXPECTED).subtracted(ACTUAL).isEmpty())
    89 #else
    89 #else
    90 #define COMPARE_REGIONS QTRY_COMPARE
    90 #define COMPARE_REGIONS QTRY_COMPARE
    91 #endif
    91 #endif
    92 
    92 
       
    93 static QGraphicsRectItem staticItem; //QTBUG-7629, we should not crash at exit.
       
    94 
    93 static void sendMousePress(QGraphicsScene *scene, const QPointF &point, Qt::MouseButton button = Qt::LeftButton)
    95 static void sendMousePress(QGraphicsScene *scene, const QPointF &point, Qt::MouseButton button = Qt::LeftButton)
    94 {
    96 {
    95     QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
    97     QGraphicsSceneMouseEvent event(QEvent::GraphicsSceneMousePress);
    96     event.setScenePos(point);
    98     event.setScenePos(point);
    97     event.setButton(button);
    99     event.setButton(button);
   247     QList<QEvent::Type> events;
   249     QList<QEvent::Type> events;
   248     QPainter::RenderHints hints;
   250     QPainter::RenderHints hints;
   249     int repaints;
   251     int repaints;
   250     QRectF br;
   252     QRectF br;
   251     QBrush brush;
   253     QBrush brush;
       
   254 };
       
   255 
       
   256 class MyGraphicsView : public QGraphicsView
       
   257 {
       
   258 public:
       
   259     int repaints;
       
   260     QRegion paintedRegion;
       
   261     MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {}
       
   262     void paintEvent(QPaintEvent *e)
       
   263     {
       
   264         paintedRegion += e->region();
       
   265         ++repaints;
       
   266         QGraphicsView::paintEvent(e);
       
   267     }
       
   268     void reset() { repaints = 0; paintedRegion = QRegion(); }
   252 };
   269 };
   253 
   270 
   254 class tst_QGraphicsItem : public QObject
   271 class tst_QGraphicsItem : public QObject
   255 {
   272 {
   256     Q_OBJECT
   273     Q_OBJECT
   365     void nestedClippingTransforms();
   382     void nestedClippingTransforms();
   366     void sceneTransformCache();
   383     void sceneTransformCache();
   367     void tabChangesFocus();
   384     void tabChangesFocus();
   368     void tabChangesFocus_data();
   385     void tabChangesFocus_data();
   369     void cacheMode();
   386     void cacheMode();
       
   387     void cacheMode2();
   370     void updateCachedItemAfterMove();
   388     void updateCachedItemAfterMove();
   371     void deviceTransform_data();
   389     void deviceTransform_data();
   372     void deviceTransform();
   390     void deviceTransform();
   373     void update();
   391     void update();
   374     void setTransformProperties_data();
   392     void setTransformProperties_data();
   417     void task243707_addChildBeforeParent();
   435     void task243707_addChildBeforeParent();
   418     void task197802_childrenVisibility();
   436     void task197802_childrenVisibility();
   419     void QTBUG_4233_updateCachedWithSceneRect();
   437     void QTBUG_4233_updateCachedWithSceneRect();
   420     void QTBUG_5418_textItemSetDefaultColor();
   438     void QTBUG_5418_textItemSetDefaultColor();
   421     void QTBUG_6738_missingUpdateWithSetParent();
   439     void QTBUG_6738_missingUpdateWithSetParent();
       
   440     void QTBUG_7714_fullUpdateDiscardingOpacityUpdate2();
       
   441     void QT_2653_fullUpdateDiscardingOpacityUpdate();
       
   442     void QT_2649_focusScope();
       
   443     void sortItemsWhileAdding();
   422 
   444 
   423 private:
   445 private:
   424     QList<QGraphicsItem *> paintedItems;
   446     QList<QGraphicsItem *> paintedItems;
   425 };
   447 };
   426 
   448 
  3162 
  3184 
  3163     QGraphicsRectItem *childChild = scene.addRect(QRectF(0, 0, 100, 100), QPen(Qt::black, 0));
  3185     QGraphicsRectItem *childChild = scene.addRect(QRectF(0, 0, 100, 100), QPen(Qt::black, 0));
  3164     childChild->setParentItem(child);
  3186     childChild->setParentItem(child);
  3165     childChild->setPos(500, 500);
  3187     childChild->setPos(500, 500);
  3166     child->rotate(90);
  3188     child->rotate(90);
  3167 
       
  3168 
  3189 
  3169     scene.addPolygon(parent->mapToScene(parent->boundingRect() | parent->childrenBoundingRect()))->setPen(QPen(Qt::red));;
  3190     scene.addPolygon(parent->mapToScene(parent->boundingRect() | parent->childrenBoundingRect()))->setPen(QPen(Qt::red));;
  3170 
  3191 
  3171     QGraphicsView view(&scene);
  3192     QGraphicsView view(&scene);
  3172     view.show();
  3193     view.show();
  6250     EventTester *grandChild = new EventTester(child);
  6271     EventTester *grandChild = new EventTester(child);
  6251 
  6272 
  6252     QGraphicsScene scene;
  6273     QGraphicsScene scene;
  6253     scene.addItem(parent);
  6274     scene.addItem(parent);
  6254 
  6275 
  6255     class MyGraphicsView : public QGraphicsView
       
  6256     { public:
       
  6257         int repaints;
       
  6258         MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {}
       
  6259         void paintEvent(QPaintEvent *e) { ++repaints; QGraphicsView::paintEvent(e); }
       
  6260     };
       
  6261 
       
  6262     MyGraphicsView view(&scene);
  6276     MyGraphicsView view(&scene);
  6263     view.show();
  6277     view.show();
  6264     QTest::qWaitForWindowShown(&view);
  6278     QTest::qWaitForWindowShown(&view);
  6265     QTRY_VERIFY(view.repaints >= 1);
  6279     QTRY_VERIFY(view.repaints >= 1);
  6266 
  6280 
  6333 
  6347 
  6334     child->setPos(10, 10);
  6348     child->setPos(10, 10);
  6335 
  6349 
  6336     QGraphicsScene scene;
  6350     QGraphicsScene scene;
  6337     scene.addItem(parent);
  6351     scene.addItem(parent);
  6338 
       
  6339     class MyGraphicsView : public QGraphicsView
       
  6340     { public:
       
  6341         int repaints;
       
  6342         QRegion paintedRegion;
       
  6343         MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {}
       
  6344         void paintEvent(QPaintEvent *e)
       
  6345         {
       
  6346             ++repaints;
       
  6347             paintedRegion += e->region();
       
  6348             QGraphicsView::paintEvent(e);
       
  6349         }
       
  6350         void reset() { repaints = 0; paintedRegion = QRegion(); }
       
  6351     };
       
  6352 
  6352 
  6353     MyGraphicsView view(&scene);
  6353     MyGraphicsView view(&scene);
  6354     view.show();
  6354     view.show();
  6355     QTest::qWaitForWindowShown(&view);
  6355     QTest::qWaitForWindowShown(&view);
  6356     QTRY_VERIFY(view.repaints > 0);
  6356     QTRY_VERIFY(view.repaints > 0);
  6823     testerChild2->rotate(22);
  6823     testerChild2->rotate(22);
  6824     QTest::qWait(25);
  6824     QTest::qWait(25);
  6825     QTRY_COMPARE(tester->repaints, 4);
  6825     QTRY_COMPARE(tester->repaints, 4);
  6826     QCOMPARE(testerChild->repaints, 4);
  6826     QCOMPARE(testerChild->repaints, 4);
  6827     QCOMPARE(testerChild2->repaints, 3);
  6827     QCOMPARE(testerChild2->repaints, 3);
       
  6828     tester->resetTransform();
       
  6829     testerChild->resetTransform();
       
  6830     testerChild2->resetTransform();
  6828 
  6831 
  6829     // Explicit update causes a repaint.
  6832     // Explicit update causes a repaint.
  6830     tester->update(0, 0, 5, 5);
  6833     tester->update(0, 0, 5, 5);
  6831     QTest::qWait(25);
  6834     QTest::qWait(25);
  6832     QTRY_COMPARE(tester->repaints, 5);
  6835     QTRY_COMPARE(tester->repaints, 5);
  6896 
  6899 
  6897     // 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,
  6898     // because the parent is rotated with a perspective.
  6901     // because the parent is rotated with a perspective.
  6899     testerChild->setPos(1, 1);
  6902     testerChild->setPos(1, 1);
  6900     QTest::qWait(25);
  6903     QTest::qWait(25);
  6901     QTRY_COMPARE(tester->repaints, 10);
  6904     QTRY_COMPARE(tester->repaints, 11);
  6902     QCOMPARE(testerChild->repaints, 10);
  6905     QCOMPARE(testerChild->repaints, 10);
  6903     QCOMPARE(testerChild2->repaints, 5);
  6906     QCOMPARE(testerChild2->repaints, 5);
       
  6907     tester->resetTransform();
  6904 
  6908 
  6905     // Make a huge item
  6909     // Make a huge item
  6906     tester->setGeometry(QRectF(-4000, -4000, 8000, 8000));
  6910     tester->setGeometry(QRectF(-4000, -4000, 8000, 8000));
  6907     QTest::qWait(25);
  6911     QTest::qWait(25);
  6908     QTRY_COMPARE(tester->repaints, 11);
  6912     QTRY_COMPARE(tester->repaints, 12);
  6909     QCOMPARE(testerChild->repaints, 10);
  6913     QCOMPARE(testerChild->repaints, 11);
  6910     QCOMPARE(testerChild2->repaints, 5);
  6914     QCOMPARE(testerChild2->repaints, 5);
  6911 
  6915 
  6912     // Move the large item - will cause a repaint as the
  6916     // Move the large item - will cause a repaint as the
  6913     // cache is clipped.
  6917     // cache is clipped.
  6914     tester->setPos(5, 0);
  6918     tester->setPos(5, 0);
  6915     QTest::qWait(25);
  6919     QTest::qWait(25);
  6916     QTRY_COMPARE(tester->repaints, 12);
  6920     QTRY_COMPARE(tester->repaints, 13);
  6917     QCOMPARE(testerChild->repaints, 10);
  6921     QCOMPARE(testerChild->repaints, 11);
  6918     QCOMPARE(testerChild2->repaints, 5);
  6922     QCOMPARE(testerChild2->repaints, 5);
  6919 
  6923 
  6920     // Hiding and showing should invalidate the cache
  6924     // Hiding and showing should invalidate the cache
  6921     tester->hide();
  6925     tester->hide();
  6922     QTest::qWait(25);
  6926     QTest::qWait(25);
  6923     tester->show();
  6927     tester->show();
  6924     QTest::qWait(25);
  6928     QTest::qWait(25);
  6925     QTRY_COMPARE(tester->repaints, 13);
  6929     QTRY_COMPARE(tester->repaints, 14);
  6926     QCOMPARE(testerChild->repaints, 11);
  6930     QCOMPARE(testerChild->repaints, 12);
  6927     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);
  6928 }
  7004 }
  6929 
  7005 
  6930 void tst_QGraphicsItem::updateCachedItemAfterMove()
  7006 void tst_QGraphicsItem::updateCachedItemAfterMove()
  6931 {
  7007 {
  6932     // A simple item that uses ItemCoordinateCache
  7008     // A simple item that uses ItemCoordinateCache
  7074     QCOMPARE(rect1->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult1);
  7150     QCOMPARE(rect1->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult1);
  7075     QCOMPARE(rect2->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult2);
  7151     QCOMPARE(rect2->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult2);
  7076     QCOMPARE(rect3->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult3);
  7152     QCOMPARE(rect3->deviceTransform(deviceX).map(QPointF(50, 50)), mapResult3);
  7077 }
  7153 }
  7078 
  7154 
  7079 class MyGraphicsView : public QGraphicsView
       
  7080 {
       
  7081 public:
       
  7082     int repaints;
       
  7083     QRegion paintedRegion;
       
  7084     MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {}
       
  7085     void paintEvent(QPaintEvent *e)
       
  7086     {
       
  7087         paintedRegion += e->region();
       
  7088         ++repaints;
       
  7089         QGraphicsView::paintEvent(e);
       
  7090     }
       
  7091     void reset() { repaints = 0; paintedRegion = QRegion(); }
       
  7092 };
       
  7093 
       
  7094 void tst_QGraphicsItem::update()
  7155 void tst_QGraphicsItem::update()
  7095 {
  7156 {
  7096     QGraphicsScene scene;
  7157     QGraphicsScene scene;
  7097     scene.setSceneRect(-100, -100, 200, 200);
  7158     scene.setSceneRect(-100, -100, 200, 200);
  7098     MyGraphicsView view(&scene);
  7159     MyGraphicsView view(&scene);
  9833     QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 4);
  9894     QCOMPARE(child1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 4);
  9834     QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1);
  9895     QCOMPARE(grandChild1->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 1);
  9835     QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0);
  9896     QCOMPARE(child2->changes.count(QGraphicsItem::ItemScenePositionHasChanged), 0);
  9836 }
  9897 }
  9837 
  9898 
  9838 void  tst_QGraphicsItem::QTBUG_5418_textItemSetDefaultColor()
  9899 void tst_QGraphicsItem::QTBUG_5418_textItemSetDefaultColor()
  9839 {
  9900 {
  9840     struct Item : public QGraphicsTextItem
  9901     struct Item : public QGraphicsTextItem
  9841     {
  9902     {
  9842         int painted;
  9903         int painted;
  9843         void paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *wid)
  9904         void paint(QPainter *painter, const QStyleOptionGraphicsItem *opt, QWidget *wid)
  9912     child4->setPos(40, 40);
  9973     child4->setPos(40, 40);
  9913 
  9974 
  9914     QGraphicsScene scene;
  9975     QGraphicsScene scene;
  9915     scene.addItem(parent);
  9976     scene.addItem(parent);
  9916 
  9977 
  9917     class MyGraphicsView : public QGraphicsView
       
  9918     { public:
       
  9919         int repaints;
       
  9920         QRegion paintedRegion;
       
  9921         MyGraphicsView(QGraphicsScene *scene) : QGraphicsView(scene), repaints(0) {}
       
  9922         void paintEvent(QPaintEvent *e)
       
  9923         {
       
  9924             ++repaints;
       
  9925             paintedRegion += e->region();
       
  9926             QGraphicsView::paintEvent(e);
       
  9927         }
       
  9928         void reset() { repaints = 0; paintedRegion = QRegion(); }
       
  9929     };
       
  9930 
       
  9931     MyGraphicsView view(&scene);
  9978     MyGraphicsView view(&scene);
  9932     view.show();
  9979     view.show();
  9933     QTest::qWaitForWindowShown(&view);
  9980     QTest::qWaitForWindowShown(&view);
  9934     QTRY_VERIFY(view.repaints > 0);
  9981     QTRY_VERIFY(view.repaints > 0);
  9935 
  9982 
  9953     child4->setVisible(false);
 10000     child4->setVisible(false);
  9954 
 10001 
  9955     QTRY_VERIFY(view.repaints == 1);
 10002     QTRY_VERIFY(view.repaints == 1);
  9956 }
 10003 }
  9957 
 10004 
       
 10005 void tst_QGraphicsItem::QT_2653_fullUpdateDiscardingOpacityUpdate()
       
 10006 {
       
 10007     QGraphicsScene scene(0, 0, 200, 200);
       
 10008     MyGraphicsView view(&scene);
       
 10009 
       
 10010     EventTester *parentGreen = new EventTester();
       
 10011     parentGreen->setGeometry(QRectF(20, 20, 100, 100));
       
 10012     parentGreen->brush = Qt::green;
       
 10013 
       
 10014     EventTester *childYellow = new EventTester(parentGreen);
       
 10015     childYellow->setGeometry(QRectF(10, 10, 50, 50));
       
 10016     childYellow->brush = Qt::yellow;
       
 10017 
       
 10018     scene.addItem(parentGreen);
       
 10019 
       
 10020     childYellow->setOpacity(0.0);
       
 10021     parentGreen->setOpacity(0.0);
       
 10022 
       
 10023     // set any of the flags below to trigger a fullUpdate to reproduce the bug:
       
 10024     // ItemIgnoresTransformations, ItemClipsChildrenToShape, ItemIsSelectable
       
 10025     parentGreen->setFlag(QGraphicsItem::ItemIgnoresTransformations);
       
 10026 
       
 10027     view.show();
       
 10028     QTest::qWaitForWindowShown(&view);
       
 10029     view.reset();
       
 10030 
       
 10031     parentGreen->setOpacity(1.0);
       
 10032 
       
 10033     QTRY_COMPARE(view.repaints, 1);
       
 10034 
       
 10035     view.reset();
       
 10036     childYellow->repaints = 0;
       
 10037 
       
 10038     childYellow->setOpacity(1.0);
       
 10039 
       
 10040     QTRY_COMPARE(view.repaints, 1);
       
 10041     QTRY_COMPARE(childYellow->repaints, 1);
       
 10042 }
       
 10043 
       
 10044 void tst_QGraphicsItem::QTBUG_7714_fullUpdateDiscardingOpacityUpdate2()
       
 10045 {
       
 10046     QGraphicsScene scene(0, 0, 200, 200);
       
 10047     MyGraphicsView view(&scene);
       
 10048     MyGraphicsView origView(&scene);
       
 10049 
       
 10050     EventTester *parentGreen = new EventTester();
       
 10051     parentGreen->setGeometry(QRectF(20, 20, 100, 100));
       
 10052     parentGreen->brush = Qt::green;
       
 10053 
       
 10054     EventTester *childYellow = new EventTester(parentGreen);
       
 10055     childYellow->setGeometry(QRectF(10, 10, 50, 50));
       
 10056     childYellow->brush = Qt::yellow;
       
 10057 
       
 10058     scene.addItem(parentGreen);
       
 10059 
       
 10060     origView.show();
       
 10061     QTest::qWaitForWindowShown(&origView);
       
 10062     origView.setGeometry(origView.width() + 20, 20,
       
 10063                          origView.width(), origView.height());
       
 10064 
       
 10065     parentGreen->setFlag(QGraphicsItem::ItemIgnoresTransformations);
       
 10066 
       
 10067     origView.reset();
       
 10068     childYellow->setOpacity(0.0);
       
 10069 
       
 10070     QTRY_COMPARE(origView.repaints, 1);
       
 10071 
       
 10072     view.show();
       
 10073 
       
 10074     QTest::qWaitForWindowShown(&view);
       
 10075     view.reset();
       
 10076     origView.reset();
       
 10077 
       
 10078     childYellow->setOpacity(1.0);
       
 10079 
       
 10080     QTRY_COMPARE(origView.repaints, 1);
       
 10081     QTRY_COMPARE(view.repaints, 1);
       
 10082 }
       
 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 
  9958 QTEST_MAIN(tst_QGraphicsItem)
 10184 QTEST_MAIN(tst_QGraphicsItem)
  9959 #include "tst_qgraphicsitem.moc"
 10185 #include "tst_qgraphicsitem.moc"