QGIS API Documentation 4.1.0-Master (60fea48833c)
Loading...
Searching...
No Matches
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
21#include "qgis_core.h"
22#include "qgis_sip.h"
24
25#include <QElapsedTimer>
26#include <QImage>
27#include <QSize>
28
29class QTemporaryDir;
30
31#ifndef SIP_RUN
32
34
41class CORE_EXPORT QgsImageSizeCacheEntry : public QgsAbstractContentCacheEntry
42{
43 public:
47 QgsImageSizeCacheEntry( const QString &path );
48
50 QSize size;
51
52 int dataSize() const override;
53 void dump() const override;
54 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
55};
56
69class CORE_EXPORT QgsImageSizeCache : public QgsAbstractContentCache< QgsImageSizeCacheEntry >
70{
71 Q_OBJECT
72
73 public:
74 QgsImageSizeCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
75 ~QgsImageSizeCache() override;
76 long maximumSize() const { return mMaxCacheSize; }
77 QSize originalSize( const QString &path, bool blocking = false );
78};
79
86class CORE_EXPORT QgsImageCacheEntry : public QgsAbstractContentCacheEntry
87{
88 public:
100 QgsImageCacheEntry( const QString &path, QSize size, bool keepAspectRatio, double opacity, double targetDpi, int frameNumber );
101
103 QSize size;
104
106 bool keepAspectRatio = true;
107
109 double opacity = 1.0;
110
112 QImage image;
113
119 bool isMissingImage = false;
120
126 double targetDpi = 96;
127
133 int frameNumber = -1;
134
140 int totalFrameCount = -1;
141
147 int nextFrameDelay = -1;
148
149 int dataSize() const override;
150 void dump() const override;
151 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
152};
153
155#endif
156
170#ifdef SIP_RUN
171class CORE_EXPORT QgsImageCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
172{
173#else
174class CORE_EXPORT QgsImageCache : public QgsAbstractContentCache< QgsImageCacheEntry >
175{
176#endif
177 Q_OBJECT
178
179 public:
183 QgsImageCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
184
185 ~QgsImageCache() override;
186
192 long maximumSize() const { return mMaxCacheSize; }
193
221 QImage pathAsImage(
222 const QString &path,
223 const QSize size,
224 const bool keepAspectRatio,
225 const double opacity,
226 bool &fitsInCache SIP_OUT,
227 bool blocking = false,
228 double targetDpi = 96,
229 int frameNumber = -1,
230 bool *isMissing SIP_PYARGREMOVE = nullptr
231 );
232
248 QSize originalSize( const QString &path, bool blocking = false ) const;
249
267 int totalFrameCount( const QString &path, bool blocking = false );
268
287 int nextFrameDelay( const QString &path, int currentFrame = 0, bool blocking = false );
288
294 void prepareAnimation( const QString &path );
295
296 signals:
297
301 void remoteImageFetched( const QString &url );
302
303 private:
304 QImage pathAsImagePrivate(
305 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
306 );
307
308 QImage renderImage(
309 const QString &path, QSize size, const bool keepAspectRatio, const double opacity, double targetDpi, int frameNumber, bool &isBroken, int &totalFrameCount, int &nextFrameDelayMs, bool blocking = false
310 ) const;
311
312 static QImage getFrameFromReader( QImageReader &reader, int frameNumber );
313
314 QSize originalSizePrivate( const QString &path, bool blocking = false ) const;
315
317 QByteArray mMissingSvg;
318
319 QByteArray mFetchingSvg;
320
321 QMap< QString, QString > mExtractedAnimationPaths;
322 std::unique_ptr< QTemporaryDir > mTemporaryDir;
323 QMap< QString, int > mTotalFrameCounts;
324 QMap< QString, QVector< int > > mImageDelays;
325
326 mutable QgsImageSizeCache mImageSizeCache;
327
328 friend class QgsImageSizeCache;
329 friend class TestQgsImageCache;
330};
331
332#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.
QgsAbstractContentCache(QObject *parent=nullptr, const QString &typeString=QString(), long maxCacheSize=20000000, int fileModifiedCheckTimeout=30000)
A cache for images derived from raster files.
friend class TestQgsImageCache
friend class QgsImageSizeCache
QgsImageCache(QObject *parent=nullptr)
Constructor for QgsImageCache, with the specified parent object.
int totalFrameCount(const QString &path, bool blocking=false)
Returns the total frame count of the image at the specified path.
~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:52
#define SIP_PYARGREMOVE
Definition qgis_sip.h:158
#define SIP_OUT
Definition qgis_sip.h:57