Skip to content

Commit

Permalink
Fixes #37804 : save original layout item picture mode
Browse files Browse the repository at this point in the history
  • Loading branch information
troopa81 authored and nyalldawson committed Oct 21, 2022
1 parent fb7b52b commit 06c29c7
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 11 deletions.
13 changes: 12 additions & 1 deletion python/core/auto_generated/layout/qgslayoutitempicture.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,20 @@ Sets the stroke ``width`` (in layout units) used for parametrized SVG files.

Format mode() const;
%Docstring
Returns the current picture mode (image format).
Returns the current picture mode (image format), FormatUnknown if given
picture format is unknown

.. seealso:: :py:func:`setMode`
%End

Format originalMode() const;
%Docstring
Returns the original set picture mode (image format).
It could differ from :py:func:`~QgsLayoutItemPicture.mode` if given picture format is unknown

.. seealso:: :py:func:`setMode`

.. versionadded:: 3.22
%End

void setMode( Format mode );
Expand Down
14 changes: 8 additions & 6 deletions src/core/layout/qgslayoutitempicture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,6 @@ 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 @@ -583,14 +582,14 @@ void QgsLayoutItemPicture::loadPicture( const QVariant &data )
{
//trying to load an invalid file or bad expression, show cross picture
mIsMissingImage = true;
if ( origFormat == FormatRaster )
if ( mOriginalMode == FormatRaster )
{
const QString badFile( QStringLiteral( ":/images/composer/missing_image.png" ) );
QImageReader imageReader( badFile );
if ( imageReader.read( &mImage ) )
mMode = FormatRaster;
}
else
else if ( mOriginalMode == FormatSVG )
{
const QString badFile( QStringLiteral( ":/images/composer/missing_image.svg" ) );
mSVG.load( badFile );
Expand Down Expand Up @@ -767,6 +766,7 @@ void QgsLayoutItemPicture::refreshDataDefinedProperty( const QgsLayoutObject::Da

void QgsLayoutItemPicture::setPicturePath( const QString &path, Format format )
{
mOriginalMode = format;
mMode = format;
mSourcePath = path;
refreshPicture();
Expand Down Expand Up @@ -796,7 +796,7 @@ bool QgsLayoutItemPicture::writePropertiesToElement( QDomElement &elem, QDomDocu
elem.setAttribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( mSvgFillColor ) );
elem.setAttribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( mSvgStrokeColor ) );
elem.setAttribute( QStringLiteral( "svgBorderWidth" ), QString::number( mSvgStrokeWidth ) );
elem.setAttribute( QStringLiteral( "mode" ), mMode );
elem.setAttribute( QStringLiteral( "mode" ), mOriginalMode );

//rotation
elem.setAttribute( QStringLiteral( "pictureRotation" ), QString::number( mPictureRotation ) );
Expand Down Expand Up @@ -824,7 +824,8 @@ bool QgsLayoutItemPicture::readPropertiesFromElement( const QDomElement &itemEle
mSvgFillColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 255, 255, 255 ) ) ) );
mSvgStrokeColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 0, 0, 0 ) ) ) );
mSvgStrokeWidth = itemElem.attribute( QStringLiteral( "svgBorderWidth" ), QStringLiteral( "0.2" ) ).toDouble();
mMode = static_cast< Format >( itemElem.attribute( QStringLiteral( "mode" ), QString::number( FormatUnknown ) ).toInt() );
mOriginalMode = static_cast< Format >( itemElem.attribute( QStringLiteral( "mode" ), QString::number( FormatUnknown ) ).toInt() );
mMode = mOriginalMode;

const QDomNodeList composerItemList = itemElem.elementsByTagName( QStringLiteral( "ComposerItem" ) );
if ( !composerItemList.isEmpty() )
Expand Down Expand Up @@ -929,9 +930,10 @@ void QgsLayoutItemPicture::setSvgStrokeWidth( double width )

