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

(cherry-picked from 5b6b035)
  • Loading branch information
nyalldawson committed Apr 19, 2017
1 parent b7088b5 commit 6c91d45
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 18 deletions.
31 changes: 15 additions & 16 deletions src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition )
, mEnabled( false )
, mHideCoverage( false )
, mFilenamePattern( "'output_'||@atlas_featurenumber" )
, mCoverageLayer( nullptr )
, mSingleFile( false )
, mSortFeatures( false )
, mSortAscending( true )
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 );
}

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

#include <memory>
#include <QString>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -355,7 +356,7 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
bool mEnabled;
bool mHideCoverage;
QString mFilenamePattern;
QgsVectorLayer* mCoverageLayer;
QgsVectorLayerRef mCoverageLayer;
bool mSingleFile;

QString mCurrentFilename;
Expand Down
40 changes: 40 additions & 0 deletions tests/src/core/testqgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class TestQgsComposition : public QObject
void legendRestoredFromTemplate();
void attributeTableRestoredFromTemplate();
void mapLayersRestoredFromTemplate();
void atlasLayerRestoredFromTemplate();

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