tests/auto/qgraphicsview/tst_qgraphicsview.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
--- a/tests/auto/qgraphicsview/tst_qgraphicsview.cpp	Tue Jan 26 12:42:25 2010 +0200
+++ b/tests/auto/qgraphicsview/tst_qgraphicsview.cpp	Tue Feb 02 00:43:10 2010 +0200
@@ -194,6 +194,8 @@
     void acceptDrops();
     void optimizationFlags();
     void optimizationFlags_dontSavePainterState();
+    void optimizationFlags_dontSavePainterState2_data();
+    void optimizationFlags_dontSavePainterState2();
     void levelOfDetail_data();
     void levelOfDetail();
     void scrollBarRanges_data();
@@ -209,6 +211,7 @@
     void centerOnDirtyItem();
     void mouseTracking();
     void mouseTracking2();
+    void mouseTracking3();
     void render();
     void exposeRegion();
     void update_data();
@@ -233,6 +236,7 @@
     void task259503_scrollingArtifacts();
     void QTBUG_4151_clipAndIgnore_data();
     void QTBUG_4151_clipAndIgnore();
+    void QTBUG_5859_exposedRect();
 };
 
 void tst_QGraphicsView::initTestCase()
@@ -2455,6 +2459,57 @@
     QTest::qWaitForWindowShown(&painter2);
 }
 
+void tst_QGraphicsView::optimizationFlags_dontSavePainterState2_data()
+{
+    QTest::addColumn<bool>("savePainter");
+    QTest::newRow("With painter state protection") << true;
+    QTest::newRow("Without painter state protection") << false;
+}
+
+void tst_QGraphicsView::optimizationFlags_dontSavePainterState2()
+{
+    QFETCH(bool, savePainter);
+
+    class MyScene : public QGraphicsScene
+    {
+    public:
+        void drawBackground(QPainter *p, const QRectF &)
+        { transformInDrawBackground = p->worldTransform(); }
+
+        void drawForeground(QPainter *p, const QRectF &)
+        { transformInDrawForeground = p->worldTransform(); }
+
+        QTransform transformInDrawBackground;
+        QTransform transformInDrawForeground;
+    };
+
+    MyScene scene;
+    // Add transformed dummy items to make sure the painter's worldTransform() is changed in drawItems.
+    scene.addRect(0, 0, 20, 20)->setTransform(QTransform::fromScale(2, 2));
+    scene.addRect(50, 50, 20, 20)->setTransform(QTransform::fromTranslate(200, 200));
+
+    CustomView view(&scene);
+    if (!savePainter)
+        view.setOptimizationFlag(QGraphicsView::DontSavePainterState);
+    view.rotate(45);
+    view.scale(1.5, 1.5);
+    view.show();
+#ifdef Q_WS_X11
+    qt_x11_wait_for_window_manager(&view);
+#endif
+
+    // Make sure the view is repainted; otherwise the tests below will fail.
+    view.viewport()->repaint();
+    QTest::qWait(200);
+    QVERIFY(view.painted);
+
+    // Make sure the painter's world transform is preserved after drawItems.
+    const QTransform expectedTransform = view.viewportTransform();
+    QVERIFY(!expectedTransform.isIdentity());
+    QCOMPARE(scene.transformInDrawForeground, expectedTransform);
+    QCOMPARE(scene.transformInDrawBackground, expectedTransform);
+}
+
 class LodItem : public QGraphicsRectItem
 {
 public:
@@ -3349,6 +3404,102 @@
     QCOMPARE(spy.count(), 1);
 }
 
