Skip to content

Commit

Permalink
[composer] Update features if atlas preview enabled and coverage filt…
Browse files Browse the repository at this point in the history
…er/sort changes.

Protect against atlas with no matching features
  • Loading branch information
nyalldawson committed Jan 1, 2014
1 parent 3362579 commit 3869169
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 21 deletions.
43 changes: 36 additions & 7 deletions src/app/composer/qgsatlascompositionwidget.cpp
Expand Up @@ -136,7 +136,7 @@ void QgsAtlasCompositionWidget::onLayerAdded( QgsMapLayer* map )
if ( mAtlasCoverageLayerComboBox->count() == 1 )
{
atlasMap->setCoverageLayer( vectorLayer );
atlasMap->updateFeatures();
updateAtlasFeatures();
checkLayerType( vectorLayer );
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ void QgsAtlasCompositionWidget::on_mAtlasCoverageLayerComboBox_currentIndexChang
{
checkLayerType( layer );
atlasMap->setCoverageLayer( layer );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

// update sorting columns
Expand Down Expand Up @@ -335,7 +335,36 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureCheckBox_stateChanged( int s
mAtlasSortFeatureKeyComboBox->setEnabled( false );
}
atlasMap->setSortFeatures( state == Qt::Checked );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::updateAtlasFeatures()
{
//only do this if composer mode is preview
if ( !mComposition->atlasPreviewEnabled() )
{
return;
}

//update atlas features
QgsAtlasComposition* atlasMap = &mComposition->atlasComposition();
if ( !atlasMap )
{
return;
}

bool updated = atlasMap->updateFeatures();
if ( !updated )
{
QMessageBox::warning( 0, tr( "Atlas preview" ),
tr( "No matching atlas features found!" ),
QMessageBox::Ok,
QMessageBox::Ok );

//Perhaps atlas preview should be disabled now? If so, it may get annoying if user is editing
//the filter expression and it keeps disabling itself.
return;
}
}

void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChanged( int index )
Expand All @@ -350,7 +379,7 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChan
{
atlasMap->setSortKeyAttributeIndex( index );
}
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterCheckBox_stateChanged( int state )
Expand All @@ -372,7 +401,7 @@ void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterCheckBox_stateChanged( int
mAtlasFeatureFilterButton->setEnabled( false );
}
atlasMap->setFilterFeatures( state == Qt::Checked );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QString& text )
Expand All @@ -384,7 +413,7 @@ void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QS
}

atlasMap->setFeatureFilter( text );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterButton_clicked()
Expand Down Expand Up @@ -421,7 +450,7 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureDirectionButton_clicked()
}

atlasMap->setSortAscending( at == Qt::UpArrow );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::fillSortColumns()
Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgsatlascompositionwidget.h
Expand Up @@ -62,6 +62,8 @@ class QgsAtlasCompositionWidget:

void updateGuiElements();

void updateAtlasFeatures();

private:
QgsComposition* mComposition;

Expand Down
16 changes: 15 additions & 1 deletion src/app/composer/qgscomposer.cpp
Expand Up @@ -807,7 +807,21 @@ void QgsComposer::on_mActionAtlasPreview_triggered( bool checked )
mActionAtlasNext->setEnabled( checked );
mActionAtlasPrev->setEnabled( checked );

mComposition->setAtlasPreviewEnabled( checked );
bool previewEnabled = mComposition->setAtlasPreviewEnabled( checked );
if ( !previewEnabled )
{
//something went wrong, eg, no matching features
QMessageBox::warning( 0, tr( "Enable atlas preview" ),
tr( "No matching atlas features found!" ),
QMessageBox::Ok,
QMessageBox::Ok );
mActionAtlasPreview->blockSignals( true );
mActionAtlasPreview->setChecked( false );
mActionAtlasPreview->blockSignals( false );
mStatusAtlasLabel->setText( QString() );
return;
}

if ( checked )
{
atlasMap->firstFeature();
Expand Down
23 changes: 16 additions & 7 deletions src/core/composer/qgsatlascomposition.cpp
Expand Up @@ -100,13 +100,13 @@ class FieldSorter
bool mAscending;
};

