Skip to content

Commit e539022

Browse files
committed
Restore serialization of picture items
1 parent 272097b commit e539022

File tree

6 files changed

+55
-84
lines changed

6 files changed

+55
-84
lines changed

python/core/layout/qgslayoutitem.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
7878
UndoPictureRotation,
7979
UndoPictureFillColor,
8080
UndoPictureStrokeColor,
81+
UndoPictureStrokeWidth,
8182
UndoPictureNorthOffset,
8283
UndoCustomCommand,
8384
};

python/core/layout/qgslayoutitempicture.sip

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,6 @@ class QgsLayoutItemPicture: QgsLayoutItem
8181
%End
8282

8383

84-
85-
8684
double pictureRotation() const;
8785
%Docstring
8886
Returns the rotation used for drawing the picture within the item's frame,
@@ -280,6 +278,9 @@ Is emitted on picture rotation change
280278

281279
virtual QSizeF applyItemSizeConstraint( const QSizeF &targetSize );
282280

281+
virtual bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const;
282+
283+
virtual bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context );
283284

284285

285286
};

src/app/layout/qgslayoutpicturewidget.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -706,7 +706,7 @@ void QgsLayoutPictureWidget::mStrokeColorButton_colorChanged( const QColor &colo
706706

707707
void QgsLayoutPictureWidget::mStrokeWidthSpinBox_valueChanged( double d )
708708
{
709-
mPicture->beginCommand( tr( "Change Picture Stroke Width" ) );
709+
mPicture->beginCommand( tr( "Change Picture Stroke Width" ), QgsLayoutItem::UndoPictureStrokeWidth );
710710
mPicture->setSvgStrokeWidth( d );
711711
mPicture->endCommand();
712712
mPicture->update();

src/core/layout/qgslayoutitem.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
111111
UndoPictureRotation, //!< Picture rotation
112112
UndoPictureFillColor, //!< Picture fill color
113113
UndoPictureStrokeColor, //!< Picture stroke color
114+
UndoPictureStrokeWidth, //!< Picture stroke width
114115
UndoPictureNorthOffset, //!< Picture north offset
115116
UndoCustomCommand, //!< Base id for plugin based item undo commands
116117
};

src/core/layout/qgslayoutitempicture.cpp

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "qgslogger.h"
3434
#include "qgsbearingutils.h"
3535
#include "qgsmapsettings.h"
36+
#include "qgsreadwritecontext.h"
3637

3738
#include <QDomDocument>
3839
#include <QDomElement>
@@ -680,63 +681,49 @@ QString QgsLayoutItemPicture::picturePath() const
680681
{
681682
return mSourcePath;
682683
}
683-
#if 0 //TODO
684-
bool QgsLayoutItemPicture::writeXml( QDomElement &elem, QDomDocument &doc ) const
684+
685+
bool QgsLayoutItemPicture::writePropertiesToElement( QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
685686
{
686-
if ( elem.isNull() )
687-
{
688-
return false;
689-
}
690-
QDomElement composerPictureElem = doc.createElement( QStringLiteral( "ComposerPicture" ) );
691687
QString imagePath = mSourcePath;
692-
if ( mComposition )
693-
{
694-
// convert from absolute path to relative. For SVG we also need to consider system SVG paths
695-
QgsPathResolver pathResolver = mComposition->project()->pathResolver();
696-
if ( imagePath.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
697-
imagePath = QgsSymbolLayerUtils::svgSymbolPathToName( imagePath, pathResolver );
698-
else
699-
imagePath = pathResolver.writePath( imagePath );
700-
}
701-
composerPictureElem.setAttribute( QStringLiteral( "file" ), imagePath );
702-
composerPictureElem.setAttribute( QStringLiteral( "pictureWidth" ), QString::number( mPictureWidth ) );
703-
composerPictureElem.setAttribute( QStringLiteral( "pictureHeight" ), QString::number( mPictureHeight ) );
704-
composerPictureElem.setAttribute( QStringLiteral( "resizeMode" ), QString::number( static_cast< int >( mResizeMode ) ) );
705-
composerPictureElem.setAttribute( QStringLiteral( "anchorPoint" ), QString::number( static_cast< int >( mPictureAnchor ) ) );
706-
composerPictureElem.setAttribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( mSvgFillColor ) );
707-
composerPictureElem.setAttribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( mSvgStrokeColor ) );
708-
composerPictureElem.setAttribute( QStringLiteral( "svgBorderWidth" ), QString::number( mSvgStrokeWidth ) );
688+
689+
// convert from absolute path to relative. For SVG we also need to consider system SVG paths
690+
QgsPathResolver pathResolver = context.pathResolver();
691+
if ( imagePath.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
692+
imagePath = QgsSymbolLayerUtils::svgSymbolPathToName( imagePath, pathResolver );
693+
else
694+
imagePath = pathResolver.writePath( imagePath );
695+
696+
elem.setAttribute( QStringLiteral( "file" ), imagePath );
697+
elem.setAttribute( QStringLiteral( "pictureWidth" ), QString::number( mPictureWidth ) );
698+
elem.setAttribute( QStringLiteral( "pictureHeight" ), QString::number( mPictureHeight ) );
699+
elem.setAttribute( QStringLiteral( "resizeMode" ), QString::number( static_cast< int >( mResizeMode ) ) );
700+
elem.setAttribute( QStringLiteral( "anchorPoint" ), QString::number( static_cast< int >( mPictureAnchor ) ) );
701+
elem.setAttribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( mSvgFillColor ) );
702+
elem.setAttribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( mSvgStrokeColor ) );
703+
elem.setAttribute( QStringLiteral( "svgBorderWidth" ), QString::number( mSvgStrokeWidth ) );
709704

710705
//rotation
711-
composerPictureElem.setAttribute( QStringLiteral( "pictureRotation" ), QString::number( mPictureRotation ) );
706+
elem.setAttribute( QStringLiteral( "pictureRotation" ), QString::number( mPictureRotation ) );
712707
if ( !mRotationMap )
713708
{
714-
composerPictureElem.setAttribute( QStringLiteral( "mapId" ), -1 );
709+
elem.setAttribute( QStringLiteral( "mapUuid" ), QString() );
715710
}
716711
else
717712
{
718-
composerPictureElem.setAttribute( QStringLiteral( "mapId" ), mRotationMap->id() );
713+
elem.setAttribute( QStringLiteral( "mapUuid" ), mRotationMap->uuid() );
719714
}
720-
composerPictureElem.setAttribute( QStringLiteral( "northMode" ), mNorthMode );
721-
composerPictureElem.setAttribute( QStringLiteral( "northOffset" ), mNorthOffset );
722-
723-
_writeXml( composerPictureElem, doc );
724-
elem.appendChild( composerPictureElem );
715+
elem.setAttribute( QStringLiteral( "northMode" ), mNorthMode );
716+
elem.setAttribute( QStringLiteral( "northOffset" ), mNorthOffset );
725717
return true;
726718
}
727719

