From 1a431e850893b6b162c833f4f148f090e2427dda Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Wed, 23 Sep 2009 12:43:50 +0200 Subject: Added BlurType to blur graphics effect API. This lets the user control whether to use a fast dynamic blur when animating the radius of the blur for example, or to use a static high quality blur for one-time or constant radius blurring. Reviewed-by: Gunnar Sletta --- src/gui/effects/qgraphicseffect.cpp | 52 +++++++++++++++++++++++++++++++++++-- src/gui/effects/qgraphicseffect.h | 9 +++++++ src/gui/image/qpixmapfilter.cpp | 22 ++++++++-------- src/gui/image/qpixmapfilter_p.h | 9 +++++-- src/opengl/qglpixmapfilter.cpp | 20 +++++++------- 5 files changed, 87 insertions(+), 25 deletions(-) diff --git a/src/gui/effects/qgraphicseffect.cpp b/src/gui/effects/qgraphicseffect.cpp index ee87323..eee9bbf 100644 --- a/src/gui/effects/qgraphicseffect.cpp +++ b/src/gui/effects/qgraphicseffect.cpp @@ -777,7 +777,7 @@ void QGraphicsPixelizeEffect::draw(QPainter *painter, QGraphicsEffectSource *sou A blur effect blurs the source. This effect is useful for reducing details, such as when the source loses focus and you want to draw attention to other elements. The level of detail can be modified using the setBlurRadius() - function. + function. Use setBlurHint() to choose the quality or performance blur hints. By default, the blur radius is 5 pixels. @@ -795,7 +795,7 @@ QGraphicsBlurEffect::QGraphicsBlurEffect(QObject *parent) : QGraphicsEffect(*new QGraphicsBlurEffectPrivate, parent) { Q_D(QGraphicsBlurEffect); - d->filter->setQuality(Qt::SmoothTransformation); + d->filter->setBlurHint(QPixmapBlurFilter::PerformanceHint); } /*! @@ -839,6 +839,54 @@ void QGraphicsBlurEffect::setBlurRadius(int radius) */ /*! + \enum QGraphicsBlurEffect::BlurHint + + \since 4.6 + + This enum describes the hint of a blur graphics effect. + + \value PerformanceHint Using this value hints that performance is the + most important factor, at the potential cost of lower quality. + + \value QualityHint Using this value hints that a higher quality blur is + preferred over a fast blur. +*/ + +/*! + \property QGraphicsBlurEffect::blurHint + \brief the blur hint of the effect. + + Use the PerformanceHint blur hint to say that you want a faster blur, + and the QualityHint blur hint to say that you prefer a higher quality blur. + + When animating the blur radius it's recommended to use the PerformanceHint. + + By default, the blur hint is PerformanceHint. +*/ +QGraphicsBlurEffect::BlurHint QGraphicsBlurEffect::blurHint() const +{ + Q_D(const QGraphicsBlurEffect); + return BlurHint(d->filter->blurHint()); +} + +void QGraphicsBlurEffect::setBlurHint(QGraphicsBlurEffect::BlurHint hint) +{ + Q_D(QGraphicsBlurEffect); + if (BlurHint(d->filter->blurHint()) == hint) + return; + + d->filter->setBlurHint(QPixmapBlurFilter::BlurHint(hint)); + emit blurHintChanged(hint); +} + +/*! + \fn void QGraphicsBlurEffect::blurHintChanged(QGraphicsBlurEffect::BlurHint hint) + + This signal is emitted whenever the effect's blur hint changes. + The \a hint parameter holds the effect's new blur hint. +*/ + +/*! \reimp */ QRectF QGraphicsBlurEffect::boundingRectFor(const QRectF &rect) const diff --git a/src/gui/effects/qgraphicseffect.h b/src/gui/effects/qgraphicseffect.h index 5062826..c256381 100644 --- a/src/gui/effects/qgraphicseffect.h +++ b/src/gui/effects/qgraphicseffect.h @@ -224,18 +224,27 @@ class Q_GUI_EXPORT QGraphicsBlurEffect: public QGraphicsEffect { Q_OBJECT Q_PROPERTY(int blurRadius READ blurRadius WRITE setBlurRadius NOTIFY blurRadiusChanged) + Q_PROPERTY(BlurHint blurHint READ blurHint WRITE setBlurHint NOTIFY blurHintChanged) public: + enum BlurHint { + PerformanceHint, + QualityHint + }; + QGraphicsBlurEffect(QObject *parent = 0); ~QGraphicsBlurEffect(); QRectF boundingRectFor(const QRectF &rect) const; int blurRadius() const; + BlurHint blurHint() const; public Q_SLOTS: void setBlurRadius(int blurRadius); + void setBlurHint(BlurHint blurHint); Q_SIGNALS: void blurRadiusChanged(int blurRadius); + void blurHintChanged(BlurHint blurHint); protected: void draw(QPainter *painter, QGraphicsEffectSource *source); diff --git a/src/gui/image/qpixmapfilter.cpp b/src/gui/image/qpixmapfilter.cpp index 4fb650f..ba9a1e2 100644 --- a/src/gui/image/qpixmapfilter.cpp +++ b/src/gui/image/qpixmapfilter.cpp @@ -489,7 +489,7 @@ void QPixmapConvolutionFilter::draw(QPainter *painter, const QPointF &p, const Q which is applied when \l{QPixmapFilter::}{draw()} is called. The filter lets you specialize the radius of the blur as well - as the quality. + as hint as to whether to prefer performance or quality. By default, the blur effect is produced by applying an exponential filter generated from the specified blurRadius(). Paint engines @@ -504,10 +504,10 @@ void QPixmapConvolutionFilter::draw(QPainter *painter, const QPointF &p, const Q class QPixmapBlurFilterPrivate : public QPixmapFilterPrivate { public: - QPixmapBlurFilterPrivate() : radius(5), quality(Qt::FastTransformation) {} + QPixmapBlurFilterPrivate() : radius(5), hint(QPixmapBlurFilter::PerformanceHint) {} int radius; - Qt::TransformationMode quality; + QPixmapBlurFilter::BlurHint hint; }; @@ -553,29 +553,29 @@ int QPixmapBlurFilter::radius() const } /*! - Setting the quality to FastTransformation causes the implementation + Setting the blur hint to PerformanceHint causes the implementation to trade off visual quality to blur the image faster. Setting the - quality to SmoothTransformation causes the implementation to improve + blur hint to QualityHint causes the implementation to improve visual quality at the expense of speed. The implementation is free to ignore this value if it only has a single blur algorithm. \internal */ -void QPixmapBlurFilter::setQuality(Qt::TransformationMode quality) +void QPixmapBlurFilter::setBlurHint(QPixmapBlurFilter::BlurHint hint) { Q_D(QPixmapBlurFilter); - d->quality = quality; + d->hint = hint; } /*! - Gets the quality of the blur filter. + Gets the blur hint of the blur filter. \internal */ -Qt::TransformationMode QPixmapBlurFilter::quality() const +QPixmapBlurFilter::BlurHint QPixmapBlurFilter::blurHint() const { Q_D(const QPixmapBlurFilter); - return d->quality; + return d->hint; } /*! @@ -672,7 +672,7 @@ void QPixmapBlurFilter::draw(QPainter *painter, const QPointF &p, const QPixmap QPixmapBlurFilter *blurFilter = static_cast(filter); if (blurFilter) { blurFilter->setRadius(d->radius); - blurFilter->setQuality(d->quality); + blurFilter->setBlurHint(d->hint); blurFilter->draw(painter, p, src, srcRect); return; } diff --git a/src/gui/image/qpixmapfilter_p.h b/src/gui/image/qpixmapfilter_p.h index 2565110..92c8e56 100644 --- a/src/gui/image/qpixmapfilter_p.h +++ b/src/gui/image/qpixmapfilter_p.h @@ -126,14 +126,19 @@ class Q_GUI_EXPORT QPixmapBlurFilter : public QPixmapFilter Q_DECLARE_PRIVATE(QPixmapBlurFilter) public: + enum BlurHint { + PerformanceHint, + QualityHint + }; + QPixmapBlurFilter(QObject *parent = 0); ~QPixmapBlurFilter(); void setRadius(int radius); - void setQuality(Qt::TransformationMode mode); + void setBlurHint(BlurHint hint); int radius() const; - Qt::TransformationMode quality() const; + BlurHint blurHint() const; QRectF boundingRectFor(const QRectF &rect) const; void draw(QPainter *painter, const QPointF &dest, const QPixmap &src, const QRectF &srcRect = QRectF()) const; diff --git a/src/opengl/qglpixmapfilter.cpp b/src/opengl/qglpixmapfilter.cpp index b68ff45..bb0306c 100644 --- a/src/opengl/qglpixmapfilter.cpp +++ b/src/opengl/qglpixmapfilter.cpp @@ -102,7 +102,7 @@ private: class QGLPixmapBlurFilter : public QGLCustomShaderStage, public QGLPixmapFilter { public: - QGLPixmapBlurFilter(Qt::TransformationMode quality); + QGLPixmapBlurFilter(QPixmapBlurFilter::BlurHint hint); void setUniforms(QGLShaderProgram *program); @@ -117,7 +117,7 @@ private: mutable bool m_haveCached; mutable int m_cachedRadius; - mutable Qt::TransformationMode m_quality; + mutable QPixmapBlurFilter::BlurHint m_hint; }; extern QGLWidget *qt_gl_share_widget(); @@ -133,13 +133,13 @@ QPixmapFilter *QGL2PaintEngineEx::pixmapFilter(int type, const QPixmapFilter *pr case QPixmapFilter::BlurFilter: { const QPixmapBlurFilter *proto = static_cast(prototype); - if (proto->quality() == Qt::FastTransformation || proto->radius() <= 5) { + if (proto->blurHint() == QPixmapBlurFilter::PerformanceHint || proto->radius() <= 5) { if (!d->fastBlurFilter) - d->fastBlurFilter.reset(new QGLPixmapBlurFilter(Qt::FastTransformation)); + d->fastBlurFilter.reset(new QGLPixmapBlurFilter(QPixmapBlurFilter::PerformanceHint)); return d->fastBlurFilter.data(); } if (!d->blurFilter) - d->blurFilter.reset(new QGLPixmapBlurFilter(Qt::SmoothTransformation)); + d->blurFilter.reset(new QGLPixmapBlurFilter(QPixmapBlurFilter::QualityHint)); return d->blurFilter.data(); } @@ -328,12 +328,12 @@ static const char *qt_gl_blur_filter_fast = " return color * (1.0 / float(samples));" "}"; -QGLPixmapBlurFilter::QGLPixmapBlurFilter(Qt::TransformationMode quality) +QGLPixmapBlurFilter::QGLPixmapBlurFilter(QPixmapBlurFilter::BlurHint hint) : m_haveCached(false) , m_cachedRadius(5) - , m_quality(quality) + , m_hint(hint) { - if (quality == Qt::FastTransformation) { + if (hint == PerformanceHint) { QGLPixmapBlurFilter *filter = const_cast(this); filter->setSource(qt_gl_blur_filter_fast); m_haveCached = true; @@ -345,7 +345,7 @@ bool QGLPixmapBlurFilter::processGL(QPainter *painter, const QPointF &pos, const QGLPixmapBlurFilter *filter = const_cast(this); int radius = this->radius(); - if (!m_haveCached || (m_quality == Qt::SmoothTransformation && radius != m_cachedRadius)) { + if (!m_haveCached || (m_hint == QualityHint && radius != m_cachedRadius)) { // Only regenerate the shader from source if parameters have changed. m_haveCached = true; m_cachedRadius = radius; @@ -407,7 +407,7 @@ bool QGLPixmapBlurFilter::processGL(QPainter *painter, const QPointF &pos, const void QGLPixmapBlurFilter::setUniforms(QGLShaderProgram *program) { - if (m_quality == Qt::SmoothTransformation) { + if (m_hint == QualityHint) { if (m_horizontalBlur) program->setUniformValue("delta", 1.0 / m_textureSize.width(), 0.0); else -- cgit v0.12