diff --git a/src/core/qgsimagecache.cpp b/src/core/qgsimagecache.cpp index f3181a39746b..c52e01d96eac 100644 --- a/src/core/qgsimagecache.cpp +++ b/src/core/qgsimagecache.cpp @@ -101,8 +101,13 @@ QgsImageCache::QgsImageCache( QObject *parent ) connect( this, &QgsAbstractContentCacheBase::remoteContentFetched, this, &QgsImageCache::remoteImageFetched ); } -QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache ) +QImage QgsImageCache::pathAsImage( const QString &f, const QSize size, const bool keepAspectRatio, const double opacity, bool &fitsInCache ) { + const QString file = f.trimmed(); + + if ( file.isEmpty() ) + return QImage(); + QMutexLocker locker( &mMutex ); fitsInCache = true; @@ -141,6 +146,9 @@ QImage QgsImageCache::pathAsImage( const QString &file, const QSize size, const QSize QgsImageCache::originalSize( const QString &path ) const { + if ( path.isEmpty() ) + return QSize(); + // direct read if path is a file -- maybe more efficient than going the bytearray route? (untested!) if ( QFile::exists( path ) ) { diff --git a/tests/src/core/testqgsimagecache.cpp b/tests/src/core/testqgsimagecache.cpp index 42b5c25bd76c..8b78b3129ef5 100644 --- a/tests/src/core/testqgsimagecache.cpp +++ b/tests/src/core/testqgsimagecache.cpp @@ -53,6 +53,7 @@ class TestQgsImageCache : public QObject void size(); // check various size-specific handling void opacity(); // check non-opaque image rendering void base64(); + void empty(); }; @@ -254,6 +255,22 @@ void TestQgsImageCache::base64() QCOMPARE( size.height(), 60 ); } +void TestQgsImageCache::empty() +{ + QgsImageCache cache; + bool inCache = false; + + QImage img = cache.pathAsImage( QString(), QSize( 200, 200 ), true, 1.0, inCache ); + QVERIFY( img.isNull() ); + + QVERIFY( !cache.originalSize( QString() ).isValid() ); + + img = cache.pathAsImage( QStringLiteral( " " ), QSize( 200, 200 ), true, 1.0, inCache ); + QVERIFY( img.isNull() ); + + QVERIFY( !cache.originalSize( QStringLiteral( " " ) ).isValid() ); +} + bool TestQgsImageCache::imageCheck( const QString &testName, QImage &image, int mismatchCount ) { //draw background