+void tst_QGraphicsView::mouseTracking3()
+{
+    // Mouse tracking should be automatically enabled if AnchorUnderMouse is used for
+    // view transform or resize. We never disable mouse tracking if it is already enabled.
+
+    { // Make sure we enable mouse tracking when using AnchorUnderMouse for view transformation.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsView view(&scene);
+        QVERIFY(!view.viewport()->hasMouseTracking());
+
+        view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+
+    { // Make sure we enable mouse tracking when using AnchorUnderMouse for view resizing.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsView view(&scene);
+        QVERIFY(!view.viewport()->hasMouseTracking());
+
+        view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+
+    { // Make sure we don't disable mouse tracking in setViewport/setScene (transformation anchor).
+        QGraphicsView view;
+        view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+        QVERIFY(view.viewport()->hasMouseTracking());
+
+        QWidget *viewport = new QWidget;
+        view.setViewport(viewport);
+        QVERIFY(viewport->hasMouseTracking());
+
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        view.setScene(&scene);
+        QVERIFY(viewport->hasMouseTracking());
+    }
+
+    { // Make sure we don't disable mouse tracking in setViewport/setScene (resize anchor).
+        QGraphicsView view;
+        view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
+        QVERIFY(view.viewport()->hasMouseTracking());
+
+        QWidget *viewport = new QWidget;
+        view.setViewport(viewport);
+        QVERIFY(viewport->hasMouseTracking());
+
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        view.setScene(&scene);
+        QVERIFY(viewport->hasMouseTracking());
+    }
+
+    // Make sure we don't disable mouse tracking when adding an item (transformation anchor).
+    { // Adding an item to the scene before the scene is set on the view.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
+        scene.addItem(item);
+
+        QGraphicsView view;
+        view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+        view.setScene(&scene);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+
+    { // Adding an item to the scene after the scene is set on the view.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsView view(&scene);
+        view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
+
+        QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
+        scene.addItem(item);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+
+    // Make sure we don't disable mouse tracking when adding an item (resize anchor).
+    { // Adding an item to the scene before the scene is set on the view.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
+        scene.addItem(item);
+
+        QGraphicsView view;
+        view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
+        view.setScene(&scene);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+
+    { // Adding an item to the scene after the scene is set on the view.
+        QGraphicsScene scene(-10000, -10000, 20000, 20000);
+        QGraphicsView view(&scene);
+        view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
+
+        QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
+        scene.addItem(item);
+        QVERIFY(view.viewport()->hasMouseTracking());
+    }
+}
+
 class RenderTester : public QGraphicsRectItem
 {
 public:
@@ -3547,6 +3698,7 @@
     item->setFlag(QGraphicsItem::ItemIsFocusable);
     scene.addItem(item);
     scene.setFocusItem(item);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
 
     item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false);
@@ -3561,27 +3713,35 @@
     scene.addItem(item2);
     scene.setFocusItem(item2);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
 
     scene.setFocusItem(item);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
 
     view.setScene(0);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
 
     view.setScene(&scene);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
 
     scene.setFocusItem(item2);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
 
     view.setScene(0);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
 
     scene.setFocusItem(item);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
 
     view.setScene(&scene);
     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
+    QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
 }
 
 class InputContextTester : public QInputContext
@@ -3735,7 +3895,7 @@
             {
 //                qDebug() << event->region();
 //                qDebug() << updateRegion;
-                QEXPECT_FAIL("", "The event region doesn't include the original item position region. See task #259503.", Continue);
+                QEXPECT_FAIL("", "The event region doesn't include the original item position region. See QTBUG-4416", Continue);
                 QCOMPARE(event->region(), updateRegion);
             }
         }
@@ -3805,5 +3965,43 @@
     QCOMPARE(view.items(view.rect()).size(), numItems);
 }
 
+void tst_QGraphicsView::QTBUG_5859_exposedRect()
+{
+    class CustomScene : public QGraphicsScene
+    {
+    public:
+        CustomScene(const QRectF &rect) : QGraphicsScene(rect) { }
+        void drawBackground(QPainter *painter, const QRectF &rect)
+        { lastBackgroundExposedRect = rect; }
+        QRectF lastBackgroundExposedRect;
+    };
+
+    class CustomRectItem : public QGraphicsRectItem
+    {
+    public:
+        CustomRectItem(const QRectF &rect) : QGraphicsRectItem(rect)
+        { setFlag(QGraphicsItem::ItemUsesExtendedStyleOption); }
+        void paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
+        { lastExposedRect = option->exposedRect; }
+        QRectF lastExposedRect;
+    };
+
+    CustomScene scene(QRectF(0,0,50,50));
+
+    CustomRectItem item(scene.sceneRect());
+
+    scene.addItem(&item);
+
+    QGraphicsView view(&scene);
+    view.scale(4.15, 4.15);
+    view.show();
+    QTest::qWaitForWindowShown(&view);
+
+    view.viewport()->repaint(10,10,20,20);
+    QApplication::processEvents();
+
+    QCOMPARE(item.lastExposedRect, scene.lastBackgroundExposedRect);
+}
+
 QTEST_MAIN(tst_QGraphicsView)
 #include "tst_qgraphicsview.moc"