From 9891c5051f02b940f6dc94ac4ccf945e036feb13 Mon Sep 17 00:00:00 2001 From: Dimitar Asenov Date: Wed, 12 Mar 2014 14:19:04 -0700 Subject: Add a new optimization property to QGraphicsScene The minimumRenderSize is a qreal value that is used as a lower bound to determine what items are visible when a scene is rendered. If an item's view-transformed width or height are less than minimumRenderSize then this item is considered to insignificantly affect the final result and is not drawn. If the item clips its children to its shape they are automatically not drawn. This greatly reduces the drawing overhead for scenes with many items rendered in a zoomed out view. [ChangeLog][QtWidgets][QGraphicsScene] Added the minimumRenderSize property which can be used to speed up rendering by not painting items, smaller than a give size. Change-Id: Ie208234707dffb4d2fc620fc5d1514e0c144d9a8 Reviewed-by: Andreas Aardal Hanssen --- .../qgraphicsscene/tst_qgraphicsscene.cpp | 73 ++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp') diff --git a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp index dfc8465210..a4752126bc 100644 --- a/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp +++ b/tests/auto/widgets/graphicsview/qgraphicsscene/tst_qgraphicsscene.cpp @@ -267,6 +267,7 @@ private slots: void removeFullyTransparentItem(); void zeroScale(); void focusItemChangedSignal(); + void minimumRenderSize(); // task specific tests below me void task139710_bspTreeCrash(); @@ -4678,6 +4679,78 @@ void tst_QGraphicsScene::focusItemChangedSignal() } +class ItemCountsPaintCalls : public QGraphicsRectItem +{ +public: + ItemCountsPaintCalls(const QRectF & rect, QGraphicsItem *parent = 0) + : QGraphicsRectItem(rect, parent), repaints(0) {} + void paint ( QPainter * painter, const QStyleOptionGraphicsItem * option, QWidget * widget = 0 ) + { + QGraphicsRectItem::paint(painter, option, widget); + ++repaints; + } + int repaints; +}; + +void tst_QGraphicsScene::minimumRenderSize() +{ + Q_CHECK_PAINTEVENTS + + ItemCountsPaintCalls *bigParent = new ItemCountsPaintCalls(QRectF(0,0,100,100)); + ItemCountsPaintCalls *smallChild = new ItemCountsPaintCalls(QRectF(0,0,10,10), bigParent); + ItemCountsPaintCalls *smallerGrandChild = new ItemCountsPaintCalls(QRectF(0,0,1,1), smallChild); + QGraphicsScene scene; + scene.addItem(bigParent); + + CustomView view; + view.setScene(&scene); + view.show(); + QVERIFY(QTest::qWaitForWindowExposed(&view)); + qApp->processEvents(); + + // Initially, everything should be repainted the same number of times + int viewRepaints = 0; + QTRY_VERIFY(view.repaints > viewRepaints); + viewRepaints = view.repaints; + + QVERIFY(viewRepaints == bigParent->repaints); + QVERIFY(viewRepaints == smallChild->repaints); + QVERIFY(viewRepaints == smallerGrandChild->repaints); + + // Setting a minimum render size should cause a repaint + scene.setMinimumRenderSize(0.5); + qApp->processEvents(); + + QTRY_VERIFY(view.repaints > viewRepaints); + viewRepaints = view.repaints; + + QVERIFY(viewRepaints == bigParent->repaints); + QVERIFY(viewRepaints == smallChild->repaints); + QVERIFY(viewRepaints == smallerGrandChild->repaints); + + // Scaling should cause a repaint of big items only. + view.scale(0.1, 0.1); + qApp->processEvents(); + + QTRY_VERIFY(view.repaints > viewRepaints); + viewRepaints = view.repaints; + + QVERIFY(viewRepaints == bigParent->repaints); + QVERIFY(viewRepaints == smallChild->repaints); + QVERIFY(smallChild->repaints > smallerGrandChild->repaints); + + // Scaling further should cause even fewer items to be repainted + view.scale(0.1, 0.1); // Stacks with previous scale + qApp->processEvents(); + + QTRY_VERIFY(view.repaints > viewRepaints); + viewRepaints = view.repaints; + + QVERIFY(viewRepaints == bigParent->repaints); + QVERIFY(bigParent->repaints > smallChild->repaints); + QVERIFY(smallChild->repaints > smallerGrandChild->repaints); +} + void tst_QGraphicsScene::taskQTBUG_15977_renderWithDeviceCoordinateCache() { QGraphicsScene scene; -- cgit v1.2.3