Skip to content

Commit 84838d1

Browse files
nyalldawsonnirvn
authored andcommitted
[FEATURE][API] Add a content cache for raster images
This new class QgsImageCache is the equivalent of QgsSvgCache but for raster images. QgsImageCache stores pre-rendered resampled versions of raster image files, allowing efficient reuse without incurring the cost of resampling on every render. Additionally, it offers the other benefits QgsSvgCache has, such as thread safety, ability to transparently download remote images, and support for base64 encoded strings.
1 parent cdba8f5 commit 84838d1

29 files changed

+788
-5
lines changed

python/core/auto_generated/qgsabstractcontentcache.sip.in

-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ Constructor for QgsAbstractContentCacheEntry for an entry relating to the specif
3333
virtual ~QgsAbstractContentCacheEntry();
3434

3535

36-
3736
QString path;
3837

3938
QDateTime fileModified;

python/core/auto_generated/qgsapplication.sip.in

+11
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,18 @@ providers that may add items to the browser tree.
649649
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement
650650
within SVG files.
651651

652+
.. seealso:: :py:func:`imageCache`
653+
652654
.. versionadded:: 3.0
655+
%End
656+
657+
static QgsImageCache *imageCache();
658+
%Docstring
659+
Returns the application's image cache, used for caching resampled versions of raster images.
660+
661+
.. seealso:: :py:func:`svgCache`
662+
663+
.. versionadded:: 3.6
653664
%End
654665

655666
static QgsNetworkContentFetcherRegistry *networkContentFetcherRegistry();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/************************************************************************
2+
* This file has been generated automatically from *
3+
* *
4+
* src/core/qgsimagecache.h *
5+
* *
6+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
7+
************************************************************************/
8+
9+
10+
11+
12+
13+
14+
class QgsImageCache : QgsAbstractContentCacheBase
15+
{
16+
%Docstring
17+
A cache for images derived from raster files.
18+
19+
QgsImageCache stores pre-rendered resampled versions of raster image files, allowing efficient
20+
reuse without incurring the cost of resampling on every render.
21+
22+
QgsImageCache is not usually directly created, but rather accessed through
23+
:py:func:`QgsApplication.imageCache()`
24+
25+
.. versionadded:: 3.6
26+
%End
27+
28+
%TypeHeaderCode
29+
#include "qgsimagecache.h"
30+
%End
31+
public:
32+
33+
QgsImageCache( QObject *parent /TransferThis/ = 0 );
34+
%Docstring
35+
Constructor for QgsImageCache, with the specified ``parent`` object.
36+
%End
37+
38+
QImage pathAsImage( const QString &path, QSize size, bool keepAspectRatio, bool &fitsInCache /Out/ );
39+
%Docstring
40+
Returns the specified ``path`` rendered as an image. If possible, a pre-existing cached
41+
version of the image will be used. If not, the image is fetched and resampled to the desired
42+
size, and then the result cached for subsequent lookups.
43+
44+
``path`` may be a local file, remote (HTTP) url, or a base 64 encoded string (with a "base64:" prefix).
45+
46+
The ``size`` parameter dictates the target size of the image. An invalid size indicates the
47+
original raster image size (with no resampling).
48+
49+
If ``keepAspectRatio`` is true, then the original raster aspect ratio will be maintained during
50+
any resampling operations.
51+
52+
If the resultant raster was of a sufficiently small size to store in the cache, then ``fitsInCache``
53+
will be set to true.
54+
%End
55+
56+
signals:
57+
58+
void remoteImageFetched( const QString &url );
59+
%Docstring
60+
Emitted when the cache has finished retrieving an image file from a remote ``url``.
61+
%End
62+
63+
};
64+
65+
/************************************************************************
66+
* This file has been generated automatically from *
67+
* *
68+
* src/core/qgsimagecache.h *
69+
* *
70+
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
71+
************************************************************************/

python/core/core_auto.sip

+1
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,7 @@
332332
%Include auto_generated/qgsgeometryvalidator.sip
333333
%Include auto_generated/qgsgml.sip
334334
%Include auto_generated/qgsgmlschema.sip
335+
%Include auto_generated/qgsimagecache.sip
335336
%Include auto_generated/qgsmaplayer.sip
336337
%Include auto_generated/qgsmaplayerlegend.sip
337338
%Include auto_generated/qgsmaplayermodel.sip

scripts/spell_check/spelling.dat

