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
819804QString QgsLayoutItemPicture::rotationMap () const
820805{
0 commit comments