Skip to content

Commit

Permalink
Unlock composition using transformed previewed cache image of labeling
Browse files Browse the repository at this point in the history
results as well as map layers

This extends the recent addition of smooth map updates for raster
layers to also apply to map labels. Now you'll see a scaled/transformed
version of the previous map render's labels while the new labeling
results are being generated in the background.

Avoids "flashy" redraws of map labels
  • Loading branch information
nyalldawson committed Jan 27, 2021
1 parent 0edd4f9 commit c4818a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions python/core/auto_generated/qgsmaprendererjob.sip.in
Expand Up @@ -156,6 +156,7 @@ Returns map settings with which this job was started.
%End



signals:

void renderingLayersFinished();
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsmaprendererjob.cpp
Expand Up @@ -47,6 +47,7 @@
///@cond PRIVATE

const QString QgsMapRendererJob::LABEL_CACHE_ID = QStringLiteral( "_labels_" );
const QString QgsMapRendererJob::LABEL_PREVIEW_CACHE_ID = QStringLiteral( "_preview_labels_" );

bool LayerRenderJob::imageCanBeComposed() const
{
Expand Down Expand Up @@ -750,6 +751,7 @@ void QgsMapRendererJob::cleanupLabelJob( LabelRenderJob &job )
{
QgsDebugMsgLevel( QStringLiteral( "caching label result image" ), 2 );
mCache->setCacheImage( LABEL_CACHE_ID, *job.img, _qgis_listQPointerToRaw( job.participatingLayers ) );
mCache->setCacheImage( LABEL_PREVIEW_CACHE_ID, *job.img, _qgis_listQPointerToRaw( job.participatingLayers ) );
}

delete job.img;
Expand Down Expand Up @@ -816,6 +818,13 @@ QImage QgsMapRendererJob::composeImage(
painter.setOpacity( 1.0 );
painter.drawImage( 0, 0, *labelJob.img );
}
else if ( cache && cache->hasAnyCacheImage( LABEL_PREVIEW_CACHE_ID ) )
{
const QImage labelCacheImage = cache->transformedCacheImage( LABEL_PREVIEW_CACHE_ID, settings.mapToPixel() );
painter.setCompositionMode( QPainter::CompositionMode_SourceOver );
painter.setOpacity( 1.0 );
painter.drawImage( 0, 0, labelCacheImage );
}

// render any layers with the renderAboveLabels flag now
for ( LayerRenderJobs::const_iterator it = jobs.constBegin(); it != jobs.constEnd(); ++it )
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsmaprendererjob.h
Expand Up @@ -325,6 +325,13 @@ class CORE_EXPORT QgsMapRendererJob : public QObject
*/
static const QString LABEL_CACHE_ID SIP_SKIP;

/**
* QgsMapRendererCache ID string for cached label image during preview compositions only.
* \note not available in Python bindings
* \since QGIS 3.18
*/
static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;

signals:

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprendererparalleljob.cpp
Expand Up @@ -231,7 +231,7 @@ void QgsMapRendererParallelJob::renderLayersFinished()
// compose final image for labeling
if ( mSecondPassLayerJobs.isEmpty() )
{
mFinalImage = composeImage( mSettings, mLayerJobs, mLabelJob );
mFinalImage = composeImage( mSettings, mLayerJobs, mLabelJob, mCache );
}

QgsDebugMsgLevel( QStringLiteral( "PARALLEL layers finished" ), 2 );
Expand Down

0 comments on commit c4818a0

Please sign in to comment.