Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow canceling background map renders
  • Loading branch information
nyalldawson committed Apr 23, 2017
1 parent 1659659 commit 4a2226a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
3 changes: 3 additions & 0 deletions python/core/qgsmaprenderertask.sip
Expand Up @@ -47,6 +47,9 @@ class QgsMapRendererTask : QgsTask
Adds ``annotations`` to be rendered on the map.
%End

virtual void cancel();


signals:

void renderingComplete();
Expand Down
27 changes: 24 additions & 3 deletions src/core/qgsmaprenderertask.cpp
Expand Up @@ -18,7 +18,6 @@
#include "qgsannotation.h"
#include "qgsannotationmanager.h"
#include "qgsmaprenderertask.h"
#include "qgsmaprenderercustompainterjob.h"


QgsMapRendererTask::QgsMapRendererTask( const QgsMapSettings &ms, const QString &fileName, const QString &fileFormat )
Expand Down Expand Up @@ -47,6 +46,16 @@ void QgsMapRendererTask::addAnnotations( QList< QgsAnnotation * > annotations )
}
}

void QgsMapRendererTask::cancel()
{
mJobMutex.lock();
if ( mJob )
mJob->cancelWithoutBlocking();
mJobMutex.unlock();

QgsTask::cancel();
}

bool QgsMapRendererTask::run()
{
QImage img;
Expand All @@ -73,14 +82,26 @@ bool QgsMapRendererTask::run()
if ( !destPainter )
return false;

QgsMapRendererCustomPainterJob r( mMapSettings, destPainter );
r.renderSynchronously();
mJobMutex.lock();
mJob.reset( new QgsMapRendererCustomPainterJob( mMapSettings, destPainter ) );
mJobMutex.unlock();
mJob->renderSynchronously();

mJobMutex.lock();
mJob.reset( nullptr );
mJobMutex.unlock();

if ( isCanceled() )
return false;

QgsRenderContext context = QgsRenderContext::fromMapSettings( mMapSettings );
context.setPainter( destPainter );

Q_FOREACH ( QgsAnnotation *annotation, mAnnotations )
{
if ( isCanceled() )
return false;

if ( !annotation || !annotation->isVisible() )
{
continue;
Expand Down
7 changes: 7 additions & 0 deletions src/core/qgsmaprenderertask.h
Expand Up @@ -24,8 +24,10 @@
#include "qgsannotationmanager.h"
#include "qgsmapsettings.h"
#include "qgstaskmanager.h"
#include "qgsmaprenderercustompainterjob.h"

#include <QPainter>
class QgsMapRendererCustomPainterJob;

/**
* \class QgsMapRendererTask
Expand Down Expand Up @@ -65,6 +67,8 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask
*/
void addAnnotations( QList< QgsAnnotation * > annotations );

void cancel() override;

signals:

/**
Expand All @@ -86,6 +90,9 @@ class CORE_EXPORT QgsMapRendererTask : public QgsTask

QgsMapSettings mMapSettings;

QMutex mJobMutex;
std::unique_ptr< QgsMapRendererCustomPainterJob > mJob;

QPainter *mPainter = nullptr;

QString mFileName;
Expand Down

0 comments on commit 4a2226a

Please sign in to comment.