tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp
changeset 19 fcece45ef507
parent 18 2f34d5167611
child 22 79de32ba3296
equal deleted inserted replaced
18:2f34d5167611 19:fcece45ef507
    44 #include <QtGui/qdesktopwidget.h>
    44 #include <QtGui/qdesktopwidget.h>
    45 #include <QtGui/qgraphicseffect.h>
    45 #include <QtGui/qgraphicseffect.h>
    46 #include <QtGui/qgraphicsview.h>
    46 #include <QtGui/qgraphicsview.h>
    47 #include <QtGui/qgraphicsscene.h>
    47 #include <QtGui/qgraphicsscene.h>
    48 #include <QtGui/qgraphicsitem.h>
    48 #include <QtGui/qgraphicsitem.h>
       
    49 #include <QtGui/qgraphicswidget.h>
    49 #include <QtGui/qstyleoption.h>
    50 #include <QtGui/qstyleoption.h>
    50 
    51 
    51 #include "../../shared/util.h"
    52 #include "../../shared/util.h"
    52 #include <private/qgraphicseffect_p.h>
    53 #include <private/qgraphicseffect_p.h>
    53 
    54 
    70     void grayscale();
    71     void grayscale();
    71     void colorize();
    72     void colorize();
    72     void drawPixmapItem();
    73     void drawPixmapItem();
    73     void deviceCoordinateTranslateCaching();
    74     void deviceCoordinateTranslateCaching();
    74     void inheritOpacity();
    75     void inheritOpacity();
       
    76     void dropShadowClipping();
       
    77     void childrenVisibilityShouldInvalidateCache();
    75 };
    78 };
    76 
    79 
    77 void tst_QGraphicsEffect::initTestCase()
    80 void tst_QGraphicsEffect::initTestCase()
    78 {}
    81 {}
    79 
    82 
   507     item->setGraphicsEffect(effect);
   510     item->setGraphicsEffect(effect);
   508 
   511 
   509     QGraphicsView view(&scene);
   512     QGraphicsView view(&scene);
   510     view.show();
   513     view.show();
   511     QTest::qWaitForWindowShown(&view);
   514     QTest::qWaitForWindowShown(&view);
       
   515     QTRY_VERIFY(effect->repaints >= 1);
   512 
   516 
   513     item->rotate(180);
   517     item->rotate(180);
   514     QTest::qWait(50);
   518     QTest::qWait(50);
   515 
   519 
   516     QTRY_VERIFY(effect->repaints >= 2);
   520     QTRY_VERIFY(effect->repaints >= 2);
   588 
   592 
   589     // item should have been rerendered due to opacity changing
   593     // item should have been rerendered due to opacity changing
   590     QTRY_VERIFY(item->numRepaints > numRepaints);
   594     QTRY_VERIFY(item->numRepaints > numRepaints);
   591 }
   595 }
   592 
   596 
       
   597 void tst_QGraphicsEffect::dropShadowClipping()
       
   598 {
       
   599     QImage img(128, 128, QImage::Format_ARGB32_Premultiplied);
       
   600     img.fill(0xffffffff);
       
   601 
       
   602     QGraphicsScene scene;
       
   603     QGraphicsRectItem *item = new QGraphicsRectItem(-5, -500, 10, 1000);
       
   604     item->setGraphicsEffect(new QGraphicsDropShadowEffect);
       
   605     item->setPen(Qt::NoPen);
       
   606     item->setBrush(Qt::red);
       
   607 
       
   608     scene.addItem(item);
       
   609 
       
   610     QPainter p(&img);
       
   611     scene.render(&p, img.rect(), QRect(-64, -64, 128, 128));
       
   612     p.end();
       
   613 
       
   614     for (int y = 1; y < img.height(); ++y)
       
   615         for (int x = 0; x < img.width(); ++x)
       
   616             QCOMPARE(img.pixel(x, y), img.pixel(x, y-1));
       
   617 }
       
   618 
       
   619 class MyGraphicsItem : public QGraphicsWidget
       
   620 {
       
   621 public:
       
   622     MyGraphicsItem(QGraphicsItem *parent = 0) :
       
   623             QGraphicsWidget(parent), nbPaint(0)
       
   624     {}
       
   625     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
       
   626     {
       
   627         nbPaint++;
       
   628         QGraphicsWidget::paint(painter, option, widget);
       
   629     }
       
   630     int nbPaint;
       
   631 };
       
   632 
       
   633 void tst_QGraphicsEffect::childrenVisibilityShouldInvalidateCache()
       
   634 {
       
   635     QGraphicsScene scene;
       
   636     MyGraphicsItem parent;
       
   637     parent.resize(200, 200);
       
   638     QGraphicsWidget child(&parent);
       
   639     child.resize(200, 200);
       
   640     child.setVisible(false);
       
   641     scene.addItem(&parent);
       
   642     QGraphicsView view(&scene);
       
   643     view.show();
       
   644     QApplication::setActiveWindow(&view);
       
   645     QTest::qWaitForWindowShown(&view);
       
   646     QTRY_COMPARE(parent.nbPaint, 1);
       
   647     //we set an effect on the parent
       
   648     parent.setGraphicsEffect(new QGraphicsDropShadowEffect(&parent));
       
   649     //flush the events
       
   650     QApplication::processEvents();
       
   651     //new effect applied->repaint
       
   652     QCOMPARE(parent.nbPaint, 2);
       
   653     child.setVisible(true);
       
   654     //flush the events
       
   655     QApplication::processEvents();
       
   656     //a new child appears we need to redraw the effect.
       
   657     QCOMPARE(parent.nbPaint, 3);
       
   658 }
       
   659 
   593 QTEST_MAIN(tst_QGraphicsEffect)
   660 QTEST_MAIN(tst_QGraphicsEffect)
   594 #include "tst_qgraphicseffect.moc"
   661 #include "tst_qgraphicseffect.moc"
   595 
   662