Skip to content

Commit

Permalink
[layouts] use missing_image.png for raster format
Browse files Browse the repository at this point in the history
(fixes #45481 and #45477)
  • Loading branch information
Joonalai authored and nyalldawson committed Oct 17, 2021
1 parent e823fb8 commit e0a3a21
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 9 deletions.
Binary file added images/composer/missing_image.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions images/images.qrc
Expand Up @@ -75,6 +75,7 @@
<file>north_arrows/gpsarrow.svg</file>
<file>north_arrows/gpsarrow2.svg</file>
<file>splash/splash.png</file>
<file>composer/missing_image.png</file>
<file>composer/missing_image.svg</file>
<file>themes/default/checks/SliverOrGap.svg</file>
<file>themes/default/checks/InvalidGeometry.svg</file>
Expand Down
28 changes: 19 additions & 9 deletions src/core/layout/qgslayoutitempicture.cpp
Expand Up @@ -538,6 +538,7 @@ void QgsLayoutItemPicture::updateNorthArrowRotation( double rotation )

void QgsLayoutItemPicture::loadPicture( const QVariant &data )
{
const Format origFormat = mMode;
mIsMissingImage = false;
QVariant imageData( data );
mEvaluatedPath = data.toString();
Expand Down Expand Up @@ -577,18 +578,27 @@ void QgsLayoutItemPicture::loadPicture( const QVariant &data )
else if ( mHasExpressionError || !mEvaluatedPath.isEmpty() )
{
//trying to load an invalid file or bad expression, show cross picture
mMode = FormatSVG;
mIsMissingImage = true;
const QString badFile( QStringLiteral( ":/images/composer/missing_image.svg" ) );
mSVG.load( badFile );
if ( mSVG.isValid() )
if ( origFormat == FormatRaster )
{
mMode = FormatSVG;
const QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
recalculateSize();
const QString badFile( QStringLiteral( ":/images/composer/missing_image.png" ) );
QImageReader imageReader( badFile );
if ( imageReader.read( &mImage ) )
mMode = FormatRaster;
}
else
{
const QString badFile( QStringLiteral( ":/images/composer/missing_image.svg" ) );
mSVG.load( badFile );
if ( mSVG.isValid() )
{
mMode = FormatSVG;
const QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
}
}
recalculateSize();
}

update();
Expand Down
15 changes: 15 additions & 0 deletions tests/src/python/test_qgslayoutpicture.py
Expand Up @@ -239,6 +239,21 @@ def testTrueNorth(self):
picture.setNorthOffset(-10)
self.assertAlmostEqual(picture.pictureRotation(), -38.18 + 35, 1)

def testMissingImage(self):
layout = QgsLayout(QgsProject.instance())

picture = QgsLayoutItemPicture(layout)

# SVG
picture.setPicturePath("invalid_path", QgsLayoutItemPicture.FormatSVG)
self.assertEqual(picture.isMissingImage(), True)
self.assertEqual(picture.mode(), QgsLayoutItemPicture.FormatSVG)

# Raster
picture.setPicturePath("invalid_path", QgsLayoutItemPicture.FormatRaster)
self.assertEqual(picture.isMissingImage(), True)
self.assertEqual(picture.mode(), QgsLayoutItemPicture.FormatRaster)


if __name__ == '__main__':
unittest.main()

0 comments on commit e0a3a21

Please sign in to comment.