diff options
author | Espen Riskedal <espenr@trolltech.com> | 2009-08-21 16:08:11 (GMT) |
---|---|---|
committer | Espen Riskedal <espenr@trolltech.com> | 2009-08-21 16:08:11 (GMT) |
commit | 69dd9175ab925720e7973d107a43d31c43f9eb5c (patch) | |
tree | 5b3055e6ab8a72376de8924bcad8b6c504e3b62c | |
parent | bdeb85b84957247574cfa6bace5267fcb33a8bdd (diff) | |
parent | 06d43c3a1ded89dc91336daa467d75f4fb3ad414 (diff) | |
download | Qt-69dd9175ab925720e7973d107a43d31c43f9eb5c.zip Qt-69dd9175ab925720e7973d107a43d31c43f9eb5c.tar.gz Qt-69dd9175ab925720e7973d107a43d31c43f9eb5c.tar.bz2 |
Merge branch 'master' of git@scm.dev.nokia.troll.no:qt/qt into qtmaster
-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 |