Skip to content

Commit ddbb170

Browse files
authoredMay 7, 2017
Merge pull request #4510 from qgis/revert-4494-composer_async
Revert "Asyncronously render composer map previews "
2 parents 4160e19 + 40319df commit ddbb170

File tree

3 files changed

+31
-109
lines changed

3 files changed

+31
-109
lines changed
 

‎src/core/composer/qgscomposermap.cpp

Lines changed: 28 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,9 @@ QgsComposerMap::~QgsComposerMap()
115115
{
116116
delete mOverviewStack;
117117
delete mGridStack;
118-
119-
if ( mPainterJob )
120-
{
121-
disconnect( mPainterJob.get(), &QgsMapRendererCustomPainterJob::finished, this, &QgsComposerMap::painterJobFinished );
122-
mPainterJob->cancel();
123-
mPainter->end();
124-
}
125118
}
126119

127-
/* This function is called by paint() to render the map. It does not override any functions
120+
/* This function is called by paint() and cache() to render the map. It does not override any functions
128121
from QGraphicsItem. */
129122
void QgsComposerMap::draw( QPainter *painter, const QgsRectangle &extent, QSizeF size, double dpi, double *forceWidthScale )
130123
{
@@ -213,28 +206,12 @@ void QgsComposerMap::cache()
213206
return;
214207
}
215208

216-
if ( mPainterJob )
217-
{
218-
disconnect( mPainterJob.get(), &QgsMapRendererCustomPainterJob::finished, this, &QgsComposerMap::painterJobFinished );
219-
QgsMapRendererCustomPainterJob *oldJob = mPainterJob.release();
220-
QPainter *oldPainter = mPainter.release();
221-
QImage *oldImage = mCacheRenderingImage.release();
222-
connect( oldJob, &QgsMapRendererCustomPainterJob::finished, this, [oldPainter, oldJob, oldImage]
223-
{
224-
oldJob->deleteLater();
225-
delete oldPainter;
226-
delete oldImage;
227-
} );
228-
oldJob->cancelWithoutBlocking();
229-
}
230-
else
209+
if ( mDrawing )
231210
{
232-
mCacheRenderingImage.reset( nullptr );
211+
return;
233212
}
234213

235-
Q_ASSERT( !mPainterJob );
236-
Q_ASSERT( !mPainter );
237-
Q_ASSERT( !mCacheRenderingImage );
214+
mDrawing = true;
238215

239216
double horizontalVScaleFactor = horizontalViewScaleFactor();
240217
if ( horizontalVScaleFactor < 0 )
@@ -265,51 +242,38 @@ void QgsComposerMap::cache()
265242
}
266243
}
267244

268-
if ( w <= 0 || h <= 0 )
269-
return;
270-
271-
mCacheRenderingImage.reset( new QImage( w, h, QImage::Format_ARGB32 ) );
245+
mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
272246

273247
// set DPI of the image
274-
mCacheRenderingImage->setDotsPerMeterX( 1000 * w / widthMM );
275-
mCacheRenderingImage->setDotsPerMeterY( 1000 * h / heightMM );
248+
mCacheImage.setDotsPerMeterX( 1000 * w / widthMM );
249+
mCacheImage.setDotsPerMeterY( 1000 * h / heightMM );
276250

277251
if ( hasBackground() )
278252
{
279253
//Initially fill image with specified background color. This ensures that layers with blend modes will
280254
//preview correctly
281-
mCacheRenderingImage->fill( backgroundColor().rgba() );
255+
mCacheImage.fill( backgroundColor().rgba() );
282256
}
283257
else
284258
{
285259
//no background, but start with empty fill to avoid artifacts
286-
mCacheRenderingImage->fill( QColor( 255, 255, 255, 0 ).rgba() );
260+
mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
287261
}
288262

