Qt 4.8
Public Functions | Public Variables | Protected Functions | Private Functions | List of all members
ShaderEffect Class Reference

#include <shadereffect.h>

Inheritance diagram for ShaderEffect:
QGraphicsEffect QObject

Public Functions

void addRenderTarget (ShaderEffectSource *target)
 
void removeRenderTarget (ShaderEffectSource *target)
 
 ShaderEffect (QObject *parent=0)
 
 ~ShaderEffect ()
 
- Public Functions inherited from QGraphicsEffect
QRectF boundingRect () const
 Returns the effective bounding rectangle for this effect, i.e., the bounding rectangle of the source in device coordinates, adjusted by any margins applied by the effect itself. More...
 
virtual QRectF boundingRectFor (const QRectF &sourceRect) const
 Returns the effective bounding rectangle for this effect, given the provided rect in the device coordinates. More...
 
bool isEnabled () const
 
 QGraphicsEffect (QObject *parent=0)
 Constructs a new QGraphicsEffect instance having the specified parent. More...
 
QGraphicsEffectSourcesource () const
 Returns a pointer to the source, which provides extra context information that can be useful for the effect. More...
 
virtual ~QGraphicsEffect ()
 Removes the effect from the source, and destroys the graphics effect. More...
 
- Public Functions inherited from QObject
bool blockSignals (bool b)
 If block is true, signals emitted by this object are blocked (i.e., emitting a signal will not invoke anything connected to it). More...
 
const QObjectListchildren () const
 Returns a list of child objects. More...
 
bool connect (const QObject *sender, const char *signal, const char *member, Qt::ConnectionType type=Qt::AutoConnection) const
 
bool disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 
bool disconnect (const QObject *receiver, const char *member=0)
 
void dumpObjectInfo ()
 Dumps information about signal connections, etc. More...
 
void dumpObjectTree ()
 Dumps a tree of children to the debug output. More...
 
QList< QByteArraydynamicPropertyNames () const
 Returns the names of all properties that were dynamically added to the object using setProperty(). More...
 
virtual bool event (QEvent *)
 This virtual function receives events to an object and should return true if the event e was recognized and processed. More...
 
virtual bool eventFilter (QObject *, QEvent *)
 Filters events if this object has been installed as an event filter for the watched object. More...
 
template<typename T >
findChild (const QString &aName=QString()) const
 Returns the child of this object that can be cast into type T and that is called name, or 0 if there is no such object. More...
 
template<typename T >
QList< T > findChildren (const QString &aName=QString()) const
 Returns all children of this object with the given name that can be cast to type T, or an empty list if there are no such objects. More...
 
template<typename T >
QList< T > findChildren (const QRegExp &re) const
 
bool inherits (const char *classname) const
 Returns true if this object is an instance of a class that inherits className or a QObject subclass that inherits className; otherwise returns false. More...
 
void installEventFilter (QObject *)
 Installs an event filter filterObj on this object. More...
 
bool isWidgetType () const
 Returns true if the object is a widget; otherwise returns false. More...
 
void killTimer (int id)
 Kills the timer with timer identifier, id. More...
 
virtual const QMetaObjectmetaObject () const
 Returns a pointer to the meta-object of this object. More...
 
void moveToThread (QThread *thread)
 Changes the thread affinity for this object and its children. More...
 
QString objectName () const
 
QObjectparent () const
 Returns a pointer to the parent object. More...
 
QVariant property (const char *name) const
 Returns the value of the object's name property. More...
 
Q_INVOKABLE QObject (QObject *parent=0)
 Constructs an object with parent object parent. More...
 
void removeEventFilter (QObject *)
 Removes an event filter object obj from this object. More...
 
void setObjectName (const QString &name)
 
void setParent (QObject *)
 Makes the object a child of parent. More...
 
bool setProperty (const char *name, const QVariant &value)
 Sets the value of the object's name property to value. More...
 
void setUserData (uint id, QObjectUserData *data)
 
bool signalsBlocked () const
 Returns true if signals are blocked; otherwise returns false. More...
 
int startTimer (int interval)
 Starts a timer and returns a timer identifier, or returns zero if it could not start a timer. More...
 
QThreadthread () const
 Returns the thread in which the object lives. More...
 
QObjectUserDatauserData (uint id) const
 
virtual ~QObject ()
 Destroys the object, deleting all its child objects. More...
 

Public Variables

bool m_changed: 1
 
QVector< ShaderEffectSource * > m_renderTargets
 

Protected Functions

virtual void draw (QPainter *painter)
 This pure virtual function draws the effect and is called whenever the source needs to be drawn. More...
 
virtual void sourceChanged (ChangeFlags flags)
 This virtual function is called by QGraphicsEffect to notify the effect that the source has changed. More...
 
