Skip to content

Commit

Permalink
Rework how composer map cache is invalidated
Browse files Browse the repository at this point in the history
Avoids unnecessary redraws and defers cache update until
when item is actually visible
  • Loading branch information
nyalldawson committed May 8, 2017
1 parent 91b046a commit abe8ead
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 99 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,7 @@ and setAtlasScalingMode() instead.
- setMapCanvas() was removed. This is no longer required to draw map annotations, which are instead retrieved from the composition's
associated project's annotationManager()
- setDrawCanvasItems() and drawCanvasItems() were renamed to setDrawAnnotations() and drawAnnotations()
- setCacheUpdated(), updateCachedImage() and cache() were removed. Use invalidateCache() instead.


QgsComposerMapGrid {#qgis_api_break_3_0_QgsComposerMapGrid}
Expand Down
1 change: 0 additions & 1 deletion python/auto_sip.blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ core/composer/qgscomposeritemcommand.sip
core/composer/qgscomposeritemgroup.sip
core/composer/qgscomposerlabel.sip
core/composer/qgscomposerlegend.sip
core/composer/qgscomposermap.sip
core/composer/qgscomposermapgrid.sip
core/composer/qgscomposermapitem.sip
core/composer/qgscomposermapoverview.sip
Expand Down
21 changes: 12 additions & 9 deletions src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,12 +230,14 @@ void QgsComposerItemWidget::on_mBackgroundColorButton_colorChanged( const QColor

//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
QgsComposerMap *cm = dynamic_cast<QgsComposerMap *>( mItem );
if ( cm )
if ( QgsComposerMap *cm = qobject_cast<QgsComposerMap *>( mItem ) )
{
cm->cache();
cm->invalidateCache();
}
else
{
mItem->updateItem();
}
mItem->update();
mItem->endCommand();
}

Expand Down Expand Up @@ -350,13 +352,14 @@ void QgsComposerItemWidget::on_mBackgroundGroupBox_toggled( bool state )

//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
QgsComposerMap *cm = dynamic_cast<QgsComposerMap *>( mItem );
if ( cm )
if ( QgsComposerMap *cm = qobject_cast<QgsComposerMap *>( mItem ) )
{
cm->cache();
cm->invalidateCache();
}
else
{
mItem->updateItem();
}

mItem->update();
mItem->endCommand();
}

Expand Down
31 changes: 10 additions & 21 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ void QgsComposerMapWidget::followVisibilityPresetSelected( int currentIndex )
mFollowVisibilityPresetCheckBox->setChecked( true );
mComposerMap->setFollowVisibilityPresetName( presetName );

mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}

void QgsComposerMapWidget::keepLayersVisibilityPresetSelected()
Expand All @@ -216,8 +215,7 @@ void QgsComposerMapWidget::keepLayersVisibilityPresetSelected()

mComposerMap->setLayerStyleOverrides( QgsProject::instance()->mapThemeCollection()->mapThemeStyleOverrides( presetName ) );

mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}
}

Expand Down Expand Up @@ -301,8 +299,7 @@ void QgsComposerMapWidget::mapCrsChanged( const QgsCoordinateReferenceSystem &cr
if ( updateExtent )
mComposerMap->zoomToExtent( newExtent );
mComposerMap->endCommand();
mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}

void QgsComposerMapWidget::on_mAtlasCheckBox_toggled( bool checked )
Expand Down Expand Up @@ -369,8 +366,7 @@ void QgsComposerMapWidget::updateMapForAtlas()
else
{
//redraw map
mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}
}

Expand Down Expand Up @@ -466,8 +462,7 @@ void QgsComposerMapWidget::on_mPreviewModeComboBox_activated( int i )
mUpdatePreviewButton->setEnabled( false );
}

mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}

void QgsComposerMapWidget::on_mScaleLineEdit_editingFinished()
Expand Down Expand Up @@ -503,8 +498,7 @@ void QgsComposerMapWidget::rotationChanged()
mComposerMap->beginCommand( tr( "Map rotation changed" ), QgsComposerMergeCommand::ComposerMapRotation );
mComposerMap->setMapRotation( mMapRotationSpinBox->value() );
mComposerMap->endCommand();
mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}

void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()
Expand Down Expand Up @@ -869,9 +863,7 @@ void QgsComposerMapWidget::on_mUpdatePreviewButton_clicked()

mUpdatePreviewButton->setEnabled( false ); //prevent crashes because of many button clicks

mComposerMap->setCacheUpdated( false );
mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();