728-
bool QgsLayoutItemPicture::readXml( const QDomElement &itemElem, const QDomDocument &doc )
720+
bool QgsLayoutItemPicture::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
729721
{
730-
if ( itemElem.isNull() )
731-
{
732-
return false;
733-
}
734-
735722
mPictureWidth = itemElem.attribute( QStringLiteral( "pictureWidth" ), QStringLiteral( "10" ) ).toDouble();
736723
mPictureHeight = itemElem.attribute( QStringLiteral( "pictureHeight" ), QStringLiteral( "10" ) ).toDouble();
737724
mResizeMode = QgsLayoutItemPicture::ResizeMode( itemElem.attribute( QStringLiteral( "resizeMode" ), QStringLiteral( "0" ) ).toInt() );
738725
//when loading from xml, default to anchor point of middle to match pre 2.4 behavior
739-
mPictureAnchor = static_cast< QgsComposerItem::ItemPositionMode >( itemElem.attribute( QStringLiteral( "anchorPoint" ), QString::number( QgsComposerItem::Middle ) ).toInt() );
726+
mPictureAnchor = static_cast< QgsLayoutItem::ReferencePoint >( itemElem.attribute( QStringLiteral( "anchorPoint" ), QString::number( QgsLayoutItem::Middle ) ).toInt() );
740727

741728
mSvgFillColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgFillColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 255, 255, 255 ) ) ) );
742729
mSvgStrokeColor = QgsSymbolLayerUtils::decodeColor( itemElem.attribute( QStringLiteral( "svgBorderColor" ), QgsSymbolLayerUtils::encodeColor( QColor( 0, 0, 0 ) ) ) );
@@ -752,8 +739,6 @@ bool QgsLayoutItemPicture::readXml( const QDomElement &itemElem, const QDomDocum
752739
//in versions prior to 2.1 picture rotation was stored in the rotation attribute
753740
mPictureRotation = composerItemElem.attribute( QStringLiteral( "rotation" ), QStringLiteral( "0" ) ).toDouble();
754741
}
755-
756-
_readXml( composerItemElem, doc );
757742
}
758743

759744
mDefaultSvgSize = QSize( 0, 0 );
@@ -766,19 +751,18 @@ bool QgsLayoutItemPicture::readXml( const QDomElement &itemElem, const QDomDocum
766751
bool expressionActive;
767752
expressionActive = ( useExpression.compare( QLatin1String( "true" ), Qt::CaseInsensitive ) == 0 );
768753

769-
mDataDefinedProperties.setProperty( QgsComposerObject::PictureSource, QgsProperty::fromExpression( sourceExpression, expressionActive ) );
754+
mDataDefinedProperties.setProperty( QgsLayoutObject::PictureSource, QgsProperty::fromExpression( sourceExpression, expressionActive ) );
770755
}
771756

