QGIS API Documentation 3.43.0-Master (3ee7834ace6)
qgsimagecache.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsimagecache.h
3 ---------------
4 begin : December 2018
5 copyright : (C) 2018 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#ifndef QGSIMAGECACHE_H
19#define QGSIMAGECACHE_H
20
22#include "qgis_sip.h"
23#include "qgis_core.h"
24
25#include <QElapsedTimer>
26#include <QSize>
27#include <QImage>
28
29class QTemporaryDir;
30
31#ifndef SIP_RUN
32
34
41class CORE_EXPORT QgsImageSizeCacheEntry : public QgsAbstractContentCacheEntry
42{
43 public:
44
48 QgsImageSizeCacheEntry( const QString &path ) ;
49
51 QSize size;
52
53 int dataSize() const override;
54 void dump() const override;
55 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
56
57};
58
71class CORE_EXPORT QgsImageSizeCache : public QgsAbstractContentCache< QgsImageSizeCacheEntry >
72{
73 Q_OBJECT
74
75 public:
76
77 QgsImageSizeCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
78 ~QgsImageSizeCache() override;
79 long maximumSize() const { return mMaxCacheSize; }
80 QSize originalSize( const QString &path, bool blocking = false );
81};
82
89class CORE_EXPORT QgsImageCacheEntry : public QgsAbstractContentCacheEntry
90{
91 public:
92
104 QgsImageCacheEntry( const QString &path, QSize size, bool keepAspectRatio, double opacity, double targetDpi, int frameNumber ) ;
105
107 QSize size;
108
110 bool keepAspectRatio = true;
111
113 double opacity = 1.0;
114
116 QImage image;
117
123 bool isMissingImage = false;
124
130 double targetDpi = 96;
131
137 int frameNumber = -1;
138
144 int totalFrameCount = -1;
145
151 int nextFrameDelay = -1;
152
153 int dataSize() const override;
154 void dump() const override;
155 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
156
157};
158
160#endif
161
175#ifdef SIP_RUN
176class CORE_EXPORT QgsImageCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
177{
178#else
179class CORE_EXPORT QgsImageCache : public QgsAbstractContentCache< QgsImageCacheEntry >
180{
181#endif
182 Q_OBJECT
183
184 public:
185
189 QgsImageCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
190
191 ~QgsImageCache() override;
192
198 long maximumSize() const { return mMaxCacheSize; }
199
227 QImage pathAsImage( const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache SIP_OUT, bool blocking = false, double targetDpi = 96, int frameNumber = -1, bool *isMissing SIP_PYARGREMOVE = nullptr );
228
244 QSize originalSize( const QString &path, bool blocking = false ) const;
245
263 int totalFrameCount( const QString &path, bool blocking = false );
264
283 int nextFrameDelay( const QString &path, int currentFrame = 0, bool blocking = false );
284
290 void prepareAnimation( const QString &path );
291
292 signals:
293
297 void remoteImageFetched( const QString &url );
298
299 private:
300
301 QImage pathAsImagePrivate( const QString &path, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache, bool blocking, double targetDpi, int frameNumber, bool *isMissing, int &totalFrameCount, int &nextFrameDelayMs );
302
303 QImage renderImage( const QString &path, QSize size, const bool keepAspectRatio, const double opacity, double targetDpi, int frameNumber, bool &isBroken, int &totalFrameCount, int &nextFrameDelayMs, bool blocking = false ) const;
304
305 static QImage getFrameFromReader( QImageReader &reader, int frameNumber );
306
307 QSize originalSizePrivate( const QString &path, bool blocking = false ) const;
308
310 QByteArray mMissingSvg;
311
312 QByteArray mFetchingSvg;
313
314 QMap< QString, QString > mExtractedAnimationPaths;
315 std::unique_ptr< QTemporaryDir > mTemporaryDir;
316 QMap< QString, int > mTotalFrameCounts;
317 QMap< QString, QVector< int > > mImageDelays;
318
319 mutable QgsImageSizeCache mImageSizeCache;
320
321 friend class QgsImageSizeCache;
322 friend class TestQgsImageCache;
323};
324
325#endif // QGSIMAGECACHE_H
A QObject derived base class for QgsAbstractContentCache.
Base class for entries in a QgsAbstractContentCache.
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
virtual void dump() const =0
Dumps debugging strings containing the item's properties.
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
Abstract base class for file content caches, such as SVG or raster image caches.
A cache for images derived from raster files.
~QgsImageCache() override
void remoteImageFetched(const QString &url)
Emitted when the cache has finished retrieving an image file from a remote url.
long maximumSize() const
Returns the maximum size of the cache, in bytes.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:53
#define SIP_PYARGREMOVE
Definition qgis_sip.h:151
#define SIP_OUT
Definition qgis_sip.h:58