From 886feff55f48ebdff0440278e611f822e6326c91 Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Mon, 14 Sep 2009 16:14:54 +0200 Subject: Add filter strength to QGraphics[Colorize,Grayscale]Effect. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Autotest: included Reviewed-by: Bjørn Erik Nilsen --- src/gui/effects/qgraphicseffect.cpp | 63 ++++++++++++++++++++++ src/gui/effects/qgraphicseffect.h | 13 +++++ src/gui/effects/qgraphicseffect_p.h | 11 +++- tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp | 40 ++++++++++++++ 4 files changed, 126 insertions(+), 1 deletion(-) diff --git a/src/gui/effects/qgraphicseffect.cpp b/src/gui/effects/qgraphicseffect.cpp index 683a984..26489c5 100644 --- a/src/gui/effects/qgraphicseffect.cpp +++ b/src/gui/effects/qgraphicseffect.cpp @@ -467,12 +467,44 @@ QGraphicsGrayscaleEffect::~QGraphicsGrayscaleEffect() { } + +/*! + \property QGraphicsGrayscaleEffect::strength + \brief the strength of the effect. + + By default, the strength is 1.0. + A strength 0.0 equals to no effect, while 1.0 means full grayscale. +*/ +qreal QGraphicsGrayscaleEffect::strength() const +{ + Q_D(const QGraphicsGrayscaleEffect); + return d->filter->strength(); +} + +void QGraphicsGrayscaleEffect::setStrength(qreal strength) +{ + Q_D(QGraphicsGrayscaleEffect); + if (qFuzzyCompare(d->filter->strength(), strength)) + return; + + d->filter->setStrength(strength); + d->opaque = !qFuzzyIsNull(strength); + update(); + emit strengthChanged(strength); +} + /*! \reimp */ void QGraphicsGrayscaleEffect::draw(QPainter *painter, QGraphicsEffectSource *source) { Q_D(QGraphicsGrayscaleEffect); + + if (!d->opaque) { + source->draw(painter); + return; + } + QPoint offset; if (source->isPixmap()) { // No point in drawing in device coordinates (pixmap will be scaled anyways). @@ -546,6 +578,31 @@ void QGraphicsColorizeEffect::setColor(const QColor &color) } /*! + \property QGraphicsColorizeEffect::strength + \brief the strength of the effect. + + By default, the strength is 1.0. + A strength 0.0 equals to no effect, while 1.0 means full colorization. +*/ +qreal QGraphicsColorizeEffect::strength() const +{ + Q_D(const QGraphicsColorizeEffect); + return d->filter->strength(); +} + +void QGraphicsColorizeEffect::setStrength(qreal strength) +{ + Q_D(QGraphicsColorizeEffect); + if (qFuzzyCompare(d->filter->strength(), strength)) + return; + + d->filter->setStrength(strength); + d->opaque = !qFuzzyIsNull(strength); + update(); + emit strengthChanged(strength); +} + +/*! \fn void QGraphicsColorizeEffect::colorChanged(const QColor &color) This signal is emitted whenever the effect's color changes. @@ -558,6 +615,12 @@ void QGraphicsColorizeEffect::setColor(const QColor &color) void QGraphicsColorizeEffect::draw(QPainter *painter, QGraphicsEffectSource *source) { Q_D(QGraphicsColorizeEffect); + + if (!d->opaque) { + source->draw(painter); + return; + } + QPoint offset; if (source->isPixmap()) { // No point in drawing in device coordinates (pixmap will be scaled anyways). diff --git a/src/gui/effects/qgraphicseffect.h b/src/gui/effects/qgraphicseffect.h index 5822d8c..5062826 100644 --- a/src/gui/effects/qgraphicseffect.h +++ b/src/gui/effects/qgraphicseffect.h @@ -144,13 +144,22 @@ class QGraphicsGrayscaleEffectPrivate; class Q_GUI_EXPORT QGraphicsGrayscaleEffect: public QGraphicsEffect { Q_OBJECT + Q_PROPERTY(qreal strength READ strength WRITE setStrength NOTIFY strengthChanged) public: QGraphicsGrayscaleEffect(QObject *parent = 0); ~QGraphicsGrayscaleEffect(); + qreal strength() const; + protected: void draw(QPainter *painter, QGraphicsEffectSource *source); +public Q_SLOTS: + void setStrength(qreal strength); + +Q_SIGNALS: + void strengthChanged(qreal strength); + private: Q_DECLARE_PRIVATE(QGraphicsGrayscaleEffect) Q_DISABLE_COPY(QGraphicsGrayscaleEffect) @@ -161,17 +170,21 @@ class Q_GUI_EXPORT QGraphicsColorizeEffect: public QGraphicsEffect { Q_OBJECT Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged) + Q_PROPERTY(qreal strength READ strength WRITE setStrength NOTIFY strengthChanged) public: QGraphicsColorizeEffect(QObject *parent = 0); ~QGraphicsColorizeEffect(); QColor color() const; + qreal strength() const; public Q_SLOTS: void setColor(const QColor &c); + void setStrength(qreal strength); Q_SIGNALS: void colorChanged(const QColor &color); + void strengthChanged(qreal strength); protected: void draw(QPainter *painter, QGraphicsEffectSource *source); diff --git a/src/gui/effects/qgraphicseffect_p.h b/src/gui/effects/qgraphicseffect_p.h index 4771384..e109790 100644 --- a/src/gui/effects/qgraphicseffect_p.h +++ b/src/gui/effects/qgraphicseffect_p.h @@ -113,6 +113,7 @@ class QGraphicsGrayscaleEffectPrivate : public QGraphicsEffectPrivate Q_DECLARE_PUBLIC(QGraphicsGrayscaleEffect) public: QGraphicsGrayscaleEffectPrivate() + : opaque(true) { filter = new QPixmapColorizeFilter; filter->setColor(Qt::black); @@ -120,16 +121,24 @@ public: ~QGraphicsGrayscaleEffectPrivate() { delete filter; } QPixmapColorizeFilter *filter; + quint32 opaque : 1; + quint32 padding : 31; }; class QGraphicsColorizeEffectPrivate : public QGraphicsEffectPrivate { Q_DECLARE_PUBLIC(QGraphicsColorizeEffect) public: - QGraphicsColorizeEffectPrivate() { filter = new QPixmapColorizeFilter; } + QGraphicsColorizeEffectPrivate() + : opaque(true) + { + filter = new QPixmapColorizeFilter; + } ~QGraphicsColorizeEffectPrivate() { delete filter; } QPixmapColorizeFilter *filter; + quint32 opaque : 1; + quint32 padding : 31; }; class QGraphicsPixelizeEffectPrivate : public QGraphicsEffectPrivate diff --git a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp index 5363fd6..d5205cd 100644 --- a/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp +++ b/tests/auto/qgraphicseffect/tst_qgraphicseffect.cpp @@ -388,6 +388,26 @@ void tst_QGraphicsEffect::grayscale() painter.end(); QCOMPARE(image.pixel(10, 10), qRgb(148, 148, 148)); + + effect->setStrength(0.5); + + image.fill(0); + painter.begin(&image); + painter.setRenderHint(QPainter::Antialiasing); + scene.render(&painter); + painter.end(); + + QCOMPARE(image.pixel(10, 10), qRgb(135, 171, 107)); + + effect->setStrength(0.0); + + image.fill(0); + painter.begin(&image); + painter.setRenderHint(QPainter::Antialiasing); + scene.render(&painter); + painter.end(); + + QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66)); } void tst_QGraphicsEffect::colorize() @@ -412,6 +432,26 @@ void tst_QGraphicsEffect::colorize() painter.end(); QCOMPARE(image.pixel(10, 10), qRgb(191, 212, 169)); + + effect->setStrength(0.5); + + image.fill(0); + painter.begin(&image); + painter.setRenderHint(QPainter::Antialiasing); + scene.render(&painter); + painter.end(); + + QCOMPARE(image.pixel(10, 10), qRgb(156, 203, 117)); + + effect->setStrength(0.0); + + image.fill(0); + painter.begin(&image); + painter.setRenderHint(QPainter::Antialiasing); + scene.render(&painter); + painter.end(); + + QCOMPARE(image.pixel(10, 10), qRgb(122, 193, 66)); } QTEST_MAIN(tst_QGraphicsEffect) -- cgit v0.12