diff options
author | Kim Motoyoshi Kalland <kim.kalland@nokia.com> | 2009-09-29 15:08:41 (GMT) |
---|---|---|
committer | Kim Motoyoshi Kalland <kim.kalland@nokia.com> | 2009-10-02 15:57:17 (GMT) |
commit | 385176ad28b3a79bcd196d2d529c4bf7abd4fcc0 (patch) | |
tree | e50e39712472bc672308dac1770261587b3f98b6 /src/gui/painting | |
parent | 4f7d94ca73556f3a40631ad07f565995f6f85176 (diff) | |
download | Qt-385176ad28b3a79bcd196d2d529c4bf7abd4fcc0.zip Qt-385176ad28b3a79bcd196d2d529c4bf7abd4fcc0.tar.gz Qt-385176ad28b3a79bcd196d2d529c4bf7abd4fcc0.tar.bz2 |
Added support for drawing a pixmap multiple times in one call.
This is internal API.
It's possible to specify a horizontal and vertical scale, rotation,
opacity and source rectangle for each pixmap item.
Useful for particle effects.
Reviewed-by: Trond
Diffstat (limited to 'src/gui/painting')
-rw-r--r-- | src/gui/painting/qdrawutil.cpp | 55 | ||||
-rw-r--r-- | src/gui/painting/qdrawutil.h | 12 | ||||
-rw-r--r-- | src/gui/painting/qpaintengineex.cpp | 25 | ||||
-rw-r--r-- | src/gui/painting/qpaintengineex_p.h | 6 |
4 files changed, 95 insertions, 3 deletions
diff --git a/src/gui/painting/qdrawutil.cpp b/src/gui/painting/qdrawutil.cpp index ac3796a..4f17e59 100644 --- a/src/gui/painting/qdrawutil.cpp +++ b/src/gui/painting/qdrawutil.cpp @@ -45,6 +45,7 @@ #include "qapplication.h" #include "qpainter.h" #include "qpalette.h" +#include <private/qpaintengineex_p.h> QT_BEGIN_NAMESPACE @@ -1351,4 +1352,58 @@ void qDrawBorderPixmap(QPainter *painter, const QRect &targetRect, const QMargin } } +/*! + \struct QDrawPixmapsData + \since 4.6 + \internal + + This structure is used with the qDrawPixmaps() function. + + QPointF point: Specifies the center of the target rectangle. + QRectF source: Specifies the source rectangle in the pixmap passed into the qDrawPixmaps() call. + qreal scaleX: Specifies the horizontal scale of the target rectangle. + qreal scaleY: Specifies the vertical scale of the target rectangle. + qreal rotation: Specifies the rotation of the target rectangle in degrees. + The target rectangle is rotated after scaling. + qreal opacity: Specifies the opacity of the rectangle. +*/ + +/*! + \internal + \since 4.6 + + This function is used to draw \a pixmap, or a sub-rectangle of \a pixmap, at multiple positions + with different scale, rotation and opacity on \a painter. \a drawingData is an array of \a + dataCount elements specifying the parameters used to draw each pixmap instance. + This can be used for example to implement a particle system. +*/ +void qDrawPixmaps(QPainter *painter, const QDrawPixmapsData *drawingData, int dataCount, const QPixmap &pixmap) +{ + QPaintEngine *engine = painter->paintEngine(); + if (!engine) + return; + + if (engine->isExtended()) { + static_cast<QPaintEngineEx *>(engine)->drawPixmaps(drawingData, dataCount, pixmap); + } else { + qreal oldOpacity = painter->opacity(); + QTransform oldTransform = painter->transform(); + + for (int i = 0; i < dataCount; ++i) { + QTransform transform = oldTransform; + transform.translate(drawingData[i].point.x(), drawingData[i].point.y()); + transform.rotate(drawingData[i].rotation); + painter->setOpacity(oldOpacity * drawingData[i].opacity); + painter->setTransform(transform); + + qreal w = drawingData[i].scaleX * drawingData[i].source.width(); + qreal h = drawingData[i].scaleY * drawingData[i].source.height(); + painter->drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, drawingData[i].source); + } + + painter->setOpacity(oldOpacity); + painter->setTransform(oldTransform); + } +} + QT_END_NAMESPACE diff --git a/src/gui/painting/qdrawutil.h b/src/gui/painting/qdrawutil.h index 22a57e9..98a36a7 100644 --- a/src/gui/painting/qdrawutil.h +++ b/src/gui/painting/qdrawutil.h @@ -158,6 +158,18 @@ inline void qDrawBorderPixmap(QPainter *painter, qDrawBorderPixmap(painter, target, margins, pixmap, pixmap.rect(), margins); } +struct QDrawPixmapsData +{ + QPointF point; + QRectF source; + qreal scaleX; + qreal scaleY; + qreal rotation; + qreal opacity; +}; + +Q_GUI_EXPORT void qDrawPixmaps(QPainter *painter, const QDrawPixmapsData *drawingData, int dataCount, const QPixmap &pixmap); + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/gui/painting/qpaintengineex.cpp b/src/gui/painting/qpaintengineex.cpp index 60e4df6..2342b9d 100644 --- a/src/gui/painting/qpaintengineex.cpp +++ b/src/gui/painting/qpaintengineex.cpp @@ -932,6 +932,31 @@ void QPaintEngineEx::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, con fill(path, brush); } +void QPaintEngineEx::drawPixmaps(const QDrawPixmapsData *drawingData, int dataCount, const QPixmap &pixmap) +{ + qreal oldOpacity = state()->opacity; + QTransform oldTransform = state()->matrix; + + for (int i = 0; i < dataCount; ++i) { + QTransform transform = oldTransform; + transform.translate(drawingData[i].point.x(), drawingData[i].point.y()); + transform.rotate(drawingData[i].rotation); + state()->opacity = oldOpacity * drawingData[i].opacity; + state()->matrix = transform; + opacityChanged(); + transformChanged(); + + qreal w = drawingData[i].scaleX * drawingData[i].source.width(); + qreal h = drawingData[i].scaleY * drawingData[i].source.height(); + drawPixmap(QRectF(-0.5 * w, -0.5 * h, w, h), pixmap, drawingData[i].source); + } + + state()->opacity = oldOpacity; + state()->matrix = oldTransform; + opacityChanged(); + transformChanged(); +} + void QPaintEngineEx::setState(QPainterState *s) { QPaintEngine::state = s; diff --git a/src/gui/painting/qpaintengineex_p.h b/src/gui/painting/qpaintengineex_p.h index 814a0f0..42c1ce9 100644 --- a/src/gui/painting/qpaintengineex_p.h +++ b/src/gui/painting/qpaintengineex_p.h @@ -70,7 +70,7 @@ QT_MODULE(Gui) class QPainterState; class QPaintEngineExPrivate; struct StrokeHandler; - +struct QDrawPixmapsData; struct QIntRect { int x1, y1, x2, y2; @@ -133,8 +133,6 @@ public: qreal pts[8]; }; - - #ifndef QT_NO_DEBUG_STREAM QDebug Q_GUI_EXPORT &operator<<(QDebug &, const QVectorPath &path); #endif @@ -198,6 +196,8 @@ public: virtual void drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s); + virtual void drawPixmaps(const QDrawPixmapsData *drawingData, int dataCount, const QPixmap &pixmap); + virtual void updateState(const QPaintEngineState &state); virtual void setState(QPainterState *s); |