Skip to content

Commit 3869169

Browse files
committed
[composer] Update features if atlas preview enabled and coverage filter/sort changes.
Protect against atlas with no matching features
1 parent 3362579 commit 3869169

7 files changed

+85
-21
lines changed

src/app/composer/qgsatlascompositionwidget.cpp

+36-7
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void QgsAtlasCompositionWidget::onLayerAdded( QgsMapLayer* map )
136136
if ( mAtlasCoverageLayerComboBox->count() == 1 )
137137
{
138138
atlasMap->setCoverageLayer( vectorLayer );
139-
atlasMap->updateFeatures();
139+
updateAtlasFeatures();
140140
checkLayerType( vectorLayer );
141141
}
142142
}
@@ -192,7 +192,7 @@ void QgsAtlasCompositionWidget::on_mAtlasCoverageLayerComboBox_currentIndexChang
192192
{
193193
checkLayerType( layer );
194194
atlasMap->setCoverageLayer( layer );
195-
atlasMap->updateFeatures();
195+
updateAtlasFeatures();
196196
}
197197

198198
// update sorting columns
@@ -335,7 +335,36 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureCheckBox_stateChanged( int s
335335
mAtlasSortFeatureKeyComboBox->setEnabled( false );
336336
}
337337
atlasMap->setSortFeatures( state == Qt::Checked );
338-
atlasMap->updateFeatures();
338+
updateAtlasFeatures();
339+
}
340+
341+
void QgsAtlasCompositionWidget::updateAtlasFeatures()
342+
{
343+
//only do this if composer mode is preview
344+
if ( !mComposition->atlasPreviewEnabled() )
345+
{
346+
return;
347+
}
348+
349+
//update atlas features
350+
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
351+
if ( !atlasMap )
352+
{
353+
return;
354+
}
355+
356+
bool updated = atlasMap->updateFeatures();
357+
if ( !updated )
358+
{
359+
QMessageBox::warning( 0, tr( "Atlas preview" ),
360+
tr( "No matching atlas features found!" ),
361+
QMessageBox::Ok,
362+
QMessageBox::Ok );
363+
364+
//Perhaps atlas preview should be disabled now? If so, it may get annoying if user is editing
365+
//the filter expression and it keeps disabling itself.
366+
return;
367+
}
339368
}
340369

341370
void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChanged( int index )
@@ -350,7 +379,7 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChan
350379
{
351380
atlasMap->setSortKeyAttributeIndex( index );
352381
}
353-
atlasMap->updateFeatures();
382+
updateAtlasFeatures();
354383
}
355384

356385
void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterCheckBox_stateChanged( int state )
@@ -372,7 +401,7 @@ void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterCheckBox_stateChanged( int
372401
mAtlasFeatureFilterButton->setEnabled( false );
373402
}
374403
atlasMap->setFilterFeatures( state == Qt::Checked );
375-
atlasMap->updateFeatures();
404+
updateAtlasFeatures();
376405
}
377406

378407
void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QString& text )
@@ -384,7 +413,7 @@ void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QS
384413
}
385414

386415
atlasMap->setFeatureFilter( text );
387-
atlasMap->updateFeatures();
416+
updateAtlasFeatures();
388417
}
389418

390419
void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterButton_clicked()
@@ -421,7 +450,7 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureDirectionButton_clicked()
421450
}
422451

423452
atlasMap->setSortAscending( at == Qt::UpArrow );
424-
atlasMap->updateFeatures();
453+
updateAtlasFeatures();
425454
}
426455

427456
void QgsAtlasCompositionWidget::fillSortColumns()

src/app/composer/qgsatlascompositionwidget.h

+2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class QgsAtlasCompositionWidget:
6262

6363
void updateGuiElements();
6464

65+
void updateAtlasFeatures();
66+
6567
private:
6668
QgsComposition* mComposition;
6769

src/app/composer/qgscomposer.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,21 @@ void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
807807
mActionAtlasNext->setEnabled( checked );
808808
mActionAtlasPrev->setEnabled( checked );
809809

810-
mComposition->setAtlasPreviewEnabled( checked );
810+
bool previewEnabled = mComposition->setAtlasPreviewEnabled( checked );
811+
if ( !previewEnabled )
812+
{
813+
//something went wrong, eg, no matching features
814+
QMessageBox::warning( 0, tr( "Enable atlas preview" ),
815+
tr( "No matching atlas features found!" ),
816+
QMessageBox::Ok,
817+
QMessageBox::Ok );
818+
mActionAtlasPreview->blockSignals( true );
819+
mActionAtlasPreview->setChecked( false );
820+
mActionAtlasPreview->blockSignals( false );
821+
mStatusAtlasLabel->setText( QString() );
822+
return;
823+
}
824+
811825
if ( checked )
812826
{
813827
atlasMap->firstFeature();

src/core/composer/qgsatlascomposition.cpp

+16-7
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ class FieldSorter
100100
bool mAscending;
101101
};
102102

