Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[composer] Disable atlas if coverage layer is removed from project
  • Loading branch information
nyalldawson committed Sep 20, 2014
1 parent 46c7599 commit 808464f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -47,6 +47,9 @@ QgsAtlasComposition::QgsAtlasComposition( QgsComposition* composition ) :
QgsExpression::setSpecialColumn( "$atlasfeatureid", QVariant(( int )0 ) );
QgsExpression::setSpecialColumn( "$atlasfeature", QVariant::fromValue( QgsFeature() ) );
QgsExpression::setSpecialColumn( "$atlasgeometry", QVariant::fromValue( QgsGeometry() ) );

//listen out for layer removal
connect( QgsMapLayerRegistry::instance(), SIGNAL( layersWillBeRemoved( QStringList ) ), this, SLOT( removeLayers( QStringList ) ) );
}

QgsAtlasComposition::~QgsAtlasComposition()
Expand All @@ -55,13 +58,43 @@ QgsAtlasComposition::~QgsAtlasComposition()

void QgsAtlasComposition::setEnabled( bool enabled )
{
if ( enabled == mEnabled )
{
return;
}

mEnabled = enabled;
mComposition->setAtlasMode( QgsComposition::AtlasOff );
emit toggled( enabled );
emit parameterChanged();
}

void QgsAtlasComposition::removeLayers( QStringList layers )
{
if ( !mCoverageLayer )
{
return;
}

foreach ( QString layerId, layers )
{
if ( layerId == mCoverageLayer->id() )
{
//current coverage layer removed
mCoverageLayer = 0;
setEnabled( false );
return;
}
}
}

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

mCoverageLayer = layer;

// update the number of features
Expand Down
4 changes: 4 additions & 0 deletions src/core/composer/qgsatlascomposition.h
Expand Up @@ -23,6 +23,7 @@
#include <QString>
#include <QDomElement>
#include <QDomDocument>
#include <QStringList>

class QgsComposerMap;
class QgsComposition;
Expand Down Expand Up @@ -305,6 +306,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
public:
typedef QMap< QgsFeatureId, QVariant > SorterKeys;

private slots:
void removeLayers( QStringList layers );

private:
// value of field that is used for ordering of features
SorterKeys mFeatureKeys;
Expand Down
25 changes: 25 additions & 0 deletions tests/src/core/testqgsatlascomposition.cpp
Expand Up @@ -63,6 +63,8 @@ class TestQgsAtlasComposition: public QObject
void filtering_render();
// test render signals
void test_signals();
// test removing coverage layer while atlas is enabled
void test_remove_layer();

private:
QgsComposition* mComposition;
Expand All @@ -73,6 +75,7 @@ class TestQgsAtlasComposition: public QObject
//QgsMapRenderer* mMapRenderer;
QgsMapSettings mMapSettings;
QgsVectorLayer* mVectorLayer;
QgsVectorLayer* mVectorLayer2;
QgsAtlasComposition* mAtlas;
QString mReport;
};
Expand All @@ -87,6 +90,9 @@ void TestQgsAtlasComposition::initTestCase()
mVectorLayer = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );
mVectorLayer2 = new QgsVectorLayer( vectorFileInfo.filePath(),
vectorFileInfo.completeBaseName(),
"ogr" );

QgsVectorSimplifyMethod simplifyMethod;
simplifyMethod.setSimplifyHints( QgsVectorSimplifyMethod::NoSimplification );
Expand Down Expand Up @@ -441,5 +447,24 @@ void TestQgsAtlasComposition::test_signals()
QVERIFY( spyRenderEnded.count() == 1 );
}

void TestQgsAtlasComposition::test_remove_layer()
{
mAtlas->setCoverageLayer( mVectorLayer2 );
mAtlas->setEnabled( true );

QSignalSpy spyToggled( mAtlas, SIGNAL( toggled( bool ) ) );

//remove coverage layer while atlas is enabled
QgsMapLayerRegistry::instance()->removeMapLayer( mVectorLayer2->id() );
mVectorLayer2 = 0;

QVERIFY( !mAtlas->enabled() );
QVERIFY( spyToggled.count() == 1 );

//clean up
mAtlas->setCoverageLayer( mVectorLayer );
mAtlas->setEnabled( true );
}

QTEST_MAIN( TestQgsAtlasComposition )
#include "moc_testqgsatlascomposition.cxx"

0 comments on commit 808464f

Please sign in to comment.