- Protected Functions inherited from QGraphicsEffect
void drawSource (QPainter *painter)
 Draws the source directly using the given painter. More...
 
 QGraphicsEffect (QGraphicsEffectPrivate &d, QObject *parent=0)
 
QRectF sourceBoundingRect (Qt::CoordinateSystem system=Qt::LogicalCoordinates) const
 Returns the bounding rectangle of the source mapped to the given system. More...
 
bool sourceIsPixmap () const
 Returns true if the source effectively is a pixmap, e.g., a QGraphicsPixmapItem. More...
 
QPixmap sourcePixmap (Qt::CoordinateSystem system=Qt::LogicalCoordinates, QPoint *offset=0, PixmapPadMode mode=PadToEffectiveBoundingRect) const
 Returns a pixmap with the source painted into it. More...
 
void updateBoundingRect ()
 This function notifies the effect framework when the effect's bounding rectangle has changed. More...
 
- Protected Functions inherited from QObject
virtual void childEvent (QChildEvent *)
 This event handler can be reimplemented in a subclass to receive child events. More...
 
virtual void connectNotify (const char *signal)
 This virtual function is called when something has been connected to signal in this object. More...
 
virtual void customEvent (QEvent *)
 This event handler can be reimplemented in a subclass to receive custom events. More...
 
virtual void disconnectNotify (const char *signal)
 This virtual function is called when something has been disconnected from signal in this object. More...
 
 QObject (QObjectPrivate &dd, QObject *parent=0)
 
int receivers (const char *signal) const
 Returns the number of receivers connected to the signal. More...
 
QObjectsender () const
 Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; otherwise it returns 0. More...
 
int senderSignalIndex () const
 
virtual void timerEvent (QTimerEvent *)
 This event handler can be reimplemented in a subclass to receive timer events for the object. More...
 

Private Functions

bool hideOriginal () const
 
void prepareBufferedDraw (QPainter *painter)
 
void updateRenderTargets ()
 

Additional Inherited Members

- Public Types inherited from QGraphicsEffect
enum  ChangeFlag { SourceAttached = 0x1, SourceDetached = 0x2, SourceBoundingRectChanged = 0x4, SourceInvalidated = 0x8 }
 This enum describes what has changed in QGraphicsEffectSource. More...
 
enum  PixmapPadMode { NoPad, PadToTransparentBorder, PadToEffectiveBoundingRect }
 This enum describes how the pixmap returned from sourcePixmap should be padded. More...
 
- Public Slots inherited from QGraphicsEffect
void setEnabled (bool enable)
 
void update ()
 Schedules a redraw of the effect. More...
 
- Public Slots inherited from QObject
void deleteLater ()
 Schedules this object for deletion. More...
 
- Signals inherited from QGraphicsEffect
void enabledChanged (bool enabled)
 This signal is emitted whenever the effect is enabled or disabled. More...
 
- Signals inherited from QObject
void destroyed (QObject *=0)
 This signal is emitted immediately before the object obj is destroyed, and can not be blocked. More...
 
- Static Public Functions inherited from QObject
static bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
 Creates a connection of the given type from the signal in the sender object to the method in the receiver object. More...
 
static bool connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type=Qt::AutoConnection)
 
static bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 Disconnects signal in object sender from method in object receiver. More...
 
static bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &member)
 
static uint registerUserData ()
 
static QString tr (const char *sourceText, const char *comment=0, int n=-1)
 
static QString trUtf8 (const char *sourceText, const char *comment=0, int n=-1)
 
- Static Public Variables inherited from QObject
static const QMetaObject staticMetaObject
 This variable stores the meta-object for the class. More...
 
- Protected Variables inherited from QObject
QScopedPointer< QObjectDatad_ptr
 
- Static Protected Variables inherited from QObject
static const QMetaObject staticQtMetaObject
 

Detailed Description

Definition at line 53 of file shadereffect.h.

Constructors and Destructors

◆ ShaderEffect()

ShaderEffect::ShaderEffect ( QObject parent = 0)

Definition at line 52 of file shadereffect.cpp.

53  : QGraphicsEffect(parent)
54  , m_changed(true)
55 {
56 }
QGraphicsEffect(QObject *parent=0)
Constructs a new QGraphicsEffect instance having the specified parent.

◆ ~ShaderEffect()

ShaderEffect::~ShaderEffect ( )

Definition at line 58 of file shadereffect.cpp.

59 {
60 }

Functions

◆ addRenderTarget()

void ShaderEffect::addRenderTarget ( ShaderEffectSource target)

Definition at line 169 of file shadereffect.cpp.

Referenced by ShaderEffectSource::attachSourceItem().

