Navigation Menu

Skip to content

Commit

Permalink
[FEATURE][API] Add a content cache for raster images
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nyalldawson authored and nirvn committed Dec 5, 2018
1 parent cdba8f5 commit 84838d1
Show file tree
Hide file tree
Showing 29 changed files with 788 additions and 5 deletions.
1 change: 0 additions & 1 deletion python/core/auto_generated/qgsabstractcontentcache.sip.in
Expand Up @@ -33,7 +33,6 @@ Constructor for QgsAbstractContentCacheEntry for an entry relating to the specif
virtual ~QgsAbstractContentCacheEntry();



QString path;

QDateTime fileModified;
Expand Down
11 changes: 11 additions & 0 deletions python/core/auto_generated/qgsapplication.sip.in
Expand Up @@ -649,7 +649,18 @@ providers that may add items to the browser tree.
Returns the application's SVG cache, used for caching SVG images and handling parameter replacement
within SVG files.

.. seealso:: :py:func:`imageCache`

.. versionadded:: 3.0
%End

static QgsImageCache *imageCache();
%Docstring
Returns the application's image cache, used for caching resampled versions of raster images.

.. seealso:: :py:func:`svgCache`

.. versionadded:: 3.6
%End

static QgsNetworkContentFetcherRegistry *networkContentFetcherRegistry();
Expand Down
71 changes: 71 additions & 0 deletions python/core/auto_generated/qgsimagecache.sip.in
@@ -0,0 +1,71 @@
/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsimagecache.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/






class QgsImageCache : QgsAbstractContentCacheBase
{
%Docstring
A cache for images derived from raster files.

QgsImageCache stores pre-rendered resampled versions of raster image files, allowing efficient
reuse without incurring the cost of resampling on every render.

QgsImageCache is not usually directly created, but rather accessed through
:py:func:`QgsApplication.imageCache()`

.. versionadded:: 3.6
%End

%TypeHeaderCode
#include "qgsimagecache.h"
%End
public:

QgsImageCache( QObject *parent /TransferThis/ = 0 );
%Docstring
Constructor for QgsImageCache, with the specified ``parent`` object.
%End

QImage pathAsImage( const QString &path, QSize size, bool keepAspectRatio, bool &fitsInCache /Out/ );
%Docstring
Returns the specified ``path`` rendered as an image. If possible, a pre-existing cached
version of the image will be used. If not, the image is fetched and resampled to the desired
size, and then the result cached for subsequent lookups.

``path`` may be a local file, remote (HTTP) url, or a base 64 encoded string (with a "base64:" prefix).

The ``size`` parameter dictates the target size of the image. An invalid size indicates the
original raster image size (with no resampling).

If ``keepAspectRatio`` is true, then the original raster aspect ratio will be maintained during
any resampling operations.

If the resultant raster was of a sufficiently small size to store in the cache, then ``fitsInCache``
will be set to true.
%End

signals:

void remoteImageFetched( const QString &url );
%Docstring
Emitted when the cache has finished retrieving an image file from a remote ``url``.
%End

};