103-
void QgsAtlasComposition::updateFeatures()
103+
int QgsAtlasComposition::updateFeatures()
104104
{
105105
//needs to be called when layer, filter, sort changes
106106

107107
if ( !mComposerMap || !mCoverageLayer )
108108
{
109-
return;
109+
return 0;
110110
}
111111

112112
const QgsCoordinateReferenceSystem& coverage_crs = mCoverageLayer->crs();
@@ -135,7 +135,7 @@ void QgsAtlasComposition::updateFeatures()
135135
QgsFeatureIterator fit = mCoverageLayer->getFeatures();
136136

137137
std::auto_ptr<QgsExpression> filterExpression;
138-
if ( mFilterFeatures )
138+
if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
139139
{
140140
filterExpression = std::auto_ptr<QgsExpression>( new QgsExpression( mFeatureFilter ) );
141141
if ( filterExpression->hasParserError() )
@@ -151,7 +151,7 @@ void QgsAtlasComposition::updateFeatures()
151151
mFeatureKeys.clear();
152152
while ( fit.nextFeature( feat ) )
153153
{
154-
if ( mFilterFeatures )
154+
if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
155155
{
156156
QVariant result = filterExpression->evaluate( &feat, mCoverageLayer->pendingFields() );
157157
if ( filterExpression->hasEvalError() )
@@ -188,17 +188,24 @@ void QgsAtlasComposition::updateFeatures()
188188
{
189189
firstFeature();
190190
}
191+
192+
return mFeatureIds.size();
191193
}
192194

193195

194-
void QgsAtlasComposition::beginRender()
196+
bool QgsAtlasComposition::beginRender()
195197
{
196198
if ( !mComposerMap || !mCoverageLayer )
197199
{
198-
return;
200+
return false;
199201
}
200202

201-
updateFeatures();
203+
bool featuresUpdated = updateFeatures();
204+
if ( !featuresUpdated )
205+
{
206+
//no matching features found
207+
return false;
208+
}
202209

203210
mRestoreLayer = false;
204211
QStringList& layerSet = mComposition->mapRenderer()->layerSet();
@@ -216,6 +223,8 @@ void QgsAtlasComposition::beginRender()
216223
// special columns for expressions
217224
QgsExpression::setSpecialColumn( "$numpages", QVariant( mComposition->numPages() ) );
218225
QgsExpression::setSpecialColumn( "$numfeatures", QVariant(( int )mFeatureIds.size() ) );
226+
227+
return true;
219228
}
220229

221230
void QgsAtlasComposition::endRender()

src/core/composer/qgsatlascomposition.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
8181
int sortKeyAttributeIndex() const { return mSortKeyAttributeIdx; }
8282
void setSortKeyAttributeIndex( int idx ) { mSortKeyAttributeIdx = idx; }
8383

84-
/** Begins the rendering. */
85-
void beginRender();
84+
/** Begins the rendering. Returns true if successful, false if no matching atlas
85+
features found.*/
86+
bool beginRender();
8687
/** Ends the rendering. Restores original extent */
8788
void endRender();
8889

@@ -100,7 +101,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
100101

101102
QgsComposition* composition() { return mComposition; }
102103

103-
void updateFeatures();
104+
/** Requeries the current atlas coverage layer and applies filtering and sorting. Returns
105+
number of matching features. Must be called after prepareForFeature( i ) */
106+
int updateFeatures();
104107

105108
void nextFeature();
106109
void prevFeature();

src/core/composer/qgscomposition.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -2327,7 +2327,7 @@ void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c
23272327
f = r[3] * s[2] + r[4] * s[5] + r[5];
23282328
}
23292329

2330-
void QgsComposition::setAtlasPreviewEnabled( bool e )
2330+
bool QgsComposition::setAtlasPreviewEnabled( bool e )
23312331
{
23322332
mAtlasPreviewEnabled = e;
23332333

@@ -2337,10 +2337,16 @@ void QgsComposition::setAtlasPreviewEnabled( bool e )
23372337
}
23382338
else
23392339
{
2340-
mAtlasComposition.beginRender();
2340+
bool atlasHasFeatures = mAtlasComposition.beginRender();
2341+
if ( ! atlasHasFeatures )
2342+
{
2343+
mAtlasPreviewEnabled = false;
2344+
return false;
2345+
}
23412346
}
23422347

23432348
update();
2349+
return true;
23442350
}
23452351

23462352
void QgsComposition::relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter )

src/core/composer/qgscomposition.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene
427427

428428
/** Is the atlas preview enabled ? */
429429
bool atlasPreviewEnabled() const { return mAtlasPreviewEnabled; }
430-
void setAtlasPreviewEnabled( bool e );
430+
/** Set atlas preview enabled. Returns false if atlas preview could not be enabled */
431+
bool setAtlasPreviewEnabled( bool e );
431432

432433

433434
public slots:

0 commit comments

Comments
 (0)