tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    47 #include <QtGui/qgraphicsscene.h>
    47 #include <QtGui/qgraphicsscene.h>
    48 #include <QtGui/qgraphicsitem.h>
    48 #include <QtGui/qgraphicsitem.h>
    49 #include <QtGui/qstyleoption.h>
    49 #include <QtGui/qstyleoption.h>
    50 
    50 
    51 #include "../../shared/util.h"
    51 #include "../../shared/util.h"
       
    52 #include <private/qgraphicseffect_p.h>
    52 
    53 
    53 //TESTED_CLASS=
    54 //TESTED_CLASS=
    54 //TESTED_FILES=
    55 //TESTED_FILES=
    55 
    56 
    56 class tst_QGraphicsEffect : public QObject
    57 class tst_QGraphicsEffect : public QObject
    66     void boundingRect();
    67     void boundingRect();
    67     void draw();
    68     void draw();
    68     void opacity();
    69     void opacity();
    69     void grayscale();
    70     void grayscale();
    70     void colorize();
    71     void colorize();
       
    72     void drawPixmapItem();
    71 };
    73 };
    72 
    74 
    73 void tst_QGraphicsEffect::initTestCase()
    75 void tst_QGraphicsEffect::initTestCase()
    74 {}
    76 {}
    75 
    77 
   129     }
   131     }
   130 
   132 
   131     int margin() const
   133     int margin() const
   132     { return m_margin; }
   134     { return m_margin; }
   133 
   135 
   134     void draw(QPainter *painter, QGraphicsEffectSource *source)
   136     void draw(QPainter *painter)
   135     {
   137     {
   136         ++numRepaints;
   138         ++numRepaints;
   137         if (doNothingInDraw)
   139         if (doNothingInDraw)
   138             return;
   140             return;
   139         m_source = source;
   141         m_source = source();
   140         m_painter = painter;
   142         m_painter = painter;
   141         m_styleOption = source->styleOption();
   143         m_styleOption = source()->styleOption();
   142         m_opacity = painter->opacity();
   144         m_opacity = painter->opacity();
   143         source->draw(painter);
   145         drawSource(painter);
   144     }
   146     }
   145 
   147 
   146     void sourceChanged(QGraphicsEffect::ChangeFlags flags)
   148     void sourceChanged(QGraphicsEffect::ChangeFlags flags)
   147     { m_sourceChangedFlags |= flags; }
   149     { m_sourceChangedFlags |= flags; }
   148 
   150 
   193     effect->reset();
   195     effect->reset();
   194 
   196 
   195     // Uninstall effect on QGraphicsItem.
   197     // Uninstall effect on QGraphicsItem.
   196     effect->reset();
   198     effect->reset();
   197     item->setGraphicsEffect(0);
   199     item->setGraphicsEffect(0);
   198     QVERIFY(!effect->source());
   200     QVERIFY(!effect);
   199     QVERIFY(effect->m_sourceChangedFlags & QGraphicsEffect::SourceDetached);
   201     effect = new CustomEffect;
   200 
   202 
   201     // The item takes ownership and should delete the effect when destroyed.
   203     // The item takes ownership and should delete the effect when destroyed.
   202     item->setGraphicsEffect(effect);
   204     item->setGraphicsEffect(effect);
   203     QPointer<QGraphicsEffectSource> source = effect->source();
   205     QPointer<QGraphicsEffectSource> source = effect->source();
   204     QVERIFY(source);
   206     QVERIFY(source);
   246     effect->setMargin(margin = 20);
   248     effect->setMargin(margin = 20);
   247     QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin));
   249     QCOMPARE(effect->boundingRect(), itemRect.adjusted(-margin, -margin, margin, margin));
   248     QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect));
   250     QCOMPARE(effect->boundingRect(), effect->boundingRectFor(itemRect));
   249 
   251 
   250     // Uninstall effect on QGraphicsItem.
   252     // Uninstall effect on QGraphicsItem.
       
   253     QPointer<CustomEffect> ptr = effect;
   251     item->setGraphicsEffect(0);
   254     item->setGraphicsEffect(0);
   252     QCOMPARE(effect->boundingRect(), QRectF());
   255     QVERIFY(!ptr);
   253 
   256 
   254     delete effect;
       
   255     delete item;
   257     delete item;
   256 }
   258 }
   257 
   259 
   258 void tst_QGraphicsEffect::draw()
   260 void tst_QGraphicsEffect::draw()
   259 {
   261 {
   340     QTest::qWait(50);
   342     QTest::qWait(50);
   341     QCOMPARE(effect->numRepaints, 0);
   343     QCOMPARE(effect->numRepaints, 0);
   342     QCOMPARE(item->numRepaints, 0);
   344     QCOMPARE(item->numRepaints, 0);
   343 
   345 
   344     // Make sure uninstalling an effect triggers a repaint.
   346     // Make sure uninstalling an effect triggers a repaint.
       
   347     QPointer<CustomEffect> ptr = effect;
   345     item->setGraphicsEffect(0);
   348     item->setGraphicsEffect(0);
   346     QTest::qWait(50);
   349     QVERIFY(!ptr);
   347     QCOMPARE(effect->numRepaints, 0);
   350     QTest::qWait(50);
   348     QCOMPARE(item->numRepaints, 1);
   351     QCOMPARE(item->numRepaints, 1);
   349     delete effect;
       
   350 }
   352 }
   351 
   353 
   352 void tst_QGraphicsEffect::opacity()
   354 void tst_QGraphicsEffect::opacity()
   353 {
   355 {
   354     // Make sure the painter's opacity is correct in QGraphicsEffect::draw.
   356     // Make sure the painter's opacity is correct in QGraphicsEffect::draw.
   377 
   379 
   378     QGraphicsRectItem *item = scene.addRect(0, 0, 50, 50);
   380     QGraphicsRectItem *item = scene.addRect(0, 0, 50, 50);
   379     item->setPen(Qt::NoPen);
   381     item->setPen(Qt::NoPen);
   380     item->setBrush(QColor(122, 193, 66)); // Qt light green
   382     item->setBrush(QColor(122, 193, 66)); // Qt light green
   381 
   383 
   382     QGraphicsGrayscaleEffect *effect = new QGraphicsGrayscaleEffect;
   384     QGraphicsColorizeEffect *effect = new QGraphicsColorizeEffect;
       
   385     effect->setColor(Qt::black);
   383     item->setGraphicsEffect(effect);
   386     item->setGraphicsEffect(effect);
   384 
   387 
   385     QPainter painter;
   388     QPainter painter;
   386     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   389     QImage image(100, 100, QImage::Format_ARGB32_Premultiplied);
   387 
   390 
   461     painter.end();
   464     painter.end();
   462 
   465 
   463     QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66));
   466     QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66));
   464 }
   467 }
   465 
   468 
       
   469 class PixmapItemEffect : public QGraphicsEffect
       
   470 {
       
   471 public:
       
   472     PixmapItemEffect(const QPixmap &source)
       
   473         : QGraphicsEffect()
       
   474         , pixmap(source)
       
   475         , repaints(0)
       
   476     {}
       
   477 
       
   478     QRectF boundingRectFor(const QRectF &rect) const
       
   479     { return rect; }
       
   480 
       
   481     void draw(QPainter *painter)
       
   482     {
       
   483         QVERIFY(sourcePixmap(Qt::LogicalCoordinates).pixmapData() == pixmap.pixmapData());
       
   484         QVERIFY((painter->worldTransform().type() <= QTransform::TxTranslate) == (sourcePixmap(Qt::DeviceCoordinates).pixmapData() == pixmap.pixmapData()));
       
   485 
       
   486         ++repaints;
       
   487     }
       
   488     QPixmap pixmap;
       
   489     int repaints;
       
   490 };
       
   491 
       
   492 void tst_QGraphicsEffect::drawPixmapItem()
       
   493 {
       
   494     QImage image(32, 32, QImage::Format_RGB32);
       
   495     QPainter p(&image);
       
   496     p.fillRect(0, 0, 32, 16, Qt::blue);
       
   497     p.fillRect(0, 16, 32, 16, Qt::red);
       
   498     p.end();
       
   499 
       
   500     QGraphicsScene scene;
       
   501     QGraphicsPixmapItem *item = new QGraphicsPixmapItem(QPixmap::fromImage(image));
       
   502     scene.addItem(item);
       
   503 
       
   504     PixmapItemEffect *effect = new PixmapItemEffect(item->pixmap());
       
   505     item->setGraphicsEffect(effect);
       
   506 
       
   507     QGraphicsView view(&scene);
       
   508     view.show();
       
   509     QTest::qWaitForWindowShown(&view);
       
   510 
       
   511     item->rotate(180);
       
   512     QTest::qWait(50);
       
   513 
       
   514     QTRY_VERIFY(effect->repaints >= 2);
       
   515 }
       
   516 
   466 QTEST_MAIN(tst_QGraphicsEffect)
   517 QTEST_MAIN(tst_QGraphicsEffect)
   467 #include "tst_qgraphicseffect.moc"
   518 #include "tst_qgraphicseffect.moc"
   468 
   519