289-
mPainter.reset( new QPainter( mCacheRenderingImage.get() ) );
290-
QgsMapSettings settings( mapSettings( ext, QSizeF( w, h ), mCacheRenderingImage->logicalDpiX() ) );
291-
mPainterJob.reset( new QgsMapRendererCustomPainterJob( settings, mPainter.get() ) );
292-
connect( mPainterJob.get(), &QgsMapRendererCustomPainterJob::finished, this, &QgsComposerMap::painterJobFinished );
293-
mPainterJob->start();
294-
}
263+
QPainter p( &mCacheImage );
295264

296-
void QgsComposerMap::painterJobFinished()
297-
{
298-
mPainter->end();
299-
mPainterJob.reset( nullptr );
300-
mPainter.reset( nullptr );
265+
draw( &p, ext, QSizeF( w, h ), mCacheImage.logicalDpiX() );
266+
p.end();
301267
mCacheUpdated = true;
302-
mCacheFinalImage = std::move( mCacheRenderingImage );
303-
mLastRenderedImageOffsetX = 0;
304-
mLastRenderedImageOffsetY = 0;
305-
updateItem();
268+
269+
mDrawing = false;
306270
}
307271

308272
void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *pWidget )
309273
{
310274
Q_UNUSED( pWidget );
311275

312-
if ( !mComposition || !painter || !painter->device() )
276+
if ( !mComposition || !painter )
313277
{
314278
return;
315279
}
@@ -319,9 +283,6 @@ void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *,
319283
}
320284

321285
QRectF thisPaintRect = QRectF( 0, 0, QGraphicsRectItem::rect().width(), QGraphicsRectItem::rect().height() );
322-
if ( thisPaintRect.width() == 0 || thisPaintRect.height() == 0 )
323-
return;
324-
325286
painter->save();
326287
painter->setClipRect( thisPaintRect );
327288

@@ -336,40 +297,22 @@ void QgsComposerMap::paint( QPainter *painter, const QStyleOptionGraphicsItem *,
336297
}
337298
else if ( mComposition->plotStyle() == QgsComposition::Preview )
338299
{
339-
if ( !mCacheFinalImage || mCacheFinalImage->isNull() )
340-
{
341-
// No initial render available - so draw some preview text alerting user
342-
drawBackground( painter );
343-
painter->setBrush( QBrush( QColor( 125, 125, 125, 125 ) ) );
344-
painter->drawRect( thisPaintRect );
345-
painter->setBrush( Qt::NoBrush );
346-
QFont messageFont;
347-
messageFont.setPointSize( 12 );
348-
painter->setFont( messageFont );
349-
painter->setPen( QColor( 255, 255, 255, 255 ) );
350-
painter->drawText( thisPaintRect, Qt::AlignCenter | Qt::AlignHCenter, tr( "Rendering map" ) );
351-
if ( !mPainterJob )
352-
{
353-
// this is the map's very first paint - trigger a cache update
354-
cache();
355-
}
356-
}
357-
else
358-
{
359-
//Background color is already included in cached image, so no need to draw
300+
if ( mCacheImage.isNull() )
301+
cache();
360302

361-
double imagePixelWidth = mCacheFinalImage->width(); //how many pixels of the image are for the map extent?
362-
double scale = rect().width() / imagePixelWidth;
303+
//Background color is already included in cached image, so no need to draw
363304

364-
painter->save();
305+
double imagePixelWidth = mCacheImage.width(); //how many pixels of the image are for the map extent?
306+
double scale = rect().width() / imagePixelWidth;
365307

366-
painter->translate( mLastRenderedImageOffsetX + mXOffset, mLastRenderedImageOffsetY + mYOffset );
367-
painter->scale( scale, scale );
368-
painter->drawImage( 0, 0, *mCacheFinalImage );
308+
painter->save();
369309

370-
//restore rotation
371-
painter->restore();
372-
}
310+
painter->translate( mXOffset, mYOffset );
311+
painter->scale( scale, scale );
312+
painter->drawImage( 0, 0, mCacheImage );
313+
314+
//restore rotation
315+
painter->restore();
373316
}
374317
else if ( mComposition->plotStyle() == QgsComposition::Print ||
375318
mComposition->plotStyle() == QgsComposition::Postscript )
@@ -622,8 +565,6 @@ void QgsComposerMap::resize( double dx, double dy )
622565

