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(); } );
77 void 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 );
93 void 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();
128 void 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;
148 void 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();
182 void QgsLayoutAtlasWidget::mAtlasHideCoverageCheckBox_stateChanged(
int state )
187 mBlockUpdates =
true;
188 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Toggle Atlas Layer" ) );
190 mLayout->undoStack()->endCommand();
191 mBlockUpdates =
false;
194 void 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 );
213 void 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();
236 void QgsLayoutAtlasWidget::changesSortFeatureExpression(
const QString &expression,
bool )
241 mBlockUpdates =
true;
242 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Sort" ) );
244 mLayout->undoStack()->endCommand();
245 mBlockUpdates =
false;
246 updateAtlasFeatures();
249 void QgsLayoutAtlasWidget::updateAtlasFeatures()
254 mMessageBar->
pushInfo( tr(
"Atlas" ),
255 tr(
"No matching atlas features found!" ) );
262 void QgsLayoutAtlasWidget::mAtlasFeatureFilterCheckBox_stateChanged(
int state )
267 if ( state == Qt::Checked )
269 mAtlasFeatureFilterEdit->setEnabled(
true );
270 mAtlasFeatureFilterButton->setEnabled(
true );
274 mAtlasFeatureFilterEdit->setEnabled(
false );
275 mAtlasFeatureFilterButton->setEnabled(
false );
277 mBlockUpdates =
true;
278 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
280 mLayout->undoStack()->endCommand();
281 mBlockUpdates =
false;
282 updateAtlasFeatures();
285 void QgsLayoutAtlasWidget::pageNameExpressionChanged(
const QString &,
bool valid )
290 const QString expression = mPageNameWidget->asExpression();
291 if ( !valid && !expression.isEmpty() )
296 mBlockUpdates =
true;
297 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Name" ) );
299 mLayout->undoStack()->endCommand();
300 mBlockUpdates =
false;
303 void QgsLayoutAtlasWidget::mAtlasFeatureFilterEdit_editingFinished()
309 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
311 mBlockUpdates =
true;
315 mMessageBar->
pushWarning( tr(
"Atlas" ), tr(
"Could not set filter expression to '%1'.\nParser error:\n%2" )
316 .arg( mAtlasFeatureFilterEdit->text(),
319 mBlockUpdates =
false;
320 mLayout->undoStack()->endCommand();
321 updateAtlasFeatures();
324 void QgsLayoutAtlasWidget::mAtlasFeatureFilterButton_clicked()
329 QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( mAtlasCoverageLayerComboBox->currentLayer() );
338 exprDlg.setWindowTitle( tr(
"Expression Based Filter" ) );
340 if ( exprDlg.exec() == QDialog::Accepted )
342 const QString expression = exprDlg.expressionText();
343 if ( !expression.isEmpty() )
345 mAtlasFeatureFilterEdit->setText( expression );
347 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Filter" ) );
348 mBlockUpdates =
true;
353 tr(
"Could not set filter expression to '%1'.\nParser error:\n%2" )
354 .arg( mAtlasFeatureFilterEdit->text(),
358 mBlockUpdates =
false;
359 mLayout->undoStack()->endCommand();
360 updateAtlasFeatures();
365 void QgsLayoutAtlasWidget::mAtlasSortFeatureDirectionButton_clicked()
370 Qt::ArrowType at = mAtlasSortFeatureDirectionButton->arrowType();
371 at = ( at == Qt::UpArrow ) ? Qt::DownArrow : Qt::UpArrow;
372 mAtlasSortFeatureDirectionButton->setArrowType( at );
374 mBlockUpdates =
true;
375 mLayout->undoStack()->beginCommand( mAtlas, tr(
"Change Atlas Sort" ) );
377 mLayout->undoStack()->endCommand();
378 mBlockUpdates =
false;
379 updateAtlasFeatures();
382 void QgsLayoutAtlasWidget::changeFileFormat()
387 mLayout->setCustomProperty( QStringLiteral(
"atlasRasterFormat" ), mAtlasFileFormat->currentText() );
390 void QgsLayoutAtlasWidget::updateGuiElements()
395 blockAllSignals(
true );
396 mUseAtlasCheckBox->setCheckState( mAtlas->
enabled() ? Qt::Checked : Qt::Unchecked );
397 mConfigurationGroup->setEnabled( mAtlas->
enabled() );
398 mOutputGroup->setEnabled( mAtlas->
enabled() );
400 mAtlasCoverageLayerComboBox->setLayer( mAtlas->
coverageLayer() );
404 mAtlasSortExpressionWidget->setLayer( mAtlas->
coverageLayer() );
408 mAtlasHideCoverageCheckBox->setCheckState( mAtlas->
hideCoverage() ? Qt::Checked : Qt::Unchecked );
410 const bool singleFile = mLayout->customProperty( QStringLiteral(
"singleFile" ) ).toBool();
411 mAtlasSingleFileCheckBox->setCheckState( singleFile ? Qt::Checked : Qt::Unchecked );
412 mAtlasFilenamePatternEdit->setEnabled( !singleFile );
413 mAtlasFilenameExpressionButton->setEnabled( !singleFile );
415 mAtlasSortFeatureCheckBox->setCheckState( mAtlas->
sortFeatures() ? Qt::Checked : Qt::Unchecked );
416 mAtlasSortFeatureDirectionButton->setEnabled( mAtlas->
sortFeatures() );
417 mAtlasSortExpressionWidget->setEnabled( mAtlas->
sortFeatures() );
419 mAtlasSortFeatureDirectionButton->setArrowType( mAtlas->
sortAscending() ? Qt::UpArrow : Qt::DownArrow );
422 mAtlasFeatureFilterCheckBox->setCheckState( mAtlas->
filterFeatures() ? Qt::Checked : Qt::Unchecked );
424 mAtlasFeatureFilterButton->setEnabled( mAtlas->
filterFeatures() );
426 mAtlasFileFormat->setCurrentIndex( mAtlasFileFormat->findText( mLayout->customProperty( QStringLiteral(
"atlasRasterFormat" ), QStringLiteral(
"png" ) ).toString() ) );
428 blockAllSignals(
false );
431 void QgsLayoutAtlasWidget::blockAllSignals(
bool b )
433 mUseAtlasCheckBox->blockSignals( b );
434 mConfigurationGroup->blockSignals( b );
435 mOutputGroup->blockSignals( b );
436 mAtlasCoverageLayerComboBox->blockSignals( b );
437 mPageNameWidget->blockSignals( b );
438 mAtlasSortExpressionWidget->blockSignals( b );
439 mAtlasFilenamePatternEdit->blockSignals( b );
440 mAtlasHideCoverageCheckBox->blockSignals( b );
441 mAtlasSingleFileCheckBox->blockSignals( b );
442 mAtlasSortFeatureCheckBox->blockSignals( b );
443 mAtlasSortFeatureDirectionButton->blockSignals( b );
444 mAtlasFeatureFilterEdit->blockSignals( b );
445 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").
QString sortExpression() const
Returns the expression (or field name) to use for sorting features.
QgsVectorLayer * coverageLayer() const
Returns the coverage layer used for the atlas 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.
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