Skip to content
Permalink
Browse files
[layouts] use missing_image.png for raster format
(fixes #45481 and #45477)
  • Loading branch information
Joonalai authored and nyalldawson committed Oct 17, 2021
1 parent e823fb8 commit e0a3a211234f7a19a5d896bef2fad2efa6d54fe9
Showing with 35 additions and 9 deletions.
  1. BIN images/composer/missing_image.png
  2. +1 −0 images/images.qrc
  3. +19 −9 src/core/layout/qgslayoutitempicture.cpp
  4. +15 −0 tests/src/python/test_qgslayoutpicture.py
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -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>
@@ -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();
@@ -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();
@@ -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.