Skip to content

Commit

Permalink
Fix QgsImageCache [fix #43787]
Browse files Browse the repository at this point in the history
  • Loading branch information
dmarteau authored and nyalldawson committed Oct 20, 2021
1 parent 0a27674 commit 4100bdd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
16 changes: 12 additions & 4 deletions src/core/qgsimagecache.cpp
Expand Up @@ -23,6 +23,7 @@
#include "qgsnetworkaccessmanager.h"
#include "qgsmessagelog.h"
#include "qgsnetworkcontentfetchertask.h"
#include "qgssettings.h"

#include <QApplication>
#include <QCoreApplication>
Expand Down Expand Up @@ -66,7 +67,7 @@ int QgsImageCacheEntry::dataSize() const
int size = 0;
if ( !image.isNull() )
{
size += ( image.width() * image.height() * 32 );
size += image.sizeInBytes();
}
return size;
}
Expand All @@ -78,10 +79,17 @@ void QgsImageCacheEntry::dump() const

///@endcond

static const int DEFAULT_IMAGE_CACHE_MAX_BYTES = 104857600;

QgsImageCache::QgsImageCache( QObject *parent )
: QgsAbstractContentCache< QgsImageCacheEntry >( parent, QObject::tr( "Image" ) )
: QgsAbstractContentCache< QgsImageCacheEntry >( parent, QObject::tr( "Image" ), DEFAULT_IMAGE_CACHE_MAX_BYTES )
{
int bytes = QgsSettings().value( QStringLiteral( "/qgis/maxImageCacheSize" ), 0 ).toInt();
if ( bytes > 0 )
{
mMaxCacheSize = bytes;
}

mMissingSvg = QStringLiteral( "<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>" ).toLatin1();

const QString downloadingSvgPath = QgsApplication::defaultThemePath() + QStringLiteral( "downloading_svg.svg" );
Expand Down Expand Up @@ -127,15 +135,15 @@ QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const boo
long cachedDataSize = 0;
bool isBroken = false;
result = renderImage( file, size, keepAspectRatio, opacity, targetDpi, isBroken, blocking );
cachedDataSize += result.width() * result.height() * 32;
cachedDataSize += result.sizeInBytes();
if ( cachedDataSize > mMaxCacheSize / 2 )
{
fitsInCache = false;
currentEntry->image = QImage();
}
else
{
mTotalSize += ( result.width() * result.height() * 32 );
mTotalSize += result.sizeInBytes();
currentEntry->image = result;
}

Expand Down
10 changes: 9 additions & 1 deletion tests/src/core/testqgsimagecache.cpp
Expand Up @@ -56,7 +56,7 @@ class TestQgsImageCache : public QObject
void base64();
void empty();
void dpi();

void cachesize();
};


Expand Down Expand Up @@ -318,6 +318,14 @@ void TestQgsImageCache::dpi()
QVERIFY( entry1.isEqual( &entry2 ) );
}

void TestQgsImageCache::cachesize()
{
uint cacheSize = 100000000;
QgsSettings().setValue( QStringLiteral( "/qgis/maxImageCacheSize" ), cacheSize );
QgsImageCache cache;
QCOMPARE( cache.mMaxCacheSize, cacheSize );
}

bool TestQgsImageCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
{
//draw background
Expand Down

0 comments on commit 4100bdd

Please sign in to comment.