24#include <QFutureWatcher>
27#include <QtConcurrentRun>
29#include "moc_qgspicturerasterizer.cpp"
36QgsPictureRasterizerJob::QgsPictureRasterizerJob(
const QPicture &picture,
const QSize &targetSize )
37 : mTargetSize( targetSize )
40 connect( &mWatcher, &QFutureWatcher< QImage >::finished,
this, &QgsPictureRasterizerJob::finished );
43 mWatcher.setFuture( QtConcurrent::run( [picture, targetSize, feedback]() -> QImage {
44 if ( picture.isNull() || ( feedback && feedback->
isCanceled() ) )
47 const QRectF bounds = QRectF( picture.boundingRect() );
48 if ( bounds.isEmpty() )
51 const QSize outputSize = !targetSize.isEmpty() ? targetSize : bounds.size().toSize();
52 if ( outputSize.isEmpty() )
55 QImage image( outputSize, QImage::Format_ARGB32_Premultiplied );
56 image.fill( Qt::transparent );
60 QPainter painter( &image );
61 painter.setRenderHint( QPainter::Antialiasing,
true );
62 painter.setRenderHint( QPainter::TextAntialiasing,
true );
63 painter.setRenderHint( QPainter::SmoothPixmapTransform,
true );
65 painter.scale( outputSize.width() / bounds.width(), outputSize.height() / bounds.height() );
66 painter.translate( -bounds.topLeft() );
77void QgsPictureRasterizerJob::cancelWithoutBlocking()
82void QgsPictureRasterizerJob::cancelAndWait()
85 mWatcher.waitForFinished();
88bool QgsPictureRasterizerJob::isCanceled()
const
90 return mFeedback->isCanceled();
101 mUpdateTimer =
new QTimer(
this );
102 mUpdateTimer->setSingleShot(
true );
103 connect( mUpdateTimer, &QTimer::timeout,
this, &QgsPictureRasterizer::startRenderJob );
108 mUpdateTimer->stop();
109 cancelRunningJob(
true );
118 cancelRunningJob(
false );
120 if ( mPicture.isNull() || mPicture.boundingRect().isEmpty() )
122 mUpdateTimer->stop();
123 mCachedImage = QImage();
124 mInvalidated =
false;
130 mUpdateTimer->stop();
131 cancelRunningJob(
false );
133 mPicture = QPicture();
134 mCachedImage = QImage();
135 mTargetSize = QSize();
136 mInvalidated =
false;
142 if ( !mPicture.isNull() && !mPicture.boundingRect().isEmpty() && !mTargetSize.isEmpty() )
144 mUpdateTimer->start( 1 );
150 if ( targetSize.isEmpty() || mPicture.isNull() || mPicture.boundingRect().isEmpty() )
154 QSize size = targetSize;
155 if ( mMaxCacheSize > 0 && ( targetSize.width() > mMaxCacheSize || targetSize.height() > mMaxCacheSize ) )
157 size = size.scaled( mMaxCacheSize, mMaxCacheSize, Qt::KeepAspectRatio );
160 if ( !mCachedImage.isNull() && !mInvalidated && mCachedImage.size() == size )
165 const bool jobMatchesTarget = mJob && !mInvalidated && mJob->targetSize() == size;
166 const bool timerMatchesTarget = mUpdateTimer->isActive() && !mInvalidated && mTargetSize == size;
168 if ( !jobMatchesTarget && !timerMatchesTarget )
171 mUpdateTimer->start( 1 );
179 return static_cast< bool >( mJob ) || mUpdateTimer->isActive();
184 if ( mMaxCacheSize == maxSize )
187 mMaxCacheSize = maxSize;
191void QgsPictureRasterizer::startRenderJob()
193 cancelRunningJob(
false );
195 if ( mPicture.isNull() || mPicture.boundingRect().isEmpty() || mTargetSize.isEmpty() )
198 mInvalidated =
false;
200 mJob = std::make_unique< QgsPictureRasterizerJob >( pictureCopy, mTargetSize );
201 connect( mJob.get(), &QgsPictureRasterizerJob::finished,
this, &QgsPictureRasterizer::onRenderJobFinished );
204void QgsPictureRasterizer::onRenderJobFinished()
209 if ( !mJob->isCanceled() )
211 const QImage result = mJob->result();
212 if ( !result.isNull() )
214 mCachedImage = result;
222void QgsPictureRasterizer::cancelRunningJob(
bool block )
227 disconnect( mJob.get(), &QgsPictureRasterizerJob::finished,
this, &QgsPictureRasterizer::onRenderJobFinished );
230 mJob->cancelAndWait();
235 QgsPictureRasterizerJob *oldJob = mJob.release();
236 connect( oldJob, &QgsPictureRasterizerJob::finished, oldJob, &QObject::deleteLater );
237 oldJob->cancelWithoutBlocking();
Base class for feedback objects to be used for cancellation of something running in a worker thread.
bool isCanceled() const
Tells whether the operation has been canceled already.
void cancel()
Tells the internal routines that the current operation should be canceled. This should be run by the ...
static QPicture deepCopyQPicture(const QPicture &picture)
Deep copies a QPicture, making a copy which is safe to use in a different thread.
static void drawPicture(QPainter *painter, const QPointF &point, const QPicture &picture)
Draws a picture onto a painter, correctly applying workarounds to avoid issues with incorrect scaling...
void clear()
Clears the source picture and cached image, canceling any in-progress background render.
void setPicture(const QPicture &picture)
Sets the source picture to cache.
QgsPictureRasterizer(QObject *parent=nullptr)
Constructor for QgsPictureRasterizer, with the specified parent object.
bool isRendering() const
Returns true if a background job is currently running or scheduled.
void invalidate()
Immediately invalidates the current cached image.
void rendered()
Emitted when a background rasterization finishes and a new cached image is available.
void setMaxCacheSize(int maxSize)
Sets the maximum width or height (in pixels) of the cached QImage.
QImage image(const QSize &targetSize)
Immediately returns the best available QImage for the given targetSize.
QPicture picture() const
Returns the current source QPicture.
~QgsPictureRasterizer() override