Skip to content

Commit

Permalink
Fix QPainter warnings when broken image paths are passed to QgsImageC…
Browse files Browse the repository at this point in the history
…ache
  • Loading branch information
nyalldawson committed Dec 5, 2018
1 parent bafda24 commit 3136885
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/core/qgsimagecache.cpp
Expand Up @@ -194,6 +194,11 @@ QImage QgsImageCache::renderImage( const QString &path, QSize size, const bool k

if ( ba == "broken" )
{
// if image size is set to respect aspect ratio, correct for broken image aspect ratio
if ( size.width() == 0 )
size.setWidth( size.height() );
if ( size.height() == 0 )
size.setHeight( size.width() );
// render "broken" svg
im = QImage( size, QImage::Format_ARGB32_Premultiplied );
im.fill( 0 ); // transparent background
Expand All @@ -208,6 +213,12 @@ QImage QgsImageCache::renderImage( const QString &path, QSize size, const bool k
}
else if ( ba == "fetching" )
{
// if image size is set to respect aspect ratio, correct for broken image aspect ratio
if ( size.width() == 0 )
size.setWidth( size.height() );
if ( size.height() == 0 )
size.setHeight( size.width() );

// render "fetching" svg
im = QImage( size, QImage::Format_ARGB32_Premultiplied );
im.fill( 0 ); // transparent background
Expand Down
19 changes: 18 additions & 1 deletion tests/src/core/testqgsimagecache.cpp
Expand Up @@ -217,6 +217,23 @@ void TestQgsImageCache::size()
img = cache.pathAsImage( originalImage, QSize( 0, 100 ), true, 1.0, inCache );
QCOMPARE( img.width(), 64 );
QCOMPARE( img.height(), 100 );

// broken images should fallback to square aspect ratios, not the originally specified 0 px width or height
img = cache.pathAsImage( QStringLiteral( "broken" ), QSize( 0, 100 ), true, 1.0, inCache );
QCOMPARE( img.width(), 100 );
QCOMPARE( img.height(), 100 );

img = cache.pathAsImage( QStringLiteral( "broken" ), QSize( 100, 0 ), true, 1.0, inCache );
QCOMPARE( img.width(), 100 );
QCOMPARE( img.height(), 100 );

img = cache.pathAsImage( QStringLiteral( "broken" ), QSize( 0, 100 ), false, 1.0, inCache );
QCOMPARE( img.width(), 100 );
QCOMPARE( img.height(), 100 );

img = cache.pathAsImage( QStringLiteral( "broken" ), QSize( 100, 0 ), false, 1.0, inCache );
QCOMPARE( img.width(), 100 );
QCOMPARE( img.height(), 100 );
}

void TestQgsImageCache::opacity()
Expand All @@ -228,7 +245,7 @@ void TestQgsImageCache::opacity()

// null size should return image using original size
img = cache.pathAsImage( originalImage, QSize( 200, 200 ), true, 0.5, inCache );
QVERIFY( imageCheck( "opaque_image", img, 30 ) );
QVERIFY( imageCheck( QStringLiteral( "opaque_image" ), img, 30 ) );
}

void TestQgsImageCache::base64()
Expand Down

0 comments on commit 3136885

Please sign in to comment.