void QgsAtlasComposition::updateFeatures()
int QgsAtlasComposition::updateFeatures()
{
//needs to be called when layer, filter, sort changes

if ( !mComposerMap || !mCoverageLayer )
{
return;
return 0;
}

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

std::auto_ptr<QgsExpression> filterExpression;
if ( mFilterFeatures )
if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
{
filterExpression = std::auto_ptr<QgsExpression>( new QgsExpression( mFeatureFilter ) );
if ( filterExpression->hasParserError() )
Expand All @@ -151,7 +151,7 @@ void QgsAtlasComposition::updateFeatures()
mFeatureKeys.clear();
while ( fit.nextFeature( feat ) )
{
if ( mFilterFeatures )
if ( mFilterFeatures && !mFeatureFilter.isEmpty() )
{
QVariant result = filterExpression->evaluate( &feat, mCoverageLayer->pendingFields() );
if ( filterExpression->hasEvalError() )
Expand Down Expand Up @@ -188,17 +188,24 @@ void QgsAtlasComposition::updateFeatures()
{
firstFeature();
}

return mFeatureIds.size();
}


void QgsAtlasComposition::beginRender()
bool QgsAtlasComposition::beginRender()
{
if ( !mComposerMap || !mCoverageLayer )
{
return;
return false;
}

updateFeatures();
bool featuresUpdated = updateFeatures();
if ( !featuresUpdated )
{
//no matching features found
return false;
}

mRestoreLayer = false;
QStringList& layerSet = mComposition->mapRenderer()->layerSet();
Expand All @@ -216,6 +223,8 @@ void QgsAtlasComposition::beginRender()
// special columns for expressions
QgsExpression::setSpecialColumn( "$numpages", QVariant( mComposition->numPages() ) );
QgsExpression::setSpecialColumn( "$numfeatures", QVariant(( int )mFeatureIds.size() ) );

return true;
}

void QgsAtlasComposition::endRender()
Expand Down
9 changes: 6 additions & 3 deletions src/core/composer/qgsatlascomposition.h
Expand Up @@ -81,8 +81,9 @@ class CORE_EXPORT QgsAtlasComposition : public QObject
int sortKeyAttributeIndex() const { return mSortKeyAttributeIdx; }
void setSortKeyAttributeIndex( int idx ) { mSortKeyAttributeIdx = idx; }

/** Begins the rendering. */
void beginRender();
/** Begins the rendering. Returns true if successful, false if no matching atlas
features found.*/
bool beginRender();
/** Ends the rendering. Restores original extent */
void endRender();

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

QgsComposition* composition() { return mComposition; }

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

void nextFeature();
void prevFeature();
Expand Down
10 changes: 8 additions & 2 deletions src/core/composer/qgscomposition.cpp
Expand Up @@ -2327,7 +2327,7 @@ void QgsComposition::computeWorldFileParameters( double& a, double& b, double& c
f = r[3] * s[2] + r[4] * s[5] + r[5];
}

void QgsComposition::setAtlasPreviewEnabled( bool e )
bool QgsComposition::setAtlasPreviewEnabled( bool e )
{
mAtlasPreviewEnabled = e;

Expand All @@ -2337,10 +2337,16 @@ void QgsComposition::setAtlasPreviewEnabled( bool e )
}
else
{
mAtlasComposition.beginRender();
bool atlasHasFeatures = mAtlasComposition.beginRender();
if ( ! atlasHasFeatures )
{
mAtlasPreviewEnabled = false;
return false;
}
}

update();
return true;
}

void QgsComposition::relativeResizeRect( QRectF& rectToResize, const QRectF& boundsBefore, const QRectF& boundsAfter )
Expand Down
3 changes: 2 additions & 1 deletion src/core/composer/qgscomposition.h
Expand Up @@ -427,7 +427,8 @@ class CORE_EXPORT QgsComposition : public QGraphicsScene

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


public slots:
Expand Down

0 comments on commit 3869169

Please sign in to comment.