|
| 1 | +/*************************************************************************** |
| 2 | + qgslayoutatlaswidget.cpp |
| 3 | + ----------------------------- |
| 4 | + begin : October 2012 |
| 5 | + copyright : (C) 2012 Hugo Mercier |
| 6 | + email : hugo dot mercier at oslandia dot com |
| 7 | + ***************************************************************************/ |
| 8 | +/*************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +#include <QComboBox> |
| 18 | +#include <QImageWriter> |
| 19 | +#include <QMessageBox> |
| 20 | + |
| 21 | +#include "qgslayoutatlaswidget.h" |
| 22 | +#include "qgsprintlayout.h" |
| 23 | +#include "qgslayoutatlas.h" |
| 24 | +#include "qgsexpressionbuilderdialog.h" |
| 25 | + |
| 26 | +QgsLayoutAtlasWidget::QgsLayoutAtlasWidget( QWidget *parent, QgsPrintLayout *layout ) |
| 27 | + : QWidget( parent ) |
| 28 | + , mAtlas( layout->atlas() ) |
| 29 | +{ |
| 30 | + setupUi( this ); |
| 31 | + connect( mUseAtlasCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutAtlasWidget::mUseAtlasCheckBox_stateChanged ); |
| 32 | + connect( mAtlasFilenamePatternEdit, &QLineEdit::editingFinished, this, &QgsLayoutAtlasWidget::mAtlasFilenamePatternEdit_editingFinished ); |
| 33 | + connect( mAtlasFilenameExpressionButton, &QToolButton::clicked, this, &QgsLayoutAtlasWidget::mAtlasFilenameExpressionButton_clicked ); |
| 34 | + connect( mAtlasHideCoverageCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutAtlasWidget::mAtlasHideCoverageCheckBox_stateChanged ); |
| 35 | + connect( mAtlasSingleFileCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutAtlasWidget::mAtlasSingleFileCheckBox_stateChanged ); |
| 36 | + connect( mAtlasSortFeatureCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutAtlasWidget::mAtlasSortFeatureCheckBox_stateChanged ); |
| 37 | + connect( mAtlasSortFeatureDirectionButton, &QToolButton::clicked, this, &QgsLayoutAtlasWidget::mAtlasSortFeatureDirectionButton_clicked ); |
| 38 | + connect( mAtlasFeatureFilterEdit, &QLineEdit::editingFinished, this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterEdit_editingFinished ); |
| 39 | + connect( mAtlasFeatureFilterButton, &QToolButton::clicked, this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterButton_clicked ); |
| 40 | + connect( mAtlasFeatureFilterCheckBox, &QCheckBox::stateChanged, this, &QgsLayoutAtlasWidget::mAtlasFeatureFilterCheckBox_stateChanged ); |
| 41 | + |
| 42 | + mAtlasCoverageLayerComboBox->setFilters( QgsMapLayerProxyModel::VectorLayer ); |
| 43 | + |
| 44 | + connect( mAtlasCoverageLayerComboBox, &QgsMapLayerComboBox::layerChanged, mAtlasSortFeatureKeyComboBox, &QgsFieldComboBox::setLayer ); |
| 45 | + connect( mAtlasCoverageLayerComboBox, &QgsMapLayerComboBox::layerChanged, mPageNameWidget, &QgsFieldExpressionWidget::setLayer ); |
| 46 | + connect( mAtlasCoverageLayerComboBox, &QgsMapLayerComboBox::layerChanged, this, &QgsLayoutAtlasWidget::changeCoverageLayer ); |
| 47 | + connect( mAtlasSortFeatureKeyComboBox, &QgsFieldComboBox::fieldChanged, this, &QgsLayoutAtlasWidget::changesSortFeatureField ); |
| 48 | + connect( mPageNameWidget, static_cast < void ( QgsFieldExpressionWidget::* )( const QString &, bool ) > ( &QgsFieldExpressionWidget::fieldChanged ), this, &QgsLayoutAtlasWidget::pageNameExpressionChanged ); |
| 49 | + |
| 50 | + // Sort direction |
| 51 | + mAtlasSortFeatureDirectionButton->setEnabled( false ); |
| 52 | + mAtlasSortFeatureKeyComboBox->setEnabled( false ); |
| 53 | + |
| 54 | + // connect to updates |
| 55 | + connect( mAtlas, &QgsLayoutAtlas::changed, this, &QgsLayoutAtlasWidget::updateGuiElements ); |
| 56 | + |
| 57 | + mPageNameWidget->registerExpressionContextGenerator( mLayout ); |
| 58 | + |
| 59 | + QList<QByteArray> formats = QImageWriter::supportedImageFormats(); |
| 60 | + for ( int i = 0; i < formats.size(); ++i ) |
| 61 | + { |
| 62 | + mAtlasFileFormat->addItem( QString( formats.at( i ) ) ); |
| 63 | + } |
| 64 | + connect( mAtlasFileFormat, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int ) { changeFileFormat(); } ); |
| 65 | + |
| 66 | + updateGuiElements(); |
| 67 | +} |
| 68 | + |
| 69 | +void QgsLayoutAtlasWidget::mUseAtlasCheckBox_stateChanged( int state ) |
| 70 | +{ |
| 71 | + if ( state == Qt::Checked ) |
| 72 | + { |
| 73 | + mAtlas->setEnabled( true ); |
| 74 | + mConfigurationGroup->setEnabled( true ); |
| 75 | + mOutputGroup->setEnabled( true ); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + mAtlas->setEnabled( false ); |
| 80 | + mConfigurationGroup->setEnabled( false ); |
| 81 | + mOutputGroup->setEnabled( false ); |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +void QgsLayoutAtlasWidget::changeCoverageLayer( QgsMapLayer *layer ) |
| 86 | +{ |
| 87 | + QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( layer ); |
| 88 | + |
| 89 | + if ( !vl ) |
| 90 | + { |
| 91 | + mAtlas->setCoverageLayer( nullptr ); |
| 92 | + } |
| 93 | + else |
| 94 | + { |
| 95 | + mAtlas->setCoverageLayer( vl ); |
| 96 | + updateAtlasFeatures(); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +void QgsLayoutAtlasWidget::mAtlasFilenamePatternEdit_editingFinished() |
| 101 | +{ |
| 102 | + QString error; |
| 103 | + if ( !mAtlas->setFilenameExpression( mAtlasFilenamePatternEdit->text(), error ) ) |
| 104 | + { |
| 105 | + //expression could not be set |
| 106 | + QMessageBox::warning( this |
| 107 | + , tr( "Could not evaluate filename pattern" ) |
| 108 | + , tr( "Could not set filename pattern as '%1'.\nParser error:\n%2" ) |
| 109 | + .arg( mAtlasFilenamePatternEdit->text(), |
| 110 | + error ) |
| 111 | + ); |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +void QgsLayoutAtlasWidget::mAtlasFilenameExpressionButton_clicked() |
| 116 | +{ |
| 117 | + if ( !mAtlas || !mAtlas->coverageLayer() ) |
| 118 | + { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + QgsExpressionContext context = mLayout->createExpressionContext(); |
| 123 | + QgsExpressionBuilderDialog exprDlg( mAtlas->coverageLayer(), mAtlasFilenamePatternEdit->text(), this, QStringLiteral( "generic" ), context ); |
| 124 | + exprDlg.setWindowTitle( tr( "Expression Based Filename" ) ); |
| 125 | + |
| 126 | + if ( exprDlg.exec() == QDialog::Accepted ) |
| 127 | + { |
| 128 | + QString expression = exprDlg.expressionText(); |
| 129 | + if ( !expression.isEmpty() ) |
| 130 | + { |
| 131 | + //set atlas filename expression |
| 132 | + mAtlasFilenamePatternEdit->setText( expression ); |
| 133 | + QString error; |
| 134 | + if ( !mAtlas->setFilenameExpression( expression, error ) ) |
| 135 | + { |
| 136 | + //expression could not be set |
| 137 | + QMessageBox::warning( this |
| 138 | + , tr( "Could not evaluate filename pattern" ) |
| 139 | + , tr( "Could not set filename pattern as '%1'.\nParser error:\n%2" ) |
| 140 | + .arg( expression, |
| 141 | + error ) |
| 142 | + ); |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +void QgsLayoutAtlasWidget::mAtlasHideCoverageCheckBox_stateChanged( int state ) |
| 149 | +{ |
| 150 | + mAtlas->setHideCoverage( state == Qt::Checked ); |
| 151 | +} |
| 152 | + |
| 153 | +void QgsLayoutAtlasWidget::mAtlasSingleFileCheckBox_stateChanged( int state ) |
| 154 | +{ |
| 155 | + if ( state == Qt::Checked ) |
| 156 | + { |
| 157 | + mAtlasFilenamePatternEdit->setEnabled( false ); |
| 158 | + mAtlasFilenameExpressionButton->setEnabled( false ); |
| 159 | + } |
| 160 | + else |
| 161 | + { |
| 162 | + mAtlasFilenamePatternEdit->setEnabled( true ); |
| 163 | + mAtlasFilenameExpressionButton->setEnabled( true ); |
| 164 | + } |
| 165 | +#if 0 //TODO |
| 166 | + mAtlas->setSingleFile( state == Qt::Checked ); |
| 167 | +#endif |
| 168 | +} |
| 169 | + |
| 170 | +void QgsLayoutAtlasWidget::mAtlasSortFeatureCheckBox_stateChanged( int state ) |
| 171 | +{ |
| 172 | + if ( state == Qt::Checked ) |
| 173 | + { |
| 174 | + mAtlasSortFeatureDirectionButton->setEnabled( true ); |
| 175 | + mAtlasSortFeatureKeyComboBox->setEnabled( true ); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + mAtlasSortFeatureDirectionButton->setEnabled( false ); |
| 180 | + mAtlasSortFeatureKeyComboBox->setEnabled( false ); |
| 181 | + } |
| 182 | + mAtlas->setSortFeatures( state == Qt::Checked ); |
| 183 | + updateAtlasFeatures(); |
| 184 | +} |
| 185 | + |
| 186 | +void QgsLayoutAtlasWidget::updateAtlasFeatures() |
| 187 | +{ |
| 188 | +#if 0 //TODO |
| 189 | + bool updated = mAtlas->updateFeatures(); |
| 190 | + if ( !updated ) |
| 191 | + { |
| 192 | + QMessageBox::warning( nullptr, tr( "Atlas preview" ), |
| 193 | + tr( "No matching atlas features found!" ), |
| 194 | + QMessageBox::Ok, |
| 195 | + QMessageBox::Ok ); |
| 196 | + |
| 197 | + //Perhaps atlas preview should be disabled now? If so, it may get annoying if user is editing |
| 198 | + //the filter expression and it keeps disabling itself. |
| 199 | + return; |
| 200 | + } |
| 201 | +#endif |
| 202 | +} |
| 203 | + |
| 204 | +void QgsLayoutAtlasWidget::changesSortFeatureField( const QString &fieldName ) |
| 205 | +{ |
| 206 | + mAtlas->setSortExpression( fieldName ); |
| 207 | + updateAtlasFeatures(); |
| 208 | +} |
| 209 | + |
| 210 | +void QgsLayoutAtlasWidget::mAtlasFeatureFilterCheckBox_stateChanged( int state ) |
| 211 | +{ |
| 212 | + if ( state == Qt::Checked ) |
| 213 | + { |
| 214 | + mAtlasFeatureFilterEdit->setEnabled( true ); |
| 215 | + mAtlasFeatureFilterButton->setEnabled( true ); |
| 216 | + } |
| 217 | + else |
| 218 | + { |
| 219 | + mAtlasFeatureFilterEdit->setEnabled( false ); |
| 220 | + mAtlasFeatureFilterButton->setEnabled( false ); |
| 221 | + } |
| 222 | + mAtlas->setFilterFeatures( state == Qt::Checked ); |
| 223 | + updateAtlasFeatures(); |
| 224 | +} |
| 225 | + |
| 226 | +void QgsLayoutAtlasWidget::pageNameExpressionChanged( const QString &, bool valid ) |
| 227 | +{ |
| 228 | + QString expression = mPageNameWidget->asExpression(); |
| 229 | + if ( !valid && !expression.isEmpty() ) |
| 230 | + { |
| 231 | + return; |
| 232 | + } |
| 233 | + |
| 234 | + mAtlas->setPageNameExpression( expression ); |
| 235 | +} |
| 236 | + |
| 237 | +void QgsLayoutAtlasWidget::mAtlasFeatureFilterEdit_editingFinished() |
| 238 | +{ |
| 239 | + QString error; |
| 240 | + mAtlas->setFilterExpression( mAtlasFeatureFilterEdit->text(), error ); |
| 241 | + updateAtlasFeatures(); |
| 242 | +} |
| 243 | + |
| 244 | +void QgsLayoutAtlasWidget::mAtlasFeatureFilterButton_clicked() |
| 245 | +{ |
| 246 | + QgsVectorLayer *vl = dynamic_cast<QgsVectorLayer *>( mAtlasCoverageLayerComboBox->currentLayer() ); |
| 247 | + |
| 248 | + if ( !vl ) |
| 249 | + { |
| 250 | + return; |
| 251 | + } |
| 252 | + |
| 253 | + QgsExpressionContext context = mLayout->createExpressionContext(); |
| 254 | + QgsExpressionBuilderDialog exprDlg( vl, mAtlasFeatureFilterEdit->text(), this, QStringLiteral( "generic" ), context ); |
| 255 | + exprDlg.setWindowTitle( tr( "Expression Based Filter" ) ); |
| 256 | + |
| 257 | + if ( exprDlg.exec() == QDialog::Accepted ) |
| 258 | + { |
| 259 | + QString expression = exprDlg.expressionText(); |
| 260 | + if ( !expression.isEmpty() ) |
| 261 | + { |
| 262 | + mAtlasFeatureFilterEdit->setText( expression ); |
| 263 | + QString error; |
| 264 | + mAtlas->setFilterExpression( mAtlasFeatureFilterEdit->text(), error ); |
| 265 | + updateAtlasFeatures(); |
| 266 | + } |
| 267 | + } |
| 268 | +} |
| 269 | + |
| 270 | +void QgsLayoutAtlasWidget::mAtlasSortFeatureDirectionButton_clicked() |
| 271 | +{ |
| 272 | + Qt::ArrowType at = mAtlasSortFeatureDirectionButton->arrowType(); |
| 273 | + at = ( at == Qt::UpArrow ) ? Qt::DownArrow : Qt::UpArrow; |
| 274 | + mAtlasSortFeatureDirectionButton->setArrowType( at ); |
| 275 | + |
| 276 | + mAtlas->setSortAscending( at == Qt::UpArrow ); |
| 277 | + updateAtlasFeatures(); |
| 278 | +} |
| 279 | + |
| 280 | +void QgsLayoutAtlasWidget::changeFileFormat() |
| 281 | +{ |
| 282 | +#if 0 //TODO |
| 283 | + QgsAtlasComposition *atlasMap = mAtlas; |
| 284 | + atlasMap->setFileFormat( mAtlasFileFormat->currentText() ); |
| 285 | +#endif |
| 286 | +} |
| 287 | +void QgsLayoutAtlasWidget::updateGuiElements() |
| 288 | +{ |
| 289 | + blockAllSignals( true ); |
| 290 | + mUseAtlasCheckBox->setCheckState( mAtlas->enabled() ? Qt::Checked : Qt::Unchecked ); |
| 291 | + mConfigurationGroup->setEnabled( mAtlas->enabled() ); |
| 292 | + mOutputGroup->setEnabled( mAtlas->enabled() ); |
| 293 | + |
| 294 | + mAtlasCoverageLayerComboBox->setLayer( mAtlas->coverageLayer() ); |
| 295 | + mPageNameWidget->setLayer( mAtlas->coverageLayer() ); |
| 296 | + mPageNameWidget->setField( mAtlas->pageNameExpression() ); |
| 297 | + |
| 298 | + mAtlasSortFeatureKeyComboBox->setLayer( mAtlas->coverageLayer() ); |
| 299 | + mAtlasSortFeatureKeyComboBox->setField( mAtlas->sortExpression() ); |
| 300 | + |
| 301 | + mAtlasFilenamePatternEdit->setText( mAtlas->filenameExpression() ); |
| 302 | + mAtlasHideCoverageCheckBox->setCheckState( mAtlas->hideCoverage() ? Qt::Checked : Qt::Unchecked ); |
| 303 | + |
| 304 | +#if 0 //TODO |
| 305 | + mAtlasSingleFileCheckBox->setCheckState( mAtlas->singleFile() ? Qt::Checked : Qt::Unchecked ); |
| 306 | + mAtlasFilenamePatternEdit->setEnabled( !mAtlas->singleFile() ); |
| 307 | + mAtlasFilenameExpressionButton->setEnabled( !mAtlas->singleFile() ); |
| 308 | +#endif |
| 309 | + |
| 310 | + mAtlasSortFeatureCheckBox->setCheckState( mAtlas->sortFeatures() ? Qt::Checked : Qt::Unchecked ); |
| 311 | + mAtlasSortFeatureDirectionButton->setEnabled( mAtlas->sortFeatures() ); |
| 312 | + mAtlasSortFeatureKeyComboBox->setEnabled( mAtlas->sortFeatures() ); |
| 313 | + |
| 314 | + mAtlasSortFeatureDirectionButton->setArrowType( mAtlas->sortAscending() ? Qt::UpArrow : Qt::DownArrow ); |
| 315 | + mAtlasFeatureFilterEdit->setText( mAtlas->filterExpression() ); |
| 316 | + |
| 317 | + mAtlasFeatureFilterCheckBox->setCheckState( mAtlas->filterFeatures() ? Qt::Checked : Qt::Unchecked ); |
| 318 | + mAtlasFeatureFilterEdit->setEnabled( mAtlas->filterFeatures() ); |
| 319 | + mAtlasFeatureFilterButton->setEnabled( mAtlas->filterFeatures() ); |
| 320 | + |
| 321 | +#if 0 //TODO |
| 322 | + mAtlasFileFormat->setCurrentIndex( mAtlasFileFormat->findText( mAtlas->fileFormat() ) ); |
| 323 | +#endif |
| 324 | + |
| 325 | + blockAllSignals( false ); |
| 326 | +} |
| 327 | + |
| 328 | +void QgsLayoutAtlasWidget::blockAllSignals( bool b ) |
| 329 | +{ |
| 330 | + mUseAtlasCheckBox->blockSignals( b ); |
| 331 | + mConfigurationGroup->blockSignals( b ); |
| 332 | + mOutputGroup->blockSignals( b ); |
| 333 | + mAtlasCoverageLayerComboBox->blockSignals( b ); |
| 334 | + mPageNameWidget->blockSignals( b ); |
| 335 | + mAtlasSortFeatureKeyComboBox->blockSignals( b ); |
| 336 | + mAtlasFilenamePatternEdit->blockSignals( b ); |
| 337 | + mAtlasHideCoverageCheckBox->blockSignals( b ); |
| 338 | + mAtlasSingleFileCheckBox->blockSignals( b ); |
| 339 | + mAtlasSortFeatureCheckBox->blockSignals( b ); |
| 340 | + mAtlasSortFeatureDirectionButton->blockSignals( b ); |
| 341 | + mAtlasFeatureFilterEdit->blockSignals( b ); |
| 342 | + mAtlasFeatureFilterCheckBox->blockSignals( b ); |
| 343 | +} |
0 commit comments