QGIS API Documentation 4.1.0-Master (376402f9aeb)
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
30class QTemporaryDir;
31
32#ifndef SIP_RUN
33
35
42class CORE_EXPORT QgsImageSizeCacheEntry : public QgsAbstractContentCacheEntry
43{
44 public:
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
70class CORE_EXPORT QgsImageSizeCache : public QgsAbstractContentCache< QgsImageSizeCacheEntry >
71{
72 Q_OBJECT
73
74 public:
75 QgsImageSizeCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
76 ~QgsImageSizeCache() override;
77 long maximumSize() const { return mMaxCacheSize; }
78 QSize originalSize( const QString &path, bool blocking = false );
79};
80
87class CORE_EXPORT QgsImageCacheEntry : public QgsAbstractContentCacheEntry
88{
89 public:
101 QgsImageCacheEntry( const QString &path, QSize size, bool keepAspectRatio, double opacity, double targetDpi, int frameNumber );
102
104 QSize size;
105
107 bool keepAspectRatio = true;
108
110 double opacity = 1.0;
111
113 QImage image;
114
120 bool isMissingImage = false;
121
127 double targetDpi = 96;
128
134 int frameNumber = -1;
135
141 int totalFrameCount = -1;
142
148 int nextFrameDelay = -1;
149
150 int dataSize() const override;
151 void dump() const override;
152 bool isEqual( const QgsAbstractContentCacheEntry *other ) const override;
153};
154
156#endif
157
171#ifdef SIP_RUN
172class CORE_EXPORT QgsImageCache : public QgsAbstractContentCacheBase // for sip we skip to the base class and avoid the template difficulty
173{
174#else
175class CORE_EXPORT QgsImageCache : public QgsAbstractContentCache< QgsImageCacheEntry >
176{
177#endif
178 Q_OBJECT
179
180 public:
184 QgsImageCache( QObject *parent SIP_TRANSFERTHIS = nullptr );
185
186 ~QgsImageCache() override;
187
189
195 long maximumSize() const { return mMaxCacheSize; }
196
224 QImage pathAsImage(
225 const QString &path,
226 const QSize size,
227 const bool keepAspectRatio,
228 const double opacity,
229 bool &fitsInCache SIP_OUT,
230 bool blocking = false,
231 double targetDpi = 96,
232 int frameNumber = -1,
233 bool *isMissing SIP_PYARGREMOVE = nullptr
234 );
235
251 QSize originalSize( const QString &path, bool blocking = false ) const;
252
270 int totalFrameCount( const QString &path, bool blocking = false );
271
290 int nextFrameDelay( const QString &path, int currentFrame = 0, bool blocking = false );
291
297 void prepareAnimation( const QString &path );
298
299 signals:
300
304 void remoteImageFetched( const QString &url );
305
306 private:
307 QImage pathAsImagePrivate(
308 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
309 );
310
311 QImage renderImage(
312 const QString &path, QSize size, const bool keepAspectRatio, const double opacity, double targetDpi, int frameNumber, bool &isBroken, int &totalFrameCount, int &nextFrameDelayMs, bool blocking = false
313 ) const;
314
315 static QImage getFrameFromReader( QImageReader &reader, int frameNumber );
316
317 QSize originalSizePrivate( const QString &path, bool blocking = false ) const;
318
320 QByteArray mMissingSvg;
321
322 QByteArray mFetchingSvg;
323
324 QMap< QString, QString > mExtractedAnimationPaths;
325 std::unique_ptr< QTemporaryDir > mTemporaryDir;
326 QMap< QString, int > mTotalFrameCounts;
327 QMap< QString, QVector< int > > mImageDelays;
328
329 mutable QgsImageSizeCache mImageSizeCache;
330
331 friend class QgsImageSizeCache;
332 friend class TestQgsImageCache;
333};
334
335#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
static const QgsSettingsEntryInteger * settingsMaxImageCacheSize
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.
An integer settings entry.
#define SIP_TRANSFERTHIS
Definition qgis_sip.h:52
#define SIP_SKIP
Definition qgis_sip.h:133
#define SIP_PYARGREMOVE
Definition qgis_sip.h:158
#define SIP_OUT
Definition qgis_sip.h:57