Skip to content
Permalink
Browse files

[composer] Update features if atlas preview enabled and coverage filt…

…er/sort changes.

Protect against atlas with no matching features
  • Loading branch information
nyalldawson committed Jan 1, 2014
1 parent 3362579 commit 386916970e7842fb174e35cf37803c087883a07a
@@ -136,7 +136,7 @@ void QgsAtlasCompositionWidget::onLayerAdded( QgsMapLayer* map )
if ( mAtlasCoverageLayerComboBox->count() == 1 )
{
atlasMap->setCoverageLayer( vectorLayer );
atlasMap->updateFeatures();
updateAtlasFeatures();
checkLayerType( vectorLayer );
}
}
@@ -192,7 +192,7 @@ void QgsAtlasCompositionWidget::on_mAtlasCoverageLayerComboBox_currentIndexChang
{
checkLayerType( layer );
atlasMap->setCoverageLayer( layer );
atlasMap->updateFeatures();
updateAtlasFeatures();
}

// update sorting columns
@@ -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 )
@@ -350,7 +379,7 @@ void QgsAtlasCompositionWidget::on_mAtlasSortFeatureKeyComboBox_currentIndexChan
{
atlasMap->setSortKeyAttributeIndex( index );
}
atlasMap->updateFeatures();
updateAtlasFeatures();
}

void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterCheckBox_stateChanged( int state )
@@ -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 )
@@ -384,7 +413,7 @@ void QgsAtlasCompositionWidget::on_mAtlasFeatureFilterEdit_textChanged( const QS
}

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

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

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

void QgsAtlasCompositionWidget::fillSortColumns()
@@ -62,6 +62,8 @@ class QgsAtlasCompositionWidget:

void updateGuiElements();

void updateAtlasFeatures();

private:
QgsComposition* mComposition;

@@ -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();
@@ -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();
@@ -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() )
@@ -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() )
@@ -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();
@@ -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()
@@ -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();

@@ -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();
@@ -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;

@@ -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 )
@@ -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:

0 comments on commit 3869169

Please sign in to comment.
You can’t perform that action at this time.