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/qgraphicseffect.cpp | |
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/qgraphicseffect.cpp')
-rw-r--r-- | src/gui/effects/qgraphicseffect.cpp | 57 |
1 files changed, 57 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 |