772757
QString imagePath = itemElem.attribute( QStringLiteral( "file" ) );
773-
if ( mComposition )
774-
{
775-
// convert from relative path to absolute. For SVG we also need to consider system SVG paths
776-
QgsPathResolver pathResolver = mComposition->project()->pathResolver();
777-
if ( imagePath.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
778-
imagePath = QgsSymbolLayerUtils::svgSymbolNameToPath( imagePath, pathResolver );
779-
else
780-
imagePath = pathResolver.readPath( imagePath );
781-
}
758+
759+
// convert from relative path to absolute. For SVG we also need to consider system SVG paths
760+
QgsPathResolver pathResolver = context.pathResolver();
761+
if ( imagePath.endsWith( QLatin1String( ".svg" ), Qt::CaseInsensitive ) )
762+
imagePath = QgsSymbolLayerUtils::svgSymbolNameToPath( imagePath, pathResolver );
763+
else
764+
imagePath = pathResolver.readPath( imagePath );
765+
782766
mSourcePath = imagePath;
783767

784768
//picture rotation
@@ -791,30 +775,31 @@ bool QgsLayoutItemPicture::readXml( const QDomElement &itemElem, const QDomDocum
791775
mNorthMode = static_cast< NorthMode >( itemElem.attribute( QStringLiteral( "northMode" ), QStringLiteral( "0" ) ).toInt() );
792776
mNorthOffset = itemElem.attribute( QStringLiteral( "northOffset" ), QStringLiteral( "0" ) ).toDouble();
793777

778+
#if 0 //TODO
794779
int rotationMapId = itemElem.attribute( QStringLiteral( "mapId" ), QStringLiteral( "-1" ) ).toInt();
795-
if ( rotationMapId == -1 )
780+
#endif
781+
782+
QString rotationMapId = itemElem.attribute( QStringLiteral( "mapUuid" ) );
783+
784+
if ( !mLayout || rotationMapId.isEmpty() )
796785
{
797786
mRotationMap = nullptr;
798787
}
799-
else if ( mComposition )
788+
else
800789
{
801-
802790
if ( mRotationMap )
803791
{
804-
disconnect( mRotationMap, &QgsComposerMap::mapRotationChanged, this, &QgsLayoutItemPicture::updateMapRotation );
805-
disconnect( mRotationMap, &QgsComposerMap::extentChanged, this, &QgsLayoutItemPicture::updateMapRotation );
792+
disconnect( mRotationMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutItemPicture::updateMapRotation );
793+
disconnect( mRotationMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemPicture::updateMapRotation );
806794
}
807-
mRotationMap = mComposition->getComposerMapById( rotationMapId );
808-
connect( mRotationMap, &QgsComposerMap::mapRotationChanged, this, &QgsLayoutItemPicture::updateMapRotation );
809-
connect( mRotationMap, &QgsComposerMap::extentChanged, this, &QgsLayoutItemPicture::updateMapRotation );
795+
mRotationMap = qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( rotationMapId ) );
796+
connect( mRotationMap, &QgsLayoutItemMap::mapRotationChanged, this, &QgsLayoutItemPicture::updateMapRotation );
797+
connect( mRotationMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemPicture::updateMapRotation );
810798
}
811799

812800
refreshPicture();
813-
814-
emit itemChanged();
815801
return true;
816802
}
817-
#endif
818803

819804
QString QgsLayoutItemPicture::rotationMap() const
820805
{

src/core/layout/qgslayoutitempicture.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,6 @@ class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
107107
void setSceneRect( const QRectF &rectangle ) override;
108108
#endif
109109

110-
111-
#if 0
112-
113-
/**
114-
* Stores state in Dom element
115-
* \param elem is Dom element corresponding to 'Composer' tag
116-
* \param doc is Dom document
117-
*/
118-
bool writeXml( QDomElement &elem, QDomDocument &doc ) const override;
119-
120-
/**
121-
* Sets state from Dom document
122-
* \param itemElem is Dom node corresponding to item tag
123-
* \param doc is Dom document
124-
*/
125-
bool readXml( const QDomElement &itemElem, const QDomDocument &doc ) override;
126-
#endif
127-
128110
/**
129111
* Returns the rotation used for drawing the picture within the item's frame,
130112
* in degrees clockwise.
@@ -300,7 +282,8 @@ class CORE_EXPORT QgsLayoutItemPicture: public QgsLayoutItem
300282

301283
void draw( QgsRenderContext &context, const QStyleOptionGraphicsItem *itemStyle = nullptr ) override;
302284
QSizeF applyItemSizeConstraint( const QSizeF &targetSize ) override;
303-
285+
bool writePropertiesToElement( QDomElement &element, QDomDocument &document, const QgsReadWriteContext &context ) const override;
286+
bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
304287

305288
private:
306289

0 commit comments

Comments
 (0)