Skip to content
Permalink
Browse files

[composer] Disable atlas if coverage layer is removed from project

  • Loading branch information
nyalldawson committed Sep 20, 2014
1 parent 46c7599 commit 808464fed6bdaf0639f17ba3e0c88c217c2ea420
@@ -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()
@@ -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
@@ -23,6 +23,7 @@
#include <QString>
#include <QDomElement>
#include <QDomDocument>
#include <QStringList>

class QgsComposerMap;
class QgsComposition;
@@ -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;
@@ -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;
@@ -73,6 +75,7 @@ class TestQgsAtlasComposition: public QObject
//QgsMapRenderer* mMapRenderer;
QgsMapSettings mMapSettings;
QgsVectorLayer* mVectorLayer;
QgsVectorLayer* mVectorLayer2;
QgsAtlasComposition* mAtlas;
QString mReport;
};
@@ -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 );
@@ -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.
You can’t perform that action at this time.