Skip to content

Commit 808464f

Browse files
committed
[composer] Disable atlas if coverage layer is removed from project
1 parent 46c7599 commit 808464f

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

src/core/composer/qgsatlascomposition.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition ) :
4747
QgsExpression::setSpecialColumn( "$atlasfeatureid", QVariant(( int )0 ) );
4848
QgsExpression::setSpecialColumn( "$atlasfeature", QVariant::fromValue( QgsFeature() ) );
4949
QgsExpression::setSpecialColumn( "$atlasgeometry", QVariant::fromValue( QgsGeometry() ) );
50+
51+
//listen out for layer removal
52+
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
5053
}
5154

5255
QgsAtlasComposition::~QgsAtlasComposition()
@@ -55,13 +58,43 @@ QgsAtlasComposition::~QgsAtlasComposition()
5558

5659
void QgsAtlasComposition::setEnabled( bool enabled )
5760
{
61+
if ( enabled == mEnabled )
62+
{
63+
return;
64+
}
65+
5866
mEnabled = enabled;
5967
mComposition->setAtlasMode( QgsComposition::AtlasOff );
6068
emit toggled( enabled );
69+
emit parameterChanged();
70+
}
71+
72+
void QgsAtlasComposition::removeLayers( QStringList layers )
73+
{
74+
if ( !mCoverageLayer )
75+
{
76+
return;
77+
}
78+
79+
foreach ( QString layerId, layers )
80+
{
81+
if ( layerId == mCoverageLayer->id() )
82+
{
83+
//current coverage layer removed
84+
mCoverageLayer = 0;
85+
setEnabled( false );
86+
return;
87+
}
88+
}
6189
}
6290

6391
void QgsAtlasComposition::setCoverageLayer( QgsVectorLayer* layer )
6492
{
93+
if ( layer == mCoverageLayer )
94+
{
95+
return;
96+
}
97+
6598
mCoverageLayer = layer;
6699

67100
// update the number of features

src/core/composer/qgsatlascomposition.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QString>
2424
#include <QDomElement>
2525
#include <QDomDocument>
26+
#include <QStringList>
2627

2728
class QgsComposerMap;
2829
class QgsComposition;
@@ -305,6 +306,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
305306
public:
306307
typedef QMap< QgsFeatureId, QVariant > SorterKeys;
307308

309+
private slots:
310+
void removeLayers( QStringList layers );
311+
308312
private:
309313
// value of field that is used for ordering of features
310314
SorterKeys mFeatureKeys;

tests/src/core/testqgsatlascomposition.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ class TestQgsAtlasComposition: public QObject
6363
void filtering_render();
6464
// test render signals
6565
void test_signals();
66+
// test removing coverage layer while atlas is enabled
67+
void test_remove_layer();
6668

6769
private:
6870
QgsComposition* mComposition;
@@ -73,6 +75,7 @@ class TestQgsAtlasComposition: public QObject
7375
//QgsMapRenderer* mMapRenderer;
7476
QgsMapSettings mMapSettings;
7577
QgsVectorLayer* mVectorLayer;
78+
QgsVectorLayer* mVectorLayer2;
7679
QgsAtlasComposition* mAtlas;
7780
QString mReport;
7881
};
@@ -87,6 +90,9 @@ void TestQgsAtlasComposition::initTestCase()
8790
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(),
8891
vectorFileInfo.completeBaseName(),
8992
"ogr" );
93+
mVectorLayer2 = new QgsVectorLayer( vectorFileInfo.filePath(),
94+
vectorFileInfo.completeBaseName(),
95+
"ogr" );
9096

9197
QgsVectorSimplifyMethod simplifyMethod;
9298
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
@@ -441,5 +447,24 @@ void TestQgsAtlasComposition::test_signals()
441447
QVERIFY( spyRenderEnded.count() == 1 );
442448
}
443449

450+
void TestQgsAtlasComposition::test_remove_layer()
451+
{
452+
mAtlas->setCoverageLayer( mVectorLayer2 );
453+
mAtlas->setEnabled( true );
454+
455+
QSignalSpy spyToggled( mAtlas, SIGNAL( toggled( bool ) ) );
456+
457+
//remove coverage layer while atlas is enabled
458+
QgsMapLayerRegistry::instance()->removeMapLayer( mVectorLayer2->id() );
459+
mVectorLayer2 = 0;
460+
461+
QVERIFY( !mAtlas->enabled() );
462+
QVERIFY( spyToggled.count() == 1 );
463+
464+
//clean up
465+
mAtlas->setCoverageLayer( mVectorLayer );
466+
mAtlas->setEnabled( true );
467+
}
468+
444469
QTEST_MAIN( TestQgsAtlasComposition )
445470
#include "moc_testqgsatlascomposition.cxx"

0 commit comments

Comments
 (0)