Skip to content
Permalink
Browse files
[composer] Correctly restore map item layers when loading a template
in a different project to that which the template was created in

On behalf of Faunalia, sponsored by ENEL

(cherry-picked from 2ca70dc)
  • Loading branch information
nyalldawson committed Apr 19, 2017
1 parent be3ddc6 commit b7088b599a0e5d9287240653dfa693c76edda91a
Showing with 80 additions and 5 deletions.
  1. +22 −5 src/core/composer/qgscomposermap.cpp
  2. +58 −0 tests/src/core/testqgscomposition.cpp
@@ -35,6 +35,7 @@
#include "qgsexpression.h"
#include "qgsvisibilitypresetcollection.h"
#include "qgsannotation.h"
#include "qgsvectorlayerref.h"

#include "qgslabel.h"
#include "qgslabelattributes.h"
@@ -1323,12 +1324,21 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const

//layer set
QDomElement layerSetElem = doc.createElement( "LayerSet" );
QStringList::const_iterator layerIt = mLayerSet.constBegin();
for ( ; layerIt != mLayerSet.constEnd(); ++layerIt )
Q_FOREACH ( const QString &layerId, mLayerSet )
{
QgsVectorLayerRef layerRef( layerId );
layerRef.resolve();

if ( !layerRef )
continue;

QDomElement layerElem = doc.createElement( "Layer" );
QDomText layerIdText = doc.createTextNode( *layerIt );
QDomText layerIdText = doc.createTextNode( layerRef.layerId );
layerElem.appendChild( layerIdText );
layerElem.setAttribute( "name", layerRef.name );
layerElem.setAttribute( "source", layerRef.source );
layerElem.setAttribute( "provider", layerRef.provider );

layerSetElem.appendChild( layerElem );
}
composerMapElem.appendChild( layerSetElem );
@@ -1456,8 +1466,15 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
layerSet.reserve( layerIdNodeList.size() );
for ( int i = 0; i < layerIdNodeList.size(); ++i )
{
const QDomElement& layerIdElement = layerIdNodeList.at( i ).toElement();
layerSet << layerIdElement.text();
QDomElement layerElem = layerIdNodeList.at( i ).toElement();
QString layerId = layerElem.text();
QString layerName = layerElem.attribute( "name" );
QString layerSource = layerElem.attribute( "source" );
QString layerProvider = layerElem.attribute( "provider" );

QgsMapLayerRef ref( layerId, layerName, layerSource, layerProvider );
ref.resolveWeakly();
layerSet << ref.layerId;
}
}
mLayerSet = layerSet;
@@ -62,6 +62,7 @@ class TestQgsComposition : public QObject
void variablesEdited();
void legendRestoredFromTemplate();
void attributeTableRestoredFromTemplate();
void mapLayersRestoredFromTemplate();

private:
QgsComposition *mComposition;
@@ -692,6 +693,8 @@ void TestQgsComposition::legendRestoredFromTemplate()

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

// load some layers
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + "/points.shp" );
QgsVectorLayer *layer = new QgsVectorLayer( vectorFileInfo.filePath(),
@@ -740,5 +743,60 @@ void TestQgsComposition::attributeTableRestoredFromTemplate()
QCOMPARE( table2->vectorLayer(), layer3 );
}

void TestQgsComposition::mapLayersRestoredFromTemplate()
{
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->setLayerSet( QStringList() << layer->id() << layer2->id() );

// 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 );

QCOMPARE( map2->layerSet(), QStringList() << layer3->id() << layer4->id() );
}

QTEST_MAIN( TestQgsComposition )
#include "testqgscomposition.moc"

0 comments on commit b7088b5

Please sign in to comment.