Skip to content
Permalink
Browse files

Stay more responsive while scheduling preview jobs

Because starting 8 jobs on the main thread in a loop can lead to reduced
responsiveness.

The new approach is to start each job 10 ms after the scheduling the
last one.
  • Loading branch information
m-kuhn committed Jul 31, 2017
1 parent 29b8853 commit 8e200767ec8bf1b2ba1005263cca3801cd0e2081
Showing with 35 additions and 24 deletions.
  1. +31 −24 src/gui/qgsmapcanvas.cpp
  2. +4 −0 src/gui/qgsmapcanvas.h
@@ -2132,43 +2132,50 @@ const QgsLabelingEngineSettings &QgsMapCanvas::labelingEngineSettings() const
void QgsMapCanvas::startPreviewJobs()
{
stopPreviewJobs(); //just in case still running
startPreviewJob( 0 );
}

void QgsMapCanvas::startPreviewJob( int number )
{
QgsRectangle mapRect = mSettings.visibleExtent();

for ( int j = 0; j < 3; ++j )
{
for ( int i = 0; i < 3; ++i )
{
if ( i == 1 && j == 1 )
{
continue;
}
if ( number == 4 )
number += 1;

int j = number / 3;
int i = number % 3;

//copy settings, only update extent
QgsMapSettings jobSettings = mSettings;
//copy settings, only update extent
QgsMapSettings jobSettings = mSettings;

double dx = ( i - 1 ) * mapRect.width();
double dy = ( 1 - j ) * mapRect.height();
QgsRectangle jobExtent = mapRect;
jobExtent.setXMaximum( jobExtent.xMaximum() + dx );
jobExtent.setXMinimum( jobExtent.xMinimum() + dx );
jobExtent.setYMaximum( jobExtent.yMaximum() + dy );
jobExtent.setYMinimum( jobExtent.yMinimum() + dy );
double dx = ( i - 1 ) * mapRect.width();
double dy = ( 1 - j ) * mapRect.height();
QgsRectangle jobExtent = mapRect;

jobSettings.setExtent( jobExtent );
jobSettings.setFlag( QgsMapSettings::DrawLabeling, false );
jobExtent.setXMaximum( jobExtent.xMaximum() + dx );
jobExtent.setXMinimum( jobExtent.xMinimum() + dx );
jobExtent.setYMaximum( jobExtent.yMaximum() + dy );
jobExtent.setYMinimum( jobExtent.yMinimum() + dy );

QgsMapRendererQImageJob *job = new QgsMapRendererParallelJob( jobSettings );
mPreviewJobs.append( job );
connect( job, &QgsMapRendererJob::finished, this, &QgsMapCanvas::previewJobFinished );
job->start();
}
jobSettings.setExtent( jobExtent );
jobSettings.setFlag( QgsMapSettings::DrawLabeling, false );

QgsMapRendererQImageJob *job = new QgsMapRendererParallelJob( jobSettings );
mPreviewJobs.append( job );
connect( job, &QgsMapRendererJob::finished, this, &QgsMapCanvas::previewJobFinished );
job->start();

if ( number < 8 )
{
mPreviewTimer.setSingleShot( true );
mPreviewTimer.setInterval( 10 );
connect( &mPreviewTimer, &QTimer::timeout, [ = ]() { startPreviewJob( number + 1 ); } );
}
}

void QgsMapCanvas::stopPreviewJobs()
{
mPreviewTimer.stop();
QList< QgsMapRendererQImageJob * >::const_iterator it = mPreviewJobs.constBegin();
for ( ; it != mPreviewJobs.constEnd(); ++it )
{
@@ -750,6 +750,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

void projectThemesChanged();

void startPreviewJob( int number );

private:
/// this class is non-copyable

@@ -845,6 +847,8 @@ class GUI_EXPORT QgsMapCanvas : public QGraphicsView

QTimer mAutoRefreshTimer;

QTimer mPreviewTimer;

QString mTheme;

bool mAnnotationsVisible = true;

0 comments on commit 8e20076

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