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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 \l Image source, set \l Image::asynchronous to \c true. When this is enabled,
140 the image request to the provider is run in a low priority thread,
141 allowing image loading to be executed in the background, and reducing the
142 performance impact on the user interface.
144 Asynchronous loading is not supported for image providers that provide
145 QPixmap rather than QImage values, as pixmaps can only be created in the
146 main thread. In this case, if \l {Image::}{asynchronous} is set to
147 \c true, the value is ignored and the image is loaded
150 \sa QDeclarativeEngine::addImageProvider()
154 \enum QDeclarativeImageProvider::ImageType
156 Defines the type of image supported by this image provider.
158 \value Image The Image Provider provides QImage images. The
159 requestImage() method will be called for all image requests.
160 \value Pixmap The Image Provider provides QPixmap images. The
161 requestPixmap() method will be called for all image requests.
165 Creates an image provider that will provide images of the given \a type.
167 QDeclarativeImageProvider::QDeclarativeImageProvider(ImageType type)
168 : d(new QDeclarativeImageProviderPrivate)
174 Destroys the QDeclarativeImageProvider
176 \note The destructor of your derived class need to be thread safe.
178 QDeclarativeImageProvider::~QDeclarativeImageProvider()
184 Returns the image type supported by this provider.
186 QDeclarativeImageProvider::ImageType QDeclarativeImageProvider::imageType() const
192 Implement this method to return the image with \a id. The default
193 implementation returns an empty image.
195 The \a id is the requested image source, with the "image:" scheme and
196 provider identifier removed. For example, if the image \l{Image::}{source}
197 was "image://myprovider/icons/home", the given \a id would be "icons/home".
199 The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
200 an Image element. If \a requestedSize is a valid size, the image
201 returned should be of that size.
203 In all cases, \a size must be set to the original size of the image. This
204 is used to set the \l {Item::}{width} and \l {Item::}{height} of the
205 relevant \l Image if these values have not been set explicitly.
207 \note this method may be called by multiple threads, so ensure the
208 implementation of this method is reentrant.
210 QImage QDeclarativeImageProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize)
214 Q_UNUSED(requestedSize);
215 if (d->type == Image)
216 qWarning("ImageProvider supports Image type but has not implemented requestImage()");
221 Implement this method to return the pixmap with \a id. The default
222 implementation returns an empty pixmap.
224 The \a id is the requested image source, with the "image:" scheme and
225 provider identifier removed. For example, if the image \l{Image::}{source}
226 was "image://myprovider/icons/home", the given \a id would be "icons/home".
228 The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
229 an Image element. If \a requestedSize is a valid size, the image
230 returned should be of that size.
232 In all cases, \a size must be set to the original size of the image. This
233 is used to set the \l {Item::}{width} and \l {Item::}{height} of the
234 relevant \l Image if these values have not been set explicitly.
236 QPixmap QDeclarativeImageProvider::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
240 Q_UNUSED(requestedSize);
241 if (d->type == Pixmap)
242 qWarning("ImageProvider supports Pixmap type but has not implemented requestPixmap()");