Skip to content
Permalink
Browse files
Fix QgsImageCache [fix #43787]
  • Loading branch information
dmarteau authored and nyalldawson committed Oct 20, 2021
1 parent 0a27674 commit 4100bdd480aa04dbe45e232e04ded4b6b4eeffe6
Showing with 21 additions and 5 deletions.
  1. +12 −4 src/core/qgsimagecache.cpp
  2. +9 −1 tests/src/core/testqgsimagecache.cpp
@@ -23,6 +23,7 @@
#include "qgsnetworkaccessmanager.h"
#include "qgsmessagelog.h"
#include "qgsnetworkcontentfetchertask.h"
#include "qgssettings.h"

#include <QApplication>
#include <QCoreApplication>
@@ -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;
}
@@ -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" );
@@ -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;
}

@@ -56,7 +56,7 @@ class TestQgsImageCache : public QObject
void base64();
void empty();
void dpi();

void cachesize();
};


@@ -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

0 comments on commit 4100bdd

Please sign in to comment.