Skip to content

Commit

Permalink
[composer] Show missing image graphic if picture item source is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 15, 2014
1 parent a1aa980 commit df37e89
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 31 deletions.
80 changes: 80 additions & 0 deletions images/composer/missing_image.svg
Loading
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<file>north_arrows/gpsarrow.svg</file>
<file>north_arrows/gpsarrow2.svg</file>
<file>splash/splash.png</file>
<file>composer/missing_image.svg</file>
<file>themes/default/cap_flat.png</file>
<file>themes/default/cap_round.png</file>
<file>themes/default/cap_square.png</file>
Expand Down
6 changes: 0 additions & 6 deletions src/app/composer/qgscomposerpicturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,6 @@ void QgsComposerPictureWidget::on_mPictureLineEdit_editingFinished()
//check if file exists
QFileInfo fileInfo( filePath );

if ( !fileInfo.exists() || !fileInfo.isReadable() )
{
QMessageBox::critical( 0, "Invalid file", "Error, file does not exist or is not readable" );
return;
}

mPicture->beginCommand( tr( "Picture changed" ) );
mPicture->setPictureFile( filePath );
mPicture->update();
Expand Down
69 changes: 44 additions & 25 deletions src/core/composer/qgscomposerpicture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,47 +357,66 @@ void QgsComposerPicture::refreshPicture()

void QgsComposerPicture::loadPicture( const QFile& file )
{
if ( !file.exists() )
if ( !file.exists()
|| ( mUseSourceExpression && mPictureExpr->hasEvalError() ) )
{
mMode = Unknown;
}

QFileInfo sourceFileInfo( file );
QString sourceFileSuffix = sourceFileInfo.suffix();
if ( sourceFileSuffix.compare( "svg", Qt::CaseInsensitive ) == 0 )
{
//try to open svg
mSVG.load( file.fileName() );
if ( mSVG.isValid() )
{
mMode = SVG;
QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
}
else
{
mMode = Unknown;
}
}
else
{
//try to open raster with QImageReader
QImageReader imageReader( file.fileName() );
if ( imageReader.read( &mImage ) )
QFileInfo sourceFileInfo( file );
QString sourceFileSuffix = sourceFileInfo.suffix();
if ( sourceFileSuffix.compare( "svg", Qt::CaseInsensitive ) == 0 )
{
mMode = RASTER;
//try to open svg
mSVG.load( file.fileName() );
if ( mSVG.isValid() )
{
mMode = SVG;
QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
}
else
{
mMode = Unknown;
}
}
else
{
mMode = Unknown;
//try to open raster with QImageReader
QImageReader imageReader( file.fileName() );
if ( imageReader.read( &mImage ) )
{
mMode = RASTER;
}
else
{
mMode = Unknown;
}
}
}

if ( mMode != Unknown ) //make sure we start with a new QImage
{
recalculateSize();
}
else if ( !( file.fileName().isEmpty() ) || ( mUseSourceExpression && mPictureExpr->hasEvalError() ) )
{
//trying to load an invalid file or bad expression, show cross picture
mMode = SVG;
QString badFile = QString( ":/images/composer/missing_image.svg" );
mSVG.load( badFile );
if ( mSVG.isValid() )
{
mMode = SVG;
QRect viewBox = mSVG.viewBox(); //take width/height ratio from view box instead of default size
mDefaultSvgSize.setWidth( viewBox.width() );
mDefaultSvgSize.setHeight( viewBox.height() );
recalculateSize();
}
}

emit itemChanged();
}

Expand Down

0 comments on commit df37e89

Please sign in to comment.