Skip to content

Commit

Permalink
Correctly handle restoring atlas coverage layer from template
Browse files Browse the repository at this point in the history
(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 5b6b035
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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" );
Expand Down
6 changes: 3 additions & 3 deletions src/core/composer/qgsatlascomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "qgsgeometry.h"
#include "qgsrectangle.h"
#include "qgsexpression.h"

#include "qgsvectorlayerref.h"
#include <memory>
#include <QString>
#include <QDomElement>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
38 changes: 38 additions & 0 deletions tests/src/core/testqgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class TestQgsComposition : public QObject
void legendRestoredFromTemplateAutoUpdate();
void attributeTableRestoredFromTemplate();
void mapLayersRestoredFromTemplate();
void atlasLayerRestoredFromTemplate();

private:
QgsComposition *mComposition = nullptr;
Expand Down Expand Up @@ -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.