From 1af063d9aaff5d0e5e8715202baeced55bbbb0bc Mon Sep 17 00:00:00 2001 From: Warwick Allison Date: Fri, 31 Jul 2009 15:10:39 +1000 Subject: tile/preservseAspect are now fillMode as per API review --- doc/src/images/declarative-image_fillMode.gif | Bin 0 -> 42648 bytes doc/src/images/declarative-qtlogo3.png | Bin 5848 -> 4783 bytes doc/src/images/declarative-qtlogo5.png | Bin 0 -> 3553 bytes doc/src/images/declarative-qtlogo6.png | Bin 0 -> 4763 bytes .../aspectratio/scale_to_fit_simple.qml | 2 +- examples/declarative/fillmode/face.png | Bin 0 -> 905 bytes examples/declarative/fillmode/fillmode.qml | 39 ++++ examples/declarative/minehunt/minehunt.qml | 2 +- examples/declarative/snow/ImageBatch.qml | 2 +- src/declarative/fx/qfximage.cpp | 207 ++++++++++++--------- src/declarative/fx/qfximage.h | 14 +- src/declarative/fx/qfximage_p.h | 8 +- 12 files changed, 176 insertions(+), 98 deletions(-) create mode 100644 doc/src/images/declarative-image_fillMode.gif create mode 100644 doc/src/images/declarative-qtlogo5.png create mode 100644 doc/src/images/declarative-qtlogo6.png create mode 100644 examples/declarative/fillmode/face.png create mode 100644 examples/declarative/fillmode/fillmode.qml diff --git a/doc/src/images/declarative-image_fillMode.gif b/doc/src/images/declarative-image_fillMode.gif new file mode 100644 index 0000000..c81b4d7 Binary files /dev/null and b/doc/src/images/declarative-image_fillMode.gif differ diff --git a/doc/src/images/declarative-qtlogo3.png b/doc/src/images/declarative-qtlogo3.png index 3f2f93f..d516524 100644 Binary files a/doc/src/images/declarative-qtlogo3.png and b/doc/src/images/declarative-qtlogo3.png differ diff --git a/doc/src/images/declarative-qtlogo5.png b/doc/src/images/declarative-qtlogo5.png new file mode 100644 index 0000000..b7b3513 Binary files /dev/null and b/doc/src/images/declarative-qtlogo5.png differ diff --git a/doc/src/images/declarative-qtlogo6.png b/doc/src/images/declarative-qtlogo6.png new file mode 100644 index 0000000..07a078f Binary files /dev/null and b/doc/src/images/declarative-qtlogo6.png differ diff --git a/examples/declarative/aspectratio/scale_to_fit_simple.qml b/examples/declarative/aspectratio/scale_to_fit_simple.qml index f9c0e04..5381bb4 100644 --- a/examples/declarative/aspectratio/scale_to_fit_simple.qml +++ b/examples/declarative/aspectratio/scale_to_fit_simple.qml @@ -13,7 +13,7 @@ Rect { Image { id: Image source: "pics/face.png" - preserveAspect: true + fillMode: "PreserveAspect" anchors.fill: parent } } diff --git a/examples/declarative/fillmode/face.png b/examples/declarative/fillmode/face.png new file mode 100644 index 0000000..9623b1a Binary files /dev/null and b/examples/declarative/fillmode/face.png differ diff --git a/examples/declarative/fillmode/fillmode.qml b/examples/declarative/fillmode/fillmode.qml new file mode 100644 index 0000000..473f553 --- /dev/null +++ b/examples/declarative/fillmode/fillmode.qml @@ -0,0 +1,39 @@ +import Qt 4.6 + +Image { + width: 400 + height: 250 + source: "face.png" + fillMode: SequentialAnimation { + running: true + repeat: true + SetPropertyAction { value: "Stretch" } + SetPropertyAction { target: Label; property: "text"; value: "Stretch" } + PauseAnimation { duration: 1000 } + SetPropertyAction { value: "PreserveAspect" } + SetPropertyAction { target: Label; property: "text"; value: "PreserveAspect" } + PauseAnimation { duration: 1000 } + SetPropertyAction { value: "Tile" } + SetPropertyAction { target: Label; property: "text"; value: "Tile" } + PauseAnimation { duration: 1000 } + SetPropertyAction { value: "TileHorizontally" } + SetPropertyAction { target: Label; property: "text"; value: "TileHorizontally" } + PauseAnimation { duration: 1000 } + SetPropertyAction { value: "TileVertically" } + SetPropertyAction { target: Label; property: "text"; value: "TileVertically" } + PauseAnimation { duration: 1000 } + } + Text { + id: Label + font.size: 24 + color: "blue" + style: "Outline" + styleColor: "white" + anchors { centeredIn: parent } + } + Rect { + pen.color: "black" + color: "transparent" + anchors { fill: parent; rightMargin: 1; bottomMargin: 1} + } +} diff --git a/examples/declarative/minehunt/minehunt.qml b/examples/declarative/minehunt/minehunt.qml index e1f48ef..6b45b79 100644 --- a/examples/declarative/minehunt/minehunt.qml +++ b/examples/declarative/minehunt/minehunt.qml @@ -119,7 +119,7 @@ Item { ] Image { source: "pics/No-Ones-Laughing-3.jpg" - tile: true + fillMode: "Tile" } Description { text: "Use the 'minehunt' executable to run this demo!" diff --git a/examples/declarative/snow/ImageBatch.qml b/examples/declarative/snow/ImageBatch.qml index 77bacab..bac4328 100644 --- a/examples/declarative/snow/ImageBatch.qml +++ b/examples/declarative/snow/ImageBatch.qml @@ -41,7 +41,7 @@ GridView { transformOrigin: "Center" width: MyGrid.imageWidth; height: MyGrid.imageHeight; - Image { id: Image; source: url; preserveAspect: true; smooth: true; anchors.fill: parent; + Image { id: Image; source: url; fillMode: "PreserveAspect"; smooth: true; anchors.fill: parent; opacity: (status == 0)?1:0; opacity: Behavior { NumberAnimation { properties: "opacity" } } } Loading { anchors.centeredIn: parent; visible: Image.status } diff --git a/src/declarative/fx/qfximage.cpp b/src/declarative/fx/qfximage.cpp index f1be4a4..7446caa 100644 --- a/src/declarative/fx/qfximage.cpp +++ b/src/declarative/fx/qfximage.cpp @@ -60,7 +60,10 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Image,QFxImage) \brief The Image element allows you to add bitmaps to a scene. \inherits Item - The Image element supports untransformed, stretched, grid-scaled and tiled images. + The Image element supports untransformed, stretched, tiled, and grid-scaled images. + + For an explanation of stretching and tiling, see the fillMode property description. + For an explanation of grid-scaling see the scaleGrid property description or the QFxScaleGrid class description. @@ -74,23 +77,53 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Image,QFxImage) \endqml \row \o \image declarative-qtlogo2.png - \o Stretched + \o fillMode: Stretch (default) \qml - Image { width: 160; height: 160; source: "pics/qtlogo.png" } + Image { + width: 160 + height: 160 + source: "pics/qtlogo.png" + } \endqml \row \o \image declarative-qtlogo4.png - \o Grid-scaled + \o Grid-scaled (only with fillMode: Stretch) \qml Image { scaleGrid.left: 20; scaleGrid.right: 10 scaleGrid.top: 14; scaleGrid.bottom: 14 - width: 160; height: 160; source: "pics/qtlogo.png" } + width: 160; height: 160 + source: "pics/qtlogo.png" + } \endqml \row \o \image declarative-qtlogo3.png - \o Tiled + \o fillMode: Tile + \qml + Image { + fillMode: "Tile" + width: 160; height: 160 + source: "pics/qtlogo.png" + } + \endqml + \row + \o \image declarative-qtlogo6.png + \o fillMode: TileVertically + \qml + Image { + fillMode: "TileVertically" + width: 160; height: 160 + source: "pics/qtlogo.png" + } + \endqml + \row + \o \image declarative-qtlogo5.png + \o fillMode: TileHorizontally \qml - Image { tile: true; width: 160; height: 160; source: "pics/qtlogo.png" } + Image { + fillMode: "TileHorizontally" + width: 160; height: 160 + source: "pics/qtlogo.png" + } \endqml \endtable */ @@ -193,33 +226,40 @@ QFxScaleGrid *QFxImage::scaleGrid() } /*! - \qmlproperty bool Image::tile + \qmlproperty FillMode Image::fillMode - Set this property to enable image tiling. Normally the Image element scales the - bitmap file to its size. If tiling is enabled, the bitmap is repeated as a set - of unscaled tiles, clipped to the size of the Image. + Set this property to define what happens when the image set for the item is smaller + than the size of the item. - \qml - Item { - Image { source: "tile.png" } - Image { x: 80; width: 100; height: 100; source: "tile.png" } - Image { x: 190; width: 100; height: 100; tile: true; source: "tile.png" } - } - \endqml - \image declarative-image_tile.png + \list + \o Stretch - the image is scaled to fit + \o PreserveAspect - the image is scaled uniformly to fit + \o Tile - the image is duplicated horizontally and vertically + \o TileVertically - the image is stretched horizontally and tiled vertically + \o TileHorizontally - the image is stretched vertically and tiled horizontally + \endlist + + \image declarative-image_fillMode.gif - If both tiling and the scaleGrid are set, tiling takes precedence. + Only fillMode: Stretch can be used with scaleGrid. Other settings + will cause a run-time warning. + + \sa examples/declarative/aspectratio */ -bool QFxImage::isTiled() const +QFxImage::FillMode QFxImage::fillMode() const { Q_D(const QFxImage); - return d->tiled; + return d->fillMode; } -void QFxImage::setTiled(bool tile) +void QFxImage::setFillMode(FillMode mode) { Q_D(QFxImage); - d->tiled = tile; + if (d->fillMode == mode) + return; + d->fillMode = mode; + update(); + emit fillModeChanged(); } void QFxImage::componentComplete() @@ -283,51 +323,72 @@ void QFxImage::paintContents(QPainter &p) QPixmap pix = d->pix; - if (d->tiled) { - p.save(); - p.setClipRect(0, 0, width(), height(), Qt::IntersectClip); - QRect me = QRect(0, 0, width(), height()); - - int pw = pix.width(); - int ph = pix.height(); - int yy = 0; - - while(yy < height()) { - int xx = 0; - while(xx < width()) { - p.drawPixmap(xx, yy, pix); - xx += pw; - } - yy += ph; - } - - p.restore(); - } else if (!d->scaleGrid || d->scaleGrid->isNull()) { + if (!d->scaleGrid || d->scaleGrid->isNull()) { if (width() != pix.width() || height() != pix.height()) { - qreal widthScale = width() / qreal(pix.width()); - qreal heightScale = height() / qreal(pix.height()); - - QTransform scale; - - if (d->preserveAspect) { - if (widthScale < heightScale) { - heightScale = widthScale; - scale.translate(0, (height() - heightScale * pix.height()) / 2); - } else if(heightScale < widthScale) { - widthScale = heightScale; - scale.translate((width() - widthScale * pix.width()) / 2, 0); + if (d->fillMode >= Tile) { + p.save(); + p.setClipRect(0, 0, width(), height(), Qt::IntersectClip); + + if (d->fillMode == Tile) { + const int pw = pix.width(); + const int ph = pix.height(); + int yy = 0; + + while(yy < height()) { + int xx = 0; + while(xx < width()) { + p.drawPixmap(xx, yy, pix); + xx += pw; + } + yy += ph; + } + } else if (d->fillMode == TileVertically) { + const int ph = pix.height(); + int yy = 0; + + while(yy < height()) { + p.drawPixmap(QRect(0, yy, width(), ph), pix); + yy += ph; + } + } else { + const int pw = pix.width(); + int xx = 0; + + while(xx < width()) { + p.drawPixmap(QRect(xx, 0, pw, height()), pix); + xx += pw; + } } - } - scale.scale(widthScale, heightScale); - QTransform old = p.transform(); - p.setWorldTransform(scale * old); - p.drawPixmap(0, 0, pix); - p.setWorldTransform(old); + p.restore(); + } else { + qreal widthScale = width() / qreal(pix.width()); + qreal heightScale = height() / qreal(pix.height()); + + QTransform scale; + + if (d->fillMode == PreserveAspect) { + if (widthScale < heightScale) { + heightScale = widthScale; + scale.translate(0, (height() - heightScale * pix.height()) / 2); + } else if(heightScale < widthScale) { + widthScale = heightScale; + scale.translate((width() - widthScale * pix.width()) / 2, 0); + } + } + + scale.scale(widthScale, heightScale); + QTransform old = p.transform(); + p.setWorldTransform(scale * old); + p.drawPixmap(0, 0, pix); + p.setWorldTransform(old); + } } else { p.drawPixmap(0, 0, pix); } } else { + if (d->fillMode != Stretch) + qWarning("Only fillmode:Stretch supported for scale grid images"); int sgl = d->scaleGrid->left(); int sgr = d->scaleGrid->right(); int sgt = d->scaleGrid->top(); @@ -458,28 +519,6 @@ QUrl QFxImage::source() const return d->url; } -/*! - \qmlproperty bool Image::preserveAspect - - Whether the image's aspect ratio should be preserved when resizing. By default this - is false. -*/ -bool QFxImage::preserveAspect() const -{ - Q_D(const QFxImage); - return d->preserveAspect; -} - -void QFxImage::setPreserveAspect(bool p) -{ - Q_D(QFxImage); - - if (p == d->preserveAspect) - return; - d->preserveAspect = p; - update(); -} - void QFxImage::setSource(const QUrl &url) { #ifdef Q_ENABLE_PERFORMANCE_LOG diff --git a/src/declarative/fx/qfximage.h b/src/declarative/fx/qfximage.h index 4b6e700..ce69ddc 100644 --- a/src/declarative/fx/qfximage.h +++ b/src/declarative/fx/qfximage.h @@ -57,24 +57,26 @@ class Q_DECLARATIVE_EXPORT QFxImage : public QFxItem { Q_OBJECT Q_ENUMS(Status) + Q_ENUMS(FillMode) Q_PROPERTY(Status status READ status NOTIFY statusChanged) Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) Q_PROPERTY(qreal progress READ progress NOTIFY progressChanged) Q_PROPERTY(QFxScaleGrid *scaleGrid READ scaleGrid) - Q_PROPERTY(bool tile READ isTiled WRITE setTiled) Q_PROPERTY(QPixmap pixmap READ pixmap WRITE setPixmap DESIGNABLE false) Q_PROPERTY(bool smooth READ smoothTransform WRITE setSmoothTransform) - Q_PROPERTY(bool preserveAspect READ preserveAspect WRITE setPreserveAspect); + Q_PROPERTY(FillMode fillMode READ fillMode WRITE setFillMode NOTIFY fillModeChanged); + public: QFxImage(QFxItem *parent=0); ~QFxImage(); QFxScaleGrid *scaleGrid(); - bool isTiled() const; - void setTiled(bool tile); + enum FillMode { Stretch, PreserveAspect, Tile, TileVertically, TileHorizontally }; + FillMode fillMode() const; + void setFillMode(FillMode); QPixmap pixmap() const; void setPixmap(const QPixmap &); @@ -86,9 +88,6 @@ public: Status status() const; qreal progress() const; - bool preserveAspect() const; - void setPreserveAspect(bool); - QUrl source() const; virtual void setSource(const QUrl &url); @@ -98,6 +97,7 @@ Q_SIGNALS: void sourceChanged(const QUrl &); void statusChanged(Status); void progressChanged(qreal progress); + void fillModeChanged(); protected: QFxImage(QFxImagePrivate &dd, QFxItem *parent); diff --git a/src/declarative/fx/qfximage_p.h b/src/declarative/fx/qfximage_p.h index 7792dbf..a9ad054 100644 --- a/src/declarative/fx/qfximage_p.h +++ b/src/declarative/fx/qfximage_p.h @@ -70,8 +70,9 @@ class QFxImagePrivate : public QFxItemPrivate public: QFxImagePrivate() - : scaleGrid(0), tiled(false), smooth(false), opaque(false), - preserveAspect(false), status(QFxImage::Idle), sciReply(0), + : scaleGrid(0), smooth(false), opaque(false), + fillMode(QFxImage::Stretch), + status(QFxImage::Idle), sciReply(0), progress(0.0) { } @@ -92,11 +93,10 @@ public: QFxScaleGrid *scaleGrid; QPixmap pix; - bool tiled : 1; bool smooth : 1; bool opaque : 1; - bool preserveAspect : 1; + QFxImage::FillMode fillMode; QFxImage::Status status; QUrl url; QUrl sciurl; -- cgit v0.12