tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp
changeset 18 2f34d5167611
parent 3 41300fa6a67c
child 19 fcece45ef507
equal deleted inserted replaced
3:41300fa6a67c 18:2f34d5167611
     1 /****************************************************************************
     1 /****************************************************************************
     2 **
     2 **
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
     4 ** All rights reserved.
     4 ** All rights reserved.
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
     6 **
     6 **
     7 ** This file is part of the test suite of the Qt Toolkit.
     7 ** This file is part of the test suite of the Qt Toolkit.
     8 **
     8 **
    68     void draw();
    68     void draw();
    69     void opacity();
    69     void opacity();
    70     void grayscale();
    70     void grayscale();
    71     void colorize();
    71     void colorize();
    72     void drawPixmapItem();
    72     void drawPixmapItem();
       
    73     void deviceCoordinateTranslateCaching();
       
    74     void inheritOpacity();
    73 };
    75 };
    74 
    76 
    75 void tst_QGraphicsEffect::initTestCase()
    77 void tst_QGraphicsEffect::initTestCase()
    76 {}
    78 {}
    77 
    79 
    78 class CustomItem : public QGraphicsRectItem
    80 class CustomItem : public QGraphicsRectItem
    79 {
    81 {
    80 public:
    82 public:
    81     CustomItem(qreal x, qreal y, qreal width, qreal height)
    83     CustomItem(qreal x, qreal y, qreal width, qreal height, QGraphicsItem *parent = 0)
    82         : QGraphicsRectItem(x, y, width, height), numRepaints(0),
    84         : QGraphicsRectItem(x, y, width, height, parent), numRepaints(0),
    83           m_painter(0), m_styleOption(0)
    85           m_painter(0), m_styleOption(0)
    84     {}
    86     {}
    85 
    87 
    86     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    88     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    87     {
    89     {
   512     QTest::qWait(50);
   514     QTest::qWait(50);
   513 
   515 
   514     QTRY_VERIFY(effect->repaints >= 2);
   516     QTRY_VERIFY(effect->repaints >= 2);
   515 }
   517 }
   516 
   518 
       
   519 class DeviceEffect : public QGraphicsEffect
       
   520 {
       
   521 public:
       
   522     QRectF boundingRectFor(const QRectF &rect) const
       
   523     { return rect; }
       
   524 
       
   525     void draw(QPainter *painter)
       
   526     {
       
   527         QPoint offset;
       
   528         QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset, QGraphicsEffect::NoPad);
       
   529 
       
   530         if (pixmap.isNull())
       
   531             return;
       
   532 
       
   533         painter->save();
       
   534         painter->setWorldTransform(QTransform());
       
   535         painter->drawPixmap(offset, pixmap);
       
   536         painter->restore();
       
   537     }
       
   538 };
       
   539 
       
   540 void tst_QGraphicsEffect::deviceCoordinateTranslateCaching()
       
   541 {
       
   542     QGraphicsScene scene;
       
   543     CustomItem *item = new CustomItem(0, 0, 10, 10);
       
   544     scene.addItem(item);
       
   545     scene.setSceneRect(0, 0, 50, 0);
       
   546 
       
   547     item->setGraphicsEffect(new DeviceEffect);
       
   548     item->setPen(Qt::NoPen);
       
   549     item->setBrush(Qt::red);
       
   550 
       
   551     QGraphicsView view(&scene);
       
   552     view.show();
       
   553     QTest::qWaitForWindowShown(&view);
       
   554 
       
   555     QTRY_VERIFY(item->numRepaints >= 1);
       
   556     int numRepaints = item->numRepaints;
       
   557 
       
   558     item->translate(10, 0);
       
   559     QTest::qWait(50);
       
   560 
       
   561     QVERIFY(item->numRepaints == numRepaints);
       
   562 }
       
   563 
       
   564 void tst_QGraphicsEffect::inheritOpacity()
       
   565 {
       
   566     QGraphicsScene scene;
       
   567     QGraphicsRectItem *rectItem = new QGraphicsRectItem(0, 0, 10, 10);
       
   568     CustomItem *item = new CustomItem(0, 0, 10, 10, rectItem);
       
   569 
       
   570     scene.addItem(rectItem);
       
   571 
       
   572     item->setGraphicsEffect(new DeviceEffect);
       
   573     item->setPen(Qt::NoPen);
       
   574     item->setBrush(Qt::red);
       
   575 
       
   576     rectItem->setOpacity(0.5);
       
   577 
       
   578     QGraphicsView view(&scene);
       
   579     view.show();
       
   580     QTest::qWaitForWindowShown(&view);
       
   581 
       
   582     QTRY_VERIFY(item->numRepaints >= 1);
       
   583 
       
   584     int numRepaints = item->numRepaints;
       
   585 
       
   586     rectItem->setOpacity(1);
       
   587     QTest::qWait(50);
       
   588 
       
   589     // item should have been rerendered due to opacity changing
       
   590     QTRY_VERIFY(item->numRepaints > numRepaints);
       
   591 }
       
   592 
   517 QTEST_MAIN(tst_QGraphicsEffect)
   593 QTEST_MAIN(tst_QGraphicsEffect)
   518 #include "tst_qgraphicseffect.moc"
   594 #include "tst_qgraphicseffect.moc"
   519 
   595