| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | |
| 4 | #include "qquickimageprovider.h" |
| 5 | |
| 6 | #include "qquickimageprovider_p.h" |
| 7 | #include "qquickpixmap_p.h" |
| 8 | #include <QtQuick/private/qsgcontext_p.h> |
| 9 | #include <private/qqmlglobal_p.h> |
| 10 | #include <QtGui/qcolorspace.h> |
| 11 | |
| 12 | QT_BEGIN_NAMESPACE |
| 13 | |
| 14 | /*! |
| 15 | \class QQuickTextureFactory |
| 16 | \since 5.0 |
| 17 | \brief The QQuickTextureFactory class provides an interface for loading custom textures from QML. |
| 18 | \inmodule QtQuick |
| 19 | |
| 20 | The purpose of the texture factory is to provide a placeholder for a image |
| 21 | data that can be converted into an OpenGL texture. |
| 22 | |
| 23 | Creating a texture directly is not possible as there is rarely an OpenGL context |
| 24 | available in the thread that is responsible for loading the image data. |
| 25 | */ |
| 26 | |
| 27 | /*! |
| 28 | Constructs a texture factory. Since QQuickTextureFactory is abstract, it |
| 29 | cannot be instantiated directly. |
| 30 | */ |
| 31 | |
| 32 | QQuickTextureFactory::QQuickTextureFactory() |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | /*! |
| 37 | Destroys the texture factory. |
| 38 | */ |
| 39 | |
| 40 | QQuickTextureFactory::~QQuickTextureFactory() |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | /*! |
| 45 | \fn int QQuickTextureFactory::textureByteCount() const |
| 46 | |
| 47 | Returns the number of bytes of memory the texture consumes. |
| 48 | */ |
| 49 | |
| 50 | /*! |
| 51 | \fn QImage QQuickTextureFactory::image() const |
| 52 | |
| 53 | Returns an image version of this texture. |
| 54 | |
| 55 | The lifespan of the returned image is unknown, so the implementation should |
| 56 | return a self contained QImage, not make use of the QImage(uchar *, ...) |
| 57 | constructor. |
| 58 | |
| 59 | This function is not commonly used and is expected to be slow. |
| 60 | */ |
| 61 | |
| 62 | QImage QQuickTextureFactory::image() const |
| 63 | { |
| 64 | return QImage(); |
| 65 | } |
| 66 | |
| 67 | /*! |
| 68 | Returns a QQuickTextureFactory holding the given \a image. |
| 69 | |
| 70 | This is typically used as a helper in QQuickImageResponse::textureFactory. |
| 71 | |
| 72 | \since 5.6 |
| 73 | */ |
| 74 | |
| 75 | QQuickTextureFactory *QQuickTextureFactory::textureFactoryForImage(const QImage &image) |
| 76 | { |
| 77 | if (image.isNull()) |
| 78 | return nullptr; |
| 79 | QQuickTextureFactory *texture = QSGContext::createTextureFactoryFromImage(image); |
| 80 | if (texture) |
| 81 | return texture; |
| 82 | return new QQuickDefaultTextureFactory(image); |
| 83 | } |
| 84 | |
| 85 | |
| 86 | |
| 87 | /*! |
| 88 | \fn QSGTexture *QQuickTextureFactory::createTexture(QQuickWindow *window) const |
| 89 | |
| 90 | This function is called on the scene graph rendering thread to create a QSGTexture |
| 91 | instance from the factory. \a window provides the context which this texture is |
| 92 | created in. |
| 93 | |
| 94 | QML will internally cache the returned texture as needed. Each call to this |
| 95 | function should return a unique instance. |
| 96 | |
| 97 | The OpenGL context used for rendering is bound when this function is called. |
| 98 | */ |
| 99 | |
| 100 | /*! |
| 101 | \fn QSize QQuickTextureFactory::textureSize() const |
| 102 | |
| 103 | Returns the size of the texture. This function will be called from arbitrary threads |
| 104 | and should not rely on an OpenGL context bound. |
| 105 | */ |
| 106 | |
| 107 | |
| 108 | /*! |
| 109 | \class QQuickImageResponse |
| 110 | \since 5.6 |
| 111 | \brief The QQuickImageResponse class provides an interface for asynchronous image loading in QQuickAsyncImageProvider. |
| 112 | \inmodule QtQuick |
| 113 | |
| 114 | The purpose of an image response is to provide a way for image provider jobs to be executed |
| 115 | in an asynchronous way. |
| 116 | |
| 117 | Responses are deleted via \l deleteLater once the finished() signal has been emitted. |
| 118 | If you are using QRunnable as base for your QQuickImageResponse |
| 119 | ensure automatic deletion is disabled. |
| 120 | |
| 121 | See the \l {imageresponseprovider}{Image Response Provider Example} for a complete implementation. |
| 122 | |
| 123 | \sa QQuickImageProvider |
| 124 | */ |
| 125 | |
| 126 | /*! |
| 127 | Constructs the image response |
| 128 | */ |
| 129 | QQuickImageResponse::QQuickImageResponse() |
| 130 | : QObject(*(new QQuickImageResponsePrivate)) |
| 131 | { |
| 132 | qmlobject_connect(this, QQuickImageResponse, SIGNAL(finished()), |
| 133 | this, QQuickImageResponse, SLOT(_q_finished())); |
| 134 | } |
| 135 | |
| 136 | /*! |
| 137 | Destructs the image response |
| 138 | */ |
| 139 | QQuickImageResponse::~QQuickImageResponse() |
| 140 | { |
| 141 | } |
| 142 | |
| 143 | /*! |
| 144 | Returns the error string for the job execution. An empty string means no error. |
| 145 | */ |
| 146 | QString QQuickImageResponse::errorString() const |
| 147 | { |
| 148 | return QString(); |
| 149 | } |
| 150 | |
| 151 | /*! |
| 152 | This method is used to communicate that the response is no longer required by the engine. |
| 153 | |
| 154 | It may be reimplemented to cancel a request in the provider side, however, it is not mandatory. |
| 155 | |
| 156 | A cancelled QQuickImageResponse still needs to emit finished() so that the |
| 157 | engine may clean up the QQuickImageResponse. |
| 158 | |
| 159 | \note finished() should not be emitted until the response is complete, |
| 160 | regardless of whether or not cancel() was called. If it is called prematurely, |
| 161 | the engine may destroy the response while it is still active, leading to a crash. |
| 162 | */ |
| 163 | void QQuickImageResponse::cancel() |
| 164 | { |
| 165 | } |
| 166 | |
| 167 | /*! |
| 168 | \fn void QQuickImageResponse::finished() |
| 169 | |
| 170 | Signals that the job execution has finished (be it successfully, because an |
| 171 | error happened or because it was cancelled). |
| 172 | |
| 173 | \note Emission of this signal must be the final action the response performs: |
| 174 | once the signal is received, the response will subsequently be destroyed by |
| 175 | the engine. |
| 176 | */ |
| 177 | |
| 178 | /*! |
| 179 | \fn QQuickTextureFactory *QQuickImageResponse::textureFactory() const |
| 180 | |
| 181 | Returns the texture factory for the job. You can use QQuickTextureFactory::textureFactoryForImage |
| 182 | if your provider works with QImage. The engine takes ownership of the returned QQuickTextureFactory. |
| 183 | |
| 184 | \note This method will be called only when needed. For example, it may not be called if there is an |
| 185 | error or the job is cancelled. Therefore, allocate the QQuickTextureFactory instance only in this |
| 186 | method or otherwise ensure its deletion. |
| 187 | */ |
| 188 | |
| 189 | |
| 190 | /*! |
| 191 | \class QQuickImageProvider |
| 192 | \since 5.0 |
| 193 | \inmodule QtQuick |
| 194 | \brief The QQuickImageProvider class provides an interface for supporting pixmaps and threaded image requests in QML. |
| 195 | |
| 196 | QQuickImageProvider is used to provide advanced image loading features |
| 197 | in QML applications. It allows images in QML to be: |
| 198 | |
| 199 | \list |
| 200 | \li Loaded using QPixmaps rather than actual image files |
| 201 | \li Loaded asynchronously in a separate thread |
| 202 | \endlist |
| 203 | |
| 204 | To specify that an image should be loaded by an image provider, use the |
| 205 | \b {"image:"} scheme for the URL source of the image, followed by the |
| 206 | identifiers of the image provider and the requested image. For example: |
| 207 | |
| 208 | \qml |
| 209 | Image { source: "image://myimageprovider/image.png" } |
| 210 | \endqml |
| 211 | |
| 212 | This specifies that the image should be loaded by the image provider named |
| 213 | "myimageprovider", and the image to be loaded is named "image.png". The QML engine |
| 214 | invokes the appropriate image provider according to the providers that have |
| 215 | been registered through QQmlEngine::addImageProvider(). |
| 216 | |
| 217 | Note that the identifiers are case-insensitive, but the rest of the URL will be passed on with |
| 218 | preserved case. For example, the below snippet would still specify that the image is loaded by the |
| 219 | image provider named "myimageprovider", but it would request a different image than the above snippet |
| 220 | ("Image.png" instead of "image.png"). |
| 221 | \qml |
| 222 | Image { source: "image://MyImageProvider/Image.png" } |
| 223 | \endqml |
| 224 | |
| 225 | If you want the rest of the URL to be case insensitive, you will have to take care |
| 226 | of that yourself inside your image provider. |
| 227 | |
| 228 | \section2 An Example |
| 229 | |
| 230 | Here are two images. Their \c source values indicate they should be loaded by |
| 231 | an image provider named "colors", and the images to be loaded are "yellow" |
| 232 | and "red", respectively: |
| 233 | |
| 234 | \snippet imgprovider/imageprovider-example.qml 0 |
| 235 | |
| 236 | When these images are loaded by QML, it looks for a matching image provider |
| 237 | and calls its requestImage() or requestPixmap() method (depending on its |
| 238 | imageType()) to load the image. The method is called with the \c id |
| 239 | parameter set to "yellow" for the first image, and "red" for the second. |
| 240 | |
| 241 | Here is an image provider implementation that can load the images |
| 242 | requested by the above QML. This implementation dynamically |
| 243 | generates QPixmap images that are filled with the requested color: |
| 244 | |
| 245 | \snippet imgprovider/imageprovider.cpp 0 |
| 246 | |
| 247 | To make this provider accessible to QML, it is registered with the QML engine |
| 248 | with a "colors" identifier: |
| 249 | |
| 250 | \snippet imgprovider/imageprovider.cpp 1 |
| 251 | \codeline |
| 252 | \snippet imgprovider/imageprovider.cpp 2 |
| 253 | |
| 254 | Now the images can be successfully loaded in QML: |
| 255 | |
| 256 | \image imageprovider.png |
| 257 | |
| 258 | See the \l {imageprovider}{Image Provider Example} for the complete implementation. |
| 259 | Note that the example registers the provider via a \l{QQmlEngineExtensionPlugin}{plugin} |
| 260 | instead of registering it in the application \c main() function as shown above. |
| 261 | |
| 262 | It is possible to provide \l {High Resolution Versions of Images}{"@nx" high DPI syntax}. |
| 263 | |
| 264 | |
| 265 | \section2 Asynchronous Image Loading |
| 266 | |
| 267 | Image providers that support QImage or Texture loading automatically include support |
| 268 | for asychronous loading of images. To enable asynchronous loading for an |
| 269 | image source, set the \c asynchronous property to \c true for the relevant |
| 270 | \l Image or \l BorderImage object. When this is enabled, |
| 271 | the image request to the provider is run in a low priority thread, |
| 272 | allowing image loading to be executed in the background, and reducing the |
| 273 | performance impact on the user interface. |
| 274 | |
| 275 | To force asynchronous image loading, even for image sources that do not |
| 276 | have the \c asynchronous property set to \c true, you may pass the |
| 277 | \c QQmlImageProviderBase::ForceAsynchronousImageLoading flag to the image |
| 278 | provider constructor. This ensures that all image requests for the |
| 279 | provider are handled in a separate thread. |
| 280 | |
| 281 | Asynchronous loading for image providers that provide QPixmap is only supported |
| 282 | in platforms that have the ThreadedPixmaps feature, in platforms where |
| 283 | pixmaps can only be created in the main thread (i.e. ThreadedPixmaps is not supported) |
| 284 | if \l {Image::}{asynchronous} is set to \c true, the value is ignored |
| 285 | and the image is loaded synchronously. |
| 286 | |
| 287 | Asynchronous image loading for providers of type other than ImageResponse are |
| 288 | executed on a single thread per engine basis. That means that a slow image provider |
| 289 | will block the loading of any other request. To avoid that we suggest using QQuickAsyncImageProvider |
| 290 | and implement threading on the provider side via a \c QThreadPool or similar. |
| 291 | See the \l {imageresponseprovider}{Image Response Provider Example} for a complete implementation. |
| 292 | |
| 293 | |
| 294 | \section2 Image Caching |
| 295 | |
| 296 | Images returned by a QQuickImageProvider are automatically cached, |
| 297 | similar to any image loaded by the QML engine. When an image with a |
| 298 | "image://" prefix is loaded from cache, requestImage() and requestPixmap() |
| 299 | will not be called for the relevant image provider. If an image should |
| 300 | always be fetched from the image provider, and should not be cached at |
| 301 | all, set the \c cache property to \c false for the relevant \l Image |
| 302 | or \l BorderImage object. |
| 303 | |
| 304 | \sa QQmlEngine::addImageProvider() |
| 305 | */ |
| 306 | |
| 307 | /*! |
| 308 | Creates an image provider that will provide images of the given \a type and |
| 309 | behave according to the given \a flags. |
| 310 | */ |
| 311 | QQuickImageProvider::QQuickImageProvider(ImageType type, Flags flags) |
| 312 | : d(new QQuickImageProviderPrivate) |
| 313 | { |
| 314 | d->type = type; |
| 315 | d->flags = flags; |
| 316 | d->isProviderWithOptions = false; |
| 317 | } |
| 318 | |
| 319 | /*! |
| 320 | Destroys the QQuickImageProvider |
| 321 | |
| 322 | \note The destructor of your derived class need to be thread safe. |
| 323 | */ |
| 324 | QQuickImageProvider::~QQuickImageProvider() |
| 325 | { |
| 326 | delete d; |
| 327 | } |
| 328 | |
| 329 | /*! |
| 330 | Returns the image type supported by this provider. |
| 331 | */ |
| 332 | QQuickImageProvider::ImageType QQuickImageProvider::imageType() const |
| 333 | { |
| 334 | return d->type; |
| 335 | } |
| 336 | |
| 337 | /*! |
| 338 | Returns the flags set for this provider. |
| 339 | */ |
| 340 | QQuickImageProvider::Flags QQuickImageProvider::flags() const |
| 341 | { |
| 342 | return d->flags; |
| 343 | } |
| 344 | |
| 345 | /*! |
| 346 | Implement this method to return the image with \a id. The default |
| 347 | implementation returns an empty image. |
| 348 | |
| 349 | The \a id is the requested image source, with the "image:" scheme and |
| 350 | provider identifier removed. For example, if the image \l{Image::}{source} |
| 351 | was "image://myprovider/icons/home", the given \a id would be "icons/home". |
| 352 | |
| 353 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by |
| 354 | an Image item. If \a requestedSize is a valid size, the image |
| 355 | returned should be of that size. |
| 356 | |
| 357 | In all cases, \a size must be set to the original size of the image. This |
| 358 | is used to set the \l {Item::}{width} and \l {Item::}{height} of the |
| 359 | relevant \l Image if these values have not been set explicitly. |
| 360 | |
| 361 | \note this method may be called by multiple threads, so ensure the |
| 362 | implementation of this method is reentrant. |
| 363 | */ |
| 364 | QImage QQuickImageProvider::requestImage(const QString &id, QSize *size, const QSize& requestedSize) |
| 365 | { |
| 366 | Q_UNUSED(id); |
| 367 | Q_UNUSED(size); |
| 368 | Q_UNUSED(requestedSize); |
| 369 | if (d->type == Image) |
| 370 | qWarning(msg: "ImageProvider supports Image type but has not implemented requestImage()" ); |
| 371 | return QImage(); |
| 372 | } |
| 373 | |
| 374 | /*! |
| 375 | Implement this method to return the pixmap with \a id. The default |
| 376 | implementation returns an empty pixmap. |
| 377 | |
| 378 | The \a id is the requested image source, with the "image:" scheme and |
| 379 | provider identifier removed. For example, if the image \l{Image::}{source} |
| 380 | was "image://myprovider/icons/home", the given \a id would be "icons/home". |
| 381 | |
| 382 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by |
| 383 | an Image item. If \a requestedSize is a valid size, the image |
| 384 | returned should be of that size. |
| 385 | |
| 386 | In all cases, \a size must be set to the original size of the image. This |
| 387 | is used to set the \l {Item::}{width} and \l {Item::}{height} of the |
| 388 | relevant \l Image if these values have not been set explicitly. |
| 389 | |
| 390 | \note this method may be called by multiple threads, so ensure the |
| 391 | implementation of this method is reentrant. |
| 392 | */ |
| 393 | QPixmap QQuickImageProvider::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize) |
| 394 | { |
| 395 | Q_UNUSED(id); |
| 396 | Q_UNUSED(size); |
| 397 | Q_UNUSED(requestedSize); |
| 398 | if (d->type == Pixmap) |
| 399 | qWarning(msg: "ImageProvider supports Pixmap type but has not implemented requestPixmap()" ); |
| 400 | return QPixmap(); |
| 401 | } |
| 402 | |
| 403 | |
| 404 | /*! |
| 405 | Implement this method to return the texture with \a id. The default |
| 406 | implementation returns \nullptr. |
| 407 | |
| 408 | The \a id is the requested image source, with the "image:" scheme and |
| 409 | provider identifier removed. For example, if the image \l{Image::}{source} |
| 410 | was "image://myprovider/icons/home", the given \a id would be "icons/home". |
| 411 | |
| 412 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by |
| 413 | an Image item. If \a requestedSize is a valid size, the image |
| 414 | returned should be of that size. |
| 415 | |
| 416 | In all cases, \a size must be set to the original size of the image. This |
| 417 | is used to set the \l {Item::}{width} and \l {Item::}{height} of the |
| 418 | relevant \l Image if these values have not been set explicitly. |
| 419 | |
| 420 | \note this method may be called by multiple threads, so ensure the |
| 421 | implementation of this method is reentrant. |
| 422 | */ |
| 423 | |
| 424 | QQuickTextureFactory *QQuickImageProvider::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) |
| 425 | { |
| 426 | Q_UNUSED(id); |
| 427 | Q_UNUSED(size); |
| 428 | Q_UNUSED(requestedSize); |
| 429 | if (d->type == Texture) |
| 430 | qWarning(msg: "ImageProvider supports Texture type but has not implemented requestTexture()" ); |
| 431 | return nullptr; |
| 432 | } |
| 433 | |
| 434 | /*! |
| 435 | \class QQuickAsyncImageProvider |
| 436 | \since 5.6 |
| 437 | \inmodule QtQuick |
| 438 | \brief The QQuickAsyncImageProvider class provides an interface for asynchronous control of QML image requests. |
| 439 | |
| 440 | See the \l {imageresponseprovider}{Image Response Provider Example} for a complete implementation. |
| 441 | |
| 442 | \sa QQuickImageProvider |
| 443 | */ |
| 444 | QQuickAsyncImageProvider::QQuickAsyncImageProvider() |
| 445 | : QQuickImageProvider(ImageResponse, ForceAsynchronousImageLoading) |
| 446 | , d(nullptr) // just as a placeholder in case we need it for the future |
| 447 | { |
| 448 | Q_UNUSED(d); |
| 449 | } |
| 450 | |
| 451 | QQuickAsyncImageProvider::~QQuickAsyncImageProvider() |
| 452 | { |
| 453 | } |
| 454 | |
| 455 | /*! |
| 456 | \fn QQuickImageResponse *QQuickAsyncImageProvider::requestImageResponse(const QString &id, const QSize &requestedSize) |
| 457 | |
| 458 | Implement this method to return the job that will provide the texture with \a id. |
| 459 | |
| 460 | The \a id is the requested image source, with the "image:" scheme and |
| 461 | provider identifier removed. For example, if the image \l{Image::}{source} |
| 462 | was "image://myprovider/icons/home", the given \a id would be "icons/home". |
| 463 | |
| 464 | The \a requestedSize corresponds to the \l {Image::sourceSize} requested by |
| 465 | an Image item. If \a requestedSize is a valid size, the image |
| 466 | returned should be of that size. |
| 467 | |
| 468 | \note this method may be called by multiple threads, so ensure the |
| 469 | implementation of this method is reentrant. |
| 470 | */ |
| 471 | |
| 472 | |
| 473 | class QQuickImageProviderOptionsPrivate : public QSharedData |
| 474 | { |
| 475 | public: |
| 476 | QQuickImageProviderOptionsPrivate() |
| 477 | { |
| 478 | } |
| 479 | |
| 480 | QColorSpace targetColorSpace; |
| 481 | QRectF sourceClipRect; |
| 482 | QQuickImageProviderOptions::AutoTransform autoTransform = QQuickImageProviderOptions::UsePluginDefaultTransform; |
| 483 | bool preserveAspectRatioCrop = false; |
| 484 | bool preserveAspectRatioFit = false; |
| 485 | }; |
| 486 | |
| 487 | /*! |
| 488 | \class QQuickImageProviderOptions |
| 489 | \brief The QQuickImageProviderOptions class provides options for QQuickImageProviderWithOptions image requests. |
| 490 | \inmodule QtQuick |
| 491 | \internal |
| 492 | |
| 493 | \sa QQuickImageProviderWithOptions |
| 494 | */ |
| 495 | |
| 496 | /*! |
| 497 | \enum QQuickImageProviderOptions::AutoTransform |
| 498 | |
| 499 | Whether the image provider should apply transformation metadata on read(). |
| 500 | |
| 501 | \value UsePluginDefaultTransform Image provider should do its default behavior on whether applying transformation metadata on read or not |
| 502 | \value ApplyTransform Image provider should apply transformation metadata on read |
| 503 | \value DoNotApplyTransform Image provider should not apply transformation metadata on read |
| 504 | */ |
| 505 | |
| 506 | QQuickImageProviderOptions::QQuickImageProviderOptions() |
| 507 | : d(new QQuickImageProviderOptionsPrivate()) |
| 508 | { |
| 509 | } |
| 510 | |
| 511 | QQuickImageProviderOptions::~QQuickImageProviderOptions() |
| 512 | { |
| 513 | } |
| 514 | |
| 515 | QQuickImageProviderOptions::QQuickImageProviderOptions(const QQuickImageProviderOptions &other) |
| 516 | : d(other.d) |
| 517 | { |
| 518 | } |
| 519 | |
| 520 | QQuickImageProviderOptions& QQuickImageProviderOptions::operator=(const QQuickImageProviderOptions &other) |
| 521 | { |
| 522 | d = other.d; |
| 523 | return *this; |
| 524 | } |
| 525 | |
| 526 | bool QQuickImageProviderOptions::operator==(const QQuickImageProviderOptions &other) const |
| 527 | { |
| 528 | return d->autoTransform == other.d->autoTransform && |
| 529 | d->preserveAspectRatioCrop == other.d->preserveAspectRatioCrop && |
| 530 | d->preserveAspectRatioFit == other.d->preserveAspectRatioFit && |
| 531 | d->targetColorSpace == other.d->targetColorSpace; |
| 532 | } |
| 533 | |
| 534 | /*! |
| 535 | Returns whether the image provider should apply transformation metadata on read(). |
| 536 | */ |
| 537 | QQuickImageProviderOptions::AutoTransform QQuickImageProviderOptions::autoTransform() const |
| 538 | { |
| 539 | return d->autoTransform; |
| 540 | } |
| 541 | |
| 542 | void QQuickImageProviderOptions::setAutoTransform(QQuickImageProviderOptions::AutoTransform autoTransform) |
| 543 | { |
| 544 | d->autoTransform = autoTransform; |
| 545 | } |
| 546 | |
| 547 | /*! |
| 548 | Returns whether the image request is for a PreserveAspectCrop Image. |
| 549 | This allows the provider to better optimize the size of the returned image. |
| 550 | */ |
| 551 | bool QQuickImageProviderOptions::preserveAspectRatioCrop() const |
| 552 | { |
| 553 | return d->preserveAspectRatioCrop; |
| 554 | } |
| 555 | |
| 556 | void QQuickImageProviderOptions::setPreserveAspectRatioCrop(bool preserveAspectRatioCrop) |
| 557 | { |
| 558 | d->preserveAspectRatioCrop = preserveAspectRatioCrop; |
| 559 | } |
| 560 | |
| 561 | /*! |
| 562 | Returns whether the image request is for a PreserveAspectFit Image. |
| 563 | This allows the provider to better optimize the size of the returned image. |
| 564 | */ |
| 565 | bool QQuickImageProviderOptions::preserveAspectRatioFit() const |
| 566 | { |
| 567 | return d->preserveAspectRatioFit; |
| 568 | } |
| 569 | |
| 570 | void QQuickImageProviderOptions::setPreserveAspectRatioFit(bool preserveAspectRatioFit) |
| 571 | { |
| 572 | d->preserveAspectRatioFit = preserveAspectRatioFit; |
| 573 | } |
| 574 | |
| 575 | /*! |
| 576 | Returns the color space the image provider should return the image in. |
| 577 | */ |
| 578 | QColorSpace QQuickImageProviderOptions::targetColorSpace() const |
| 579 | { |
| 580 | return d->targetColorSpace; |
| 581 | } |
| 582 | |
| 583 | void QQuickImageProviderOptions::setTargetColorSpace(const QColorSpace &colorSpace) |
| 584 | { |
| 585 | d->targetColorSpace = colorSpace; |
| 586 | } |
| 587 | |
| 588 | /*! |
| 589 | Returns the requested source clip rect. |
| 590 | */ |
| 591 | QRectF QQuickImageProviderOptions::sourceClipRect() const |
| 592 | { |
| 593 | return d->sourceClipRect; |
| 594 | } |
| 595 | |
| 596 | void QQuickImageProviderOptions::setSourceClipRect(const QRectF &rect) |
| 597 | { |
| 598 | d->sourceClipRect = rect; |
| 599 | } |
| 600 | |
| 601 | QQuickImageProviderWithOptions::QQuickImageProviderWithOptions(ImageType type, Flags flags) |
| 602 | : QQuickAsyncImageProvider() |
| 603 | { |
| 604 | QQuickImageProvider::d->type = type; |
| 605 | QQuickImageProvider::d->flags = flags; |
| 606 | QQuickImageProvider::d->isProviderWithOptions = true; |
| 607 | } |
| 608 | |
| 609 | QImage QQuickImageProviderWithOptions::requestImage(const QString &id, QSize *size, const QSize& requestedSize) |
| 610 | { |
| 611 | return requestImage(id, size, requestedSize, options: QQuickImageProviderOptions()); |
| 612 | } |
| 613 | |
| 614 | QPixmap QQuickImageProviderWithOptions::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize) |
| 615 | { |
| 616 | return requestPixmap(id, size, requestedSize, options: QQuickImageProviderOptions()); |
| 617 | } |
| 618 | |
| 619 | QQuickTextureFactory *QQuickImageProviderWithOptions::requestTexture(const QString &id, QSize *size, const QSize &requestedSize) |
| 620 | { |
| 621 | return requestTexture(id, size, requestedSize, options: QQuickImageProviderOptions()); |
| 622 | } |
| 623 | |
| 624 | QImage QQuickImageProviderWithOptions::requestImage(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options) |
| 625 | { |
| 626 | Q_UNUSED(options); |
| 627 | return QQuickAsyncImageProvider::requestImage(id, size, requestedSize); |
| 628 | } |
| 629 | |
| 630 | QPixmap QQuickImageProviderWithOptions::requestPixmap(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options) |
| 631 | { |
| 632 | Q_UNUSED(options); |
| 633 | return QQuickAsyncImageProvider::requestPixmap(id, size, requestedSize); |
| 634 | } |
| 635 | |
| 636 | QQuickTextureFactory *QQuickImageProviderWithOptions::requestTexture(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options) |
| 637 | { |
| 638 | Q_UNUSED(options); |
| 639 | return QQuickAsyncImageProvider::requestTexture(id, size, requestedSize); |
| 640 | } |
| 641 | |
| 642 | QQuickImageResponse *QQuickImageProviderWithOptions::requestImageResponse(const QString &id, const QSize &requestedSize) |
| 643 | { |
| 644 | Q_UNUSED(id); |
| 645 | Q_UNUSED(requestedSize); |
| 646 | if (imageType() == ImageResponse) |
| 647 | qWarning(msg: "ImageProvider is of ImageResponse type but has not implemented requestImageResponse()" ); |
| 648 | return nullptr; |
| 649 | } |
| 650 | |
| 651 | QQuickImageResponse *QQuickImageProviderWithOptions::requestImageResponse(const QString &id, const QSize &requestedSize, const QQuickImageProviderOptions &options) |
| 652 | { |
| 653 | Q_UNUSED(options); |
| 654 | return requestImageResponse(id, requestedSize); |
| 655 | } |
| 656 | |
| 657 | /*! |
| 658 | Returns the recommended scaled image size for loading and storage. This is |
| 659 | calculated according to the native pixel size of the image \a originalSize, |
| 660 | the requested sourceSize \a requestedSize, the image file format \a format, |
| 661 | and \a options. If the calculation otherwise concludes that scaled loading |
| 662 | is not recommended, an invalid size is returned. |
| 663 | */ |
| 664 | QSize QQuickImageProviderWithOptions::loadSize(const QSize &originalSize, const QSize &requestedSize, const QByteArray &format, const QQuickImageProviderOptions &options, |
| 665 | qreal devicePixelRatio) |
| 666 | { |
| 667 | QSize res; |
| 668 | const bool formatIsScalable = (format == "svg" || format == "svgz" || format == "pdf" ); |
| 669 | const bool noRequestedSize = requestedSize.width() <= 0 && requestedSize.height() <= 0; |
| 670 | if ((noRequestedSize && !formatIsScalable) || originalSize.isEmpty()) |
| 671 | return res; |
| 672 | |
| 673 | // If no sourceSize was set and we're loading an SVG, ensure that we provide |
| 674 | // a default size that accounts for DPR so that the image isn't blurry. |
| 675 | if (noRequestedSize && formatIsScalable) |
| 676 | return originalSize * devicePixelRatio; |
| 677 | |
| 678 | const bool preserveAspectCropOrFit = options.preserveAspectRatioCrop() || options.preserveAspectRatioFit(); |
| 679 | |
| 680 | if (!preserveAspectCropOrFit && formatIsScalable && !requestedSize.isEmpty()) |
| 681 | return requestedSize; |
| 682 | |
| 683 | qreal ratio = 0.0; |
| 684 | if (requestedSize.width() && (preserveAspectCropOrFit || formatIsScalable || |
| 685 | requestedSize.width() < originalSize.width())) { |
| 686 | ratio = qreal(requestedSize.width()) / originalSize.width(); |
| 687 | } |
| 688 | if (requestedSize.height() && (preserveAspectCropOrFit || formatIsScalable || |
| 689 | requestedSize.height() < originalSize.height())) { |
| 690 | qreal hr = qreal(requestedSize.height()) / originalSize.height(); |
| 691 | if (ratio == 0.0) |
| 692 | ratio = hr; |
| 693 | else if (!preserveAspectCropOrFit && (hr < ratio)) |
| 694 | ratio = hr; |
| 695 | else if (preserveAspectCropOrFit && (hr > ratio)) |
| 696 | ratio = hr; |
| 697 | } |
| 698 | if (ratio > 0.0) { |
| 699 | res.setHeight(qRound(d: originalSize.height() * ratio)); |
| 700 | res.setWidth(qRound(d: originalSize.width() * ratio)); |
| 701 | } |
| 702 | return res; |
| 703 | } |
| 704 | |
| 705 | QQuickImageProviderWithOptions *QQuickImageProviderWithOptions::checkedCast(QQuickImageProvider *provider) |
| 706 | { |
| 707 | if (provider && provider->d && provider->d->isProviderWithOptions) |
| 708 | return static_cast<QQuickImageProviderWithOptions *>(provider); |
| 709 | |
| 710 | return nullptr; |
| 711 | } |
| 712 | |
| 713 | QT_END_NAMESPACE |
| 714 | |
| 715 | #include "moc_qquickimageprovider.cpp" |
| 716 | |