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
  • Loading branch information
nyalldawson committed Apr 18, 2017
1 parent 832d2dd commit 5b6b035d83f256d7a1d8062255ca0cb6e89d196b
Showing with 56 additions and 11 deletions.
  1. +15 −8 src/core/composer/qgsatlascomposition.cpp
  2. +3 −3 src/core/composer/qgsatlascomposition.h
  3. +38 −0 tests/src/core/testqgscomposition.cpp
@@ -36,7 +36,6 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition *composition )
, mEnabled( false )
, mHideCoverage( false )
, mFilenamePattern( QStringLiteral( "'output_'||@atlas_featurenumber" ) )
, mCoverageLayer( nullptr )
, mSingleFile( false )
, mSortFeatures( false )
, mSortAscending( true )
@@ -70,10 +69,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;
}
@@ -82,12 +81,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 );
}

@@ -586,7 +585,10 @@ void QgsAtlasComposition::writeXml( QDomElement &elem, QDomDocument &doc ) const

if ( mCoverageLayer )
{
atlasElem.setAttribute( QStringLiteral( "coverageLayer" ), mCoverageLayer->id() );
atlasElem.setAttribute( QStringLiteral( "coverageLayer" ), mCoverageLayer.layerId );
atlasElem.setAttribute( QStringLiteral( "coverageLayerName" ), mCoverageLayer.name );
atlasElem.setAttribute( QStringLiteral( "coverageLayerSource" ), mCoverageLayer.source );
atlasElem.setAttribute( QStringLiteral( "coverageLayerProvider" ), mCoverageLayer.provider );
}
else
{
@@ -624,8 +626,13 @@ void QgsAtlasComposition::readXml( const QDomElement &atlasElem, const QDomDocum
}

// look for stored layer name
QString coverageLayerId = atlasElem.attribute( QStringLiteral( "coverageLayer" ) );
mCoverageLayer = qobject_cast<QgsVectorLayer *>( mComposition->project()->mapLayer( coverageLayerId ) );
QString layerId = atlasElem.attribute( QStringLiteral( "coverageLayer" ) );
QString layerName = atlasElem.attribute( QStringLiteral( "coverageLayerName" ) );
QString layerSource = atlasElem.attribute( QStringLiteral( "coverageLayerSource" ) );
QString layerProvider = atlasElem.attribute( QStringLiteral( "coverageLayerProvider" ) );

mCoverageLayer = QgsVectorLayerRef( layerId, layerName, layerSource, layerProvider );
mCoverageLayer.resolveWeakly( mComposition->project() );

mPageNameExpression = atlasElem.attribute( QStringLiteral( "pageNameExpression" ), QString() );
mSingleFile = atlasElem.attribute( QStringLiteral( "singleFile" ), QStringLiteral( "false" ) ) == QLatin1String( "true" );
@@ -21,7 +21,7 @@
#include "qgsgeometry.h"
#include "qgsrectangle.h"
#include "qgsexpression.h"

#include "qgsvectorlayerref.h"
#include <memory>
#include <QString>
#include <QDomElement>
@@ -101,7 +101,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
@@ -304,7 +304,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
bool mEnabled;
bool mHideCoverage;
QString mFilenamePattern;
QgsVectorLayer *mCoverageLayer = nullptr;
QgsVectorLayerRef mCoverageLayer;
bool mSingleFile;

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

private:
QgsComposition *mComposition = nullptr;
@@ -912,5 +913,42 @@ void TestQgsComposition::mapLayersRestoredFromTemplate()
QCOMPARE( map2->layers(), QList<QgsMapLayer *>() << layer3 << layer4 );
}

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

// create composition
QgsComposition c( &p );
// 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
QgsProject p2;
QgsVectorLayer *layer2 = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
p2.addMapLayer( layer2 );

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

QGSTEST_MAIN( TestQgsComposition )
#include "testqgscomposition.moc"

0 comments on commit 5b6b035

Please sign in to comment.