diff options
author | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-07-24 02:51:31 (GMT) |
---|---|---|
committer | Rhys Weatherley <rhys.weatherley@nokia.com> | 2009-07-24 02:51:31 (GMT) |
commit | 610aa1737a206fe97628a3375a543400ea0761fa (patch) | |
tree | a1f1880feb2c868b6a30f6302a5b873badaf2b24 | |
parent | 940b5bf333d6022fe5d3a037d6b1d74c6b9fe36e (diff) | |
download | Qt-610aa1737a206fe97628a3375a543400ea0761fa.zip Qt-610aa1737a206fe97628a3375a543400ea0761fa.tar.gz Qt-610aa1737a206fe97628a3375a543400ea0761fa.tar.bz2 |
Update QGraphicsShaderEffect to match new API
-rw-r--r-- | examples/graphicsview/customshader/blureffect.cpp | 19 | ||||
-rw-r--r-- | examples/graphicsview/customshader/blureffect.h | 11 | ||||
-rw-r--r-- | examples/graphicsview/customshader/blurpicker.cpp | 11 | ||||
-rw-r--r-- | examples/graphicsview/customshader/blurpicker.h | 3 | ||||
-rw-r--r-- | examples/graphicsview/customshader/customshadereffect.cpp | 4 | ||||
-rw-r--r-- | examples/graphicsview/customshader/customshadereffect.h | 2 | ||||
-rw-r--r-- | src/gui/graphicsview/qgraphicseffect_p.h | 4 | ||||
-rw-r--r-- | src/opengl/qgraphicsshadereffect.cpp | 60 | ||||
-rw-r--r-- | src/opengl/qgraphicsshadereffect.h | 9 |
9 files changed, 61 insertions, 62 deletions
diff --git a/examples/graphicsview/customshader/blureffect.cpp b/examples/graphicsview/customshader/blureffect.cpp index 8345d0b..43791c6 100644 --- a/examples/graphicsview/customshader/blureffect.cpp +++ b/examples/graphicsview/customshader/blureffect.cpp @@ -43,28 +43,27 @@ #include <QDebug> -BlurEffect::BlurEffect(QObject *parent) +BlurEffect::BlurEffect(QGraphicsItem *item) : QGraphicsBlurEffect() - , m_baseLine(200) + , m_baseLine(200), item(item) { } -void BlurEffect::adjustForItem(const QGraphicsItem *item) +void BlurEffect::adjustForItem() { qreal y = m_baseLine - item->pos().y(); qreal radius = qBound(0.0, y / 32, 16.0); setBlurRadius(radius); } -QRectF BlurEffect::boundingRectFor(const QGraphicsItem *item) +QRectF BlurEffect::boundingRect() const { - adjustForItem(item); - return QGraphicsBlurEffect::boundingRectFor(item); + const_cast<BlurEffect *>(this)->adjustForItem(); + return QGraphicsBlurEffect::boundingRect(); } -void BlurEffect::drawItem(QGraphicsItem *item, QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) +void BlurEffect::draw(QPainter *painter) { - adjustForItem(item); - QGraphicsBlurEffect::drawItem(item, painter, option, widget); + adjustForItem(); + QGraphicsBlurEffect::draw(painter); } diff --git a/examples/graphicsview/customshader/blureffect.h b/examples/graphicsview/customshader/blureffect.h index 24a6867..2aea8bf 100644 --- a/examples/graphicsview/customshader/blureffect.h +++ b/examples/graphicsview/customshader/blureffect.h @@ -48,21 +48,20 @@ class BlurEffect: public QGraphicsBlurEffect { public: - BlurEffect(QObject *parent = 0); + BlurEffect(QGraphicsItem *item); void setBaseLine(qreal y) { m_baseLine = y; } - QRectF boundingRectFor(const QGraphicsItem *item); + QRectF boundingRect() const; - void drawItem(QGraphicsItem *item, QPainter *painter, - const QStyleOptionGraphicsItem *option = 0, - QWidget *widget = 0); + void draw(QPainter *painter); private: - void adjustForItem(const QGraphicsItem *item); + void adjustForItem(); private: qreal m_baseLine; + QGraphicsItem *item; }; #endif // BLUREFFECT_H diff --git a/examples/graphicsview/customshader/blurpicker.cpp b/examples/graphicsview/customshader/blurpicker.cpp index 32bfc89..de80312 100644 --- a/examples/graphicsview/customshader/blurpicker.cpp +++ b/examples/graphicsview/customshader/blurpicker.cpp @@ -44,6 +44,7 @@ #include <QtGui> #include "blureffect.h" +#include "customshadereffect.h" #ifndef M_PI #define M_PI 3.14159265358979323846 @@ -79,9 +80,10 @@ void BlurPicker::updateIconPositions() pos -= QPointF(40, 40); icon->setPos(pos); baseline = qMax(baseline, ys); + if (i != 3) + static_cast<BlurEffect *>(icon->graphicsEffect())->setBaseLine(baseline); } - m_blurEffect->setBaseLine(baseline); m_scene.update(); } @@ -89,9 +91,6 @@ void BlurPicker::setupScene() { m_scene.setSceneRect(-200, -120, 400, 240); - m_blurEffect = new BlurEffect(this); - m_customEffect = new CustomShaderEffect(this); - QStringList names; names << ":/images/accessories-calculator.png"; names << ":/images/accessories-text-editor.png"; @@ -107,9 +106,9 @@ void BlurPicker::setupScene() QGraphicsPixmapItem *icon = m_scene.addPixmap(pixmap); icon->setZValue(1); if (i == 3) - icon->setEffect(m_customEffect); + icon->setGraphicsEffect(new CustomShaderEffect()); else - icon->setEffect(m_blurEffect); + icon->setGraphicsEffect(new BlurEffect(icon)); m_icons << icon; } diff --git a/examples/graphicsview/customshader/blurpicker.h b/examples/graphicsview/customshader/blurpicker.h index b4ac97b..b7ea3b4 100644 --- a/examples/graphicsview/customshader/blurpicker.h +++ b/examples/graphicsview/customshader/blurpicker.h @@ -47,7 +47,6 @@ #include <QTimeLine> #include "blureffect.h" -#include "customshadereffect.h" class BlurPicker: public QGraphicsView { @@ -68,8 +67,6 @@ private: private: qreal m_index; QGraphicsScene m_scene; - BlurEffect *m_blurEffect; - CustomShaderEffect *m_customEffect; QList<QGraphicsItem*> m_icons; QTimeLine m_timeLine; }; diff --git a/examples/graphicsview/customshader/customshadereffect.cpp b/examples/graphicsview/customshader/customshadereffect.cpp index 9f1945d..293123c 100644 --- a/examples/graphicsview/customshader/customshadereffect.cpp +++ b/examples/graphicsview/customshader/customshadereffect.cpp @@ -53,8 +53,8 @@ static char const colorizeShaderCode[] = " return vec4(colorize.rgb, src.a);\n" "}"; -CustomShaderEffect::CustomShaderEffect(QObject *parent) - : QGraphicsShaderEffect(parent), +CustomShaderEffect::CustomShaderEffect() + : QGraphicsShaderEffect(), color(Qt::red) { setPixelShaderFragment(colorizeShaderCode); diff --git a/examples/graphicsview/customshader/customshadereffect.h b/examples/graphicsview/customshader/customshadereffect.h index b4e0fb9..6482bd5 100644 --- a/examples/graphicsview/customshader/customshadereffect.h +++ b/examples/graphicsview/customshader/customshadereffect.h @@ -49,7 +49,7 @@ class CustomShaderEffect: public QGraphicsShaderEffect { public: - CustomShaderEffect(QObject *parent = 0); + CustomShaderEffect(); QColor effectColor() const { return color; } void setEffectColor(const QColor& c); diff --git a/src/gui/graphicsview/qgraphicseffect_p.h b/src/gui/graphicsview/qgraphicseffect_p.h index 9204789..37bcfdf 100644 --- a/src/gui/graphicsview/qgraphicseffect_p.h +++ b/src/gui/graphicsview/qgraphicseffect_p.h @@ -61,7 +61,7 @@ QT_BEGIN_NAMESPACE -class QGraphicsEffectSource +class Q_GUI_EXPORT QGraphicsEffectSource { public: QGraphicsEffectSource() {} @@ -72,7 +72,7 @@ public: virtual bool drawIntoPixmap(QPixmap *, const QTransform & = QTransform()) = 0; }; -class QGraphicsEffectPrivate : public QObjectPrivate +class Q_GUI_EXPORT QGraphicsEffectPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QGraphicsEffect) public: diff --git a/src/opengl/qgraphicsshadereffect.cpp b/src/opengl/qgraphicsshadereffect.cpp index 121dd90..d4f5fa0 100644 --- a/src/opengl/qgraphicsshadereffect.cpp +++ b/src/opengl/qgraphicsshadereffect.cpp @@ -47,6 +47,7 @@ #endif #include <QtGui/qpainter.h> #include <QtGui/qgraphicsitem.h> +#include <QtGui/private/qgraphicseffect_p.h> QT_BEGIN_NAMESPACE @@ -160,8 +161,9 @@ void QGLCustomShaderEffectStage::setUniforms(QGLShaderProgram *program) #endif -class QGraphicsShaderEffectPrivate +class QGraphicsShaderEffectPrivate : public QGraphicsEffectPrivate { + Q_DECLARE_PUBLIC(QGraphicsShaderEffect) public: QGraphicsShaderEffectPrivate() : pixelShaderFragment(qglslDefaultImageFragmentShader) @@ -180,10 +182,9 @@ public: /*! Constructs a shader effect and attaches it to \a parent. */ -QGraphicsShaderEffect::QGraphicsShaderEffect(QObject *parent) - : QGraphicsEffect(parent) +QGraphicsShaderEffect::QGraphicsShaderEffect() + : QGraphicsEffect(*new QGraphicsShaderEffectPrivate()) { - d_ptr = new QGraphicsShaderEffectPrivate(); } /*! @@ -192,9 +193,9 @@ QGraphicsShaderEffect::QGraphicsShaderEffect(QObject *parent) QGraphicsShaderEffect::~QGraphicsShaderEffect() { #ifdef QGL_HAVE_CUSTOM_SHADERS - delete d_ptr->customShaderStage; + Q_D(QGraphicsShaderEffect); + delete d->customShaderStage; #endif - delete d_ptr; } /*! @@ -207,7 +208,8 @@ QGraphicsShaderEffect::~QGraphicsShaderEffect() */ QByteArray QGraphicsShaderEffect::pixelShaderFragment() const { - return d_ptr->pixelShaderFragment; + Q_D(const QGraphicsShaderEffect); + return d->pixelShaderFragment; } /*! @@ -231,11 +233,12 @@ QByteArray QGraphicsShaderEffect::pixelShaderFragment() const */ void QGraphicsShaderEffect::setPixelShaderFragment(const QByteArray& code) { - if (d_ptr->pixelShaderFragment != code) { - d_ptr->pixelShaderFragment = code; + Q_D(QGraphicsShaderEffect); + if (d->pixelShaderFragment != code) { + d->pixelShaderFragment = code; #ifdef QGL_HAVE_CUSTOM_SHADERS - delete d_ptr->customShaderStage; - d_ptr->customShaderStage = 0; + delete d->customShaderStage; + d->customShaderStage = 0; #endif } } @@ -243,41 +246,45 @@ void QGraphicsShaderEffect::setPixelShaderFragment(const QByteArray& code) /*! \reimp */ -void QGraphicsShaderEffect::drawItem - (QGraphicsItem *item, QPainter *painter, - const QStyleOptionGraphicsItem *option, QWidget *widget) +void QGraphicsShaderEffect::draw(QPainter *painter) { + Q_D(QGraphicsShaderEffect); + #ifdef QGL_HAVE_CUSTOM_SHADERS // Find the item's bounds in device coordinates. - QRectF deviceBounds = painter->worldTransform().mapRect(item->boundingRect()); + QTransform itemToPixmapTransform(painter->worldTransform()); + QRectF deviceBounds = itemToPixmapTransform.mapRect(sourceBoundingRect()); QRect deviceRect = deviceBounds.toRect().adjusted(-1, -1, 1, 1); if (deviceRect.isEmpty()) return; - QPixmap *pixmap = QGraphicsEffect::drawItemOnPixmap(painter, item, option, widget, 0); - if (!pixmap) + if (deviceRect.x() != 0 || deviceRect.y() != 0) + itemToPixmapTransform *= QTransform::fromTranslate(-deviceRect.x(), -deviceRect.y()); + + QPixmap pixmap(deviceRect.size()); + if (!d->source->drawIntoPixmap(&pixmap, itemToPixmapTransform)) return; // Set the custom shader on the paint engine. The setOnPainter() // call may fail if the paint engine is not GL2. In that case, // we fall through to drawing the pixmap normally. - if (!d_ptr->customShaderStage) { - d_ptr->customShaderStage = new QGLCustomShaderEffectStage - (this, d_ptr->pixelShaderFragment); + if (!d->customShaderStage) { + d->customShaderStage = new QGLCustomShaderEffectStage + (this, d->pixelShaderFragment); } - bool usingShader = d_ptr->customShaderStage->setOnPainter(painter); + bool usingShader = d->customShaderStage->setOnPainter(painter); // Draw using an untransformed painter. QTransform restoreTransform = painter->worldTransform(); painter->setWorldTransform(QTransform()); - painter->drawPixmap(deviceRect.topLeft(), *pixmap); + painter->drawPixmap(deviceRect.topLeft(), pixmap); painter->setWorldTransform(restoreTransform); // Remove the custom shader to return to normal painting operations. if (usingShader) - d_ptr->customShaderStage->removeFromPainter(painter); + d->customShaderStage->removeFromPainter(painter); #else - item->paint(painter, option, widget); + drawSource(painter); #endif } @@ -294,8 +301,9 @@ void QGraphicsShaderEffect::drawItem void QGraphicsShaderEffect::setUniformsDirty() { #ifdef QGL_HAVE_CUSTOM_SHADERS - if (d_ptr->customShaderStage) - d_ptr->customShaderStage->setUniformsDirty(); + Q_D(QGraphicsShaderEffect); + if (d->customShaderStage) + d->customShaderStage->setUniformsDirty(); #endif } diff --git a/src/opengl/qgraphicsshadereffect.h b/src/opengl/qgraphicsshadereffect.h index 124f30c..032a233 100644 --- a/src/opengl/qgraphicsshadereffect.h +++ b/src/opengl/qgraphicsshadereffect.h @@ -60,22 +60,19 @@ class Q_OPENGL_EXPORT QGraphicsShaderEffect : public QGraphicsEffect { Q_OBJECT public: - QGraphicsShaderEffect(QObject *parent = 0); + QGraphicsShaderEffect(); virtual ~QGraphicsShaderEffect(); QByteArray pixelShaderFragment() const; void setPixelShaderFragment(const QByteArray& code); - void drawItem(QGraphicsItem *item, QPainter *painter, - const QStyleOptionGraphicsItem *option, - QWidget *widget); - protected: + void draw(QPainter *painter); void setUniformsDirty(); virtual void setUniforms(QGLShaderProgram *program); private: - QGraphicsShaderEffectPrivate *d_ptr; + Q_DECLARE_PRIVATE(QGraphicsShaderEffect) Q_DISABLE_COPY(QGraphicsShaderEffect) friend class QGLCustomShaderEffectStage; |