tests/auto/qgraphicsview/tst_qgraphicsview.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
   192     void viewportUpdateMode();
   192     void viewportUpdateMode();
   193     void viewportUpdateMode2();
   193     void viewportUpdateMode2();
   194     void acceptDrops();
   194     void acceptDrops();
   195     void optimizationFlags();
   195     void optimizationFlags();
   196     void optimizationFlags_dontSavePainterState();
   196     void optimizationFlags_dontSavePainterState();
       
   197     void optimizationFlags_dontSavePainterState2_data();
       
   198     void optimizationFlags_dontSavePainterState2();
   197     void levelOfDetail_data();
   199     void levelOfDetail_data();
   198     void levelOfDetail();
   200     void levelOfDetail();
   199     void scrollBarRanges_data();
   201     void scrollBarRanges_data();
   200     void scrollBarRanges();
   202     void scrollBarRanges();
   201     void acceptMousePressEvent();
   203     void acceptMousePressEvent();
   207     void moveItemWhileScrolling_data();
   209     void moveItemWhileScrolling_data();
   208     void moveItemWhileScrolling();
   210     void moveItemWhileScrolling();
   209     void centerOnDirtyItem();
   211     void centerOnDirtyItem();
   210     void mouseTracking();
   212     void mouseTracking();
   211     void mouseTracking2();
   213     void mouseTracking2();
       
   214     void mouseTracking3();
   212     void render();
   215     void render();
   213     void exposeRegion();
   216     void exposeRegion();
   214     void update_data();
   217     void update_data();
   215     void update();
   218     void update();
   216     void inputMethodSensitivity();
   219     void inputMethodSensitivity();
   231     void task253415_reconnectUpdateSceneOnSceneChanged();
   234     void task253415_reconnectUpdateSceneOnSceneChanged();
   232     void task255529_transformationAnchorMouseAndViewportMargins();
   235     void task255529_transformationAnchorMouseAndViewportMargins();
   233     void task259503_scrollingArtifacts();
   236     void task259503_scrollingArtifacts();
   234     void QTBUG_4151_clipAndIgnore_data();
   237     void QTBUG_4151_clipAndIgnore_data();
   235     void QTBUG_4151_clipAndIgnore();
   238     void QTBUG_4151_clipAndIgnore();
       
   239     void QTBUG_5859_exposedRect();
   236 };
   240 };
   237 
   241 
   238 void tst_QGraphicsView::initTestCase()
   242 void tst_QGraphicsView::initTestCase()
   239 {
   243 {
   240 #ifdef Q_OS_WINCE_WM
   244 #ifdef Q_OS_WINCE_WM
  2451 
  2455 
  2452     MyGraphicsView painter2(&scene);
  2456     MyGraphicsView painter2(&scene);
  2453     painter2.setOptimizationFlag(QGraphicsView::DontSavePainterState,true);
  2457     painter2.setOptimizationFlag(QGraphicsView::DontSavePainterState,true);
  2454     painter2.show();
  2458     painter2.show();
  2455     QTest::qWaitForWindowShown(&painter2);
  2459     QTest::qWaitForWindowShown(&painter2);
       
  2460 }
       
  2461 
       
  2462 void tst_QGraphicsView::optimizationFlags_dontSavePainterState2_data()
       
  2463 {
       
  2464     QTest::addColumn<bool>("savePainter");
       
  2465     QTest::newRow("With painter state protection") << true;
       
  2466     QTest::newRow("Without painter state protection") << false;
       
  2467 }
       
  2468 
       
  2469 void tst_QGraphicsView::optimizationFlags_dontSavePainterState2()
       
  2470 {
       
  2471     QFETCH(bool, savePainter);
       
  2472 
       
  2473     class MyScene : public QGraphicsScene
       
  2474     {
       
  2475     public:
       
  2476         void drawBackground(QPainter *p, const QRectF &)
       
  2477         { transformInDrawBackground = p->worldTransform(); }
       
  2478 
       
  2479         void drawForeground(QPainter *p, const QRectF &)
       
  2480         { transformInDrawForeground = p->worldTransform(); }
       
  2481 
       
  2482         QTransform transformInDrawBackground;
       
  2483         QTransform transformInDrawForeground;
       
  2484     };
       
  2485 
       
  2486     MyScene scene;
       
  2487     // Add transformed dummy items to make sure the painter's worldTransform() is changed in drawItems.
       
  2488     scene.addRect(0, 0, 20, 20)->setTransform(QTransform::fromScale(2, 2));
       
  2489     scene.addRect(50, 50, 20, 20)->setTransform(QTransform::fromTranslate(200, 200));
       
  2490 
       
  2491     CustomView view(&scene);
       
  2492     if (!savePainter)
       
  2493         view.setOptimizationFlag(QGraphicsView::DontSavePainterState);
       
  2494     view.rotate(45);
       
  2495     view.scale(1.5, 1.5);
       
  2496     view.show();
       
  2497 #ifdef Q_WS_X11
       
  2498     qt_x11_wait_for_window_manager(&view);
       
  2499 #endif
       
  2500 
       
  2501     // Make sure the view is repainted; otherwise the tests below will fail.
       
  2502     view.viewport()->repaint();
       
  2503     QTest::qWait(200);
       
  2504     QVERIFY(view.painted);
       
  2505 
       
  2506     // Make sure the painter's world transform is preserved after drawItems.
       
  2507     const QTransform expectedTransform = view.viewportTransform();
       
  2508     QVERIFY(!expectedTransform.isIdentity());
       
  2509     QCOMPARE(scene.transformInDrawForeground, expectedTransform);
       
  2510     QCOMPARE(scene.transformInDrawBackground, expectedTransform);
  2456 }
  2511 }
  2457 
  2512 
  2458 class LodItem : public QGraphicsRectItem
  2513 class LodItem : public QGraphicsRectItem
  2459 {
  2514 {
  2460 public:
  2515 public:
  3347                       Qt::MouseButtons(Qt::NoButton), 0);
  3402                       Qt::MouseButtons(Qt::NoButton), 0);
  3348     QApplication::sendEvent(view.viewport(), &event);
  3403     QApplication::sendEvent(view.viewport(), &event);
  3349     QCOMPARE(spy.count(), 1);
  3404     QCOMPARE(spy.count(), 1);
  3350 }
  3405 }
  3351 
  3406 
       
  3407 void tst_QGraphicsView::mouseTracking3()
       
  3408 {
       
  3409     // Mouse tracking should be automatically enabled if AnchorUnderMouse is used for
       
  3410     // view transform or resize. We never disable mouse tracking if it is already enabled.
       
  3411 
       
  3412     { // Make sure we enable mouse tracking when using AnchorUnderMouse for view transformation.
       
  3413         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3414         QGraphicsView view(&scene);
       
  3415         QVERIFY(!view.viewport()->hasMouseTracking());
       
  3416 
       
  3417         view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
       
  3418         QVERIFY(view.viewport()->hasMouseTracking());
       
  3419     }
       
  3420 
       
  3421     { // Make sure we enable mouse tracking when using AnchorUnderMouse for view resizing.
       
  3422         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3423         QGraphicsView view(&scene);
       
  3424         QVERIFY(!view.viewport()->hasMouseTracking());
       
  3425 
       
  3426         view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
       
  3427         QVERIFY(view.viewport()->hasMouseTracking());
       
  3428     }
       
  3429 
       
  3430     { // Make sure we don't disable mouse tracking in setViewport/setScene (transformation anchor).
       
  3431         QGraphicsView view;
       
  3432         view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
       
  3433         QVERIFY(view.viewport()->hasMouseTracking());
       
  3434 
       
  3435         QWidget *viewport = new QWidget;
       
  3436         view.setViewport(viewport);
       
  3437         QVERIFY(viewport->hasMouseTracking());
       
  3438 
       
  3439         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3440         view.setScene(&scene);
       
  3441         QVERIFY(viewport->hasMouseTracking());
       
  3442     }
       
  3443 
       
  3444     { // Make sure we don't disable mouse tracking in setViewport/setScene (resize anchor).
       
  3445         QGraphicsView view;
       
  3446         view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
       
  3447         QVERIFY(view.viewport()->hasMouseTracking());
       
  3448 
       
  3449         QWidget *viewport = new QWidget;
       
  3450         view.setViewport(viewport);
       
  3451         QVERIFY(viewport->hasMouseTracking());
       
  3452 
       
  3453         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3454         view.setScene(&scene);
       
  3455         QVERIFY(viewport->hasMouseTracking());
       
  3456     }
       
  3457 
       
  3458     // Make sure we don't disable mouse tracking when adding an item (transformation anchor).
       
  3459     { // Adding an item to the scene before the scene is set on the view.
       
  3460         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3461         QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
       
  3462         scene.addItem(item);
       
  3463 
       
  3464         QGraphicsView view;
       
  3465         view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
       
  3466         view.setScene(&scene);
       
  3467         QVERIFY(view.viewport()->hasMouseTracking());
       
  3468     }
       
  3469 
       
  3470     { // Adding an item to the scene after the scene is set on the view.
       
  3471         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3472         QGraphicsView view(&scene);
       
  3473         view.setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
       
  3474 
       
  3475         QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
       
  3476         scene.addItem(item);
       
  3477         QVERIFY(view.viewport()->hasMouseTracking());
       
  3478     }
       
  3479 
       
  3480     // Make sure we don't disable mouse tracking when adding an item (resize anchor).
       
  3481     { // Adding an item to the scene before the scene is set on the view.
       
  3482         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3483         QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
       
  3484         scene.addItem(item);
       
  3485 
       
  3486         QGraphicsView view;
       
  3487         view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
       
  3488         view.setScene(&scene);
       
  3489         QVERIFY(view.viewport()->hasMouseTracking());
       
  3490     }
       
  3491 
       
  3492     { // Adding an item to the scene after the scene is set on the view.
       
  3493         QGraphicsScene scene(-10000, -10000, 20000, 20000);
       
  3494         QGraphicsView view(&scene);
       
  3495         view.setResizeAnchor(QGraphicsView::AnchorUnderMouse);
       
  3496 
       
  3497         QGraphicsRectItem *item = new QGraphicsRectItem(10, 10, 10, 10);
       
  3498         scene.addItem(item);
       
  3499         QVERIFY(view.viewport()->hasMouseTracking());
       
  3500     }
       
  3501 }
       
  3502 
  3352 class RenderTester : public QGraphicsRectItem
  3503 class RenderTester : public QGraphicsRectItem
  3353 {
  3504 {
  3354 public:
  3505 public:
  3355     RenderTester(const QRectF &rect)
  3506     RenderTester(const QRectF &rect)
  3356         : QGraphicsRectItem(rect), paints(0)
  3507         : QGraphicsRectItem(rect), paints(0)
  3545     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3696     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3546 
  3697 
  3547     item->setFlag(QGraphicsItem::ItemIsFocusable);
  3698     item->setFlag(QGraphicsItem::ItemIsFocusable);
  3548     scene.addItem(item);
  3699     scene.addItem(item);
  3549     scene.setFocusItem(item);
  3700     scene.setFocusItem(item);
       
  3701     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3550     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
  3702     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
  3551 
  3703 
  3552     item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false);
  3704     item->setFlag(QGraphicsItem::ItemAcceptsInputMethod, false);
  3553     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3705     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3554 
  3706 
  3559     QGraphicsRectItem *item2 = new QGraphicsRectItem;
  3711     QGraphicsRectItem *item2 = new QGraphicsRectItem;
  3560     item2->setFlag(QGraphicsItem::ItemIsFocusable);
  3712     item2->setFlag(QGraphicsItem::ItemIsFocusable);
  3561     scene.addItem(item2);
  3713     scene.addItem(item2);
  3562     scene.setFocusItem(item2);
  3714     scene.setFocusItem(item2);
  3563     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3715     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
       
  3716     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
  3564 
  3717 
  3565     scene.setFocusItem(item);
  3718     scene.setFocusItem(item);
  3566     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
  3719     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
       
  3720     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3567 
  3721 
  3568     view.setScene(0);
  3722     view.setScene(0);
  3569     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3723     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
       
  3724     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3570 
  3725 
  3571     view.setScene(&scene);
  3726     view.setScene(&scene);
  3572     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
  3727     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
       
  3728     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3573 
  3729 
  3574     scene.setFocusItem(item2);
  3730     scene.setFocusItem(item2);
  3575     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3731     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
       
  3732     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
  3576 
  3733 
  3577     view.setScene(0);
  3734     view.setScene(0);
  3578     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3735     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
       
  3736     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item2));
  3579 
  3737 
  3580     scene.setFocusItem(item);
  3738     scene.setFocusItem(item);
  3581     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
  3739     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), false);
       
  3740     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3582 
  3741 
  3583     view.setScene(&scene);
  3742     view.setScene(&scene);
  3584     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
  3743     QCOMPARE(view.testAttribute(Qt::WA_InputMethodEnabled), true);
       
  3744     QCOMPARE(scene.focusItem(), static_cast<QGraphicsItem *>(item));
  3585 }
  3745 }
  3586 
  3746 
  3587 class InputContextTester : public QInputContext
  3747 class InputContextTester : public QInputContext
  3588 {
  3748 {
  3589     Q_OBJECT
  3749     Q_OBJECT
  3733 
  3893 
  3734             if (itSTimeToTest)
  3894             if (itSTimeToTest)
  3735             {
  3895             {
  3736 //                qDebug() << event->region();
  3896 //                qDebug() << event->region();
  3737 //                qDebug() << updateRegion;
  3897 //                qDebug() << updateRegion;
  3738                 QEXPECT_FAIL("", "The event region doesn't include the original item position region. See task #259503.", Continue);
  3898                 QEXPECT_FAIL("", "The event region doesn't include the original item position region. See QTBUG-4416", Continue);
  3739                 QCOMPARE(event->region(), updateRegion);
  3899                 QCOMPARE(event->region(), updateRegion);
  3740             }
  3900             }
  3741         }
  3901         }
  3742     };
  3902     };
  3743 
  3903 
  3803     QTRY_COMPARE(QApplication::activeWindow(), (QWidget *)&view);
  3963     QTRY_COMPARE(QApplication::activeWindow(), (QWidget *)&view);
  3804 
  3964 
  3805     QCOMPARE(view.items(view.rect()).size(), numItems);
  3965     QCOMPARE(view.items(view.rect()).size(), numItems);
  3806 }
  3966 }
  3807 
  3967 
       
  3968 void tst_QGraphicsView::QTBUG_5859_exposedRect()
       
  3969 {
       
  3970     class CustomScene : public QGraphicsScene
       
  3971     {
       
  3972     public:
       
  3973         CustomScene(const QRectF &rect) : QGraphicsScene(rect) { }
       
  3974         void drawBackground(QPainter *painter, const QRectF &rect)
       
  3975         { lastBackgroundExposedRect = rect; }
       
  3976         QRectF lastBackgroundExposedRect;
       
  3977     };
       
  3978 
       
  3979     class CustomRectItem : public QGraphicsRectItem
       
  3980     {
       
  3981     public:
       
  3982         CustomRectItem(const QRectF &rect) : QGraphicsRectItem(rect)
       
  3983         { setFlag(QGraphicsItem::ItemUsesExtendedStyleOption); }
       
  3984         void paint(QPainter * painter, const QStyleOptionGraphicsItem *option, QWidget *widget = 0)
       
  3985         { lastExposedRect = option->exposedRect; }
       
  3986         QRectF lastExposedRect;
       
  3987     };
       
  3988 
       
  3989     CustomScene scene(QRectF(0,0,50,50));
       
  3990 
       
  3991     CustomRectItem item(scene.sceneRect());
       
  3992 
       
  3993     scene.addItem(&item);
       
  3994 
       
  3995     QGraphicsView view(&scene);
       
  3996     view.scale(4.15, 4.15);
       
  3997     view.show();
       
  3998     QTest::qWaitForWindowShown(&view);
       
  3999 
       
  4000     view.viewport()->repaint(10,10,20,20);
       
  4001     QApplication::processEvents();
       
  4002 
       
  4003     QCOMPARE(item.lastExposedRect, scene.lastBackgroundExposedRect);
       
  4004 }
       
  4005 
  3808 QTEST_MAIN(tst_QGraphicsView)
  4006 QTEST_MAIN(tst_QGraphicsView)
  3809 #include "tst_qgraphicsview.moc"
  4007 #include "tst_qgraphicsview.moc"