623566
void QgsComposerMap::moveContent( double dx, double dy )
624567
{
625-
mLastRenderedImageOffsetX -= dx;
626-
mLastRenderedImageOffsetY -= dy;
627568
if ( !mDrawing )
628569
{
629570
transformShift( dx, dy );
@@ -732,7 +673,7 @@ void QgsComposerMap::setSceneRect( const QRectF &rectangle )
732673
mCacheUpdated = false;
733674

734675
updateBoundingRect();
735-
updateItem();
676+
update();
736677
emit itemChanged();
737678
emit extentChanged();
738679
}

‎src/core/composer/qgscomposermap.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ class QgsFillSymbol;
4343
class QgsLineSymbol;
4444
class QgsVectorLayer;
4545
class QgsAnnotation;
46-
class QgsMapRendererCustomPainterJob;
4746

4847
/** \ingroup core
4948
* \class QgsComposerMap
@@ -492,8 +491,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
492491
private slots:
493492
void layersAboutToBeRemoved( QList<QgsMapLayer *> layers );
494493

495-
void painterJobFinished();
496-
497494
private:
498495

499496
//! Unique identifier
@@ -516,15 +513,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
516513
// to manually tweak each atlas preview page without affecting the actual original map extent.
517514
QgsRectangle mAtlasFeatureExtent;
518515

519-
// We have two images used for rendering/storing cached map images.
520-
// the first (mCacheFinalImage) is used ONLY for storing the most recent completed map render. It's always
521-
// used when drawing map item previews. The second (mCacheRenderingImage) is used temporarily while
522-
// rendering a new preview image in the background. If (and only if) the background render completes, then
523-
// mCacheRenderingImage is pushed into mCacheFinalImage, and used from then on when drawing the item preview.
524-
// This ensures that something is always shown in the map item, even while refreshing the preview image in the
525-
// background
526-
std::unique_ptr< QImage > mCacheFinalImage;
527-
std::unique_ptr< QImage > mCacheRenderingImage;
516+
// Cache used in composer preview
517+
QImage mCacheImage;
528518

529519
// Is cache up to date
530520
bool mCacheUpdated = false;
@@ -543,9 +533,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
543533
//! Offset in y direction for showing map cache image
544534
double mYOffset = 0.0;
545535

546-
double mLastRenderedImageOffsetX = 0.0;
547-
double mLastRenderedImageOffsetY = 0.0;
548-
549536
//! Map rotation
550537
double mMapRotation = 0;
551538

@@ -600,10 +587,6 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
600587
//! Margin size for atlas driven extents (percentage of feature size) - when in auto scaling mode
601588
double mAtlasMargin = 0.10;
602589

603-
std::unique_ptr< QPainter > mPainter;
604-
std::unique_ptr< QgsMapRendererCustomPainterJob > mPainterJob;
605-
bool mPainterCancelWait = false;
606-
607590
void init();
608591

609592
//! Resets the item tooltip to reflect current map id

‎tests/src/python/test_qgspallabeling_composer.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import os
2424
import subprocess
2525

26-
from qgis.PyQt.QtCore import QRect, QRectF, QSize, QSizeF, qDebug, QThreadPool
26+
from qgis.PyQt.QtCore import QRect, QRectF, QSize, QSizeF, qDebug
2727
from qgis.PyQt.QtGui import QImage, QColor, QPainter
2828
from qgis.PyQt.QtPrintSupport import QPrinter
2929
from qgis.PyQt.QtSvg import QSvgRenderer, QSvgGenerator
@@ -88,8 +88,6 @@ def tearDownClass(cls):
8888
TestQgsPalLabeling.tearDownClass()
8989
cls.removeMapLayer(cls.layer)
9090
cls.layer = None
91-
# avoid crash on finish, probably related to https://bugreports.qt.io/browse/QTBUG-35760
92-
QThreadPool.globalInstance().waitForDone()
9391

9492
def setUp(self):
9593
"""Run before each test."""

0 commit comments

Comments
 (0)
Please sign in to comment.