170 {
171  if (!m_renderTargets.contains(target))
172  m_renderTargets.append(target);
173 }
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
QVector< ShaderEffectSource * > m_renderTargets
Definition: shadereffect.h:73
bool contains(const T &t) const
Returns true if the vector contains an occurrence of value; otherwise returns false.
Definition: qvector.h:731

◆ draw()

void ShaderEffect::draw ( QPainter painter)
protectedvirtual

This pure virtual function draws the effect and is called whenever the source needs to be drawn.

Reimplement this function in a QGraphicsEffect subclass to provide the effect's drawing implementation, using painter.

For example:

MyGraphicsEffect::draw(QPainter *painter)
{
...
QPoint offset;
if (sourceIsPixmap()) {
// No point in drawing in device coordinates (pixmap will be scaled anyways).
const QPixmap pixmap = sourcePixmap(Qt::LogicalCoordinates, &offset);
...
painter->drawPixmap(offset, pixmap);
} else {
// Draw pixmap in device coordinates to avoid pixmap scaling;
const QPixmap pixmap = sourcePixmap(Qt::DeviceCoordinates, &offset);
...
painter->drawPixmap(offset, pixmap);
}
...
}

This function should not be called explicitly by the user, since it is meant for reimplementation purposes only.

Implements QGraphicsEffect.

Definition at line 79 of file shadereffect.cpp.

80 {
81  const QGLContext *context = QGLContext::currentContext();
82 
83  prepareBufferedDraw(painter);
84 
85  if (context) {
87  }
88 
89  if (!context || m_renderTargets.count() == 0 || !hideOriginal())
90  drawSource(painter);
91 }
bool hideOriginal() const
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
void drawSource(QPainter *painter)
Draws the source directly using the given painter.
static const QGLContext * currentContext()
Returns the current context, i.e.
Definition: qgl.cpp:3545
The QGLContext class encapsulates an OpenGL rendering context.
Definition: qgl.h:310
void prepareBufferedDraw(QPainter *painter)
void updateRenderTargets()
QVector< ShaderEffectSource * > m_renderTargets
Definition: shadereffect.h:73

◆ hideOriginal()

bool ShaderEffect::hideOriginal ( ) const
private

Definition at line 184 of file shadereffect.cpp.

Referenced by draw().

185 {
186  if (m_renderTargets.count() == 0)
187  return false;
188 
189  // Just like scenegraph version, if there is even one source that says "hide original" we hide it.
190  int count = m_renderTargets.count();
191  for (int i = 0; i < count; i++) {
192  if (m_renderTargets[i]->hideSource())
193  return true;
194  }
195  return false;
196 }
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QVector< ShaderEffectSource * > m_renderTargets
Definition: shadereffect.h:73

◆ prepareBufferedDraw()

void ShaderEffect::prepareBufferedDraw ( QPainter painter)
private

Definition at line 62 of file shadereffect.cpp.

Referenced by draw().

63 {
64 #ifndef QT_NO_DYNAMIC_CAST
65  // This workaround needed because QGraphicsEffect seems to always utilize default painters worldtransform
66  // instead of the active painters worldtransform.
67  const ShaderEffectBuffer *effectBuffer = dynamic_cast<ShaderEffectBuffer*> (painter->device());
68  if (effectBuffer) {
71  } else {
73  }
74 #else
75  Q_UNUSED(painter);
76 #endif
77 }
QPaintDevice * device() const
Returns the paint device on which this painter is currently painting, or 0 if the painter is not acti...
Definition: qpainter.cpp:1530
static QTransform savedWorldTransform
const QTransform & worldTransform() const
Returns the world transformation matrix.
Definition: qpainter.cpp:9652
void setWorldTransform(const QTransform &matrix, bool combine=false)
Sets the world transformation matrix.
Definition: qpainter.cpp:9630
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ removeRenderTarget()

void ShaderEffect::removeRenderTarget ( ShaderEffectSource target)

Definition at line 175 of file shadereffect.cpp.

Referenced by ShaderEffectSource::detachSourceItem().

176 {
177  int index = m_renderTargets.indexOf(target);
178  if (index >= 0)
179  m_renderTargets.remove(index);
180  else
181  qWarning() << "ShaderEffect::removeRenderTarget - did not find target.";
182 }
void remove(int i)
Removes the element at index position i.
Definition: qvector.h:374
Q_CORE_EXPORT void qWarning(const char *,...)
int indexOf(const T &t, int from=0) const
Returns the index position of the first occurrence of value in the vector, searching forward from ind...
Definition: qvector.h:698
QVector< ShaderEffectSource * > m_renderTargets
Definition: shadereffect.h:73
quint16 index

◆ sourceChanged()

void ShaderEffect::sourceChanged ( ChangeFlags  flags)
protectedvirtual

This virtual function is called by QGraphicsEffect to notify the effect that the source has changed.

