Skip to content

Commit

Permalink
Asyncronously render composer map previews
Browse files Browse the repository at this point in the history
(Forward port from Sourcepole fork)
  • Loading branch information
manisandro authored and nyalldawson committed May 6, 2017
1 parent 8d03642 commit 855e1f6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
37 changes: 29 additions & 8 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,12 +206,20 @@ void QgsComposerMap::cache()
return;
}

if ( mDrawing )
if ( mPainterJob )
{
return;
if ( mPainterCancelWait )
{
return; // Already waiting
}
QgsDebugMsg( "Aborting composer painter job" );
mPainterCancelWait = true;
mPainterJob->cancel();
mPainterCancelWait = false;
}

mDrawing = true;
Q_ASSERT( !mPainterJob );
Q_ASSERT( !mPainter );

double horizontalVScaleFactor = horizontalViewScaleFactor();
if ( horizontalVScaleFactor < 0 )
Expand Down Expand Up @@ -260,13 +268,24 @@ void QgsComposerMap::cache()
mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
}

QPainter p( &mCacheImage );
mPainter = new QPainter( &mCacheImage );
QgsMapSettings settings( mapSettings( ext, QSizeF( w, h ), mCacheImage.logicalDpiX() ) );
mPainterJob = new QgsMapRendererCustomPainterJob( settings, mPainter );
connect( mPainterJob, SIGNAL( finished() ), this, SLOT( painterJobFinished() ) );
QgsDebugMsg( "Starting new composer painter job" );
mPainterJob->start();
}

draw( &p, ext, QSizeF( w, h ), mCacheImage.logicalDpiX() );
p.end();
void QgsComposerMap::painterJobFinished()
{
QgsDebugMsg( "Finished composer painter job" );
mPainter->end();
delete mPainterJob;
mPainterJob = nullptr;
delete mPainter;
mPainter = nullptr;
mCacheUpdated = true;

mDrawing = false;
updateItem();
}

void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *pWidget )
Expand Down Expand Up @@ -1031,8 +1050,10 @@ void QgsComposerMap::updateItem()

if ( mPreviewMode != QgsComposerMap::Rectangle && !mCacheUpdated )
{
QgsDebugMsg( "Requesting new cache image item" );
cache();
}
QgsDebugMsg( "Updating item" );
QgsComposerItem::updateItem();
}

Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposermap.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class QgsFillSymbol;
class QgsLineSymbol;
class QgsVectorLayer;
class QgsAnnotation;
class QgsMapRendererCustomPainterJob;

/** \ingroup core
* \class QgsComposerMap
Expand Down Expand Up @@ -491,6 +492,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
private slots:
void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );

void painterJobFinished();

private:

//! Unique identifier
Expand Down Expand Up @@ -587,6 +590,10 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
//! Margin size for atlas driven extents (percentage of feature size) - when in auto scaling mode
double mAtlasMargin = 0.10;

QPainter *mPainter = nullptr;
QgsMapRendererCustomPainterJob *mPainterJob = nullptr;
bool mPainterCancelWait = false;

void init();

//! Resets the item tooltip to reflect current map id
Expand Down

0 comments on commit 855e1f6

Please sign in to comment.