Skip to content

Commit 866dce8

Browse files
author
Hugo Mercier
committedFeb 6, 2013
[Atlas] Add UI management for feature sorting and filtering
1 parent b477179 commit 866dce8

File tree

3 files changed

+158
-8
lines changed

3 files changed

+158
-8
lines changed
 

‎src/app/composer/qgsatlascompositionwidget.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ QgsAtlasCompositionWidget::QgsAtlasCompositionWidget( QWidget* parent, QgsCompos
3939
mAtlasCoverageLayerComboBox->insertItem( idx++, it.value()->name(), /* userdata */ qVariantFromValue(( void* )it.value() ) );
4040
}
4141
}
42+
// update sort columns
43+
fillSortColumns();
4244

4345
// Connect to addition / removal of layers
4446
QgsMapLayerRegistry* layerRegistry = QgsMapLayerRegistry::instance();
@@ -58,6 +60,13 @@ QgsAtlasCompositionWidget::QgsAtlasCompositionWidget( QWidget* parent, QgsCompos
5860
mComposerMapComboBox->addItem( tr( "Map %1" ).arg(( *mapItemIt )->id() ), qVariantFromValue(( void* )*mapItemIt ) );
5961
}
6062

63+
// Sort direction
64+
mAtlasSortFeatureDirectionComboBox->insertItem( 0, tr("Ascending") );
65+
mAtlasSortFeatureDirectionComboBox->insertItem( 1, tr("Descending") );
66+
mAtlasSortFeatureDirectionComboBox->setEnabled( false );
67+
68+
mAtlasSortFeatureKeyComboBox->setEnabled( false );
69+
6170
// Connect to addition / removal of maps
6271
connect( mComposition, SIGNAL( composerMapAdded( QgsComposerMap* ) ), this, SLOT( onComposerMapAdded( QgsComposerMap* ) ) );
6372
connect( mComposition, SIGNAL( itemRemoved( QgsComposerItem* ) ), this, SLOT( onItemRemoved( QgsComposerItem* ) ) );
@@ -159,11 +168,17 @@ void QgsAtlasCompositionWidget::on_mAtlasCoverageLayerComboBox_currentIndexChang
159168
if ( index == -1 )
160169
{
161170
atlasMap->setCoverageLayer( 0 );
171+
172+
// clean up the sorting columns
173+
mAtlasSortFeatureKeyComboBox->clear();
162174
}
163175
else
164176
{
165177
QgsVectorLayer* layer = reinterpret_cast<QgsVectorLayer*>( mAtlasCoverageLayerComboBox->itemData( index ).value<void*>() );
166178
atlasMap->setCoverageLayer( layer );
179+
180+
// update sorting columns
181+
fillSortColumns();
167182
}
168183
}
169184

@@ -257,6 +272,99 @@ void QgsAtlasCompositionWidget::on_mAtlasSingleFileCheckBox_stateChanged( int st
257272
atlasMap->setSingleFile( state == Qt::Checked );
258273
}
259274

275+
void QgsAtlasCompositionWidget::on_mAtlasSortFeatureCheckBox_stateChanged( int state )
276+
{
277+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
278+
if ( !atlasMap )
279+
{
280+
return;
281+
}
282+
283+
if ( state == Qt::Checked ) {
284+
mAtlasSortFeatureDirectionComboBox->setEnabled( true );
285+
mAtlasSortFeatureKeyComboBox->setEnabled( true );
286+
}
287+
else {
288+
mAtlasSortFeatureDirectionComboBox->setEnabled( false );
289+
mAtlasSortFeatureKeyComboBox->setEnabled( false );
290+
}
291+
atlasMap->setSortFeatures( state == Qt::Checked );
292+
}
293+
294+
void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChanged( int index )
295+
{
296+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
297+
if ( !atlasMap )
298+
{
299+
return;
300+
}
301+
302+
if ( index != -1 ) {
303+
atlasMap->setSortKeyAttributeIndex( index );
304+
}
305+
}
306+
307+
void QgsAtlasCompositionWidget::on_mAtlasSortFeatureDirectionComboBox_currentIndexChanged( int index )
308+
{
309+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
310+
if ( !atlasMap )
311+
{
312+
return;
313+
}
314+
315+
if ( index != -1 ) {
316+
atlasMap->setSortAscending( index == 0 ? true : false );
317+
}
318+
}
319+
320+
void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QString& text )
321+
{
322+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
323+
if ( !atlasMap )
324+
{
325+
return;
326+
}
327+
328+
atlasMap->setFeatureFilter( text );
329+
}
330+
331+
void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterButton_clicked()
332+
{
333+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
334+
if ( !atlasMap || !atlasMap->coverageLayer() )
335+
{
336+
return;
337+
}
338+
339+
QgsExpressionBuilderDialog exprDlg( atlasMap->coverageLayer(), mAtlasFeatureFilterEdit->text(), this );
340+
exprDlg.setWindowTitle( tr( "Expression based filter" ) );
341+
if ( exprDlg.exec() == QDialog::Accepted )
342+
{
343+
QString expression = exprDlg.expressionText();
344+
if ( !expression.isEmpty() )
345+
{
346+
// will emit a textChanged signal
347+
mAtlasFeatureFilterEdit->setText( expression );
348+
}
349+
}
350+
}
351+
352+
void QgsAtlasCompositionWidget::fillSortColumns()
353+
{
354+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
355+
if ( !atlasMap || !atlasMap->coverageLayer() )
356+
{
357+
return;
358+
}
359+
360+
mAtlasSortFeatureKeyComboBox->clear();
361+
// Get fields of the selected coverage layer
362+
const QgsFields& fields = atlasMap->coverageLayer()->pendingFields();
363+
for ( int i = 0; i < fields.count(); ++i ) {
364+
mAtlasSortFeatureKeyComboBox->insertItem( i, fields.at(i).name() );
365+
}
366+
}
367+
260368
void QgsAtlasCompositionWidget::updateGuiElements()
261369
{
262370
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
@@ -285,6 +393,10 @@ void QgsAtlasCompositionWidget::updateGuiElements()
285393
mAtlasFixedScaleCheckBox->setCheckState( atlasMap->fixedScale() ? Qt::Checked : Qt::Unchecked );
286394
mAtlasHideCoverageCheckBox->setCheckState( atlasMap->hideCoverage() ? Qt::Checked : Qt::Unchecked );
287395
mAtlasSingleFileCheckBox->setCheckState( atlasMap->singleFile() ? Qt::Checked : Qt::Unchecked );
396+
mAtlasSortFeatureCheckBox->setCheckState( atlasMap->sortFeatures() ? Qt::Checked : Qt::Unchecked );
397+
mAtlasSortFeatureKeyComboBox->setCurrentIndex( atlasMap->sortKeyAttributeIndex() );
398+
mAtlasSortFeatureDirectionComboBox->setCurrentIndex( atlasMap->sortAscending() ? 0 : 1 );
399+
mAtlasFeatureFilterEdit->setText( atlasMap->featureFilter() );
288400
}
289401

290402
void QgsAtlasCompositionWidget::blockAllSignals( bool b )

‎src/app/composer/qgsatlascompositionwidget.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ class QgsAtlasCompositionWidget:
4343
void on_mAtlasFixedScaleCheckBox_stateChanged( int state );
4444
void on_mAtlasSingleFileCheckBox_stateChanged( int state );
4545

46+
void on_mAtlasSortFeatureCheckBox_stateChanged( int state );
47+
void on_mAtlasSortFeatureKeyComboBox_currentIndexChanged( int index );
48+
void on_mAtlasSortFeatureDirectionComboBox_currentIndexChanged( int index );
49+
void on_mAtlasFeatureFilterEdit_textChanged( const QString& text );
50+
void on_mAtlasFeatureFilterButton_clicked();
51+
52+
// extract fields from the current coverage layer and populate the corresponding combo box
53+
void fillSortColumns();
4654
private slots:
4755
void onLayerRemoved( QString );
4856
void onLayerAdded( QgsMapLayer* );

‎src/ui/qgsatlascompositionwidgetbase.ui

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,14 @@
7575
<number>0</number>
7676
</property>
7777
<item>
78-
<layout class="QGridLayout" name="gridLayout_7" rowstretch="0,0,0,0,0,0,0,0,0" columnstretch="0,0,0">
78+
<layout class="QGridLayout" name="gridLayout_7" rowstretch="0,0,0,0,0,0,0,0,0,0,0" columnstretch="0,0,0">
79+
<item row="5" column="0">
80+
<widget class="QLabel" name="mAtlasFeatureFilterLabel">
81+
<property name="text">
82+
<string>Feature filter</string>
83+
</property>
84+
</widget>
85+
</item>
7986
<item row="2" column="0" colspan="2">
8087
<widget class="QCheckBox" name="mAtlasHideCoverageCheckBox">
8188
<property name="toolTip">
@@ -86,21 +93,21 @@
8693
</property>
8794
</widget>
8895
</item>
89-
<item row="4" column="0">
96+
<item row="6" column="0">
9097
<widget class="QLabel" name="label">
9198
<property name="text">
9299
<string>Margin around coverage</string>
93100
</property>
94101
</widget>
95102
</item>
96-
<item row="7" column="0">
103+
<item row="9" column="0">
97104
<widget class="QLabel" name="label_5">
98105
<property name="text">
99106
<string>Output filename expression</string>
100107
</property>
101108
</widget>
102109
</item>
103-
<item row="4" column="1">
110+
<item row="6" column="1">
104111
<widget class="QSpinBox" name="mAtlasMarginSpinBox">
105112
<property name="suffix">
106113
<string> %</string>
@@ -113,14 +120,14 @@
113120
</property>
114121
</widget>
115122
</item>
116-
<item row="7" column="2">
123+
<item row="9" column="2">
117124
<widget class="QToolButton" name="mAtlasFilenameExpressionButton">
118125
<property name="text">
119126
<string>...</string>
120127
</property>
121128
</widget>
122129
</item>
123-
<item row="7" column="1">
130+
<item row="9" column="1">
124131
<widget class="QLineEdit" name="mAtlasFilenamePatternEdit"/>
125132
</item>
126133
<item row="1" column="0">
@@ -137,14 +144,14 @@
137144
</property>
138145
</widget>
139146
</item>
140-
<item row="5" column="0" colspan="2">
147+
<item row="7" column="0" colspan="2">
141148
<widget class="QCheckBox" name="mAtlasFixedScaleCheckBox">
142149
<property name="text">
143150
<string>Fixed scale</string>
144151
</property>
145152
</widget>
146153
</item>
147-
<item row="8" column="0" colspan="2">
154+
<item row="10" column="0" colspan="2">
148155
<widget class="QCheckBox" name="mAtlasSingleFileCheckBox">
149156
<property name="text">
150157
<string>Single file export when possible</string>
@@ -161,6 +168,29 @@
161168
</property>
162169
</widget>
163170
</item>
171+
<item row="3" column="1">
172+
<widget class="QComboBox" name="mAtlasSortFeatureKeyComboBox"/>
173+
</item>
174+
<item row="3" column="0">
175+
<widget class="QCheckBox" name="mAtlasSortFeatureCheckBox">
176+
<property name="text">
177+
<string>Sort features</string>
178+
</property>
179+
</widget>
180+
</item>
181+
<item row="5" column="2">
182+
<widget class="QToolButton" name="mAtlasFeatureFilterButton">
183+
<property name="text">
184+
<string>...</string>
185+
</property>
186+
</widget>
187+
</item>
188+
<item row="5" column="1">
189+
<widget class="QLineEdit" name="mAtlasFeatureFilterEdit"/>
190+
</item>
191+
<item row="4" column="1">
192+
<widget class="QComboBox" name="mAtlasSortFeatureDirectionComboBox"/>
193+
</item>
164194
</layout>
165195
</item>
166196
</layout>

0 commit comments

Comments
 (0)
Please sign in to comment.