Skip to content

Commit 1752d44

Browse files
authored
Merge pull request #4204 from nyalldawson/cancel_without_blocking
Don't block when canceling canvas render jobs
2 parents e9dee40 + 6243f78 commit 1752d44

13 files changed

+120
-11
lines changed

python/core/qgsmaprenderercustompainterjob.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class QgsMapRendererCustomPainterJob : QgsMapRendererJob
1919

2020
virtual void start();
2121
virtual void cancel();
22+
virtual void cancelWithoutBlocking();
2223
virtual void waitForFinished();
2324
virtual bool isActive() const;
2425
virtual bool usedCachedLabels() const;

python/core/qgsmaprendererjob.sip

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class QgsMapRendererJob : QObject
1818
//! Does nothing if the rendering is not active.
1919
virtual void cancel() = 0;
2020

21+
virtual void cancelWithoutBlocking() = 0;
22+
2123
//! Block until the job has finished.
2224
virtual void waitForFinished() = 0;
2325

python/core/qgsmaprendererparalleljob.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class QgsMapRendererParallelJob : QgsMapRendererQImageJob
1818

1919
virtual void start();
2020
virtual void cancel();
21+
virtual void cancelWithoutBlocking();
2122
virtual void waitForFinished();
2223
virtual bool isActive() const;
2324
virtual bool usedCachedLabels() const;

python/core/qgsmaprenderersequentialjob.sip

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class QgsMapRendererSequentialJob : QgsMapRendererQImageJob
1919

2020
virtual void start();
2121
virtual void cancel();
22+
virtual void cancelWithoutBlocking();
2223
virtual void waitForFinished();
2324
virtual bool isActive() const;
2425
virtual bool usedCachedLabels() const;

src/core/qgsmaprenderercustompainterjob.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,7 @@ void QgsMapRendererCustomPainterJob::cancel()
109109

110110
QgsDebugMsg( "QPAINTER canceling" );
111111
disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererCustomPainterJob::futureFinished );
112-
113-
mLabelJob.context.setRenderingStopped( true );
114-
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
115-
{
116-
it->context.setRenderingStopped( true );
117-
if ( it->renderer && it->renderer->feedback() )
118-
it->renderer->feedback()->cancel();
119-
}
112+
cancelWithoutBlocking();
120113

121114
QTime t;
122115
t.start();
@@ -130,6 +123,23 @@ void QgsMapRendererCustomPainterJob::cancel()
130123
QgsDebugMsg( "QPAINTER canceled" );
131124
}
132125

126+
void QgsMapRendererCustomPainterJob::cancelWithoutBlocking()
127+
{
128+
if ( !isActive() )
129+
{
130+
QgsDebugMsg( "QPAINTER not running!" );
131+
return;
132+
}
133+
134+
mLabelJob.context.setRenderingStopped( true );
135+
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
136+
{
137+
it->context.setRenderingStopped( true );
138+
if ( it->renderer && it->renderer->feedback() )
139+
it->renderer->feedback()->cancel();
140+
}
141+
}
142+
133143
void QgsMapRendererCustomPainterJob::waitForFinished()
134144
{
135145
if ( !isActive() )

src/core/qgsmaprenderercustompainterjob.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class CORE_EXPORT QgsMapRendererCustomPainterJob : public QgsMapRendererJob
3939

4040
virtual void start() override;
4141
virtual void cancel() override;
42+
virtual void cancelWithoutBlocking() override;
4243
virtual void waitForFinished() override;
4344
virtual bool isActive() const override;
4445
virtual bool usedCachedLabels() const override;

src/core/qgsmaprendererjob.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,13 @@ class CORE_EXPORT QgsMapRendererJob : public QObject
121121
//! Does nothing if the rendering is not active.
122122
virtual void cancel() = 0;
123123

124+
/**
125+
* Triggers cancelation of the rendering job without blocking. The render job will continue
126+
* to operate until it is able to cancel, at which stage the finished() signal will be emitted.
127+
* Does nothing if the rendering is not active.
128+
*/
129+
virtual void cancelWithoutBlocking() = 0;
130+
124131
//! Block until the job has finished.
125132
virtual void waitForFinished() = 0;
126133

@@ -296,6 +303,7 @@ class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
296303

297304
//! Get a preview/resulting image
298305
virtual QImage renderedImage() = 0;
306+
299307
};
300308

301309

src/core/qgsmaprendererparalleljob.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ void QgsMapRendererParallelJob::cancel()
108108
Q_ASSERT( mStatus == Idle );
109109
}
110110

111+
void QgsMapRendererParallelJob::cancelWithoutBlocking()
112+
{
113+
if ( !isActive() )
114+
return;
115+
116+
QgsDebugMsg( QString( "PARALLEL cancel at status %1" ).arg( mStatus ) );
117+
118+
mLabelJob.context.setRenderingStopped( true );
119+
for ( LayerRenderJobs::iterator it = mLayerJobs.begin(); it != mLayerJobs.end(); ++it )
120+
{
121+
it->context.setRenderingStopped( true );
122+
if ( it->renderer && it->renderer->feedback() )
123+
it->renderer->feedback()->cancel();
124+
}
125+
126+
if ( mStatus == RenderingLayers )
127+
{
128+
disconnect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererParallelJob::renderLayersFinished );
129+
connect( &mFutureWatcher, &QFutureWatcher<void>::finished, this, &QgsMapRendererParallelJob::renderingFinished );
130+
}
131+
}
132+
111133
void QgsMapRendererParallelJob::waitForFinished()
112134
{
113135
if ( !isActive() )

src/core/qgsmaprendererparalleljob.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class CORE_EXPORT QgsMapRendererParallelJob : public QgsMapRendererQImageJob
3636

3737
virtual void start() override;
3838
virtual void cancel() override;
39+
virtual void cancelWithoutBlocking() override;
3940
virtual void waitForFinished() override;
4041
virtual bool isActive() const override;
4142

src/core/qgsmaprenderersequentialjob.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,15 @@ void QgsMapRendererSequentialJob::cancel()
8383
Q_ASSERT( !mInternalJob && !mPainter );
8484
}
8585

86+
void QgsMapRendererSequentialJob::cancelWithoutBlocking()
87+
{
88+
if ( !isActive() )
89+
return;
90+
91+
QgsDebugMsg( "sequential - cancel internal" );
92+
mInternalJob->cancelWithoutBlocking();
93+
}
94+
8695
void QgsMapRendererSequentialJob::waitForFinished()
8796
{
8897
if ( !isActive() )

0 commit comments

Comments
 (0)