source: trunk/src/declarative/graphicsitems/qdeclarativeimage.cpp@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 17.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtDeclarative module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "private/qdeclarativeimage_p.h"
43#include "private/qdeclarativeimage_p_p.h"
44
45#include <QKeyEvent>
46#include <QPainter>
47
48QT_BEGIN_NAMESPACE
49
50
51/*!
52 \qmlclass Image QDeclarativeImage
53 \since 4.7
54 \ingroup qml-basic-visual-elements
55 \brief The Image element displays an image in a declarative user interface
56 \inherits Item
57
58 The Image element is used to display images in a declarative user interface.
59
60 The source of the image is specified as a URL using the \l source property.
61 Images can be supplied in any of the standard image formats supported by Qt,
62 including bitmap formats such as PNG and JPEG, and vector graphics formats
63 such as SVG. If you need to display animated images, use the \l AnimatedImage
64 element.
65
66 If the \l{Item::width}{width} and \l{Item::height}{height} properties are not
67 specified, the Image element automatically uses the size of the loaded image.
68 By default, specifying the width and height of the element causes the image
69 to be scaled to that size. This behavior can be changed by setting the
70 \l fillMode property, allowing the image to be stretched and tiled instead.
71
72 \section1 Example Usage
73
74 The following example shows the simplest usage of the Image element.
75
76 \snippet doc/src/snippets/declarative/image.qml document
77
78 \beginfloatleft
79 \image declarative-qtlogo.png
80 \endfloat
81
82 \clearfloat
83
84 \section1 Performance
85
86 By default, locally available images are loaded immediately, and the user interface
87 is blocked until loading is complete. If a large image is to be loaded, it may be
88 preferable to load the image in a low priority thread, by enabling the \l asynchronous
89 property.
90
91 If the image is obtained from a network rather than a local resource, it is
92 automatically loaded asynchronously, and the \l progress and \l status properties
93 are updated as appropriate.
94
95 Images are cached and shared internally, so if several Image elements have the same \l source,
96 only one copy of the image will be loaded.
97
98 \bold Note: Images are often the greatest user of memory in QML user interfaces. It is recommended
99 that images which do not form part of the user interface have their
100 size bounded via the \l sourceSize property. This is especially important for content
101 that is loaded from external sources or provided by the user.
102
103 \sa {declarative/imageelements/image}{Image example}, QDeclarativeImageProvider
104*/
105
106QDeclarativeImage::QDeclarativeImage(QDeclarativeItem *parent)
107 : QDeclarativeImageBase(*(new QDeclarativeImagePrivate), parent)
108{
109}
110
111QDeclarativeImage::QDeclarativeImage(QDeclarativeImagePrivate &dd, QDeclarativeItem *parent)
112 : QDeclarativeImageBase(dd, parent)
113{
114}
115
116QDeclarativeImage::~QDeclarativeImage()
117{
118}
119
120QPixmap QDeclarativeImage::pixmap() const
121{
122 Q_D(const QDeclarativeImage);
123 return d->pix.pixmap();
124}
125
126void QDeclarativeImage::setPixmap(const QPixmap &pix)
127{
128 Q_D(QDeclarativeImage);
129 if (!d->url.isEmpty())
130 return;
131 d->setPixmap(pix);
132}
133
134void QDeclarativeImagePrivate::setPixmap(const QPixmap &pixmap)
135{
136 Q_Q(QDeclarativeImage);
137 pix.setPixmap(pixmap);
138
139 q->setImplicitWidth(pix.width());
140 q->setImplicitHeight(pix.height());
141 status = pix.isNull() ? QDeclarativeImageBase::Null : QDeclarativeImageBase::Ready;
142
143 q->update();
144 q->pixmapChange();
145}
146
147/*!
148 \qmlproperty enumeration Image::fillMode
149
150 Set this property to define what happens when the image set for the item is smaller
151 than the size of the item.
152
153 \list
154 \o Image.Stretch - the image is scaled to fit
155 \o Image.PreserveAspectFit - the image is scaled uniformly to fit without cropping
156 \o Image.PreserveAspectCrop - the image is scaled uniformly to fill, cropping if necessary
157 \o Image.Tile - the image is duplicated horizontally and vertically
158 \o Image.TileVertically - the image is stretched horizontally and tiled vertically
159 \o Image.TileHorizontally - the image is stretched vertically and tiled horizontally
160 \endlist
161
162 \table
163
164 \row
165 \o \image declarative-qtlogo-stretch.png
166 \o Stretch (default)
167 \qml
168 Image {
169 width: 130; height: 100
170 smooth: true
171 source: "qtlogo.png"
172 }
173 \endqml
174
175 \row
176 \o \image declarative-qtlogo-preserveaspectfit.png
177 \o PreserveAspectFit
178 \qml
179 Image {
180 width: 130; height: 100
181 fillMode: Image.PreserveAspectFit
182 smooth: true
183 source: "qtlogo.png"
184 }
185 \endqml
186
187 \row
188 \o \image declarative-qtlogo-preserveaspectcrop.png
189 \o PreserveAspectCrop
190 \qml
191 Image {
192 width: 130; height: 100
193 fillMode: Image.PreserveAspectCrop
194 smooth: true
195 source: "qtlogo.png"
196 clip: true
197 }
198 \endqml
199
200 \row
201 \o \image declarative-qtlogo-tile.png
202 \o Tile
203 \qml
204 Image {
205 width: 120; height: 120
206 fillMode: Image.Tile
207 source: "qtlogo.png"
208 }
209 \endqml
210
211 \row
212 \o \image declarative-qtlogo-tilevertically.png
213 \o TileVertically
214 \qml
215 Image {
216 width: 120; height: 120
217 fillMode: Image.TileVertically
218 smooth: true
219 source: "qtlogo.png"
220 }
221 \endqml
222
223 \row
224 \o \image declarative-qtlogo-tilehorizontally.png
225 \o TileHorizontally
226 \qml
227 Image {
228 width: 120; height: 120
229 fillMode: Image.TileHorizontally
230 smooth: true
231 source: "qtlogo.png"
232 }
233 \endqml
234
235 \endtable
236
237 \sa {declarative/imageelements/image}{Image example}
238*/
239QDeclarativeImage::FillMode QDeclarativeImage::fillMode() const
240{
241 Q_D(const QDeclarativeImage);
242 return d->fillMode;
243}
244
245void QDeclarativeImage::setFillMode(FillMode mode)
246{
247 Q_D(QDeclarativeImage);
248 if (d->fillMode == mode)
249 return;
250 d->fillMode = mode;
251 update();
252 updatePaintedGeometry();
253 emit fillModeChanged();
254}
255
256/*!
257
258 \qmlproperty real Image::paintedWidth
259 \qmlproperty real Image::paintedHeight
260
261 These properties hold the size of the image that is actually painted.
262 In most cases it is the same as \c width and \c height, but when using a
263 \c fillMode \c PreserveAspectFit or \c fillMode \c PreserveAspectCrop
264 \c paintedWidth or \c paintedHeight can be smaller or larger than
265 \c width and \c height of the Image element.
266*/
267qreal QDeclarativeImage::paintedWidth() const
268{
269 Q_D(const QDeclarativeImage);
270 return d->paintedWidth;
271}
272
273qreal QDeclarativeImage::paintedHeight() const
274{
275 Q_D(const QDeclarativeImage);
276 return d->paintedHeight;
277}
278
279/*!
280 \qmlproperty enumeration Image::status
281
282 This property holds the status of image loading. It can be one of:
283 \list
284 \o Image.Null - no image has been set
285 \o Image.Ready - the image has been loaded
286 \o Image.Loading - the image is currently being loaded
287 \o Image.Error - an error occurred while loading the image
288 \endlist
289
290 Use this status to provide an update or respond to the status change in some way.
291 For example, you could:
292
293 \list
294 \o Trigger a state change:
295 \qml
296 State { name: 'loaded'; when: image.status == Image.Ready }
297 \endqml
298
299 \o Implement an \c onStatusChanged signal handler:
300 \qml
301 Image {
302 id: image
303 onStatusChanged: if (image.status == Image.Ready) console.log('Loaded')
304 }
305 \endqml
306
307 \o Bind to the status value:
308 \qml
309 Text { text: image.status == Image.Ready ? 'Loaded' : 'Not loaded' }
310 \endqml
311 \endlist
312
313 \sa progress
314*/
315
316/*!
317 \qmlproperty real Image::progress
318
319 This property holds the progress of image loading, from 0.0 (nothing loaded)
320 to 1.0 (finished).
321
322 \sa status
323*/
324
325/*!
326 \qmlproperty bool Image::smooth
327
328 Set this property if you want the image to be smoothly filtered when scaled or
329 transformed. Smooth filtering gives better visual quality, but is slower. If
330 the image is displayed at its natural size, this property has no visual or
331 performance effect.
332
333 \note Generally scaling artifacts are only visible if the image is stationary on
334 the screen. A common pattern when animating an image is to disable smooth
335 filtering at the beginning of the animation and reenable it at the conclusion.
336*/
337
338/*!
339 \qmlproperty QSize Image::sourceSize
340
341 This property holds the actual width and height of the loaded image.
342
343 Unlike the \l {Item::}{width} and \l {Item::}{height} properties, which scale
344 the painting of the image, this property sets the actual number of pixels
345 stored for the loaded image so that large images do not use more
346 memory than necessary. For example, this ensures the image in memory is no
347 larger than 1024x1024 pixels, regardless of the Image's \l {Item::}{width} and
348 \l {Item::}{height} values:
349
350 \code
351 Rectangle {
352 width: ...
353 height: ...
354
355 Image {
356 anchors.fill: parent
357 source: "reallyBigImage.jpg"
358 sourceSize.width: 1024
359 sourceSize.height: 1024
360 }
361 }
362 \endcode
363
364 If the image's actual size is larger than the sourceSize, the image is scaled down.
365 If only one dimension of the size is set to greater than 0, the
366 other dimension is set in proportion to preserve the source image's aspect ratio.
367 (The \l fillMode is independent of this.)
368
369 If the source is an instrinsically scalable image (eg. SVG), this property
370 determines the size of the loaded image regardless of intrinsic size.
371 Avoid changing this property dynamically; rendering an SVG is \e slow compared
372 to an image.
373
374 If the source is a non-scalable image (eg. JPEG), the loaded image will
375 be no greater than this property specifies. For some formats (currently only JPEG),
376 the whole image will never actually be loaded into memory.
377
378 \note \e {Changing this property dynamically causes the image source to be reloaded,
379 potentially even from the network, if it is not in the disk cache.}
380*/
381
382void QDeclarativeImage::updatePaintedGeometry()
383{
384 Q_D(QDeclarativeImage);
385
386 if (d->fillMode == PreserveAspectFit) {
387 if (!d->pix.width() || !d->pix.height())
388 return;
389 qreal widthScale = width() / qreal(d->pix.width());
390 qreal heightScale = height() / qreal(d->pix.height());
391 if (widthScale <= heightScale) {
392 d->paintedWidth = width();
393 d->paintedHeight = widthScale * qreal(d->pix.height());
394 } else if(heightScale < widthScale) {
395 d->paintedWidth = heightScale * qreal(d->pix.width());
396 d->paintedHeight = height();
397 }
398 if (widthValid() && !heightValid()) {
399 setImplicitHeight(d->paintedHeight);
400 }
401 if (heightValid() && !widthValid()) {
402 setImplicitWidth(d->paintedWidth);
403 }
404 } else if (d->fillMode == PreserveAspectCrop) {
405 if (!d->pix.width() || !d->pix.height())
406 return;
407 qreal widthScale = width() / qreal(d->pix.width());
408 qreal heightScale = height() / qreal(d->pix.height());
409 if (widthScale < heightScale) {
410 widthScale = heightScale;
411 } else if(heightScale < widthScale) {
412 heightScale = widthScale;
413 }
414
415 d->paintedHeight = heightScale * qreal(d->pix.height());
416 d->paintedWidth = widthScale * qreal(d->pix.width());
417 } else {
418 d->paintedWidth = width();
419 d->paintedHeight = height();
420 }
421 emit paintedGeometryChanged();
422}
423
424void QDeclarativeImage::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
425{
426 QDeclarativeImageBase::geometryChanged(newGeometry, oldGeometry);
427 updatePaintedGeometry();
428}
429
430QRectF QDeclarativeImage::boundingRect() const
431{
432 Q_D(const QDeclarativeImage);
433 return QRectF(0, 0, qMax(d->mWidth, d->paintedWidth), qMax(d->mHeight, d->paintedHeight));
434}
435
436/*!
437 \qmlproperty url Image::source
438
439 Image can handle any image format supported by Qt, loaded from any URL scheme supported by Qt.
440
441 The URL may be absolute, or relative to the URL of the component.
442
443 \sa QDeclarativeImageProvider
444*/
445
446/*!
447 \qmlproperty bool Image::asynchronous
448
449 Specifies that images on the local filesystem should be loaded
450 asynchronously in a separate thread. The default value is
451 false, causing the user interface thread to block while the
452 image is loaded. Setting \a asynchronous to true is useful where
453 maintaining a responsive user interface is more desirable
454 than having images immediately visible.
455
456 Note that this property is only valid for images read from the
457 local filesystem. Images loaded via a network resource (e.g. HTTP)
458 are always loaded asynchonously.
459*/
460
461void QDeclarativeImage::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
462{
463 Q_D(QDeclarativeImage);
464 if (d->pix.pixmap().isNull() )
465 return;
466
467 bool oldAA = p->testRenderHint(QPainter::Antialiasing);
468 bool oldSmooth = p->testRenderHint(QPainter::SmoothPixmapTransform);
469 if (d->smooth)
470 p->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform, d->smooth);
471
472 if (width() != d->pix.width() || height() != d->pix.height()) {
473 if (d->fillMode >= Tile) {
474 if (d->fillMode == Tile) {
475 p->drawTiledPixmap(QRectF(0,0,width(),height()), d->pix);
476 } else {
477 qreal widthScale = width() / qreal(d->pix.width());
478 qreal heightScale = height() / qreal(d->pix.height());
479
480 QTransform scale;
481 if (d->fillMode == TileVertically) {
482 scale.scale(widthScale, 1.0);
483 QTransform old = p->transform();
484 p->setWorldTransform(scale * old);
485 p->drawTiledPixmap(QRectF(0,0,d->pix.width(),height()), d->pix);
486 p->setWorldTransform(old);
487 } else {
488 scale.scale(1.0, heightScale);
489 QTransform old = p->transform();
490 p->setWorldTransform(scale * old);
491 p->drawTiledPixmap(QRectF(0,0,width(),d->pix.height()), d->pix);
492 p->setWorldTransform(old);
493 }
494 }
495 } else {
496 qreal widthScale = width() / qreal(d->pix.width());
497 qreal heightScale = height() / qreal(d->pix.height());
498
499 QTransform scale;
500
501 if (d->fillMode == PreserveAspectFit) {
502 if (widthScale <= heightScale) {
503 heightScale = widthScale;
504 scale.translate(0, (height() - heightScale * d->pix.height()) / 2);
505 } else if(heightScale < widthScale) {
506 widthScale = heightScale;
507 scale.translate((width() - widthScale * d->pix.width()) / 2, 0);
508 }
509 } else if (d->fillMode == PreserveAspectCrop) {
510 if (widthScale < heightScale) {
511 widthScale = heightScale;
512 scale.translate((width() - widthScale * d->pix.width()) / 2, 0);
513 } else if(heightScale < widthScale) {
514 heightScale = widthScale;
515 scale.translate(0, (height() - heightScale * d->pix.height()) / 2);
516 }
517 }
518 if (clip()) {
519 p->save();
520 p->setClipRect(QRectF(0, 0, d->mWidth, d->mHeight), Qt::IntersectClip);
521 }
522 scale.scale(widthScale, heightScale);
523 QTransform old = p->transform();
524 p->setWorldTransform(scale * old);
525 p->drawPixmap(0, 0, d->pix);
526 p->setWorldTransform(old);
527 if (clip()) {
528 p->restore();
529 }
530 }
531 } else {
532 p->drawPixmap(0, 0, d->pix);
533 }
534
535 if (d->smooth) {
536 p->setRenderHint(QPainter::Antialiasing, oldAA);
537 p->setRenderHint(QPainter::SmoothPixmapTransform, oldSmooth);
538 }
539}
540
541void QDeclarativeImage::pixmapChange()
542{
543 updatePaintedGeometry();
544}
545
546QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.