1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 "qdeclarativeimageprovider.h"
46 class QDeclarativeImageProviderPrivate
49 QDeclarativeImageProvider::ImageType type;
53 \class QDeclarativeImageProvider
55 \brief The QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML.
57 QDeclarativeImageProvider is used to provide advanced image loading features
58 in QML applications. It allows images in QML to be:
61 \o Loaded using QPixmaps rather than actual image files
62 \o Loaded asynchronously in a separate thread, if imageType() is \l{QDeclarativeImageProvider::ImageType}{ImageType::Image}
65 To specify that an image should be loaded by an image provider, use the
66 \bold {"image:"} scheme for the URL source of the image, followed by the
67 identifiers of the image provider and the requested image. For example:
70 Image { source: "image://myimageprovider/image.png" }
73 This specifies that the image should be loaded by the image provider named
74 "myimageprovider", and the image to be loaded is named "image.png". The QML engine
75 invokes the appropriate image provider according to the providers that have
76 been registered through QDeclarativeEngine::addImageProvider().
78 Note that the identifiers are case-insensitive, but the rest of the URL will be passed on with
79 preserved case. For example, the below snippet would still specify that the image is loaded by the
80 image provider named "myimageprovider", but it would request a different image than the above snippet
81 ("Image.png" instead of "image.png").
83 Image { source: "image://MyImageProvider/Image.png" }
86 If you want the rest of the URL to be case insensitive, you will have to take care
87 of that yourself inside your image provider.
91 Here are two images. Their \c source values indicate they should be loaded by
92 an image provider named "colors", and the images to be loaded are "yellow"
93 and "red", respectively:
95 \snippet examples/declarative/cppextensions/imageprovider/imageprovider-example.qml 0
97 When these images are loaded by QML, it looks for a matching image provider
98 and calls its requestImage() or requestPixmap() method (depending on its
99 imageType()) to load the image. The method is called with the \c id
100 parameter set to "yellow" for the first image, and "red" for the second.
102 Here is an image provider implementation that can load the images
103 requested by the above QML. This implementation dynamically
104 generates QPixmap images that are filled with the requested color:
106 \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 0
108 \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 1
110 To make this provider accessible to QML, it is registered with the QML engine
111 with a "colors" identifier:
114 int main(int argc, char *argv[])
118 QDeclarativeEngine engine;
119 engine->addImageProvider(QLatin1String("colors"), new ColorPixmapProvider);
125 Now the images can be successfully loaded in QML:
127 \image imageprovider.png
129 A complete example is available in Qt's
130 \l {declarative/cppextensions/imageprovider}{examples/declarative/cppextensions/imageprovider}
131 directory. Note the example registers the provider via a \l{QDeclarativeExtensionPlugin}{plugin}
132 instead of registering it in the application \c main() function as shown above.
135 \section2 Asynchronous image loading
137 Image providers that support QImage loading automatically include support
138 for asychronous loading of images. To enable asynchronous loading for an
139 image source, set the \c asynchronous property to \c true for the relevant
140 \l Image, \l BorderImage or \l AnimatedImage object. When this is enabled,
141 the image request to the provider is run in a low priority thread,
142 allowing image loading to be executed in the background, and reducing the
143 performance impact on the user interface.
145 Asynchronous loading is not supported for image providers that provide
146 QPixmap rather than QImage values, as pixmaps can only be created in the
147 main thread. In this case, if \l {Image::}{asynchronous} is set to
148 \c true, the value is ignored and the image is loaded
152 \section2 Image caching
154 Images returned by a QDeclarativeImageProvider are automatically cached,
155 similar to any image loaded by the QML engine. When an image with a
156 "image://" prefix is loaded from cache, requestImage() and requestPixmap()
157 will not be called for the relevant image provider. If an image should always
158 be fetched from the image provider, and should not be cached at all, set the
159 \c cache property to \c false for the relevant \l Image, \l BorderImage or
160 \l AnimatedImage object.
162 \sa QDeclarativeEngine::addImageProvider()
166 \enum QDeclarativeImageProvider::ImageType
168 Defines the type of image supported by this image provider.
170 \value Image The Image Provider provides QImage images. The
171 requestImage() method will be called for all image requests.
172 \value Pixmap The Image Provider provides QPixmap images. The
173 requestPixmap() method will be called for all image requests.
174 \value Texture The Image Provider provides QSGTextureProvider based images.
175 The requestTexture() method will be called for all image requests. \omitvalue
179 Creates an image provider that will provide images of the given \a type.
181 QDeclarativeImageProvider::QDeclarativeImageProvider(ImageType type)
182 : d(new QDeclarativeImageProviderPrivate)
188 Destroys the QDeclarativeImageProvider
190 \note The destructor of your derived class need to be thread safe.
192 QDeclarativeImageProvider::~QDeclarativeImageProvider()
198 Returns the image type supported by this provider.
200 QDeclarativeImageProvider::ImageType QDeclarativeImageProvider::imageType() const
206 Implement this method to return the image with \a id. The default
207 implementation returns an empty image.
209 The \a id is the requested image source, with the "image:" scheme and
210 provider identifier removed. For example, if the image \l{Image::}{source}
211 was "image://myprovider/icons/home", the given \a id would be "icons/home".
213 The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
214 an Image element. If \a requestedSize is a valid size, the image
215 returned should be of that size.
217 In all cases, \a size must be set to the original size of the image. This
218 is used to set the \l {Item::}{width} and \l {Item::}{height} of the
219 relevant \l Image if these values have not been set explicitly.
221 \note this method may be called by multiple threads, so ensure the
222 implementation of this method is reentrant.
224 QImage QDeclarativeImageProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize)
228 Q_UNUSED(requestedSize);
229 if (d->type == Image)
230 qWarning("ImageProvider supports Image type but has not implemented requestImage()");
235 Implement this method to return the pixmap with \a id. The default
236 implementation returns an empty pixmap.
238 The \a id is the requested image source, with the "image:" scheme and
239 provider identifier removed. For example, if the image \l{Image::}{source}
240 was "image://myprovider/icons/home", the given \a id would be "icons/home".
242 The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
243 an Image element. If \a requestedSize is a valid size, the image
244 returned should be of that size.
246 In all cases, \a size must be set to the original size of the image. This
247 is used to set the \l {Item::}{width} and \l {Item::}{height} of the
248 relevant \l Image if these values have not been set explicitly.
250 QPixmap QDeclarativeImageProvider::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
254 Q_UNUSED(requestedSize);
255 if (d->type == Pixmap)
256 qWarning("ImageProvider supports Pixmap type but has not implemented requestPixmap()");
262 Implement this method to return the texture with \a id. The default
263 implementation returns 0.
265 The \a id is the requested image source, with the "image:" scheme and
266 provider identifier removed. For example, if the image \l{Image::}{source}
267 was "image://myprovider/icons/home", the given \a id would be "icons/home".
269 The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
270 an Image element. If \a requestedSize is a valid size, the image
271 returned should be of that size.
273 In all cases, \a size must be set to the original size of the image. This
274 is used to set the \l {Item::}{width} and \l {Item::}{height} of the
275 relevant \l Image if these values have not been set explicitly.
277 \note this method may be called by multiple threads, so ensure the
278 implementation of this method is reentrant.
281 QSGTexture *QDeclarativeImageProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
285 Q_UNUSED(requestedSize);
286 if (d->type == Texture)
287 qWarning("ImageProvider supports Texture type but has not implemented requestTexture()");