Skip to content

Commit

Permalink
Correctly handle empty paths in QgsImageCache
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Dec 5, 2018
1 parent 58219d6 commit bafda24
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/qgsimagecache.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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 ) )
{
Expand Down
17 changes: 17 additions & 0 deletions tests/src/core/testqgsimagecache.cpp
Expand Up @@ -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();

};

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit bafda24

Please sign in to comment.