mUpdatePreviewButton->setEnabled( true );
}
Expand All @@ -891,8 +883,7 @@ void QgsComposerMapWidget::on_mFollowVisibilityPresetCheckBox_stateChanged( int
mKeepLayerListCheckBox->setCheckState( Qt::Unchecked );
mKeepLayerStylesCheckBox->setCheckState( Qt::Unchecked );

mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
}
else
{
Expand Down Expand Up @@ -924,7 +915,7 @@ void QgsComposerMapWidget::on_mKeepLayerListCheckBox_stateChanged( int state )
else
{
mKeepLayerStylesCheckBox->setChecked( Qt::Unchecked );
mComposerMap->updateCachedImage();
mComposerMap->invalidateCache();
}

mKeepLayerStylesCheckBox->setEnabled( state == Qt::Checked );
Expand Down Expand Up @@ -959,9 +950,7 @@ void QgsComposerMapWidget::on_mDrawCanvasItemsCheckBox_stateChanged( int state )
mComposerMap->beginCommand( tr( "Canvas items toggled" ) );
mComposerMap->setDrawAnnotations( state == Qt::Checked );
mUpdatePreviewButton->setEnabled( false ); //prevent crashes because of many button clicks
mComposerMap->setCacheUpdated( false );
mComposerMap->cache();
mComposerMap->update();
mComposerMap->invalidateCache();
mUpdatePreviewButton->setEnabled( true );
mComposerMap->endCommand();
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ void QgsAtlasComposition::updateAtlasMaps()
continue;
}

currentMap->cache();
currentMap->invalidateCache();
}
}

Expand Down Expand Up @@ -426,7 +426,7 @@ bool QgsAtlasComposition::prepareForFeature( const int featureI, const bool upda
{
// map is not atlas driven, so manually force a redraw (to reflect possibly atlas
// dependent symbology)
( *mit )->cache();
( *mit )->invalidateCache();
}
}

Expand Down
75 changes: 26 additions & 49 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ QgsMapSettings QgsComposerMap::mapSettings( const QgsRectangle &extent, QSizeF s
return jobMapSettings;
}

void QgsComposerMap::cache()
void QgsComposerMap::recreateCachedImageInBackground()
{
if ( mPreviewMode == Rectangle )
{
Expand Down Expand Up @@ -286,6 +286,7 @@ void QgsComposerMap::cache()
mCacheRenderingImage->fill( QColor( 255, 255, 255, 0 ).rgba() );
}

mCacheInvalidated = false;
mPainter.reset( new QPainter( mCacheRenderingImage.get() ) );
QgsMapSettings settings( mapSettings( ext, QSizeF( w, h ), mCacheRenderingImage->logicalDpiX() ) );
mPainterJob.reset( new QgsMapRendererCustomPainterJob( settings, mPainter.get() ) );
Expand All @@ -298,7 +299,6 @@ void QgsComposerMap::painterJobFinished()
mPainter->end();
mPainterJob.reset( nullptr );
mPainter.reset( nullptr );
mCacheUpdated = true;
mCacheFinalImage = std::move( mCacheRenderingImage );
mLastRenderedImageOffsetX = 0;
mLastRenderedImageOffsetY = 0;
Expand Down Expand Up @@ -351,11 +351,17 @@ void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *,
if ( !mPainterJob )
{
// this is the map's very first paint - trigger a cache update
cache();
recreateCachedImageInBackground();
}
}
else
{
if ( mCacheInvalidated )
{
// cache was invalidated - trigger a background update
recreateCachedImageInBackground();
}

//Background color is already included in cached image, so no need to draw

double imagePixelWidth = mCacheFinalImage->width(); //how many pixels of the image are for the map extent?
Expand Down Expand Up @@ -436,6 +442,12 @@ void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *,
painter->restore();
}

void QgsComposerMap::invalidateCache()
{
mCacheInvalidated = true;
updateItem();
}

int QgsComposerMap::numberExportLayers() const
{
return
Expand Down Expand Up @@ -495,26 +507,14 @@ bool QgsComposerMap::shouldDrawPart( PartType part ) const
return true; // for Layer
}

void QgsComposerMap::updateCachedImage()
{
mCacheUpdated = false;
cache();
update();
}

void QgsComposerMap::renderModeUpdateCachedImage()
{
if ( mPreviewMode == Render )
{
updateCachedImage();
invalidateCache();
}
}

void QgsComposerMap::setCacheUpdated( bool u )
{
mCacheUpdated = u;
}