If the effect applies any cache, then this cache must be purged in order to reflect the new appearance of the source.

The flags describes what has changed.

Reimplemented from QGraphicsEffect.

Definition at line 163 of file shadereffect.cpp.

164 {
165  Q_UNUSED(flags);
166  m_changed = true;
167 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ updateRenderTargets()

void ShaderEffect::updateRenderTargets ( )
private

Definition at line 93 of file shadereffect.cpp.

Referenced by draw().

94 {
95  if (!m_changed)
96  return;
97 
98  m_changed = false;
99 
100  int count = m_renderTargets.count();
101  for (int i = 0; i < count; i++) {
102  if (m_renderTargets[i]->isLive() || m_renderTargets[i]->isDirtyTexture()) {
103  m_renderTargets[i]->updateBackbuffer();
104  ShaderEffectBuffer* target = m_renderTargets[i]->fbo();
105  if (target && target->isValid() && target->width() > 0 && target->height() > 0) {
106  QPainter p(target);
107  p.setCompositionMode(QPainter::CompositionMode_Clear);
108  p.fillRect(QRect(QPoint(0, 0), target->size()), Qt::transparent);
109  p.setCompositionMode(QPainter::CompositionMode_SourceOver);
110 
111  QRectF sourceRect = m_renderTargets[i]->sourceRect();
112  QSize textureSize = m_renderTargets[i]->textureSize();
113 
114  qreal yflip = m_renderTargets[i]->isMirrored() ? -1.0 : 1.0; // flip y to match scenegraph, it also flips texturecoordinates
115  qreal xscale = 1.0;
116  qreal yscale = 1.0 * yflip;
117 
118  qreal leftMargin = 0.0;
119  qreal rightMargin = 0.0;
120  qreal topMargin = 0.0;
121  qreal bottomMargin = 0.0;
122 
123  qreal width = m_renderTargets[i]->sourceItem()->width();
124  qreal height = m_renderTargets[i]->sourceItem()->height();
125 
126  if (!sourceRect.isEmpty()) {
127  leftMargin = -sourceRect.left();
128  rightMargin = sourceRect.right() - width;
129  topMargin = -sourceRect.top();
130  bottomMargin = sourceRect.bottom() - height;
131  }
132 
133  if ((width + leftMargin + rightMargin) > 0 && (height + topMargin + bottomMargin) > 0) {
134  if (!textureSize.isEmpty()) {
135  qreal textureWidth = textureSize.width();
136  qreal textureHeight = textureSize.height();
137 
138  xscale = width / (width + leftMargin + rightMargin);
139  yscale = height / (height + topMargin + bottomMargin);
140 
141  p.translate(textureWidth / 2, textureHeight / 2);
142  p.scale(xscale, yscale * yflip);
143  p.translate(-textureWidth / 2, -textureHeight / 2);
144  p.scale(textureWidth / width, textureHeight / height);
145  } else {
146  xscale = width / (width + leftMargin + rightMargin);
147  yscale = height / (height + topMargin + bottomMargin);
148 
149  p.translate(width / 2, height / 2);
150  p.scale(xscale, yscale * yflip);
151  p.translate(-width / 2, -height / 2);
152  }
153  }
154 
155  drawSource(&p);
156  p.end();
157  m_renderTargets[i]->markSceneGraphDirty();
158  }
159  }
160  }
161 }
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
QSize size() const
Returns the size of the texture attached to this framebuffer object.
qreal right() const
Returns the x-coordinate of the rectangle&#39;s right edge.
Definition: qrect.h:527
double qreal
Definition: qglobal.h:1193
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
bool isValid() const
Returns true if the framebuffer object is valid.
int height() const
Definition: qpaintdevice.h:92
qreal left() const
Returns the x-coordinate of the rectangle&#39;s left edge.
Definition: qrect.h:525
void drawSource(QPainter *painter)
Draws the source directly using the given painter.
int width() const
Returns the width.
Definition: qsize.h:126
int width() const
Definition: qpaintdevice.h:91
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QVector< ShaderEffectSource * > m_renderTargets
Definition: shadereffect.h:73
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
int height() const
Returns the height.
Definition: qsize.h:129
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58
qreal top() const
Returns the y-coordinate of the rectangle&#39;s top edge.
Definition: qrect.h:526
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
qreal bottom() const
Returns the y-coordinate of the rectangle&#39;s bottom edge.
Definition: qrect.h:528
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
bool isEmpty() const
Returns true if the rectangle is empty, otherwise returns false.
Definition: qrect.h:658

Properties

◆ m_changed

bool ShaderEffect::m_changed

◆ m_renderTargets

QVector<ShaderEffectSource*> ShaderEffect::m_renderTargets

The documentation for this class was generated from the following files: