|
| 1 | +/*************************************************************************** |
| 2 | + qgsatlascompositionwidget.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 "qgsatlascompositionwidget.h" |
| 18 | +#include "qgsatlascomposition.h" |
| 19 | +#include "qgscomposition.h" |
| 20 | +#include "qgsmaplayerregistry.h" |
| 21 | +#include "qgsvectorlayer.h" |
| 22 | +#include "qgsexpressionbuilderdialog.h" |
| 23 | +#include "qgscomposermap.h" |
| 24 | + |
| 25 | +QgsAtlasCompositionWidget::QgsAtlasCompositionWidget( QWidget* parent, QgsAtlasComposition* atlas, QgsComposition* c ): |
| 26 | + QWidget( parent ), mAtlas( atlas ), mComposition( c ) |
| 27 | +{ |
| 28 | + setupUi( this ); |
| 29 | + |
| 30 | + // populate the layer list |
| 31 | + mAtlasCoverageLayerComboBox->clear(); |
| 32 | + QMap< QString, QgsMapLayer * >& layers = QgsMapLayerRegistry::instance()->mapLayers(); |
| 33 | + int idx = 0; |
| 34 | + for ( QMap<QString, QgsMapLayer*>::const_iterator it = layers.begin(); it != layers.end(); ++it ) |
| 35 | + { |
| 36 | + // Only consider vector layers |
| 37 | + if ( dynamic_cast<QgsVectorLayer*>(it.value()) ) |
| 38 | + { |
| 39 | + mAtlasCoverageLayerComboBox->insertItem( idx++, it.key(), /* userdata */ qVariantFromValue( (void*)it.value() ) ); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Connect to addition / removal of layers |
| 44 | + QgsMapLayerRegistry* layerRegistry = QgsMapLayerRegistry::instance(); |
| 45 | + if ( layerRegistry ) |
| 46 | + { |
| 47 | + connect( layerRegistry, SIGNAL( layerWillBeRemoved( QString ) ), this, SLOT( onLayerRemoved( QString ) ) ); |
| 48 | + connect( layerRegistry, SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( onLayerAdded( QgsMapLayer* ) ) ); |
| 49 | + } |
| 50 | + |
| 51 | + // update the composer map combo box |
| 52 | + // populate the map list |
| 53 | + mComposerMapComboBox->clear(); |
| 54 | + QList<const QgsComposerMap*> availableMaps = mComposition->composerMapItems(); |
| 55 | + QList<const QgsComposerMap*>::const_iterator mapItemIt = availableMaps.constBegin(); |
| 56 | + for ( ; mapItemIt != availableMaps.constEnd(); ++mapItemIt ) |
| 57 | + { |
| 58 | + mComposerMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), qVariantFromValue( (void*)*mapItemIt ) ); |
| 59 | + } |
| 60 | + |
| 61 | + // Connect to addition / removal of maps |
| 62 | + connect( mComposition, SIGNAL( composerMapAdded( QgsComposerMap* ) ), this, SLOT( onComposerMapAdded( QgsComposerMap* ) ) ); |
| 63 | + connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( onItemRemoved( QgsComposerItem* ) ) ); |
| 64 | + |
| 65 | + // connect to updates |
| 66 | + connect( mAtlas, SIGNAL( parameterChanged() ), this, SLOT( updateGuiElements() ) ); |
| 67 | + |
| 68 | + updateGuiElements(); |
| 69 | +} |
| 70 | + |
| 71 | +QgsAtlasCompositionWidget::~QgsAtlasCompositionWidget() |
| 72 | +{ |
| 73 | +} |
| 74 | + |
| 75 | +void QgsAtlasCompositionWidget::on_mUseAtlasCheckBox_stateChanged( int state ) |
| 76 | +{ |
| 77 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 78 | + if ( state == Qt::Checked ) |
| 79 | + { |
| 80 | + atlasMap->setEnabled( true ); |
| 81 | + mAtlasFrame->setEnabled( true ); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + atlasMap->setEnabled( false ); |
| 86 | + mAtlasFrame->setEnabled( false ); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void QgsAtlasCompositionWidget::onLayerRemoved( QString layerName ) |
| 91 | +{ |
| 92 | + // update the atlas coverage layer combo box |
| 93 | + for ( int i = 0; i < mAtlasCoverageLayerComboBox->count(); ++i ) |
| 94 | + { |
| 95 | + if ( mAtlasCoverageLayerComboBox->itemText( i ) == layerName ) |
| 96 | + { |
| 97 | + mAtlasCoverageLayerComboBox->removeItem( i ); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +void QgsAtlasCompositionWidget::onLayerAdded( QgsMapLayer* map ) |
| 104 | +{ |
| 105 | + // update the atlas coverage layer combo box |
| 106 | + QgsVectorLayer* vectorLayer = dynamic_cast<QgsVectorLayer*>( map ); |
| 107 | + if ( vectorLayer ) |
| 108 | + { |
| 109 | + mAtlasCoverageLayerComboBox->addItem( map->id(), qVariantFromValue( (void*)map ) ); |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +void QgsAtlasCompositionWidget::onComposerMapAdded( QgsComposerMap* map ) |
| 114 | +{ |
| 115 | + mComposerMapComboBox->addItem( tr( "Map %1" ).arg( map->id() ), map->id() ); |
| 116 | +} |
| 117 | + |
| 118 | +void QgsAtlasCompositionWidget::onItemRemoved( QgsComposerItem* item ) |
| 119 | +{ |
| 120 | + QgsComposerMap* map = dynamic_cast<QgsComposerMap*>( item ); |
| 121 | + if ( map ) |
| 122 | + { |
| 123 | + int idx = mComposerMapComboBox->findData( map->id() ); |
| 124 | + if ( idx != -1 ) |
| 125 | + { |
| 126 | + mComposerMapComboBox->removeItem( idx ); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +void QgsAtlasCompositionWidget::on_mAtlasCoverageLayerComboBox_currentIndexChanged( int index ) |
| 132 | +{ |
| 133 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 134 | + if ( !atlasMap ) |
| 135 | + { |
| 136 | + return; |
| 137 | + } |
| 138 | + QgsVectorLayer* layer = reinterpret_cast<QgsVectorLayer*>(mAtlasCoverageLayerComboBox->itemData( index ).value<void*>()); |
| 139 | + atlasMap->setCoverageLayer( layer ); |
| 140 | +} |
| 141 | + |
| 142 | +void QgsAtlasCompositionWidget::on_mComposerMapComboBox_currentIndexChanged( int index ) |
| 143 | +{ |
| 144 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 145 | + if ( !atlasMap ) |
| 146 | + { |
| 147 | + return; |
| 148 | + } |
| 149 | + QgsComposerMap* map = reinterpret_cast<QgsComposerMap*>(mComposerMapComboBox->itemData( index ).value<void*>()); |
| 150 | + atlasMap->setComposerMap( map ); |
| 151 | +} |
| 152 | + |
| 153 | +void QgsAtlasCompositionWidget::on_mAtlasFilenamePatternEdit_textChanged( const QString& text ) |
| 154 | +{ |
| 155 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 156 | + if ( !atlasMap ) |
| 157 | + { |
| 158 | + return; |
| 159 | + } |
| 160 | + |
| 161 | + atlasMap->setFilenamePattern( text ); |
| 162 | +} |
| 163 | + |
| 164 | +void QgsAtlasCompositionWidget::on_mAtlasFilenameExpressionButton_clicked() |
| 165 | +{ |
| 166 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 167 | + if ( !atlasMap || !atlasMap->coverageLayer() ) |
| 168 | + { |
| 169 | + return; |
| 170 | + } |
| 171 | + |
| 172 | + QgsExpressionBuilderDialog exprDlg( atlasMap->coverageLayer(), mAtlasFilenamePatternEdit->text(), this ); |
| 173 | + exprDlg.setWindowTitle( tr( "Expression based filename" ) ); |
| 174 | + if ( exprDlg.exec() == QDialog::Accepted ) |
| 175 | + { |
| 176 | + QString expression = exprDlg.expressionText(); |
| 177 | + if ( !expression.isEmpty() ) |
| 178 | + { |
| 179 | + // will emit a textChanged signal |
| 180 | + mAtlasFilenamePatternEdit->setText( expression ); |
| 181 | + } |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +void QgsAtlasCompositionWidget::on_mAtlasHideCoverageCheckBox_stateChanged( int state ) |
| 186 | +{ |
| 187 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 188 | + if ( !atlasMap ) |
| 189 | + { |
| 190 | + return; |
| 191 | + } |
| 192 | + atlasMap->setHideCoverage( state == Qt::Checked ); |
| 193 | +} |
| 194 | + |
| 195 | +void QgsAtlasCompositionWidget::on_mAtlasFixedScaleCheckBox_stateChanged( int state ) |
| 196 | +{ |
| 197 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 198 | + if ( !atlasMap ) |
| 199 | + { |
| 200 | + return; |
| 201 | + } |
| 202 | + atlasMap->setFixedScale( state == Qt::Checked ); |
| 203 | + |
| 204 | + // in fixed scale mode, the margin is meaningless |
| 205 | + if ( state == Qt::Checked ) |
| 206 | + { |
| 207 | + mAtlasMarginSpinBox->setEnabled( false ); |
| 208 | + } |
| 209 | + else |
| 210 | + { |
| 211 | + mAtlasMarginSpinBox->setEnabled( true ); |
| 212 | + } |
| 213 | +} |
| 214 | + |
| 215 | +void QgsAtlasCompositionWidget::on_mAtlasSingleFileCheckBox_stateChanged( int state ) |
| 216 | +{ |
| 217 | + QgsAtlasComposition* atlasMap = mAtlas; |
| 218 | + if ( !atlasMap ) |
| 219 | + { |
| 220 | + return; |
| 221 | + } |
| 222 | + atlasMap->setSingleFile( state == Qt::Checked ); |
| 223 | +} |
| 224 | + |
| 225 | +void QgsAtlasCompositionWidget::updateGuiElements() |
| 226 | +{ |
| 227 | + if ( mAtlas->enabled() ) |
| 228 | + { |
| 229 | + mUseAtlasCheckBox->setCheckState( Qt::Checked ); |
| 230 | + } |
| 231 | + else |
| 232 | + { |
| 233 | + mUseAtlasCheckBox->setCheckState( Qt::Unchecked ); |
| 234 | + } |
| 235 | + |
| 236 | + int idx = mAtlasCoverageLayerComboBox->findData( qVariantFromValue( (void*)mAtlas->coverageLayer() )); |
| 237 | + if ( idx != -1 ) |
| 238 | + { |
| 239 | + mAtlasCoverageLayerComboBox->setCurrentIndex( idx ); |
| 240 | + } |
| 241 | + idx = mComposerMapComboBox->findData( qVariantFromValue( (void*)mAtlas->composerMap() )); |
| 242 | + if ( idx != -1 ) |
| 243 | + { |
| 244 | + mComposerMapComboBox->setCurrentIndex( idx ); |
| 245 | + } |
| 246 | + |
| 247 | + mAtlasMarginSpinBox->setValue( static_cast<int>(mAtlas->margin() * 100) ); |
| 248 | + mAtlasFilenamePatternEdit->setText( mAtlas->filenamePattern() ); |
| 249 | + mAtlasFixedScaleCheckBox->setCheckState( mAtlas->fixedScale() ? Qt::Checked : Qt::Unchecked ); |
| 250 | + mAtlasHideCoverageCheckBox->setCheckState( mAtlas->hideCoverage() ? Qt::Checked : Qt::Unchecked ); |
| 251 | + mAtlasSingleFileCheckBox->setCheckState( mAtlas->singleFile() ? Qt::Checked : Qt::Unchecked ); |
| 252 | +} |
| 253 | + |
| 254 | +void QgsAtlasCompositionWidget::blockAllSignals( bool b ) |
| 255 | +{ |
| 256 | + mUseAtlasCheckBox->blockSignals( b ); |
| 257 | + mAtlasFrame->blockSignals( b ); |
| 258 | +} |
0 commit comments