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" ) );
243 QgsVectorLayer *vlayer = qobject_cast<QgsVectorLayer *>( mAtlasCoverageLayerComboBox->currentLayer() );
245 mLayout->undoStack()->endCommand();
246 mBlockUpdates =
false;
247 updateAtlasFeatures();
250 void QgsLayoutAtlasWidget::updateAtlasFeatures()
255 mMessageBar->
pushInfo( tr(
"Atlas" ),
256 tr(
"No matching atlas features found!" ) );
263 void 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();
286 void 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;
304 void 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();
325 void 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();
366 void 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();
383 void QgsLayoutAtlasWidget::changeFileFormat()
388 mLayout->setCustomProperty( QStringLiteral(
"atlasRasterFormat" ), mAtlasFileFormat->currentText() );
391 void 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 );
432 void 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 );