+1-1
Original file line numberDiff line numberDiff line change
@@ -6554,7 +6554,7 @@ stength:strength
65546554
steriods:steroids
65556555
sterotypes:stereotypes
65566556
stilus:stylus
6557-
stingent:stringent
6557+
stingent:stringent:*
65586558
stiring:stirring
65596559
stirng:string
65606560
stirngs:strings

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ SET(QGIS_CORE_SRCS
214214
qgshistogram.cpp
215215
qgshstoreutils.cpp
216216
qgshtmlutils.cpp
217+
qgsimagecache.cpp
217218
qgsinterval.cpp
218219
qgsjsonutils.cpp
219220
qgslabelfeature.cpp
@@ -616,6 +617,7 @@ SET(QGIS_CORE_MOC_HDRS
616617
qgsgeometryvalidator.h
617618
qgsgml.h
618619
qgsgmlschema.h
620+
qgsimagecache.h
619621
qgsmaplayer.h
620622
qgsmaplayerlegend.h
621623
qgsmaplayermodel.h

src/core/qgsabstractcontentcache.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ class CORE_EXPORT QgsAbstractContentCacheEntry
5252

5353
virtual ~QgsAbstractContentCacheEntry() = default;
5454

55-
QgsAbstractContentCacheEntry() = delete;
56-
5755
//! QgsAbstractContentCacheEntry cannot be copied.
5856
QgsAbstractContentCacheEntry( const QgsAbstractContentCacheEntry &rh ) = delete;
5957
//! QgsAbstractContentCacheEntry cannot be copied.
@@ -176,7 +174,7 @@ class CORE_EXPORT QgsAbstractContentCacheBase: public QObject
176174
#ifndef SIP_RUN
177175

178176
/**
179-
* \class QgsAbstractContentCacheBase
177+
* \class QgsAbstractContentCache
180178
* \ingroup core
181179
*
182180
* Abstract base class for file content caches, such as SVG or raster image caches.
@@ -560,6 +558,7 @@ class CORE_EXPORT QgsAbstractContentCache : public QgsAbstractContentCacheBase
560558
QString mTypeString;
561559

562560
friend class TestQgsSvgCache;
561+
friend class TestQgsImageCache;
563562
};
564563

565564
#endif

src/core/qgsapplication.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "qgstaskmanager.h"
3131
#include "qgsfieldformatterregistry.h"
3232
#include "qgssvgcache.h"
33+
#include "qgsimagecache.h"
3334
#include "qgscolorschemeregistry.h"
3435
#include "qgspainteffectregistry.h"
3536
#include "qgsprojectstorageregistry.h"
@@ -1772,6 +1773,11 @@ QgsSvgCache *QgsApplication::svgCache()
17721773
return members()->mSvgCache;
17731774
}
17741775

1776+
QgsImageCache *QgsApplication::imageCache()
1777+
{
1778+
return members()->mImageCache;
1779+
}
1780+
17751781
QgsNetworkContentFetcherRegistry *QgsApplication::networkContentFetcherRegistry()
17761782
{
17771783
return members()->mNetworkContentFetcherRegistry;
@@ -1842,6 +1848,7 @@ QgsApplication::ApplicationMembers::ApplicationMembers()
18421848
mActionScopeRegistry = new QgsActionScopeRegistry();
18431849
mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
18441850
mSvgCache = new QgsSvgCache();
1851+
mImageCache = new QgsImageCache();
18451852
mColorSchemeRegistry = new QgsColorSchemeRegistry();
18461853
mPaintEffectRegistry = new QgsPaintEffectRegistry();
18471854
mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
@@ -1878,6 +1885,7 @@ QgsApplication::ApplicationMembers::~ApplicationMembers()
18781885
delete mRasterRendererRegistry;
18791886
delete mRendererRegistry;
18801887
delete mSvgCache;
1888+
delete mImageCache;
18811889
delete mSymbolLayerRegistry;
18821890
delete mTaskManager;
18831891
delete mNetworkContentFetcherRegistry;

src/core/qgsapplication.h

+12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class QgsPaintEffectRegistry;
3434
class QgsProjectStorageRegistry;
3535
class QgsRendererRegistry;
3636
class QgsSvgCache;
37+
class QgsImageCache;
3738
class QgsSymbolLayerRegistry;
3839
class QgsRasterRendererRegistry;
3940
class QgsGpsConnectionRegistry;
@@ -595,10 +596,20 @@ class CORE_EXPORT QgsApplication : public QApplication
595596
/**
596597
* Returns the application's SVG cache, used for caching SVG images and handling parameter replacement
597598
* within SVG files.
599+
*
600+
* \see imageCache()
598601
* \since QGIS 3.0
599602
*/
600603
static QgsSvgCache *svgCache();
601604

605+
/**
606+
* Returns the application's image cache, used for caching resampled versions of raster images.
607+
*
608+
* \see svgCache()
609+
* \since QGIS 3.6
610+
*/
611+
static QgsImageCache *imageCache();
612+
602613
/**
603614
* Returns the application's network content registry used for fetching temporary files during QGIS session
604615
* \since QGIS 3.2
@@ -867,6 +878,7 @@ class CORE_EXPORT QgsApplication : public QApplication
867878
QgsRendererRegistry *mRendererRegistry = nullptr;
868879
QgsRuntimeProfiler *mProfiler = nullptr;
869880
QgsSvgCache *mSvgCache = nullptr;
881+
QgsImageCache *mImageCache = nullptr;
870882
QgsSymbolLayerRegistry *mSymbolLayerRegistry = nullptr;
871883
QgsTaskManager *mTaskManager = nullptr;
872884
QgsLayoutItemRegistry *mLayoutItemRegistry = nullptr;

0 commit comments

Comments
 (0)