/************************************************************************
* This file has been generated automatically from *
* *
* src/core/qgsimagecache.h *
* *
* Do not edit manually ! Edit header and run scripts/sipify.pl again *
************************************************************************/
1 change: 1 addition & 0 deletions python/core/core_auto.sip
Expand Up @@ -332,6 +332,7 @@
%Include auto_generated/qgsgeometryvalidator.sip
%Include auto_generated/qgsgml.sip
%Include auto_generated/qgsgmlschema.sip
%Include auto_generated/qgsimagecache.sip
%Include auto_generated/qgsmaplayer.sip
%Include auto_generated/qgsmaplayerlegend.sip
%Include auto_generated/qgsmaplayermodel.sip
Expand Down
2 changes: 1 addition & 1 deletion scripts/spell_check/spelling.dat
Expand Up @@ -6554,7 +6554,7 @@ stength:strength
steriods:steroids
sterotypes:stereotypes
stilus:stylus
stingent:stringent
stingent:stringent:*
stiring:stirring
stirng:string
stirngs:strings
Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Expand Up @@ -214,6 +214,7 @@ SET(QGIS_CORE_SRCS
qgshistogram.cpp
qgshstoreutils.cpp
qgshtmlutils.cpp
qgsimagecache.cpp
qgsinterval.cpp
qgsjsonutils.cpp
qgslabelfeature.cpp
Expand Down Expand Up @@ -616,6 +617,7 @@ SET(QGIS_CORE_MOC_HDRS
qgsgeometryvalidator.h
qgsgml.h
qgsgmlschema.h
qgsimagecache.h
qgsmaplayer.h
qgsmaplayerlegend.h
qgsmaplayermodel.h
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgsabstractcontentcache.h
Expand Up @@ -52,8 +52,6 @@ class CORE_EXPORT QgsAbstractContentCacheEntry

virtual ~QgsAbstractContentCacheEntry() = default;

QgsAbstractContentCacheEntry() = delete;

//! QgsAbstractContentCacheEntry cannot be copied.
QgsAbstractContentCacheEntry( const QgsAbstractContentCacheEntry &rh ) = delete;
//! QgsAbstractContentCacheEntry cannot be copied.
Expand Down Expand Up @@ -176,7 +174,7 @@ class CORE_EXPORT QgsAbstractContentCacheBase: public QObject
#ifndef SIP_RUN

/**
* \class QgsAbstractContentCacheBase
* \class QgsAbstractContentCache
* \ingroup core
*
* Abstract base class for file content caches, such as SVG or raster image caches.
Expand Down Expand Up @@ -560,6 +558,7 @@ class CORE_EXPORT QgsAbstractContentCache : public QgsAbstractContentCacheBase
QString mTypeString;

friend class TestQgsSvgCache;
friend class TestQgsImageCache;
};

#endif
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgsapplication.cpp
Expand Up @@ -30,6 +30,7 @@
#include "qgstaskmanager.h"
#include "qgsfieldformatterregistry.h"
#include "qgssvgcache.h"
#include "qgsimagecache.h"
#include "qgscolorschemeregistry.h"
#include "qgspainteffectregistry.h"
#include "qgsprojectstorageregistry.h"
Expand Down Expand Up @@ -1772,6 +1773,11 @@ QgsSvgCache *QgsApplication::svgCache()
return members()->mSvgCache;
}

QgsImageCache *QgsApplication::imageCache()
{
return members()->mImageCache;
}

QgsNetworkContentFetcherRegistry *QgsApplication::networkContentFetcherRegistry()
{
return members()->mNetworkContentFetcherRegistry;
Expand Down Expand Up @@ -1842,6 +1848,7 @@ QgsApplication::ApplicationMembers::ApplicationMembers()
mActionScopeRegistry = new QgsActionScopeRegistry();
mFieldFormatterRegistry = new QgsFieldFormatterRegistry();
mSvgCache = new QgsSvgCache();
mImageCache = new QgsImageCache();
mColorSchemeRegistry = new QgsColorSchemeRegistry();
mPaintEffectRegistry = new QgsPaintEffectRegistry();
mSymbolLayerRegistry = new QgsSymbolLayerRegistry();
Expand Down Expand Up @@ -1878,6 +1885,7 @@ QgsApplication::ApplicationMembers::~ApplicationMembers()
delete mRasterRendererRegistry;
delete mRendererRegistry;
delete mSvgCache;
delete mImageCache;
delete mSymbolLayerRegistry;
delete mTaskManager;
delete mNetworkContentFetcherRegistry;
Expand Down
12 changes: 12 additions & 0 deletions src/core/qgsapplication.h
Expand Up @@ -34,6 +34,7 @@ class QgsPaintEffectRegistry;
class QgsProjectStorageRegistry;
class QgsRendererRegistry;
class QgsSvgCache;
class QgsImageCache;
class QgsSymbolLayerRegistry;
class QgsRasterRendererRegistry;
class QgsGpsConnectionRegistry;
Expand Down Expand Up @@ -595,10 +596,20 @@ class CORE_EXPORT QgsApplication : public QApplication
/**
* Returns the application's SVG cache, used for caching SVG images and handling parameter replacement
* within SVG files.
*
* \see imageCache()
* \since QGIS 3.0
*/
static QgsSvgCache *svgCache();

/**
* Returns the application's image cache, used for caching resampled versions of raster images.
*
* \see svgCache()
* \since QGIS 3.6
*/
static QgsImageCache *imageCache();

/**
* Returns the application's network content registry used for fetching temporary files during QGIS session
* \since QGIS 3.2
Expand Down Expand Up @@ -867,6 +878,7 @@ class CORE_EXPORT QgsApplication : public QApplication
QgsRendererRegistry *mRendererRegistry = nullptr;
QgsRuntimeProfiler *mProfiler = nullptr;
QgsSvgCache *mSvgCache = nullptr;
QgsImageCache *mImageCache = nullptr;
QgsSymbolLayerRegistry *mSymbolLayerRegistry = nullptr;
QgsTaskManager *mTaskManager = nullptr;
QgsLayoutItemRegistry *mLayoutItemRegistry = nullptr;
Expand Down

0 comments on commit 84838d1

Please sign in to comment.