Skip to content
Permalink
Browse files
Correctly handle restoring atlas coverage layer from template
(when template was created in a different project)

On behalf of Faunalia, sponsored by ENEL

(cherry-picked from 5b6b035)
  • Loading branch information
nyalldawson committed Apr 19, 2017
1 parent b7088b5 commit 6c91d4532997174c9e460255780e670c9d7a5fa1
Showing with 58 additions and 18 deletions.
  1. +15 −16 src/core/composer/qgsatlascomposition.cpp
  2. +3 −2 src/core/composer/qgsatlascomposition.h
  3. +40 −0 tests/src/core/testqgscomposition.cpp
@@ -35,7 +35,6 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition )
, mEnabled( false )
, mHideCoverage( false )
, mFilenamePattern( "'output_'||@atlas_featurenumber" )
, mCoverageLayer( nullptr )
, mSingleFile( false )
, mSortFeatures( false )
, mSortAscending( true )
@@ -73,10 +72,10 @@ void QgsAtlasComposition::removeLayers( const QStringList& layers )

Q_FOREACH ( const QString& layerId, layers )
{
if ( layerId == mCoverageLayer->id() )
if ( layerId == mCoverageLayer.layerId )
{
//current coverage layer removed
mCoverageLayer = nullptr;
mCoverageLayer.setLayer( nullptr );
setEnabled( false );
return;
}
@@ -85,12 +84,12 @@ void QgsAtlasComposition::removeLayers( const QStringList& layers )

void QgsAtlasComposition::setCoverageLayer( QgsVectorLayer* layer )
{
if ( layer == mCoverageLayer )
if ( layer == mCoverageLayer.get() )
{
return;
}

mCoverageLayer = layer;
mCoverageLayer.setLayer( layer );
emit coverageLayerChanged( layer );
}

@@ -644,7 +643,10 @@ void QgsAtlasComposition::writeXML( QDomElement& elem, QDomDocument& doc ) const

if ( mCoverageLayer )
{
atlasElem.setAttribute( "coverageLayer", mCoverageLayer->id() );
atlasElem.setAttribute( "coverageLayer", mCoverageLayer.layerId );
atlasElem.setAttribute( "coverageLayerName", mCoverageLayer.name );
atlasElem.setAttribute( "coverageLayerSource", mCoverageLayer.source );
atlasElem.setAttribute( "coverageLayerProvider", mCoverageLayer.provider );
}
else
{
@@ -682,16 +684,13 @@ void QgsAtlasComposition::readXML( const QDomElement& atlasElem, const QDomDocum
}

// look for stored layer name
mCoverageLayer = nullptr;
QMap<QString, QgsMapLayer*> layers = QgsMapLayerRegistry::instance()->mapLayers();
for ( QMap<QString, QgsMapLayer*>::const_iterator it = layers.begin(); it != layers.end(); ++it )
{
if ( it.key() == atlasElem.attribute( "coverageLayer" ) )
{
mCoverageLayer = dynamic_cast<QgsVectorLayer*>( it.value() );
break;
}
}
QString layerId = atlasElem.attribute( "coverageLayer" );
QString layerName = atlasElem.attribute( "coverageLayerName" );
QString layerSource = atlasElem.attribute( "coverageLayerSource" );
QString layerProvider = atlasElem.attribute( "coverageLayerProvider" );

mCoverageLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
mCoverageLayer.resolveWeakly();

mPageNameExpression = atlasElem.attribute( "pageNameExpression", QString() );
mSingleFile = atlasElem.attribute( "singleFile", "false" ) == "true" ? true : false;
@@ -19,6 +19,7 @@
#include "qgscoordinatetransform.h"
#include "qgsfeature.h"
#include "qgsgeometry.h"
#include "qgsvectorlayerref.h"

#include <memory>
#include <QString>
@@ -101,7 +102,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
* @returns atlas coverage layer
* @see setCoverageLayer
*/
QgsVectorLayer* coverageLayer() const { return mCoverageLayer; }
QgsVectorLayer* coverageLayer() const { return mCoverageLayer.get(); }

/** Sets the coverage layer to use for the atlas features
* @param layer vector coverage layer
@@ -355,7 +356,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
bool mEnabled;
bool mHideCoverage;
QString mFilenamePattern;
QgsVectorLayer* mCoverageLayer;
QgsVectorLayerRef mCoverageLayer;
bool mSingleFile;

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

private:
QgsComposition *mComposition;
@@ -798,5 +799,44 @@ void TestQgsComposition::mapLayersRestoredFromTemplate()
QCOMPARE( map2->layerSet(), QStringList() << layer3->id() << layer4->id() );
}

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

// load some layers
QFileInfo vectorFileInfo( QString( TEST_DATA_DIR ) + "/points.shp" );
QgsVectorLayer *layer = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
QgsMapLayerRegistry::instance()->addMapLayer( layer );

// create composition
QgsMapSettings ms;
QgsComposition c( ms );
// set atlas layer
c.atlasComposition().setEnabled( true );
c.atlasComposition().setCoverageLayer( layer );

// 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 *layer2 = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
QgsMapLayerRegistry::instance()->addMapLayer( layer2 );

// make a new composition from template
QgsComposition c2( ms );
QVERIFY( c2.loadFromTemplate( doc ) );
// check atlas layer
QCOMPARE( c2.atlasComposition().coverageLayer(), layer2 );
}

QTEST_MAIN( TestQgsComposition )
#include "testqgscomposition.moc"

0 comments on commit 6c91d45

Please sign in to comment.