1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativeimage_p.h"
43 #include "QtQuick1/private/qdeclarativeimage_p_p.h"
54 \qmlclass Image QDeclarative1Image
55 \inqmlmodule QtQuick 1
57 \ingroup qml-basic-visual-elements
58 \brief The Image element displays an image in a declarative user interface
61 The Image element is used to display images in a declarative user interface.
63 The source of the image is specified as a URL using the \l source property.
64 Images can be supplied in any of the standard image formats supported by Qt,
65 including bitmap formats such as PNG and JPEG, and vector graphics formats
66 such as SVG. If you need to display animated images, use the \l AnimatedImage
69 If the \l{Item::width}{width} and \l{Item::height}{height} properties are not
70 specified, the Image element automatically uses the size of the loaded image.
71 By default, specifying the width and height of the element causes the image
72 to be scaled to that size. This behavior can be changed by setting the
73 \l fillMode property, allowing the image to be stretched and tiled instead.
75 \section1 Example Usage
77 The following example shows the simplest usage of the Image element.
79 \snippet doc/src/snippets/qtquick1/image.qml document
82 \image declarative-qtlogo.png
89 By default, locally available images are loaded immediately, and the user interface
90 is blocked until loading is complete. If a large image is to be loaded, it may be
91 preferable to load the image in a low priority thread, by enabling the \l asynchronous
94 If the image is obtained from a network rather than a local resource, it is
95 automatically loaded asynchronously, and the \l progress and \l status properties
96 are updated as appropriate.
98 Images are cached and shared internally, so if several Image elements have the same \l source,
99 only one copy of the image will be loaded.
101 \bold Note: Images are often the greatest user of memory in QML user interfaces. It is recommended
102 that images which do not form part of the user interface have their
103 size bounded via the \l sourceSize property. This is especially important for content
104 that is loaded from external sources or provided by the user.
106 \sa {declarative/imageelements/image}{Image example}, QDeclarativeImageProvider
109 QDeclarative1Image::QDeclarative1Image(QDeclarativeItem *parent)
110 : QDeclarative1ImageBase(*(new QDeclarative1ImagePrivate), parent)
114 QDeclarative1Image::QDeclarative1Image(QDeclarative1ImagePrivate &dd, QDeclarativeItem *parent)
115 : QDeclarative1ImageBase(dd, parent)
119 QDeclarative1Image::~QDeclarative1Image()
123 QPixmap QDeclarative1Image::pixmap() const
125 Q_D(const QDeclarative1Image);
126 return d->pix.pixmap();
129 void QDeclarative1Image::setPixmap(const QPixmap &pix)
131 Q_D(QDeclarative1Image);
132 if (!d->url.isEmpty())
137 void QDeclarative1ImagePrivate::setPixmap(const QPixmap &pixmap)
139 Q_Q(QDeclarative1Image);
140 pix.setPixmap(pixmap);
143 status = pix.isNull() ? QDeclarative1ImageBase::Null : QDeclarative1ImageBase::Ready;
149 \qmlproperty enumeration QtQuick1::Image::fillMode
151 Set this property to define what happens when the source image has a different size
155 \o Image.Stretch - the image is scaled to fit
156 \o Image.PreserveAspectFit - the image is scaled uniformly to fit without cropping
157 \o Image.PreserveAspectCrop - the image is scaled uniformly to fill, cropping if necessary
158 \o Image.Tile - the image is duplicated horizontally and vertically
159 \o Image.TileVertically - the image is stretched horizontally and tiled vertically
160 \o Image.TileHorizontally - the image is stretched vertically and tiled horizontally
166 \o \image declarative-qtlogo-stretch.png
170 width: 130; height: 100
177 \o \image declarative-qtlogo-preserveaspectfit.png
181 width: 130; height: 100
182 fillMode: Image.PreserveAspectFit
189 \o \image declarative-qtlogo-preserveaspectcrop.png
190 \o PreserveAspectCrop
193 width: 130; height: 100
194 fillMode: Image.PreserveAspectCrop
202 \o \image declarative-qtlogo-tile.png
206 width: 120; height: 120
213 \o \image declarative-qtlogo-tilevertically.png
217 width: 120; height: 120
218 fillMode: Image.TileVertically
225 \o \image declarative-qtlogo-tilehorizontally.png
229 width: 120; height: 120
230 fillMode: Image.TileHorizontally
238 Note that \c clip is \c false by default which means that the element might
239 paint outside its bounding rectangle even if the fillMode is set to \c PreserveAspectCrop.
241 \sa {declarative/imageelements/image}{Image example}
243 QDeclarative1Image::FillMode QDeclarative1Image::fillMode() const
245 Q_D(const QDeclarative1Image);
249 void QDeclarative1Image::setFillMode(FillMode mode)
251 Q_D(QDeclarative1Image);
252 if (d->fillMode == mode)
256 updatePaintedGeometry();
257 emit fillModeChanged();
262 \qmlproperty real QtQuick1::Image::paintedWidth
263 \qmlproperty real QtQuick1::Image::paintedHeight
265 These properties hold the size of the image that is actually painted.
266 In most cases it is the same as \c width and \c height, but when using a
267 \c fillMode \c PreserveAspectFit or \c fillMode \c PreserveAspectCrop
268 \c paintedWidth or \c paintedHeight can be smaller or larger than
269 \c width and \c height of the Image element.
271 qreal QDeclarative1Image::paintedWidth() const
273 Q_D(const QDeclarative1Image);
274 return d->paintedWidth;
277 qreal QDeclarative1Image::paintedHeight() const
279 Q_D(const QDeclarative1Image);
280 return d->paintedHeight;
284 \qmlproperty enumeration QtQuick1::Image::status
286 This property holds the status of image loading. It can be one of:
288 \o Image.Null - no image has been set
289 \o Image.Ready - the image has been loaded
290 \o Image.Loading - the image is currently being loaded
291 \o Image.Error - an error occurred while loading the image
294 Use this status to provide an update or respond to the status change in some way.
295 For example, you could:
298 \o Trigger a state change:
300 State { name: 'loaded'; when: image.status == Image.Ready }
303 \o Implement an \c onStatusChanged signal handler:
307 onStatusChanged: if (image.status == Image.Ready) console.log('Loaded')
311 \o Bind to the status value:
313 Text { text: image.status == Image.Ready ? 'Loaded' : 'Not loaded' }
321 \qmlproperty real QtQuick1::Image::progress
323 This property holds the progress of image loading, from 0.0 (nothing loaded)
330 \qmlproperty bool QtQuick1::Image::smooth
332 Set this property if you want the image to be smoothly filtered when scaled or
333 transformed. Smooth filtering gives better visual quality, but is slower. If
334 the image is displayed at its natural size, this property has no visual or
337 \note Generally scaling artifacts are only visible if the image is stationary on
338 the screen. A common pattern when animating an image is to disable smooth
339 filtering at the beginning of the animation and reenable it at the conclusion.
343 \qmlproperty QSize QtQuick1::Image::sourceSize
345 This property holds the actual width and height of the loaded image.
347 Unlike the \l {Item::}{width} and \l {Item::}{height} properties, which scale
348 the painting of the image, this property sets the actual number of pixels
349 stored for the loaded image so that large images do not use more
350 memory than necessary. For example, this ensures the image in memory is no
351 larger than 1024x1024 pixels, regardless of the Image's \l {Item::}{width} and
352 \l {Item::}{height} values:
361 source: "reallyBigImage.jpg"
362 sourceSize.width: 1024
363 sourceSize.height: 1024
368 If the image's actual size is larger than the sourceSize, the image is scaled down.
369 If only one dimension of the size is set to greater than 0, the
370 other dimension is set in proportion to preserve the source image's aspect ratio.
371 (The \l fillMode is independent of this.)
373 If the source is an instrinsically scalable image (eg. SVG), this property
374 determines the size of the loaded image regardless of intrinsic size.
375 Avoid changing this property dynamically; rendering an SVG is \e slow compared
378 If the source is a non-scalable image (eg. JPEG), the loaded image will
379 be no greater than this property specifies. For some formats (currently only JPEG),
380 the whole image will never actually be loaded into memory.
382 Since QtQuick 1.1 the sourceSize can be cleared to the natural size of the image
383 by setting sourceSize to \c undefined.
385 \note \e {Changing this property dynamically causes the image source to be reloaded,
386 potentially even from the network, if it is not in the disk cache.}
389 void QDeclarative1Image::updatePaintedGeometry()
391 Q_D(QDeclarative1Image);
393 if (d->fillMode == PreserveAspectFit) {
394 if (!d->pix.width() || !d->pix.height()) {
396 setImplicitHeight(0);
399 qreal w = widthValid() ? width() : d->pix.width();
400 qreal widthScale = w / qreal(d->pix.width());
401 qreal h = heightValid() ? height() : d->pix.height();
402 qreal heightScale = h / qreal(d->pix.height());
403 if (widthScale <= heightScale) {
405 d->paintedHeight = widthScale * qreal(d->pix.height());
406 } else if(heightScale < widthScale) {
407 d->paintedWidth = heightScale * qreal(d->pix.width());
408 d->paintedHeight = h;
410 if (widthValid() && !heightValid()) {
411 setImplicitHeight(d->paintedHeight);
413 setImplicitHeight(d->pix.height());
415 if (heightValid() && !widthValid()) {
416 setImplicitWidth(d->paintedWidth);
418 setImplicitWidth(d->pix.width());
420 } else if (d->fillMode == PreserveAspectCrop) {
421 if (!d->pix.width() || !d->pix.height())
423 qreal widthScale = width() / qreal(d->pix.width());
424 qreal heightScale = height() / qreal(d->pix.height());
425 if (widthScale < heightScale) {
426 widthScale = heightScale;
427 } else if(heightScale < widthScale) {
428 heightScale = widthScale;
431 d->paintedHeight = heightScale * qreal(d->pix.height());
432 d->paintedWidth = widthScale * qreal(d->pix.width());
434 d->paintedWidth = width();
435 d->paintedHeight = height();
437 emit paintedGeometryChanged();
440 void QDeclarative1Image::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
442 QDeclarative1ImageBase::geometryChanged(newGeometry, oldGeometry);
443 updatePaintedGeometry();
446 QRectF QDeclarative1Image::boundingRect() const
448 Q_D(const QDeclarative1Image);
449 QRectF boundingRect(0, 0, qMax(d->mWidth, d->paintedWidth), qMax(d->mHeight, d->paintedHeight));
451 if (d->fillMode == PreserveAspectCrop) {
452 if (!d->pix.width() || !d->pix.height())
454 qreal widthScale = width() / qreal(d->pix.width());
455 qreal heightScale = height() / qreal(d->pix.height());
456 if (widthScale < heightScale) {
457 widthScale = heightScale;
458 boundingRect.moveTo((width() - widthScale * d->pix.width()) / 2, 0);
459 } else if (heightScale < widthScale) {
460 heightScale = widthScale;
461 boundingRect.moveTo(0, (height() - heightScale * d->pix.height()) / 2);
468 \qmlproperty url QtQuick1::Image::source
470 Image can handle any image format supported by Qt, loaded from any URL scheme supported by Qt.
472 The URL may be absolute, or relative to the URL of the component.
474 \sa QDeclarativeImageProvider
478 \qmlproperty bool QtQuick1::Image::asynchronous
480 Specifies that images on the local filesystem should be loaded
481 asynchronously in a separate thread. The default value is
482 false, causing the user interface thread to block while the
483 image is loaded. Setting \a asynchronous to true is useful where
484 maintaining a responsive user interface is more desirable
485 than having images immediately visible.
487 Note that this property is only valid for images read from the
488 local filesystem. Images loaded via a network resource (e.g. HTTP)
489 are always loaded asynchonously.
493 \qmlproperty bool QtQuick1::Image::cache
496 Specifies whether the image should be cached. The default value is
497 true. Setting \a cache to false is useful when dealing with large images,
498 to make sure that they aren't cached at the expense of small 'ui element' images.
502 \qmlproperty bool QtQuick1::Image::mirror
505 This property holds whether the image should be horizontally inverted
506 (effectively displaying a mirrored image).
508 The default value is false.
512 void QDeclarative1Image::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
514 Q_D(QDeclarative1Image);
515 if (d->pix.pixmap().isNull() )
518 int drawWidth = width();
519 int drawHeight = height();
521 QTransform transform;
522 qreal widthScale = width() / qreal(d->pix.width());
523 qreal heightScale = height() / qreal(d->pix.height());
525 if (width() != d->pix.width() || height() != d->pix.height()) {
526 if (d->fillMode >= Tile) {
527 if (d->fillMode == TileVertically) {
528 transform.scale(widthScale, 1.0);
529 drawWidth = d->pix.width();
530 } else if (d->fillMode == TileHorizontally) {
531 transform.scale(1.0, heightScale);
532 drawHeight = d->pix.height();
535 if (d->fillMode == PreserveAspectFit) {
536 if (widthScale <= heightScale) {
537 heightScale = widthScale;
538 transform.translate(0, (height() - heightScale * d->pix.height()) / 2);
539 } else if(heightScale < widthScale) {
540 widthScale = heightScale;
541 transform.translate((width() - widthScale * d->pix.width()) / 2, 0);
543 } else if (d->fillMode == PreserveAspectCrop) {
544 if (widthScale < heightScale) {
545 widthScale = heightScale;
546 transform.translate((width() - widthScale * d->pix.width()) / 2, 0);
547 } else if(heightScale < widthScale) {
548 heightScale = widthScale;
549 transform.translate(0, (height() - heightScale * d->pix.height()) / 2);
552 transform.scale(widthScale, heightScale);
553 drawWidth = d->pix.width();
554 drawHeight = d->pix.height();
559 QTransform oldTransform;
560 bool oldAA = p->testRenderHint(QPainter::Antialiasing);
561 bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform);
563 p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);
566 p->setClipRect(QRectF(0, 0, d->mWidth, d->mHeight), Qt::IntersectClip);
569 transform.translate(drawWidth, 0).scale(-1.0, 1.0);
570 if (!transform.isIdentity()) {
571 oldTransform = p->transform();
572 p->setWorldTransform(transform * oldTransform);
575 if (d->fillMode >= Tile)
576 p->drawTiledPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix);
578 p->drawPixmap(QRectF(0, 0, drawWidth, drawHeight), d->pix, QRectF(0, 0, drawWidth, drawHeight));
581 p->setRenderHint(QPainter::Antialiasing, oldAA);
582 p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
586 if (!transform.isIdentity())
587 p->setWorldTransform(oldTransform);
590 void QDeclarative1Image::pixmapChange()
592 Q_D(QDeclarative1Image);
593 // PreserveAspectFit calculates the implicit size differently so we
594 // don't call our superclass pixmapChange(), since that would
595 // result in the implicit size being set incorrectly, then updated
596 // in updatePaintedGeometry()
597 if (d->fillMode != PreserveAspectFit)
598 QDeclarative1ImageBase::pixmapChange();
599 updatePaintedGeometry();