| 1 | // Copyright (C) 2023 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 | #ifndef QQUICKPIXMAP_H |
| 5 | #define QQUICKPIXMAP_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtCore/qcoreapplication.h> |
| 19 | #include <QtCore/qstring.h> |
| 20 | #include <QtGui/qpixmap.h> |
| 21 | #include <QtCore/qmutex.h> |
| 22 | #include <QtCore/qurl.h> |
| 23 | #include <private/qtquickglobal_p.h> |
| 24 | #include <QtQuick/qquickimageprovider.h> |
| 25 | |
| 26 | QT_BEGIN_NAMESPACE |
| 27 | |
| 28 | class QQmlEngine; |
| 29 | class QQuickPixmapData; |
| 30 | class QQuickTextureFactory; |
| 31 | class QQuickImageProviderOptionsPrivate; |
| 32 | |
| 33 | class QQuickDefaultTextureFactory : public QQuickTextureFactory |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | public: |
| 37 | QQuickDefaultTextureFactory(const QImage &i); |
| 38 | QSGTexture *createTexture(QQuickWindow *window) const override; |
| 39 | QSize textureSize() const override { return size; } |
| 40 | int textureByteCount() const override { return size.width() * size.height() * 4; } |
| 41 | QImage image() const override { return im; } |
| 42 | |
| 43 | private: |
| 44 | QImage im; |
| 45 | QSize size; |
| 46 | }; |
| 47 | |
| 48 | class QQuickImageProviderPrivate |
| 49 | { |
| 50 | public: |
| 51 | QQuickImageProvider::ImageType type; |
| 52 | QQuickImageProvider::Flags flags; |
| 53 | bool isProviderWithOptions; |
| 54 | }; |
| 55 | |
| 56 | // ### Qt 6: Make public moving to qquickimageprovider.h |
| 57 | class Q_QUICK_EXPORT QQuickImageProviderOptions |
| 58 | { |
| 59 | public: |
| 60 | enum AutoTransform { |
| 61 | UsePluginDefaultTransform = -1, |
| 62 | ApplyTransform = 0, |
| 63 | DoNotApplyTransform = 1 |
| 64 | }; |
| 65 | |
| 66 | QQuickImageProviderOptions(); |
| 67 | ~QQuickImageProviderOptions(); |
| 68 | |
| 69 | QQuickImageProviderOptions(const QQuickImageProviderOptions&); |
| 70 | QQuickImageProviderOptions& operator=(const QQuickImageProviderOptions&); |
| 71 | |
| 72 | bool operator==(const QQuickImageProviderOptions&) const; |
| 73 | |
| 74 | AutoTransform autoTransform() const; |
| 75 | void setAutoTransform(AutoTransform autoTransform); |
| 76 | |
| 77 | bool preserveAspectRatioCrop() const; |
| 78 | void setPreserveAspectRatioCrop(bool preserveAspectRatioCrop); |
| 79 | |
| 80 | bool preserveAspectRatioFit() const; |
| 81 | void setPreserveAspectRatioFit(bool preserveAspectRatioFit); |
| 82 | |
| 83 | QColorSpace targetColorSpace() const; |
| 84 | void setTargetColorSpace(const QColorSpace &colorSpace); |
| 85 | |
| 86 | QRectF sourceClipRect() const; |
| 87 | void setSourceClipRect(const QRectF &rect); |
| 88 | |
| 89 | private: |
| 90 | QSharedDataPointer<QQuickImageProviderOptionsPrivate> d; |
| 91 | }; |
| 92 | |
| 93 | /*! \internal |
| 94 | A class that encapsulates the action of fetching a pixmap, as well as the |
| 95 | pixmap itself (indirectly via QQuickPixmapData::textureFactory) and the |
| 96 | responsibility of canceling outstanding requests. Rather than relying on |
| 97 | QPixmapCache which doesn't cache all the information Qt Quick needs, |
| 98 | QQuickPixmap implements its own cache, that correctly degrades over time. |
| 99 | (QQuickPixmapData::release() marks it as being not-currently-used, and |
| 100 | QQuickPixmapCache::shrinkCache() sweeps away the least-recently-released |
| 101 | instances until the remaining bytes are less than cache_limit.) |
| 102 | */ |
| 103 | class Q_QUICK_EXPORT QQuickPixmap |
| 104 | { |
| 105 | Q_DECLARE_TR_FUNCTIONS(QQuickPixmap) |
| 106 | public: |
| 107 | enum Status { Null, Ready, Error, Loading }; |
| 108 | |
| 109 | enum Option { |
| 110 | Asynchronous = 0x00000001, |
| 111 | Cache = 0x00000002 |
| 112 | }; |
| 113 | Q_DECLARE_FLAGS(Options, Option) |
| 114 | |
| 115 | QQuickPixmap(); |
| 116 | QQuickPixmap(QQmlEngine *, const QUrl &); |
| 117 | QQuickPixmap(QQmlEngine *, const QUrl &, Options options); |
| 118 | QQuickPixmap(QQmlEngine *, const QUrl &, const QRect ®ion, const QSize &); |
| 119 | QQuickPixmap(const QUrl &, const QImage &image); |
| 120 | ~QQuickPixmap(); |
| 121 | |
| 122 | bool isNull() const; |
| 123 | bool isReady() const; |
| 124 | bool isError() const; |
| 125 | bool isLoading() const; |
| 126 | |
| 127 | Status status() const; |
| 128 | QString error() const; |
| 129 | const QUrl &url() const; |
| 130 | const QSize &implicitSize() const; |
| 131 | const QRect &requestRegion() const; |
| 132 | const QSize &requestSize() const; |
| 133 | QQuickImageProviderOptions::AutoTransform autoTransform() const; |
| 134 | int frameCount() const; |
| 135 | QImage image() const; |
| 136 | void setImage(const QImage &); |
| 137 | void setPixmap(const QQuickPixmap &other); |
| 138 | |
| 139 | QColorSpace colorSpace() const; |
| 140 | |
| 141 | QQuickTextureFactory *textureFactory() const; |
| 142 | |
| 143 | QRect rect() const; |
| 144 | int width() const; |
| 145 | int height() const; |
| 146 | |
| 147 | void load(QQmlEngine *, const QUrl &); |
| 148 | void load(QQmlEngine *, const QUrl &, QQuickPixmap::Options options); |
| 149 | void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize); |
| 150 | void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize, QQuickPixmap::Options options); |
| 151 | void load(QQmlEngine *, const QUrl &, const QRect &requestRegion, const QSize &requestSize, |
| 152 | QQuickPixmap::Options options, const QQuickImageProviderOptions &providerOptions, int frame = 0, int frameCount = 1, |
| 153 | qreal devicePixelRatio = 1.0); |
| 154 | void loadImageFromDevice(QQmlEngine *engine, QIODevice *device, const QUrl &url, |
| 155 | const QRect &requestRegion, const QSize &requestSize, |
| 156 | const QQuickImageProviderOptions &providerOptions, int frame = 0, int frameCount = 1); |
| 157 | |
| 158 | void clear(); |
| 159 | void clear(QObject *); |
| 160 | |
| 161 | bool connectFinished(QObject *, const char *); |
| 162 | bool connectFinished(QObject *, int); |
| 163 | bool connectDownloadProgress(QObject *, const char *); |
| 164 | bool connectDownloadProgress(QObject *, int); |
| 165 | |
| 166 | static void purgeCache(); |
| 167 | static bool isCached(const QUrl &url, const QRect &requestRegion, const QSize &requestSize, |
| 168 | const int frame, const QQuickImageProviderOptions &options); |
| 169 | static bool isScalableImageFormat(const QUrl &url); |
| 170 | |
| 171 | static const QLatin1String itemGrabberScheme; |
| 172 | |
| 173 | private: |
| 174 | Q_DISABLE_COPY(QQuickPixmap) |
| 175 | QQuickPixmapData *d; |
| 176 | friend class QQuickPixmapData; |
| 177 | }; |
| 178 | |
| 179 | Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickPixmap::Options) |
| 180 | |
| 181 | // ### Qt 6: This should be made public in Qt 6. It's functionality can't be merged into |
| 182 | // QQuickImageProvider without breaking source compatibility. |
| 183 | class Q_QUICK_EXPORT QQuickImageProviderWithOptions : public QQuickAsyncImageProvider |
| 184 | { |
| 185 | public: |
| 186 | QQuickImageProviderWithOptions(ImageType type, Flags flags = Flags()); |
| 187 | |
| 188 | QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize) override; |
| 189 | QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize) override; |
| 190 | QQuickTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize) override; |
| 191 | QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize) override; |
| 192 | |
| 193 | virtual QImage requestImage(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options); |
| 194 | virtual QPixmap requestPixmap(const QString &id, QSize *size, const QSize& requestedSize, const QQuickImageProviderOptions &options); |
| 195 | virtual QQuickTextureFactory *requestTexture(const QString &id, QSize *size, const QSize &requestedSize, const QQuickImageProviderOptions &options); |
| 196 | virtual QQuickImageResponse *requestImageResponse(const QString &id, const QSize &requestedSize, const QQuickImageProviderOptions &options); |
| 197 | |
| 198 | static QSize loadSize(const QSize &originalSize, const QSize &requestedSize, const QByteArray &format, const QQuickImageProviderOptions &options, |
| 199 | qreal devicePixelRatio = 1.0); |
| 200 | static QQuickImageProviderWithOptions *checkedCast(QQuickImageProvider *provider); |
| 201 | }; |
| 202 | |
| 203 | QT_END_NAMESPACE |
| 204 | |
| 205 | #endif // QQUICKPIXMAP_H |
| 206 | |