Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickimageprovider.cpp
Go to the documentation of this file.
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
5
8#include <QtQuick/private/qsgcontext_p.h>
9#include <private/qqmlglobal_p.h>
10#include <QtGui/qcolorspace.h>
11
13
33{
34}
35
41{
42}
43
63{
64 return QImage();
65}
66
76{
77 if (image.isNull())
78 return nullptr;
80 if (texture)
81 return texture;
83}
84
85
86
131{
133 this, QQuickImageResponse, SLOT(_q_finished()));
134}
135
140{
141}
142
147{
148 return QString();
149}
150
164{
165}
166
311{
312 d->type = type;
313 d->flags = flags;
314 d->isProviderWithOptions = false;
315}
316
323{
324 delete d;
325}
326
331{
332 return d->type;
333}
334
338QQuickImageProvider::Flags QQuickImageProvider::flags() const
339{
340 return d->flags;
341}
342
363{
364 Q_UNUSED(id);
365 Q_UNUSED(size);
366 Q_UNUSED(requestedSize);
367 if (d->type == Image)
368 qWarning("ImageProvider supports Image type but has not implemented requestImage()");
369 return QImage();
370}
371
392{
393 Q_UNUSED(id);
394 Q_UNUSED(size);
395 Q_UNUSED(requestedSize);
396 if (d->type == Pixmap)
397 qWarning("ImageProvider supports Pixmap type but has not implemented requestPixmap()");
398 return QPixmap();
399}
400
401
423{
424 Q_UNUSED(id);
425 Q_UNUSED(size);
426 Q_UNUSED(requestedSize);
427 if (d->type == Texture)
428 qWarning("ImageProvider supports Texture type but has not implemented requestTexture()");
429 return nullptr;
430}
431
443 : QQuickImageProvider(ImageResponse, ForceAsynchronousImageLoading)
444 , d(nullptr) // just as a placeholder in case we need it for the future
445{
446 Q_UNUSED(d);
447}
448
450{
451}
452
472{
473public:
475 {
476 }
477
482};
483
505{
506}
507
509{
510}
511
513 : d(other.d)
514{
515}
516
518{
519 d = other.d;
520 return *this;
521}
522
524{
525 return d->autoTransform == other.d->autoTransform &&
526 d->preserveAspectRatioCrop == other.d->preserveAspectRatioCrop &&
527 d->preserveAspectRatioFit == other.d->preserveAspectRatioFit &&
528 d->targetColorSpace == other.d->targetColorSpace;
529}
530
535{
536 return d->autoTransform;
537}
538
540{
542}
543
549{
550 return d->preserveAspectRatioCrop;
551}
552
554{
556}
557
563{
564 return d->preserveAspectRatioFit;
565}
566
568{
570}
571
576{
577 return d->targetColorSpace;
578}
579
581{
582 d->targetColorSpace = colorSpace;
583}
584
587{
588 QQuickImageProvider::d->type = type;
589 QQuickImageProvider::d->flags = flags;
590 QQuickImageProvider::d->isProviderWithOptions = true;
591}
592
594{
595 return requestImage(id, size, requestedSize, QQuickImageProviderOptions());
596}
597
599{
600 return requestPixmap(id, size, requestedSize, QQuickImageProviderOptions());
601}
602
604{
605 return requestTexture(id, size, requestedSize, QQuickImageProviderOptions());
606}
607
609{
610 Q_UNUSED(options);
611 return QQuickAsyncImageProvider::requestImage(id, size, requestedSize);
612}
613
615{
616 Q_UNUSED(options);
617 return QQuickAsyncImageProvider::requestPixmap(id, size, requestedSize);
618}
619
621{
622 Q_UNUSED(options);
623 return QQuickAsyncImageProvider::requestTexture(id, size, requestedSize);
624}
625
627{
628 Q_UNUSED(id);
629 Q_UNUSED(requestedSize);
630 if (imageType() == ImageResponse)
631 qWarning("ImageProvider is of ImageResponse type but has not implemented requestImageResponse()");
632 return nullptr;
633}
634
636{
637 Q_UNUSED(options);
638 return requestImageResponse(id, requestedSize);
639}
640
648QSize QQuickImageProviderWithOptions::loadSize(const QSize &originalSize, const QSize &requestedSize, const QByteArray &format, const QQuickImageProviderOptions &options,
649 qreal devicePixelRatio)
650{
651 QSize res;
652 const bool formatIsScalable = (format == "svg" || format == "svgz" || format == "pdf");
653 const bool noRequestedSize = requestedSize.width() <= 0 && requestedSize.height() <= 0;
654 if ((noRequestedSize && !formatIsScalable) || originalSize.isEmpty())
655 return res;
656
657 // If no sourceSize was set and we're loading an SVG, ensure that we provide
658 // a default size that accounts for DPR so that the image isn't blurry.
659 if (noRequestedSize && formatIsScalable)
660 return originalSize * devicePixelRatio;
661
662 const bool preserveAspectCropOrFit = options.preserveAspectRatioCrop() || options.preserveAspectRatioFit();
663
664 if (!preserveAspectCropOrFit && formatIsScalable && !requestedSize.isEmpty())
665 return requestedSize;
666
667 qreal ratio = 0.0;
668 if (requestedSize.width() && (preserveAspectCropOrFit || formatIsScalable ||
669 requestedSize.width() < originalSize.width())) {
670 ratio = qreal(requestedSize.width()) / originalSize.width();
671 }
672 if (requestedSize.height() && (preserveAspectCropOrFit || formatIsScalable ||
673 requestedSize.height() < originalSize.height())) {
674 qreal hr = qreal(requestedSize.height()) / originalSize.height();
675 if (ratio == 0.0)
676 ratio = hr;
677 else if (!preserveAspectCropOrFit && (hr < ratio))
678 ratio = hr;
679 else if (preserveAspectCropOrFit && (hr > ratio))
680 ratio = hr;
681 }
682 if (ratio > 0.0) {
683 res.setHeight(qRound(originalSize.height() * ratio));
684 res.setWidth(qRound(originalSize.width() * ratio));
685 }
686 return res;
687}
688
690{
691 if (provider && provider->d && provider->d->isProviderWithOptions)
692 return static_cast<QQuickImageProviderWithOptions *>(provider);
693
694 return nullptr;
695}
696
698
699#include "moc_qquickimageprovider.cpp"
\inmodule QtCore
Definition qbytearray.h:57
The QColorSpace class provides a color space abstraction.
Definition qcolorspace.h:21
\inmodule QtGui
Definition qimage.h:37
\inmodule QtCore
Definition qobject.h:90
Returns a copy of the pixmap that is transformed using the given transformation transform and transfo...
Definition qpixmap.h:27
friend class QQuickImageProvider
Definition qqmlengine.h:40
ImageType
Defines the type of image supported by this image provider.
Definition qqmlengine.h:21
The QQuickAsyncImageProvider class provides an interface for asynchronous control of QML image reques...
QQuickImageProviderOptions::AutoTransform autoTransform
The QQuickImageProviderOptions class provides options for QQuickImageProviderWithOptions image reques...
bool operator==(const QQuickImageProviderOptions &) const
bool preserveAspectRatioFit() const
Returns whether the image request is for a PreserveAspectFit Image.
void setPreserveAspectRatioCrop(bool preserveAspectRatioCrop)
QColorSpace targetColorSpace() const
Returns the color space the image provider should return the image in.
AutoTransform
Whether the image provider should apply transformation metadata on read().
AutoTransform autoTransform() const
Returns whether the image provider should apply transformation metadata on read().
void setPreserveAspectRatioFit(bool preserveAspectRatioFit)
void setAutoTransform(AutoTransform autoTransform)
QQuickImageProviderOptions & operator=(const QQuickImageProviderOptions &)
bool preserveAspectRatioCrop() const
Returns whether the image request is for a PreserveAspectCrop Image.
void setTargetColorSpace(const QColorSpace &colorSpace)
QQuickImageProvider::ImageType type
QQuickImageProvider::Flags flags
static QQuickImageProviderWithOptions * checkedCast(QQuickImageProvider *provider)
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize) override
Implement this method to return the pixmap with id.
QQuickTextureFactory * requestTexture(const QString &id, QSize *size, const QSize &requestedSize) override
Implement this method to return the texture with id.
QQuickImageResponse * requestImageResponse(const QString &id, const QSize &requestedSize) override
Implement this method to return the job that will provide the texture with id.
static QSize loadSize(const QSize &originalSize, const QSize &requestedSize, const QByteArray &format, const QQuickImageProviderOptions &options, qreal devicePixelRatio=1.0)
Returns the recommended scaled image size for loading and storage.
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override
Implement this method to return the image with id.
The QQuickImageProvider class provides an interface for supporting pixmaps and threaded image request...
ImageType imageType() const override
Returns the image type supported by this provider.
virtual QQuickTextureFactory * requestTexture(const QString &id, QSize *size, const QSize &requestedSize)
Implement this method to return the texture with id.
Flags flags() const override
Returns the flags set for this provider.
friend class QQuickImageProviderWithOptions
virtual QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)
Implement this method to return the image with id.
virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
Implement this method to return the pixmap with id.
~QQuickImageProvider() override
Destroys the QQuickImageProvider.
The QQuickImageResponse class provides an interface for asynchronous image loading in QQuickAsyncImag...
virtual QString errorString() const
Returns the error string for the job execution.
void finished()
Signals that the job execution has finished (be it successfully, because an error happened or because...
~QQuickImageResponse() override
Destructs the image response.
QQuickImageResponse()
Constructs the image response.
virtual void cancel()
This method is used to communicate that the response is no longer required by the engine.
The QQuickTextureFactory class provides an interface for loading custom textures from QML....
~QQuickTextureFactory() override
Destroys the texture factory.
virtual QImage image() const
Returns an image version of this texture.
static QQuickTextureFactory * textureFactoryForImage(const QImage &image)
Returns a QQuickTextureFactory holding the given image.
QQuickTextureFactory()
Constructs a texture factory.
static QQuickTextureFactory * createTextureFactoryFromImage(const QImage &image)
Calls into the scene graph adaptation if available and creates a texture factory.
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
constexpr bool isEmpty() const noexcept
Returns true if either of the width and height is less than or equal to 0; otherwise returns false.
Definition qsize.h:123
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
Combined button and popup list for selecting options.
Definition image.cpp:4
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:281
Flags
#define qWarning
Definition qlogging.h:162
#define SLOT(a)
Definition qobjectdefs.h:51
#define SIGNAL(a)
Definition qobjectdefs.h:52
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLenum type
GLbitfield flags
GLenum GLuint texture
GLint GLsizei GLsizei GLenum format
GLuint res
XID Pixmap
#define qmlobject_connect(Sender, SenderType, Signal, Receiver, ReceiverType, Method)
Connect Signal of Sender to Method of Receiver.
#define Q_UNUSED(x)
double qreal
Definition qtypes.h:92
QObject::connect nullptr
QSharedPointer< T > other(t)
[5]