[844] | 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 | ** 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 qt-info@nokia.com.
|
---|
| 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 |
|
---|
| 79 | \section2 An example
|
---|
| 80 |
|
---|
| 81 | Here are two images. Their \c source values indicate they should be loaded by
|
---|
| 82 | an image provider named "colors", and the images to be loaded are "yellow"
|
---|
| 83 | and "red", respectively:
|
---|
| 84 |
|
---|
| 85 | \snippet examples/declarative/cppextensions/imageprovider/imageprovider-example.qml 0
|
---|
| 86 |
|
---|
| 87 | When these images are loaded by QML, it looks for a matching image provider
|
---|
| 88 | and calls its requestImage() or requestPixmap() method (depending on its
|
---|
| 89 | imageType()) to load the image. The method is called with the \c id
|
---|
| 90 | parameter set to "yellow" for the first image, and "red" for the second.
|
---|
| 91 |
|
---|
| 92 | Here is an image provider implementation that can load the images
|
---|
| 93 | requested by the above QML. This implementation dynamically
|
---|
| 94 | generates QPixmap images that are filled with the requested color:
|
---|
| 95 |
|
---|
| 96 | \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 0
|
---|
| 97 | \codeline
|
---|
| 98 | \snippet examples/declarative/cppextensions/imageprovider/imageprovider.cpp 1
|
---|
| 99 |
|
---|
| 100 | To make this provider accessible to QML, it is registered with the QML engine
|
---|
| 101 | with a "colors" identifier:
|
---|
| 102 |
|
---|
| 103 | \code
|
---|
| 104 | int main(int argc, char *argv[])
|
---|
| 105 | {
|
---|
| 106 | ...
|
---|
| 107 |
|
---|
| 108 | QDeclarativeEngine engine;
|
---|
| 109 | engine->addImageProvider(QLatin1String("colors"), new ColorPixmapProvider);
|
---|
| 110 |
|
---|
| 111 | ...
|
---|
| 112 | }
|
---|
| 113 | \endcode
|
---|
| 114 |
|
---|
| 115 | Now the images can be successfully loaded in QML:
|
---|
| 116 |
|
---|
| 117 | \image imageprovider.png
|
---|
| 118 |
|
---|
| 119 | A complete example is available in Qt's
|
---|
| 120 | \l {declarative/cppextensions/imageprovider}{examples/declarative/cppextensions/imageprovider}
|
---|
| 121 | directory. Note the example registers the provider via a \l{QDeclarativeExtensionPlugin}{plugin}
|
---|
| 122 | instead of registering it in the application \c main() function as shown above.
|
---|
| 123 |
|
---|
| 124 |
|
---|
| 125 | \section2 Asynchronous image loading
|
---|
| 126 |
|
---|
| 127 | Image providers that support QImage loading automatically include support
|
---|
| 128 | for asychronous loading of images. To enable asynchronous loading for an
|
---|
| 129 | \l Image source, set \l Image::asynchronous to \c true. When this is enabled,
|
---|
| 130 | the image request to the provider is run in a low priority thread,
|
---|
| 131 | allowing image loading to be executed in the background, and reducing the
|
---|
| 132 | performance impact on the user interface.
|
---|
| 133 |
|
---|
| 134 | Asynchronous loading is not supported for image providers that provide
|
---|
| 135 | QPixmap rather than QImage values, as pixmaps can only be created in the
|
---|
| 136 | main thread. In this case, if \l {Image::}{asynchronous} is set to
|
---|
| 137 | \c true, the value is ignored and the image is loaded
|
---|
| 138 | synchronously.
|
---|
| 139 |
|
---|
| 140 | \sa QDeclarativeEngine::addImageProvider()
|
---|
| 141 | */
|
---|
| 142 |
|
---|
| 143 | /*!
|
---|
| 144 | \enum QDeclarativeImageProvider::ImageType
|
---|
| 145 |
|
---|
| 146 | Defines the type of image supported by this image provider.
|
---|
| 147 |
|
---|
| 148 | \value Image The Image Provider provides QImage images. The
|
---|
| 149 | requestImage() method will be called for all image requests.
|
---|
| 150 | \value Pixmap The Image Provider provides QPixmap images. The
|
---|
| 151 | requestPixmap() method will be called for all image requests.
|
---|
| 152 | */
|
---|
| 153 |
|
---|
| 154 | /*!
|
---|
| 155 | Creates an image provider that will provide images of the given \a type.
|
---|
| 156 | */
|
---|
| 157 | QDeclarativeImageProvider::QDeclarativeImageProvider(ImageType type)
|
---|
| 158 | : d(new QDeclarativeImageProviderPrivate)
|
---|
| 159 | {
|
---|
| 160 | d->type = type;
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /*!
|
---|
| 164 | Destroys the QDeclarativeImageProvider
|
---|
| 165 |
|
---|
| 166 | \note The destructor of your derived class need to be thread safe.
|
---|
| 167 | */
|
---|
| 168 | QDeclarativeImageProvider::~QDeclarativeImageProvider()
|
---|
| 169 | {
|
---|
| 170 | delete d;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | /*!
|
---|
| 174 | Returns the image type supported by this provider.
|
---|
| 175 | */
|
---|
| 176 | QDeclarativeImageProvider::ImageType QDeclarativeImageProvider::imageType() const
|
---|
| 177 | {
|
---|
| 178 | return d->type;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | /*!
|
---|
| 182 | Implement this method to return the image with \a id. The default
|
---|
| 183 | implementation returns an empty image.
|
---|
| 184 |
|
---|
| 185 | The \a id is the requested image source, with the "image:" scheme and
|
---|
| 186 | provider identifier removed. For example, if the image \l{Image::}{source}
|
---|
| 187 | was "image://myprovider/icons/home", the given \a id would be "icons/home".
|
---|
| 188 |
|
---|
| 189 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
|
---|
| 190 | an Image element. If \a requestedSize is a valid size, the image
|
---|
| 191 | returned should be of that size.
|
---|
| 192 |
|
---|
| 193 | In all cases, \a size must be set to the original size of the image. This
|
---|
| 194 | is used to set the \l {Item::}{width} and \l {Item::}{height} of the
|
---|
| 195 | relevant \l Image if these values have not been set explicitly.
|
---|
| 196 |
|
---|
| 197 | \note this method may be called by multiple threads, so ensure the
|
---|
| 198 | implementation of this method is reentrant.
|
---|
| 199 | */
|
---|
| 200 | QImage QDeclarativeImageProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize)
|
---|
| 201 | {
|
---|
| 202 | Q_UNUSED(id);
|
---|
| 203 | Q_UNUSED(size);
|
---|
| 204 | Q_UNUSED(requestedSize);
|
---|
| 205 | if (d->type == Image)
|
---|
| 206 | qWarning("ImageProvider supports Image type but has not implemented requestImage()");
|
---|
| 207 | return QImage();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /*!
|
---|
| 211 | Implement this method to return the pixmap with \a id. The default
|
---|
| 212 | implementation returns an empty pixmap.
|
---|
| 213 |
|
---|
| 214 | The \a id is the requested image source, with the "image:" scheme and
|
---|
| 215 | provider identifier removed. For example, if the image \l{Image::}{source}
|
---|
| 216 | was "image://myprovider/icons/home", the given \a id would be "icons/home".
|
---|
| 217 |
|
---|
| 218 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by
|
---|
| 219 | an Image element. If \a requestedSize is a valid size, the image
|
---|
| 220 | returned should be of that size.
|
---|
| 221 |
|
---|
| 222 | In all cases, \a size must be set to the original size of the image. This
|
---|
| 223 | is used to set the \l {Item::}{width} and \l {Item::}{height} of the
|
---|
| 224 | relevant \l Image if these values have not been set explicitly.
|
---|
| 225 | */
|
---|
| 226 | QPixmap QDeclarativeImageProvider::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize)
|
---|
| 227 | {
|
---|
| 228 | Q_UNUSED(id);
|
---|
| 229 | Q_UNUSED(size);
|
---|
| 230 | Q_UNUSED(requestedSize);
|
---|
| 231 | if (d->type == Pixmap)
|
---|
| 232 | qWarning("ImageProvider supports Pixmap type but has not implemented requestPixmap()");
|
---|
| 233 | return QPixmap();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | QT_END_NAMESPACE
|
---|
| 237 |
|
---|