Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeimageprovider.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativeimageprovider.h"
43
44 QT_BEGIN_NAMESPACE
45
46 class QDeclarativeImageProviderPrivate
47 {
48 public:
49     QDeclarativeImageProvider::ImageType type;
50 };
51
52 /*!
53     \class QDeclarativeImageProvider
54     \since 4.7
55     \brief The QDeclarativeImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML.
56
57     QDeclarativeImageProvider is used to provide advanced image loading features
58     in QML applications. It allows images in QML to be:
59
60     \list
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}
63     \endlist
64
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:
68
69     \qml
70     Image { source: "image://myimageprovider/image.png" }
71     \endqml
72
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().
77
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").
82     \qml
83     Image { source: "image://MyImageProvider/Image.png" }
84     \endqml
85
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.
88
89     \section2 An example
90
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:
94
95     \snippet examples/declarative/cppextensions/imageprovider/imageprovider-example.qml 0
96
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.
101
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:
105
106     \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 0
107     \codeline
108     \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 1
109
110     To make this provider accessible to QML, it is registered with the QML engine
111     with a "colors" identifier:
112
113     \code
114     int main(int argc, char *argv[]) 
115     {
116         ...
117
118         QDeclarativeEngine engine;
119         engine->addImageProvider(QLatin1String("colors"), new ColorPixmapProvider);
120
121         ...
122     }
123     \endcode
124
125     Now the images can be successfully loaded in QML:
126
127     \image imageprovider.png
128
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.
133
134
135     \section2 Asynchronous image loading
136
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.
143
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
148     synchronously.
149
150     \sa QDeclarativeEngine::addImageProvider()
151 */
152
153 /*!
154     \enum QDeclarativeImageProvider::ImageType
155
156     Defines the type of image supported by this image provider.
157
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.
162 */
163
164 /*!
165     Creates an image provider that will provide images of the given \a type.
166 */
167 QDeclarativeImageProvider::QDeclarativeImageProvider(ImageType type)
168     : d(new QDeclarativeImageProviderPrivate)
169 {
170     d->type = type;
171 }
172
173 /*!
174     Destroys the QDeclarativeImageProvider
175
176     \note The destructor of your derived class need to be thread safe.
177 */
178 QDeclarativeImageProvider::~QDeclarativeImageProvider()
179 {
180     delete d;
181 }
182
183 /*!
184     Returns the image type supported by this provider.
185 */
186 QDeclarativeImageProvider::ImageType QDeclarativeImageProvider::imageType() const
187 {
188     return d->type;
189 }
190
191 /*!
192     Implement this method to return the image with \a id. The default 
193     implementation returns an empty image.
194
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".
198
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.
202
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.
206
207     \note this method may be called by multiple threads, so ensure the
208     implementation of this method is reentrant.
209 */
210 QImage QDeclarativeImageProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize)
211 {
212     Q_UNUSED(id);
213     Q_UNUSED(size);
214     Q_UNUSED(requestedSize);
215     if (d->type == Image)
216         qWarning("ImageProvider supports Image type but has not implemented requestImage()");
217     return QImage();
218 }
219
220 /*!
221     Implement this method to return the pixmap with \a id. The default
222     implementation returns an empty pixmap.
223
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".
227
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.
231
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.
235 */
236 QPixmap QDeclarativeImageProvider::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
237 {
238     Q_UNUSED(id);
239     Q_UNUSED(size);
240     Q_UNUSED(requestedSize);
241     if (d->type == Pixmap)
242         qWarning("ImageProvider supports Pixmap type but has not implemented requestPixmap()");
243     return QPixmap();
244 }
245
246 QT_END_NAMESPACE
247