Skip to content

Commit

Permalink
When an atlas layer is set, auto set the page name field to default
Browse files Browse the repository at this point in the history
to the layer's display expression

Gives a nicer atlas experience by default
  • Loading branch information
nyalldawson committed Nov 29, 2021
1 parent 1689bb7 commit 262bf11
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/gui/layout/qgslayoutatlaswidget.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qgslayoutatlas.h"
#include "qgsexpressionbuilderdialog.h"
#include "qgslayoutundostack.h"
#include "qgsexpressioncontextutils.h"
#include "qgsmessagebar.h"

QgsLayoutAtlasWidget::QgsLayoutAtlasWidget( QWidget *parent, QgsPrintLayout *layout )
Expand Down Expand Up @@ -96,6 +97,7 @@ void QgsLayoutAtlasWidget::changeCoverageLayer( QgsMapLayer *layer )

QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );

const QString prevPageNameExpression = mAtlas->pageNameExpression();
mLayout->undoStack()->beginCommand( mAtlas, tr( "Change Atlas Layer" ) );
mLayout->reportContext().setLayer( vl );
if ( !vl )
Expand All @@ -107,6 +109,19 @@ void QgsLayoutAtlasWidget::changeCoverageLayer( QgsMapLayer *layer )
mAtlas->setCoverageLayer( vl );
updateAtlasFeatures();
}

// if page name expression is still valid, retain it. Otherwise switch to a nice default.
QgsExpression exp( prevPageNameExpression );
QgsExpressionContext context( QgsExpressionContextUtils::globalProjectLayerScopes( vl ) );
if ( exp.prepare( &context ) && !exp.hasParserError() )
{
mAtlas->setPageNameExpression( prevPageNameExpression );
}
else if ( vl )
{
mAtlas->setPageNameExpression( vl->displayExpression() );
}

mLayout->undoStack()->endCommand();
}

Expand Down

0 comments on commit 262bf11

Please sign in to comment.