18#include <QImageWriter>
31 , mAtlas( layout->atlas() )
34 connect( mUseAtlasCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutAtlasWidget::mUseAtlasCheckBox_stateChanged );
35 connect( mAtlasFilenamePatternEdit, &QLineEdit::editingFinished,
this, &QgsLayoutAtlasWidget::mAtlasFilenamePatternEdit_editingFinished );
36 connect( mAtlasFilenameExpressionButton, &QToolButton::clicked,
this, &QgsLayoutAtlasWidget::mAtlasFilenameExpressionButton_clicked );
37 connect( mAtlasHideCoverageCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutAtlasWidget::mAtlasHideCoverageCheckBox_stateChanged );
38 connect( mAtlasSingleFileCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutAtlasWidget::mAtlasSingleFileCheckBox_stateChanged );
39 connect( mAtlasSortFeatureCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutAtlasWidget::mAtlasSortFeatureCheckBox_stateChanged );
40 connect( mAtlasSortFeatureDirectionButton, &QToolButton::clicked,
this, &QgsLayoutAtlasWidget::mAtlasSortFeatureDirectionButton_clicked );
41 connect( mAtlasFeatureFilterEdit, &QLineEdit::editingFinished,
this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterEdit_editingFinished );
42 connect( mAtlasFeatureFilterButton, &QToolButton::clicked,
this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterButton_clicked );
43 connect( mAtlasFeatureFilterCheckBox, &QCheckBox::stateChanged,
this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterCheckBox_stateChanged );
54 mAtlasSortFeatureDirectionButton->setEnabled(
false );
55 mAtlasSortExpressionWidget->setEnabled(
false );
60 mPageNameWidget->registerExpressionContextGenerator( mLayout );
62 const QList<QByteArray> formats = QImageWriter::supportedImageFormats();
63 for (
int i = 0; i < formats.size(); ++i )
65 mAtlasFileFormat->addItem( QString( formats.at( i ) ) );
67 connect( mAtlasFileFormat, qOverload<int>( &QComboBox::currentIndexChanged ),
this, [ = ](
int ) { changeFileFormat(); } );
77void QgsLayoutAtlasWidget::mUseAtlasCheckBox_stateChanged(
int state )
79 if ( state == Qt::Checked )
82 mConfigurationGroup->setEnabled(
true );
83 mOutputGroup->setEnabled(
true );
88 mConfigurationGroup->setEnabled(
false );
89 mOutputGroup->setEnabled(
false );
93void QgsLayoutAtlasWidget::changeCoverageLayer(
QgsMapLayer *layer )
101 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Layer" ) );
102 mLayout->reportContext().setLayer( vl );
110 updateAtlasFeatures();
116 if ( exp.prepare( &context ) && !exp.hasParserError() )
125 mLayout->undoStack()->endCommand();
128void QgsLayoutAtlasWidget::mAtlasFilenamePatternEdit_editingFinished()
134 mBlockUpdates =
true;
135 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filename" ) );
140 tr(
"Could not set filename expression to '%1'.\nParser error:\n%2" )
141 .arg( mAtlasFilenamePatternEdit->text(),
144 mLayout->undoStack()->endCommand();
145 mBlockUpdates =
false;
148void QgsLayoutAtlasWidget::mAtlasFilenameExpressionButton_clicked()
157 exprDlg.setWindowTitle( tr(
"Expression Based Filename" ) );
159 if ( exprDlg.exec() == QDialog::Accepted )
161 const QString expression = exprDlg.expressionText();
162 if ( !expression.isEmpty() )
165 mAtlasFilenamePatternEdit->setText( expression );
167 mBlockUpdates =
true;
168 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filename" ) );
172 mMessageBar->
pushWarning( tr(
"Atlas" ), tr(
"Could not set filename expression to '%1'.\nParser error:\n%2" )
176 mBlockUpdates =
false;
177 mLayout->undoStack()->endCommand();
182void QgsLayoutAtlasWidget::mAtlasHideCoverageCheckBox_stateChanged(
int state )
187 mBlockUpdates =
true;
188 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Toggle Atlas Layer" ) );
190 mLayout->undoStack()->endCommand();
191 mBlockUpdates =
false;
194void QgsLayoutAtlasWidget::mAtlasSingleFileCheckBox_stateChanged(
int state )
199 if ( state == Qt::Checked )
201 mAtlasFilenamePatternEdit->setEnabled(
false );
202 mAtlasFilenameExpressionButton->setEnabled(
false );
206 mAtlasFilenamePatternEdit->setEnabled(
true );
207 mAtlasFilenameExpressionButton->setEnabled(
true );
210 mLayout->setCustomProperty( QStringLiteral(
"singleFile" ), state == Qt::Checked );
213void QgsLayoutAtlasWidget::mAtlasSortFeatureCheckBox_stateChanged(
int state )
218 if ( state == Qt::Checked )
220 mAtlasSortFeatureDirectionButton->setEnabled(
true );
221 mAtlasSortExpressionWidget->setEnabled(
true );
225 mAtlasSortFeatureDirectionButton->setEnabled(
false );
226 mAtlasSortExpressionWidget->setEnabled(
false );
228 mBlockUpdates =
true;
229 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Toggle Atlas Sorting" ) );
231 mLayout->undoStack()->endCommand();
232 mBlockUpdates =
false;
233 updateAtlasFeatures();
236void QgsLayoutAtlasWidget::changesSortFeatureExpression(
const QString &expression,
bool )
241 mBlockUpdates =
true;
242 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Sort" ) );
243 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mAtlasCoverageLayerComboBox->currentLayer() );
245 mLayout->undoStack()->endCommand();
246 mBlockUpdates =
false;
247 updateAtlasFeatures();
250void QgsLayoutAtlasWidget::updateAtlasFeatures()
255 mMessageBar->
pushInfo( tr(
"Atlas" ),
256 tr(
"No matching atlas features found!" ) );
263void QgsLayoutAtlasWidget::mAtlasFeatureFilterCheckBox_stateChanged(
int state )
268 if ( state == Qt::Checked )
270 mAtlasFeatureFilterEdit->setEnabled(
true );
271 mAtlasFeatureFilterButton->setEnabled(
true );
275 mAtlasFeatureFilterEdit->setEnabled(
false );
276 mAtlasFeatureFilterButton->setEnabled(
false );
278 mBlockUpdates =
true;
279 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
281 mLayout->undoStack()->endCommand();
282 mBlockUpdates =
false;
283 updateAtlasFeatures();
286void QgsLayoutAtlasWidget::pageNameExpressionChanged(
const QString &,
bool valid )
291 const QString expression = mPageNameWidget->asExpression();
292 if ( !valid && !expression.isEmpty() )
297 mBlockUpdates =
true;
298 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Name" ) );
300 mLayout->undoStack()->endCommand();
301 mBlockUpdates =
false;
304void QgsLayoutAtlasWidget::mAtlasFeatureFilterEdit_editingFinished()
310 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
312 mBlockUpdates =
true;
316 mMessageBar->
pushWarning( tr(
"Atlas" ), tr(
"Could not set filter expression to '%1'.\nParser error:\n%2" )
317 .arg( mAtlasFeatureFilterEdit->text(),
320 mBlockUpdates =
false;
321 mLayout->undoStack()->endCommand();
322 updateAtlasFeatures();
325void QgsLayoutAtlasWidget::mAtlasFeatureFilterButton_clicked()
330 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mAtlasCoverageLayerComboBox->currentLayer() );
339 exprDlg.setWindowTitle( tr(
"Expression Based Filter" ) );
341 if ( exprDlg.exec() == QDialog::Accepted )
343 const QString expression = exprDlg.expressionText();
344 if ( !expression.isEmpty() )
346 mAtlasFeatureFilterEdit->setText( expression );
348 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
349 mBlockUpdates =
true;
354 tr(
"Could not set filter expression to '%1'.\nParser error:\n%2" )
355 .arg( mAtlasFeatureFilterEdit->text(),
359 mBlockUpdates =
false;
360 mLayout->undoStack()->endCommand();
361 updateAtlasFeatures();
366void QgsLayoutAtlasWidget::mAtlasSortFeatureDirectionButton_clicked()
371 Qt::ArrowType at = mAtlasSortFeatureDirectionButton->arrowType();
372 at = ( at == Qt::UpArrow ) ? Qt::DownArrow : Qt::UpArrow;
373 mAtlasSortFeatureDirectionButton->setArrowType( at );
375 mBlockUpdates =
true;
376 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Sort" ) );
378 mLayout->undoStack()->endCommand();
379 mBlockUpdates =
false;
380 updateAtlasFeatures();
383void QgsLayoutAtlasWidget::changeFileFormat()
388 mLayout->setCustomProperty( QStringLiteral(
"atlasRasterFormat" ), mAtlasFileFormat->currentText() );
391void QgsLayoutAtlasWidget::updateGuiElements()
396 blockAllSignals(
true );
397 mUseAtlasCheckBox->setCheckState( mAtlas->
enabled() ? Qt::Checked : Qt::Unchecked );
398 mConfigurationGroup->setEnabled( mAtlas->
enabled() );
399 mOutputGroup->setEnabled( mAtlas->
enabled() );
401 mAtlasCoverageLayerComboBox->setLayer( mAtlas->
coverageLayer() );
405 mAtlasSortExpressionWidget->setLayer( mAtlas->
coverageLayer() );
409 mAtlasHideCoverageCheckBox->setCheckState( mAtlas->
hideCoverage() ? Qt::Checked : Qt::Unchecked );
411 const bool singleFile = mLayout->customProperty( QStringLiteral(
"singleFile" ) ).toBool();
412 mAtlasSingleFileCheckBox->setCheckState( singleFile ? Qt::Checked : Qt::Unchecked );
413 mAtlasFilenamePatternEdit->setEnabled( !singleFile );
414 mAtlasFilenameExpressionButton->setEnabled( !singleFile );
416 mAtlasSortFeatureCheckBox->setCheckState( mAtlas->
sortFeatures() ? Qt::Checked : Qt::Unchecked );
417 mAtlasSortFeatureDirectionButton->setEnabled( mAtlas->
sortFeatures() );
418 mAtlasSortExpressionWidget->setEnabled( mAtlas->
sortFeatures() );
420 mAtlasSortFeatureDirectionButton->setArrowType( mAtlas->
sortAscending() ? Qt::UpArrow : Qt::DownArrow );
423 mAtlasFeatureFilterCheckBox->setCheckState( mAtlas->
filterFeatures() ? Qt::Checked : Qt::Unchecked );
425 mAtlasFeatureFilterButton->setEnabled( mAtlas->
filterFeatures() );
427 mAtlasFileFormat->setCurrentIndex( mAtlasFileFormat->findText( mLayout->customProperty( QStringLiteral(
"atlasRasterFormat" ), QStringLiteral(
"png" ) ).toString() ) );
429 blockAllSignals(
false );
432void QgsLayoutAtlasWidget::blockAllSignals(
bool b )
434 mUseAtlasCheckBox->blockSignals( b );
435 mConfigurationGroup->blockSignals( b );
436 mOutputGroup->blockSignals( b );
437 mAtlasCoverageLayerComboBox->blockSignals( b );
438 mPageNameWidget->blockSignals( b );
439 mAtlasSortExpressionWidget->blockSignals( b );
440 mAtlasFilenamePatternEdit->blockSignals( b );
441 mAtlasHideCoverageCheckBox->blockSignals( b );
442 mAtlasSingleFileCheckBox->blockSignals( b );
443 mAtlasSortFeatureCheckBox->blockSignals( b );
444 mAtlasSortFeatureDirectionButton->blockSignals( b );
445 mAtlasFeatureFilterEdit->blockSignals( b );
446 mAtlasFeatureFilterCheckBox->blockSignals( b );
A generic dialog for building expression strings.
static QList< QgsExpressionContextScope * > globalProjectLayerScopes(const QgsMapLayer *layer)
Creates a list of three scopes: global, layer's project and layer.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Class for parsing and evaluation of expressions (formerly called "search strings").
static QString quoteFieldExpression(const QString &expression, const QgsVectorLayer *layer)
Validate if the expression is a field in the layer and ensure it is quoted.
QString sortExpression() const
Returns the expression (or field name) to use for sorting features.
bool filterFeatures() const
Returns true if features should be filtered in the coverage layer.
QString filenameExpression() const
Returns the filename expression used for generating output filenames for each atlas page.
bool sortAscending() const
Returns true if features should be sorted in an ascending order.
void setCoverageLayer(QgsVectorLayer *layer)
Sets the coverage layer to use for the atlas features.
bool setFilterExpression(const QString &expression, QString &errorString)
Sets the expression used for filtering features in the coverage layer.
void setSortAscending(bool ascending)
Sets whether features should be sorted in an ascending order.
bool hideCoverage() const
Returns true if the atlas is set to hide the coverage layer.
void setEnabled(bool enabled)
Sets whether the atlas is enabled.
void setPageNameExpression(const QString &expression)
Sets the expression (or field name) used for calculating the page name.
QString filterExpression() const
Returns the expression used for filtering features in the coverage layer.
bool enabled() const
Returns whether the atlas generation is enabled.
bool setFilenameExpression(const QString &expression, QString &errorString)
Sets the filename expression used for generating output filenames for each atlas page.
void setSortFeatures(bool enabled)
Sets whether features should be sorted in the atlas.
QString pageNameExpression() const
Returns the expression (or field name) used for calculating the page name.
void setSortExpression(const QString &expression)
Sets the expression (or field name) to use for sorting features.
void setFilterFeatures(bool filtered)
Sets whether features should be filtered in the coverage layer.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas features.
void changed()
Emitted when one of the atlas parameters changes.
int updateFeatures()
Requeries the current atlas coverage layer and applies filtering and sorting.
bool sortFeatures() const
Returns true if features should be sorted in the atlas.
void setHideCoverage(bool hide)
Sets whether the coverage layer should be hidden in map items in the layouts.
void layerChanged(QgsMapLayer *layer)
Emitted whenever the currently selected layer changes.
Base class for all map layer types.
A bar for displaying non-blocking messages to the user.
void pushInfo(const QString &title, const QString &message)
Pushes a information message with default timeout to the message bar.
void pushWarning(const QString &title, const QString &message)
Pushes a warning message that must be manually dismissed by the user.
Print layout, a QgsLayout subclass for static or atlas-based layouts.
Represents a vector layer which manages a vector based data sets.
QString displayExpression