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

(cherry-picked from 58ded28)
  • Loading branch information
nyalldawson committed Apr 19, 2017
1 parent 6c91d45 commit 4c7cff5ae74af944e7a8857fb87bb82ecbbc60cc
Showing with 81 additions and 2 deletions.
  1. +16 −2 src/core/composer/qgscomposermap.cpp
  2. +65 −0 tests/src/core/testqgscomposition.cpp
@@ -1351,7 +1351,15 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
for ( ; styleIt != mLayerStyleOverrides.constEnd(); ++styleIt )
{
QDomElement styleElem = doc.createElement( "LayerStyle" );
styleElem.setAttribute( "layerid", styleIt.key() );

QgsMapLayerRef ref( styleIt.key() );
ref.resolve();

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

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

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

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

private:
@@ -799,6 +800,70 @@ void TestQgsComposition::mapLayersRestoredFromTemplate()
QCOMPARE( map2->layerSet(), QStringList() << layer3->id() << layer4->id() );
}

void TestQgsComposition::mapLayersStyleOverrideRestoredFromTemplate()
{
QgsMapLayerRegistry::instance()->removeAllMapLayers();

// 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" );
QgsMapLayerRegistry::instance()->addMapLayer( layer2 );
QgsMapLayerRegistry::instance()->addMapLayer( layer );

// create composition
QgsMapSettings ms;
QgsComposition c( ms );
// 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(), "<b>xxxxx</b>" );
styles.insert( layer2->id(), "<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
QgsMapLayerRegistry::instance()->removeAllMapLayers();
QgsVectorLayer *layer3 = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
QgsVectorLayer *layer4 = new QgsVectorLayer( vectorFileInfo2.filePath(),
vectorFileInfo2.completeBaseName(),
"ogr" );
QgsMapLayerRegistry::instance()->addMapLayer( layer4 );
QgsMapLayerRegistry::instance()->addMapLayer( layer3 );

// make a new composition from template
QgsComposition c2( ms );
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(), QString( "<b>xxxxx</b>" ) );
QVERIFY( restoredStyles.contains( layer4->id() ) );
QCOMPARE( restoredStyles.value( layer4->id() ).trimmed(), QString( "<blink>yyyyy</blink>" ) );
}

void TestQgsComposition::atlasLayerRestoredFromTemplate()
{
QgsMapLayerRegistry::instance()->removeAllMapLayers();

0 comments on commit 4c7cff5

Please sign in to comment.