Skip to content
Permalink
Browse files
Allow canceling background map renders
  • Loading branch information
nyalldawson committed Apr 23, 2017
1 parent 1659659 commit 4a2226af31f7f932ae65dee352a0cb40fb086d41
Showing with 34 additions and 3 deletions.
  1. +3 −0 python/core/qgsmaprenderertask.sip
  2. +24 −3 src/core/qgsmaprenderertask.cpp
  3. +7 −0 src/core/qgsmaprenderertask.h
@@ -47,6 +47,9 @@ class QgsMapRendererTask : QgsTask
Adds ``annotations`` to be rendered on the map.
%End

virtual void cancel();


signals:

void renderingComplete();
@@ -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 )
@@ -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;
@@ -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;
@@ -24,8 +24,10 @@
#include "qgsannotationmanager.h"
#include "qgsmapsettings.h"
#include "qgstaskmanager.h"
#include "qgsmaprenderercustompainterjob.h"

#include <QPainter>
class QgsMapRendererCustomPainterJob;

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

void cancel() override;

signals:

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

QgsMapSettings mMapSettings;

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

QPainter *mPainter = nullptr;

QString mFileName;

0 comments on commit 4a2226a

Please sign in to comment.