QList<QgsMapLayer *> QgsComposerMap::layersToRender( const QgsExpressionContext *context ) const
{
QgsExpressionContext scopedContext = createExpressionContext();
Expand Down Expand Up @@ -635,8 +635,7 @@ void QgsComposerMap::moveContent( double dx, double dy )
//in case data defined extents are set, these override the calculated values
refreshMapExtents();

cache();
update();
invalidateCache();
emit itemChanged();
emit extentChanged();
}
Expand Down Expand Up @@ -709,8 +708,7 @@ void QgsComposerMap::zoomContent( const double factor, const QPointF point, cons
//recalculate data defined scale and extents, since that may override zoom
refreshMapExtents();

cache();
update();
invalidateCache();
emit itemChanged();
emit extentChanged();
}
Expand All @@ -729,10 +727,8 @@ void QgsComposerMap::setSceneRect( const QRectF &rectangle )

//recalculate data defined scale and extents
refreshMapExtents();
mCacheUpdated = false;

updateBoundingRect();
updateItem();
invalidateCache();
emit itemChanged();
emit extentChanged();
}
Expand Down Expand Up @@ -796,8 +792,7 @@ void QgsComposerMap::zoomToExtent( const QgsRectangle &extent )
//recalculate data defined scale and extents, since that may override extent
refreshMapExtents();

mCacheUpdated = false;
updateItem();
invalidateCache();
emit itemChanged();
emit extentChanged();
}
Expand Down Expand Up @@ -836,9 +831,8 @@ void QgsComposerMap::setNewAtlasFeatureExtent( const QgsRectangle &extent )
//recalculate data defined scale and extents, since that may override extents
refreshMapExtents();

mCacheUpdated = false;
emit preparedForAtlas();
updateItem();
invalidateCache();
emit itemChanged();
emit extentChanged();
}
Expand Down Expand Up @@ -913,11 +907,9 @@ void QgsComposerMap::setNewScale( double scaleDenominator, bool forceUpdate )
mExtent.scale( scaleRatio );
}

mCacheUpdated = false;
invalidateCache();
if ( forceUpdate )
{
cache();
update();
emit itemChanged();
}
emit extentChanged();
Expand All @@ -939,9 +931,9 @@ void QgsComposerMap::setMapRotation( double r )
{
mMapRotation = r;
mEvaluatedMapRotation = mMapRotation;
invalidateCache();
emit mapRotationChanged( r );
emit itemChanged();
update();
}

double QgsComposerMap::mapRotation( QgsComposerObject::PropertyValueType valueType ) const
Expand Down Expand Up @@ -1078,21 +1070,6 @@ void QgsComposerMap::refreshMapExtents( const QgsExpressionContext *context )
mEvaluatedMapRotation = mapRotation;
emit mapRotationChanged( mapRotation );
}

}

void QgsComposerMap::updateItem()
{
if ( !updatesEnabled() )
{
return;
}

if ( mPreviewMode != QgsComposerMap::Rectangle && !mCacheUpdated )
{
cache();
}
QgsComposerItem::updateItem();
}

bool QgsComposerMap::containsWmsLayer() const
Expand Down Expand Up @@ -1182,7 +1159,7 @@ void QgsComposerMap::connectUpdateSlot()
connect( project, &QgsProject::layersRemoved, this, &QgsComposerMap::renderModeUpdateCachedImage );
connect( project, &QgsProject::legendLayersAdded, this, &QgsComposerMap::renderModeUpdateCachedImage );
}
connect( mComposition, &QgsComposition::refreshItemsTriggered, this, &QgsComposerMap::updateCachedImage );
connect( mComposition, &QgsComposition::refreshItemsTriggered, this, &QgsComposerMap::invalidateCache );
}

bool QgsComposerMap::writeXml( QDomElement &elem, QDomDocument &doc ) const
Expand Down Expand Up @@ -1448,7 +1425,7 @@ bool QgsComposerMap::readXml( const QDomElement &itemElem, const QDomDocument &d

mDrawing = false;
mNumCachedLayers = 0;
mCacheUpdated = false;
mCacheInvalidated = true;

//overviews
mOverviewStack->readXml( itemElem, doc );
Expand Down Expand Up @@ -1870,7 +1847,7 @@ void QgsComposerMap::refreshDataDefinedProperty( const QgsComposerObject::DataDe
}

//force redraw
mCacheUpdated = false;
mCacheInvalidated = true;

QgsComposerItem::refreshDataDefinedProperty( property, evalContext );
}
Expand Down

0 comments on commit abe8ead

Please sign in to comment.