void QgsLayoutItemPicture::setMode( QgsLayoutItemPicture::Format mode )
{
if ( mMode == mode )
if ( mOriginalMode == mode )
return;

mOriginalMode = mode;
mMode = mode;
refreshPicture();
}
Expand Down
14 changes: 12 additions & 2 deletions src/core/layout/qgslayoutitempicture.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,20 @@ class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
void setSvgStrokeWidth( double width );

/**
* Returns the current picture mode (image format).
* \see setMode()
* Returns the current picture mode (image format), FormatUnknown if given
* picture format is unknown
* \see setMode() originalMode()
*/
Format mode() const { return mMode; }

/**
* Returns the original set picture mode (image format).
* It could differ from mode() if given picture format is unknown
* \see setMode() mode()
* \since 3.22
*/
Format originalMode() const { return mOriginalMode; }

/**
* Sets the current picture \a mode (image format).
* \see mode()
Expand Down Expand Up @@ -330,6 +339,7 @@ class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
//! Absolute path to the image (may be also HTTP URL)
QString mSourcePath;
Format mMode = FormatUnknown;
Format mOriginalMode = FormatUnknown;

QSize mDefaultSvgSize;

Expand Down
3 changes: 1 addition & 2 deletions src/gui/layout/qgslayoutpicturewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void QgsLayoutPictureWidget::setGuiElementValues()
mAnchorPointComboBox->setEnabled( false );
}

switch ( mPicture->mode() )
switch ( mPicture->originalMode() )
{
case QgsLayoutItemPicture::FormatSVG:
case QgsLayoutItemPicture::FormatUnknown:
Expand Down Expand Up @@ -496,4 +496,3 @@ void QgsLayoutPictureWidget::populateDataDefinedButtons()
updateDataDefinedButton( mStrokeColorDDBtn );
updateDataDefinedButton( mStrokeWidthDDBtn );
}

58 changes: 58 additions & 0 deletions tests/src/core/testqgslayoutpicture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,20 +492,78 @@ void TestQgsLayoutPicture::valid()
picture->setPicturePath( mPngImage );
QVERIFY( !picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), mPngImage );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatRaster );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

picture->setPicturePath( QStringLiteral( "bad" ) );
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatUnknown );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

picture->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'%1'" ).arg( mSvgImage ) ) );
picture->refreshPicture();
QVERIFY( !picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), mSvgImage );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatSVG );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

picture->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'bad'" ) ) );
picture->refreshPicture();
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatUnknown );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

// same tests with a given format

picture->dataDefinedProperties().clear();

picture->setPicturePath( mPngImage, QgsLayoutItemPicture::FormatRaster );
QVERIFY( !picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), mPngImage );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatRaster );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatRaster );

picture->setPicturePath( mPngImage, QgsLayoutItemPicture::FormatUnknown );
QVERIFY( !picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), mPngImage );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatRaster );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

picture->setPicturePath( QStringLiteral( "bad" ), QgsLayoutItemPicture::FormatUnknown );
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatUnknown );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );

picture->setPicturePath( QStringLiteral( "bad" ), QgsLayoutItemPicture::FormatRaster );
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatRaster ); // cross image for missing image
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatRaster );

picture->setPicturePath( QStringLiteral( "bad" ), QgsLayoutItemPicture::FormatSVG );
picture->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'%1'" ).arg( mSvgImage ) ) );
picture->refreshPicture();
QVERIFY( !picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), mSvgImage );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatSVG );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatSVG );

picture->dataDefinedProperties().setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( QStringLiteral( "'bad'" ) ) );
picture->refreshPicture();
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatSVG ); // cross image for missing picture
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatSVG );

picture->setPicturePath( QStringLiteral( "bad" ), QgsLayoutItemPicture::FormatUnknown );
picture->refreshPicture();
QVERIFY( picture->isMissingImage() );
QCOMPARE( picture->evaluatedPath(), QStringLiteral( "bad" ) );
QCOMPARE( picture->mode(), QgsLayoutItemPicture::FormatUnknown );
QCOMPARE( picture->originalMode(), QgsLayoutItemPicture::FormatUnknown );
}

QGSTEST_MAIN( TestQgsLayoutPicture )
Expand Down

0 comments on commit 06c29c7

Please sign in to comment.