diff options
author | Bjørn Erik Nilsen <bjorn.nilsen@nokia.com> | 2009-08-21 15:49:25 (GMT) |
---|---|---|
committer | Bjørn Erik Nilsen <bjorn.nilsen@nokia.com> | 2009-08-21 16:04:18 (GMT) |
commit | 06d43c3a1ded89dc91336daa467d75f4fb3ad414 (patch) | |
tree | 0741075794a26b59c616c24baa30f704e70e2a8d /src/gui/effects | |
parent | fdc159ad6fd03d0e179e7dde7fe47e592936573c (diff) | |
download | Qt-06d43c3a1ded89dc91336daa467d75f4fb3ad414.zip Qt-06d43c3a1ded89dc91336daa467d75f4fb3ad414.tar.gz Qt-06d43c3a1ded89dc91336daa467d75f4fb3ad414.tar.bz2 |
Add QGraphicsOpacityEffect.
This is a common effect that many many customers have asked for.
Reviewed-by: Samuel
Diffstat (limited to 'src/gui/effects')
-rw-r--r-- | src/gui/effects/qgraphicseffect.cpp | 57 | ||||
-rw-r--r-- | src/gui/effects/qgraphicseffect.h | 25 | ||||
-rw-r--r-- | src/gui/effects/qgraphicseffect_p.h | 10 |
3 files changed, 92 insertions, 0 deletions
diff --git a/src/gui/effects/qgraphicseffect.cpp b/src/gui/effects/qgraphicseffect.cpp index 949922a..0653204 100644 --- a/src/gui/effects/qgraphicseffect.cpp +++ b/src/gui/effects/qgraphicseffect.cpp @@ -64,6 +64,7 @@ \o QGraphicsPixelizeEffect - pixelizes the item with any pixel size \o QGraphicsBlurEffect - blurs the item by a given radius \o QGraphicsDropShadowEffect - renders a dropshadow behind the item + \o QGraphicsBlurEffect - renders the item with an opacity \endlist If all you want is to add an effect to an item, you should visit the @@ -568,5 +569,61 @@ void QGraphicsDropShadowEffect::draw(QPainter *painter, QGraphicsEffectSource *s painter->setWorldTransform(restoreTransform); } +QGraphicsOpacityEffect::QGraphicsOpacityEffect(QObject *parent) + : QGraphicsEffect(*new QGraphicsOpacityEffectPrivate, parent) +{ +} + +QGraphicsOpacityEffect::~QGraphicsOpacityEffect() +{ +} + +qreal QGraphicsOpacityEffect::opacity() const +{ + Q_D(const QGraphicsOpacityEffect); + return d->opacity; +} + +void QGraphicsOpacityEffect::setOpacity(qreal opacity) +{ + Q_D(QGraphicsOpacityEffect); + if (qFuzzyCompare(d->opacity, opacity)) + return; + d->opacity = opacity; + emit opacityChanged(opacity); +} + +void QGraphicsOpacityEffect::draw(QPainter *painter, QGraphicsEffectSource *source) +{ + Q_D(QGraphicsOpacityEffect); + + // Transparent; nothing to draw. + if (qFuzzyIsNull(d->opacity)) + return; + + // Opaque; draw directly without going through a pixmap. + if (qFuzzyIsNull(d->opacity - 1)) { + source->draw(painter); + return; + } + + painter->save(); + painter->setOpacity(d->opacity); + + QPoint offset; + if (source->isPixmap()) { + // No point in drawing in device coordinates (pixmap will be scaled anyways). + const QPixmap pixmap = source->pixmap(Qt::LogicalCoordinates, &offset); + painter->drawPixmap(offset, pixmap); + } else { + // Draw pixmap in device coordinates to avoid pixmap scaling; + const QPixmap pixmap = source->pixmap(Qt::DeviceCoordinates, &offset); + painter->setWorldTransform(QTransform()); + painter->drawPixmap(offset, pixmap); + } + + painter->restore(); +} + QT_END_NAMESPACE diff --git a/src/gui/effects/qgraphicseffect.h b/src/gui/effects/qgraphicseffect.h index 99d8548..18ae3cf 100644 --- a/src/gui/effects/qgraphicseffect.h +++ b/src/gui/effects/qgraphicseffect.h @@ -269,6 +269,31 @@ private: Q_DISABLE_COPY(QGraphicsDropShadowEffect) }; +class QGraphicsOpacityEffectPrivate; +class Q_GUI_EXPORT QGraphicsOpacityEffect: public QGraphicsEffect +{ + Q_OBJECT + Q_PROPERTY(int opacity READ opacity WRITE setOpacity NOTIFY opacityChanged) +public: + QGraphicsOpacityEffect(QObject *parent = 0); + ~QGraphicsOpacityEffect(); + + qreal opacity() const; + +public Q_SLOTS: + void setOpacity(qreal opacity); + +Q_SIGNALS: + void opacityChanged(qreal opacity); + +protected: + void draw(QPainter *painter, QGraphicsEffectSource *source); + +private: + Q_DECLARE_PRIVATE(QGraphicsOpacityEffect) + Q_DISABLE_COPY(QGraphicsOpacityEffect) +}; + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/gui/effects/qgraphicseffect_p.h b/src/gui/effects/qgraphicseffect_p.h index 35f2d40..f5d0e80 100644 --- a/src/gui/effects/qgraphicseffect_p.h +++ b/src/gui/effects/qgraphicseffect_p.h @@ -160,6 +160,16 @@ public: QPixmapDropShadowFilter *filter; }; +class QGraphicsOpacityEffectPrivate : public QGraphicsEffectPrivate +{ + Q_DECLARE_PUBLIC(QGraphicsOpacityEffect) +public: + QGraphicsOpacityEffectPrivate() : opacity(0.7) {} + ~QGraphicsOpacityEffectPrivate() {} + + qreal opacity; +}; + QT_END_NAMESPACE #endif // QGRAPHICSEFFECT_P_H |