1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "qquickborderimage_p.h"
43 #include "qquickborderimage_p_p.h"
44 #include "qquickninepatchnode_p.h"
46 #include <QtQml/qqmlinfo.h>
47 #include <QtQml/qqmlfile.h>
48 #include <QtQml/qqmlengine.h>
49 #include <QtNetwork/qnetworkreply.h>
50 #include <QtCore/qfile.h>
52 #include <private/qqmlglobal_p.h>
58 \qmlclass BorderImage QQuickBorderImage
59 \inqmlmodule QtQuick 2
60 \brief The BorderImage element provides an image that can be used as a border.
62 \ingroup qml-basic-visual-elements
64 The BorderImage element is used to create borders out of images by scaling or tiling
67 A BorderImage element breaks a source image, specified using the \l url property,
68 into 9 regions, as shown below:
70 \image declarative-scalegrid.png
72 When the image is scaled, regions of the source image are scaled or tiled to
73 create the displayed border image in the following way:
76 \li The corners (regions 1, 3, 7, and 9) are not scaled at all.
77 \li Regions 2 and 8 are scaled according to
78 \l{BorderImage::horizontalTileMode}{horizontalTileMode}.
79 \li Regions 4 and 6 are scaled according to
80 \l{BorderImage::verticalTileMode}{verticalTileMode}.
81 \li The middle (region 5) is scaled according to both
82 \l{BorderImage::horizontalTileMode}{horizontalTileMode} and
83 \l{BorderImage::verticalTileMode}{verticalTileMode}.
86 The regions of the image are defined using the \l border property group, which
87 describes the distance from each edge of the source image to use as a border.
89 \section1 Example Usage
91 The following examples show the effects of the different modes on an image.
92 Guide lines are overlaid onto the image to show the different regions of the
93 image as described above.
96 \image qml-borderimage-normal-image.png
99 An unscaled image is displayed using an Image element. The \l border property is
100 used to determine the parts of the image that will lie inside the unscaled corner
101 areas and the parts that will be stretched horizontally and vertically.
103 \snippet doc/snippets/qml/borderimage/normal-image.qml normal image
107 \image qml-borderimage-scaled.png
110 A BorderImage element is used to display the image, and it is given a size that is
111 larger than the original image. Since the \l horizontalTileMode property is set to
112 \l{BorderImage::horizontalTileMode}{BorderImage.Stretch}, the parts of image in
113 regions 2 and 8 are stretched horizontally. Since the \l verticalTileMode property
114 is set to \l{BorderImage::verticalTileMode}{BorderImage.Stretch}, the parts of image
115 in regions 4 and 6 are stretched vertically.
117 \snippet doc/snippets/qml/borderimage/borderimage-scaled.qml scaled border image
121 \image qml-borderimage-tiled.png
124 Again, a large BorderImage element is used to display the image. With the
125 \l horizontalTileMode property set to \l{BorderImage::horizontalTileMode}{BorderImage.Repeat},
126 the parts of image in regions 2 and 8 are tiled so that they fill the space at the
127 top and bottom of the element. Similarly, the \l verticalTileMode property is set to
128 \l{BorderImage::verticalTileMode}{BorderImage.Repeat}, the parts of image in regions
129 4 and 6 are tiled so that they fill the space at the left and right of the element.
131 \snippet doc/snippets/qml/borderimage/borderimage-tiled.qml tiled border image
134 In some situations, the width of regions 2 and 8 may not be an exact multiple of the width
135 of the corresponding regions in the source image. Similarly, the height of regions 4 and 6
136 may not be an exact multiple of the height of the corresponding regions. It can be useful
137 to use \l{BorderImage::horizontalTileMode}{BorderImage.Round} instead of
138 \l{BorderImage::horizontalTileMode}{BorderImage.Repeat} in cases like these.
140 The \l{declarative/imageelements/borderimage}{BorderImage example} shows how a BorderImage
141 can be used to simulate a shadow effect on a rectangular item.
143 \section1 Quality and Performance
145 By default, any scaled regions of the image are rendered without smoothing to improve
146 rendering speed. Setting the \l smooth property improves rendering quality of scaled
147 regions, but may slow down rendering.
149 The source image may not be loaded instantaneously, depending on its original location.
150 Loading progress can be monitored with the \l progress property.
152 \sa Image, AnimatedImage
156 \qmlproperty bool QtQuick2::BorderImage::asynchronous
158 Specifies that images on the local filesystem should be loaded
159 asynchronously in a separate thread. The default value is
160 false, causing the user interface thread to block while the
161 image is loaded. Setting \a asynchronous to true is useful where
162 maintaining a responsive user interface is more desirable
163 than having images immediately visible.
165 Note that this property is only valid for images read from the
166 local filesystem. Images loaded via a network resource (e.g. HTTP)
167 are always loaded asynchronously.
169 QQuickBorderImage::QQuickBorderImage(QQuickItem *parent)
170 : QQuickImageBase(*(new QQuickBorderImagePrivate), parent)
174 QQuickBorderImage::~QQuickBorderImage()
176 Q_D(QQuickBorderImage);
178 d->sciReply->deleteLater();
182 \qmlproperty enumeration QtQuick2::BorderImage::status
184 This property describes the status of image loading. It can be one of:
187 \li BorderImage.Null - no image has been set
188 \li BorderImage.Ready - the image has been loaded
189 \li BorderImage.Loading - the image is currently being loaded
190 \li BorderImage.Error - an error occurred while loading the image
197 \qmlproperty real QtQuick2::BorderImage::progress
199 This property holds the progress of image loading, from 0.0 (nothing loaded)
206 \qmlproperty bool QtQuick2::BorderImage::smooth
208 Set this property if you want the image to be smoothly filtered when scaled or
209 transformed. Smooth filtering gives better visual quality, but is slower. If
210 the image is displayed at its natural size, this property has no visual or
213 By default, this property is set to false.
215 \note Generally scaling artifacts are only visible if the image is stationary on
216 the screen. A common pattern when animating an image is to disable smooth
217 filtering at the beginning of the animation and enable it at the conclusion.
221 \qmlproperty bool QtQuick2::BorderImage::cache
223 Specifies whether the image should be cached. The default value is
224 true. Setting \a cache to false is useful when dealing with large images,
225 to make sure that they aren't cached at the expense of small 'ui element' images.
229 \qmlproperty bool QtQuick2::BorderImage::mirror
231 This property holds whether the image should be horizontally inverted
232 (effectively displaying a mirrored image).
234 The default value is false.
238 \qmlproperty url QtQuick2::BorderImage::source
240 This property holds the URL that refers to the source image.
242 BorderImage can handle any image format supported by Qt, loaded from any
243 URL scheme supported by Qt.
245 This property can also be used to refer to .sci files, which are
246 written in a QML-specific, text-based format that specifies the
247 borders, the image file and the tile rules for a given border image.
249 The following .sci file sets the borders to 10 on each side for the
250 image \c picture.png:
257 source: "picture.png"
260 The URL may be absolute, or relative to the URL of the component.
262 \sa QQuickImageProvider
266 \qmlproperty QSize QtQuick2::BorderImage::sourceSize
268 This property holds the actual width and height of the loaded image.
270 In BorderImage, this property is read-only.
272 \sa Image::sourceSize
274 void QQuickBorderImage::setSource(const QUrl &url)
276 Q_D(QQuickBorderImage);
277 //equality is fairly expensive, so we bypass for simple, common case
278 if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
282 d->sciReply->deleteLater();
288 emit sourceChanged(d->url);
290 if (isComponentComplete())
294 void QQuickBorderImage::load()
296 Q_D(QQuickBorderImage);
297 if (d->progress != 0.0) {
299 emit progressChanged(d->progress);
302 if (d->url.isEmpty()) {
305 setImplicitSize(0, 0);
306 emit statusChanged(d->status);
310 if (d->url.path().endsWith(QLatin1String("sci"))) {
311 QString lf = QQmlFile::urlToLocalFileOrQrc(d->url);
314 file.open(QIODevice::ReadOnly);
315 setGridScaledImage(QQuickGridScaledImage(&file));
317 QNetworkRequest req(d->url);
318 d->sciReply = qmlEngine(this)->networkAccessManager()->get(req);
319 FAST_CONNECT(d->sciReply, SIGNAL(finished()), this, SLOT(sciRequestFinished()))
323 QQuickPixmap::Options options;
325 options |= QQuickPixmap::Asynchronous;
327 options |= QQuickPixmap::Cache;
329 d->pix.load(qmlEngine(this), d->url, options);
331 if (d->pix.isLoading()) {
332 d->pix.connectFinished(this, SLOT(requestFinished()));
333 d->pix.connectDownloadProgress(this, SLOT(requestProgress(qint64,qint64)));
335 QSize impsize = d->pix.implicitSize();
336 setImplicitSize(impsize.width(), impsize.height());
338 if (d->pix.isReady()) {
342 qmlInfo(this) << d->pix.error();
346 emit statusChanged(d->status);
347 emit progressChanged(d->progress);
353 emit statusChanged(d->status);
357 \qmlproperty int QtQuick2::BorderImage::border.left
358 \qmlproperty int QtQuick2::BorderImage::border.right
359 \qmlproperty int QtQuick2::BorderImage::border.top
360 \qmlproperty int QtQuick2::BorderImage::border.bottom
362 The 4 border lines (2 horizontal and 2 vertical) break the image into 9 sections,
365 \image declarative-scalegrid.png
367 Each border line (left, right, top, and bottom) specifies an offset in pixels
368 from the respective edge of the source image. By default, each border line has
371 For example, the following definition sets the bottom line 10 pixels up from
372 the bottom of the image:
381 The border lines can also be specified using a
382 \l {BorderImage::source}{.sci file}.
385 QQuickScaleGrid *QQuickBorderImage::border()
387 Q_D(QQuickBorderImage);
388 return d->getScaleGrid();
392 \qmlproperty enumeration QtQuick2::BorderImage::horizontalTileMode
393 \qmlproperty enumeration QtQuick2::BorderImage::verticalTileMode
395 This property describes how to repeat or stretch the middle parts of the border image.
398 \li BorderImage.Stretch - Scales the image to fit to the available area.
399 \li BorderImage.Repeat - Tile the image until there is no more space. May crop the last image.
400 \li BorderImage.Round - Like Repeat, but scales the images down to ensure that the last image is not cropped.
403 The default tile mode for each property is BorderImage.Stretch.
405 QQuickBorderImage::TileMode QQuickBorderImage::horizontalTileMode() const
407 Q_D(const QQuickBorderImage);
408 return d->horizontalTileMode;
411 void QQuickBorderImage::setHorizontalTileMode(TileMode t)
413 Q_D(QQuickBorderImage);
414 if (t != d->horizontalTileMode) {
415 d->horizontalTileMode = t;
416 emit horizontalTileModeChanged();
421 QQuickBorderImage::TileMode QQuickBorderImage::verticalTileMode() const
423 Q_D(const QQuickBorderImage);
424 return d->verticalTileMode;
427 void QQuickBorderImage::setVerticalTileMode(TileMode t)
429 Q_D(QQuickBorderImage);
430 if (t != d->verticalTileMode) {
431 d->verticalTileMode = t;
432 emit verticalTileModeChanged();
437 void QQuickBorderImage::setGridScaledImage(const QQuickGridScaledImage& sci)
439 Q_D(QQuickBorderImage);
440 if (!sci.isValid()) {
442 emit statusChanged(d->status);
444 QQuickScaleGrid *sg = border();
445 sg->setTop(sci.gridTop());
446 sg->setBottom(sci.gridBottom());
447 sg->setLeft(sci.gridLeft());
448 sg->setRight(sci.gridRight());
449 d->horizontalTileMode = sci.horizontalTileRule();
450 d->verticalTileMode = sci.verticalTileRule();
452 d->sciurl = d->url.resolved(QUrl(sci.pixmapUrl()));
454 QQuickPixmap::Options options;
456 options |= QQuickPixmap::Asynchronous;
458 options |= QQuickPixmap::Cache;
460 d->pix.load(qmlEngine(this), d->sciurl, options);
462 if (d->pix.isLoading()) {
463 static int thisRequestProgress = -1;
464 static int thisRequestFinished = -1;
465 if (thisRequestProgress == -1) {
466 thisRequestProgress =
467 QQuickBorderImage::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
468 thisRequestFinished =
469 QQuickBorderImage::staticMetaObject.indexOfSlot("requestFinished()");
472 d->pix.connectFinished(this, thisRequestFinished);
473 d->pix.connectDownloadProgress(this, thisRequestProgress);
477 QSize impsize = d->pix.implicitSize();
478 setImplicitSize(impsize.width(), impsize.height());
480 if (d->pix.isReady()) {
484 qmlInfo(this) << d->pix.error();
488 emit statusChanged(d->status);
489 emit progressChanged(1.0);
496 void QQuickBorderImage::requestFinished()
498 Q_D(QQuickBorderImage);
500 QSize impsize = d->pix.implicitSize();
501 if (d->pix.isError()) {
503 qmlInfo(this) << d->pix.error();
508 setImplicitSize(impsize.width(), impsize.height());
510 if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
511 emit sourceSizeChanged();
514 emit statusChanged(d->status);
515 emit progressChanged(1.0);
519 #define BORDERIMAGE_MAX_REDIRECT 16
521 void QQuickBorderImage::sciRequestFinished()
523 Q_D(QQuickBorderImage);
526 if (d->redirectCount < BORDERIMAGE_MAX_REDIRECT) {
527 QVariant redirect = d->sciReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
528 if (redirect.isValid()) {
529 QUrl url = d->sciReply->url().resolved(redirect.toUrl());
536 if (d->sciReply->error() != QNetworkReply::NoError) {
538 d->sciReply->deleteLater();
540 emit statusChanged(d->status);
542 QQuickGridScaledImage sci(d->sciReply);
543 d->sciReply->deleteLater();
545 setGridScaledImage(sci);
549 void QQuickBorderImage::doUpdate()
554 QSGNode *QQuickBorderImage::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
556 Q_D(QQuickBorderImage);
558 QSGTexture *texture = d->sceneGraphContext()->textureForFactory(d->pix.textureFactory(), canvas());
560 if (!texture || width() <= 0 || height() <= 0) {
565 QQuickNinePatchNode *node = static_cast<QQuickNinePatchNode *>(oldNode);
568 node = new QQuickNinePatchNode();
571 node->setTexture(texture);
573 // Don't implicitly create the scalegrid in the rendering thread...
575 const QQuickScaleGrid *border = d->getScaleGrid();
576 node->setInnerRect(QRectF(border->left(),
578 qMax(1, d->pix.width() - border->right() - border->left()),
579 qMax(1, d->pix.height() - border->bottom() - border->top())));
581 node->setInnerRect(QRectF(0, 0, d->pix.width(), d->pix.height()));
583 node->setRect(QRectF(0, 0, width(), height()));
584 node->setFiltering(d->smooth ? QSGTexture::Linear : QSGTexture::Nearest);
585 node->setHorzontalTileMode(d->horizontalTileMode);
586 node->setVerticalTileMode(d->verticalTileMode);
587 node->setMirror(d->mirror);
593 void QQuickBorderImage::pixmapChange()
595 Q_D(QQuickBorderImage);
597 d->pixmapChanged = true;
599 // When the pixmap changes, such as being deleted, we need to update the textures