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); |
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" |