Skip to content

Commit 4100bdd

Browse files
dmarteaunyalldawson
authored andcommittedOct 20, 2021
Fix QgsImageCache [fix #43787]
1 parent 0a27674 commit 4100bdd

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed
 

‎src/core/qgsimagecache.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "qgsnetworkaccessmanager.h"
2424
#include "qgsmessagelog.h"
2525
#include "qgsnetworkcontentfetchertask.h"
26+
#include "qgssettings.h"
2627

2728
#include <QApplication>
2829
#include <QCoreApplication>
@@ -66,7 +67,7 @@ int QgsImageCacheEntry::dataSize() const
6667
int size = 0;
6768
if ( !image.isNull() )
6869
{
69-
size += ( image.width() * image.height() * 32 );
70+
size += image.sizeInBytes();
7071
}
7172
return size;
7273
}
@@ -78,10 +79,17 @@ void QgsImageCacheEntry::dump() const
7879

7980
///@endcond
8081

82+
static const int DEFAULT_IMAGE_CACHE_MAX_BYTES = 104857600;
8183

8284
QgsImageCache::QgsImageCache( QObject *parent )
83-
: QgsAbstractContentCache< QgsImageCacheEntry >( parent, QObject::tr( "Image" ) )
85+
: QgsAbstractContentCache< QgsImageCacheEntry >( parent, QObject::tr( "Image" ), DEFAULT_IMAGE_CACHE_MAX_BYTES )
8486
{
87+
int bytes = QgsSettings().value( QStringLiteral( "/qgis/maxImageCacheSize" ), 0 ).toInt();
88+
if ( bytes > 0 )
89+
{
90+
mMaxCacheSize = bytes;
91+
}
92+
8593
mMissingSvg = QStringLiteral( "<svg width='10' height='10'><text x='5' y='10' font-size='10' text-anchor='middle'>?</text></svg>" ).toLatin1();
8694

8795
const QString downloadingSvgPath = QgsApplication::defaultThemePath() + QStringLiteral( "downloading_svg.svg" );
@@ -127,15 +135,15 @@ QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const boo
127135
long cachedDataSize = 0;
128136
bool isBroken = false;
129137
result = renderImage( file, size, keepAspectRatio, opacity, targetDpi, isBroken, blocking );
130-
cachedDataSize += result.width() * result.height() * 32;
138+
cachedDataSize += result.sizeInBytes();
131139
if ( cachedDataSize > mMaxCacheSize / 2 )
132140
{
133141
fitsInCache = false;
134142
currentEntry->image = QImage();
135143
}
136144
else
137145
{
138-
mTotalSize += ( result.width() * result.height() * 32 );
146+
mTotalSize += result.sizeInBytes();
139147
currentEntry->image = result;
140148
}
141149

‎tests/src/core/testqgsimagecache.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class TestQgsImageCache : public QObject
5656
void base64();
5757
void empty();
5858
void dpi();
59-
59+
void cachesize();
6060
};
6161

6262

@@ -318,6 +318,14 @@ void TestQgsImageCache::dpi()
318318
QVERIFY( entry1.isEqual( &entry2 ) );
319319
}
320320

321+
void TestQgsImageCache::cachesize()
322+
{
323+
uint cacheSize = 100000000;
324+
QgsSettings().setValue( QStringLiteral( "/qgis/maxImageCacheSize" ), cacheSize );
325+
QgsImageCache cache;
326+
QCOMPARE( cache.mMaxCacheSize, cacheSize );
327+
}
328+
321329
bool TestQgsImageCache::imageCheck( const QString &testName, QImage &image, int mismatchCount )
322330
{
323331
//draw background

0 commit comments

Comments
 (0)
Please sign in to comment.