QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
29 class QTemporaryDir;
30 
31 #ifndef SIP_RUN
32 
34 
41 class CORE_EXPORT QgsImageCacheEntry : public QgsAbstractContentCacheEntry
42 {
43  public:
44 
56  QgsImageCacheEntry( const QString &path, QSize size, bool keepAspectRatio, double opacity, double targetDpi, int frameNumber ) ;
57 
59  QSize size;
60 
62  bool keepAspectRatio = true;
63 
65  double opacity = 1.0;
66 
68  QImage image;
69 
75  bool isMissingImage = false;
76 
82  double targetDpi = 96;
83 
89  int frameNumber = -1;
90 
96  int totalFrameCount = -1;
97 
103  int nextFrameDelay = -1;
104 
105  int dataSize() const override;
106  void dump() const override;
107  bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
108 
109 };
110 
112 #endif
113 
127 #ifdef SIP_RUN
128 class CORE_EXPORT QgsImageCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
129 {
130 #else
131 class CORE_EXPORT QgsImageCache : public QgsAbstractContentCache< QgsImageCacheEntry >
132 {
133 #endif
134  Q_OBJECT
135 
136  public:
137 
141  QgsImageCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
142 
143  ~QgsImageCache() override;
144 
150  long maximumSize() const { return mMaxCacheSize; }
151 
179  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 );
180 
196  QSize originalSize( const QString &path, bool blocking = false ) const;
197 
215  int totalFrameCount( const QString &path, bool blocking = false );
216 
235  int nextFrameDelay( const QString &path, int currentFrame = 0, bool blocking = false );
236 
242  void prepareAnimation( const QString &path );
243 
244  signals:
245 
249  void remoteImageFetched( const QString &url );
250 
251  private:
252 
253  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 );
254 
255  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;
256 
257  static QImage getFrameFromReader( QImageReader &reader, int frameNumber );
258 
260  QByteArray mMissingSvg;
261 
262  QByteArray mFetchingSvg;
263 
264  QMap< QString, QString > mExtractedAnimationPaths;
265  std::unique_ptr< QTemporaryDir > mTemporaryDir;
266  QMap< QString, int > mTotalFrameCounts;
267  QMap< QString, QVector< int > > mImageDelays;
268 
269  friend class TestQgsImageCache;
270 };
271 
272 #endif // QGSIMAGECACHE_H
SIP_OUT
#define SIP_OUT
Definition: qgis_sip.h:58
QgsAbstractContentCacheEntry::isEqual
virtual bool isEqual(const QgsAbstractContentCacheEntry *other) const =0
Tests whether this entry matches another entry.
SIP_PYARGREMOVE
#define SIP_PYARGREMOVE
Definition: qgis_sip.h:151
QgsAbstractContentCacheEntry::dump
virtual void dump() const =0
Dumps debugging strings containing the item's properties.
QgsImageCache::maximumSize
long maximumSize() const
Returns the maximum size of the cache, in bytes.
Definition: qgsimagecache.h:150
qgis_sip.h
QgsAbstractContentCacheEntry::dataSize
virtual int dataSize() const =0
Returns the memory usage in bytes for the entry.
QgsAbstractContentCacheEntry
Base class for entries in a QgsAbstractContentCache.
Definition: qgsabstractcontentcache.h:54
QgsAbstractContentCacheBase
A QObject derived base class for QgsAbstractContentCache.
Definition: qgsabstractcontentcache.h:137
qgsabstractcontentcache.h
QgsImageCache
A cache for images derived from raster files.
Definition: qgsimagecache.h:131
SIP_TRANSFERTHIS
#define SIP_TRANSFERTHIS
Definition: qgis_sip.h:53
QgsAbstractContentCache
Abstract base class for file content caches, such as SVG or raster image caches.
Definition: qgsabstractcontentcache.h:196
QgsAbstractContentCache::mMaxCacheSize
long mMaxCacheSize
Maximum cache size.
Definition: qgsabstractcontentcache.h:566