Skip to content
Permalink
Browse files
[composer] Correctly handle restoring map layer style overrides from …
…template

(when template was created in a different project)

On behalf of Faunalia, sponsored by ENEL
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent 5b6b035 commit 58ded28974b74ceae6728a7bf183660465d2fa28
Showing with 79 additions and 2 deletions.
  1. +16 −2 src/core/composer/qgscomposermap.cpp
  2. +63 −0 tests/src/core/testqgscomposition.cpp
@@ -1214,7 +1214,15 @@ bool QgsComposerMap::writeXml( QDomElement &elem, QDomDocument &doc ) const
for ( ; styleIt != mLayerStyleOverrides.constEnd(); ++styleIt )
{
QDomElement styleElem = doc.createElement( QStringLiteral( "LayerStyle" ) );
styleElem.setAttribute( QStringLiteral( "layerid" ), styleIt.key() );

QgsMapLayerRef ref( styleIt.key() );
ref.resolve( mComposition->project() );

styleElem.setAttribute( QStringLiteral( "layerid" ), ref.layerId );
styleElem.setAttribute( QStringLiteral( "name" ), ref.name );
styleElem.setAttribute( QStringLiteral( "source" ), ref.source );
styleElem.setAttribute( QStringLiteral( "provider" ), ref.provider );

QgsMapLayerStyle style( styleIt.value() );
style.writeXml( styleElem );
stylesElem.appendChild( styleElem );
@@ -1365,9 +1373,15 @@ bool QgsComposerMap::readXml( const QDomElement &itemElem, const QDomDocument &d
{
const QDomElement &layerStyleElement = layerStyleNodeList.at( i ).toElement();
QString layerId = layerStyleElement.attribute( QStringLiteral( "layerid" ) );
QString layerName = layerStyleElement.attribute( QStringLiteral( "name" ) );
QString layerSource = layerStyleElement.attribute( QStringLiteral( "source" ) );
QString layerProvider = layerStyleElement.attribute( QStringLiteral( "provider" ) );
QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly( mComposition->project() );

QgsMapLayerStyle style;
style.readXml( layerStyleElement );
mLayerStyleOverrides.insert( layerId, style.xmlData() );
mLayerStyleOverrides.insert( ref.layerId, style.xmlData() );
}
}

@@ -68,6 +68,7 @@ class TestQgsComposition : public QObject
void legendRestoredFromTemplateAutoUpdate();
void attributeTableRestoredFromTemplate();
void mapLayersRestoredFromTemplate();
void mapLayersStyleOverrideRestoredFromTemplate();
void atlasLayerRestoredFromTemplate();

private:
@@ -913,6 +914,68 @@ void TestQgsComposition::mapLayersRestoredFromTemplate()
QCOMPARE( map2->layers(), QList<QgsMapLayer *>() << layer3 << layer4 );
}

void TestQgsComposition::mapLayersStyleOverrideRestoredFromTemplate()
{
// load some layers
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + "/points.shp" );
QgsVectorLayer *layer = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
QFileInfo vectorFileInfo2( QString( TEST_DATA_DIR ) + "/polys.shp" );
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorFileInfo2.filePath(),
vectorFileInfo2.completeBaseName(),
"ogr" );
QgsProject p;
p.addMapLayer( layer2 );
p.addMapLayer( layer );

// create composition
QgsComposition c( &p );
// add a map
QgsComposerMap *map = new QgsComposerMap( &c, 1, 1, 10, 10 );
c.addComposerMap( map );
map->setKeepLayerStyles( true );
QgsStringMap styles;
// just close your eyes and pretend these are real styles
styles.insert( layer->id(), QStringLiteral( "<b>xxxxx</b>" ) );
styles.insert( layer2->id(), QStringLiteral( "<blink>yyyyy</blink>" ) );
map->setLayerStyleOverrides( styles );

// save composition to template
QDomDocument doc;
QDomElement composerElem = doc.createElement( "Composer" );
doc.appendChild( composerElem );
c.writeXml( composerElem, doc );
c.atlasComposition().writeXml( composerElem, doc );

// new project
QgsProject p2;
QgsVectorLayer *layer3 = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
QgsVectorLayer *layer4 = new QgsVectorLayer( vectorFileInfo2.filePath(),
vectorFileInfo2.completeBaseName(),
"ogr" );
p2.addMapLayer( layer4 );
p2.addMapLayer( layer3 );

// make a new composition from template
QgsComposition c2( &p2 );
QVERIFY( c2.loadFromTemplate( doc ) );
// get map from new composition
QList< QgsComposerMap * > maps;
c2.composerItems( maps );
QgsComposerMap *map2 = static_cast< QgsComposerMap *>( maps.at( 0 ) );
QVERIFY( map2 );
QVERIFY( map2->keepLayerStyles() );

QgsStringMap restoredStyles = map2->layerStyleOverrides();
QVERIFY( restoredStyles.contains( layer3->id() ) );
QCOMPARE( restoredStyles.value( layer3->id() ).trimmed(), QStringLiteral( "<b>xxxxx</b>" ) );
QVERIFY( restoredStyles.contains( layer4->id() ) );
QCOMPARE( restoredStyles.value( layer4->id() ).trimmed(), QStringLiteral( "<blink>yyyyy</blink>" ) );
}

void TestQgsComposition::atlasLayerRestoredFromTemplate()
{
// load some layers

0 comments on commit 58ded28

Please sign in to comment.