QGIS API Documentation 4.3.0-Master (0cef2469b72)
Loading...
Searching...
No Matches
qgspicturerasterizer.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspicturerasterizer.cpp
3 -------------------
4 begin : November 2026
5 copyright : (C) 2026 by Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19
20#include "qgis.h"
21#include "qgsfeedback.h"
22#include "qgspainting.h"
23
24#include <QFutureWatcher>
25#include <QPainter>
26#include <QTimer>
27#include <QtConcurrentRun>
28
29#include "moc_qgspicturerasterizer.cpp"
30
31//
32// QgsPictureRasterizerJob
33//
34
36QgsPictureRasterizerJob::QgsPictureRasterizerJob( const QPicture &picture, const QSize &targetSize )
37 : mTargetSize( targetSize )
38 , mFeedback( std::make_unique< QgsFeedback >() )
39{
40 connect( &mWatcher, &QFutureWatcher< QImage >::finished, this, &QgsPictureRasterizerJob::finished );
41
42 QgsFeedback *feedback = mFeedback.get();
43 mWatcher.setFuture( QtConcurrent::run( [picture, targetSize, feedback]() -> QImage {
44 if ( picture.isNull() || ( feedback && feedback->isCanceled() ) )
45 return QImage();
46
47 const QRectF bounds = QRectF( picture.boundingRect() );
48 if ( bounds.isEmpty() )
49 return QImage();
50
51 const QSize outputSize = !targetSize.isEmpty() ? targetSize : bounds.size().toSize();
52 if ( outputSize.isEmpty() )
53 return QImage();
54
55 QImage image( outputSize, QImage::Format_ARGB32_Premultiplied );
56 image.fill( Qt::transparent );
57 if ( feedback && feedback->isCanceled() )
58 return QImage();
59
60 QPainter painter( &image );
61 painter.setRenderHint( QPainter::Antialiasing, true );
62 painter.setRenderHint( QPainter::TextAntialiasing, true );
63 painter.setRenderHint( QPainter::SmoothPixmapTransform, true );
64
65 painter.scale( outputSize.width() / bounds.width(), outputSize.height() / bounds.height() );
66 painter.translate( -bounds.topLeft() );
67 QgsPainting::drawPicture( &painter, QPointF( 0, 0 ), picture );
68 painter.end();
69
70 if ( feedback && feedback->isCanceled() )
71 return QImage();
72
73 return image;
74 } ) );
75}
76
77void QgsPictureRasterizerJob::cancelWithoutBlocking()
78{
79 mFeedback->cancel();
80}
81
82void QgsPictureRasterizerJob::cancelAndWait()
83{
84 mFeedback->cancel();
85 mWatcher.waitForFinished();
86}
87
88bool QgsPictureRasterizerJob::isCanceled() const
89{
90 return mFeedback->isCanceled();
91}
93
94//
95// QgsPictureRasterizer
96//
97
99 : QObject( parent )
100{
101 mUpdateTimer = new QTimer( this );
102 mUpdateTimer->setSingleShot( true );
103 connect( mUpdateTimer, &QTimer::timeout, this, &QgsPictureRasterizer::startRenderJob );
104}
105
107{
108 mUpdateTimer->stop();
109 cancelRunningJob( true );
110}
111
113{
115
116 mInvalidated = true;
117
118 cancelRunningJob( false );
119
120 if ( mPicture.isNull() || mPicture.boundingRect().isEmpty() )
121 {
122 mUpdateTimer->stop();
123 mCachedImage = QImage();
124 mInvalidated = false;
125 }
126}
127
129{
130 mUpdateTimer->stop();
131 cancelRunningJob( false );
132
133 mPicture = QPicture();
134 mCachedImage = QImage();
135 mTargetSize = QSize();
136 mInvalidated = false;
137}
138
140{
141 mInvalidated = true;
142 if ( !mPicture.isNull() && !mPicture.boundingRect().isEmpty() && !mTargetSize.isEmpty() )
143 {
144 mUpdateTimer->start( 1 );
145 }
146}
147
148QImage QgsPictureRasterizer::image( const QSize &targetSize )
149{
150 if ( targetSize.isEmpty() || mPicture.isNull() || mPicture.boundingRect().isEmpty() )
151 return QImage();
152
153 // force size to match picture aspect ratio
154 QSize size = targetSize;
155 if ( mMaxCacheSize > 0 && ( targetSize.width() > mMaxCacheSize || targetSize.height() > mMaxCacheSize ) )
156 {
157 size = size.scaled( mMaxCacheSize, mMaxCacheSize, Qt::KeepAspectRatio );
158 }
159
160 if ( !mCachedImage.isNull() && !mInvalidated && mCachedImage.size() == size )
161 {
162 return mCachedImage;
163 }
164
165 const bool jobMatchesTarget = mJob && !mInvalidated && mJob->targetSize() == size;
166 const bool timerMatchesTarget = mUpdateTimer->isActive() && !mInvalidated && mTargetSize == size;
167
168 if ( !jobMatchesTarget && !timerMatchesTarget )
169 {
170 mTargetSize = size;
171 mUpdateTimer->start( 1 );
172 }
173
174 return mCachedImage;
175}
176
178{
179 return static_cast< bool >( mJob ) || mUpdateTimer->isActive();
180}
181
183{
184 if ( mMaxCacheSize == maxSize )
185 return;
186
187 mMaxCacheSize = maxSize;
188 invalidate();
189}
190
191void QgsPictureRasterizer::startRenderJob()
192{
193 cancelRunningJob( false );
194
195 if ( mPicture.isNull() || mPicture.boundingRect().isEmpty() || mTargetSize.isEmpty() )
196 return;
197
198 mInvalidated = false;
199 const QPicture pictureCopy = QgsPainting::deepCopyQPicture( mPicture );
200 mJob = std::make_unique< QgsPictureRasterizerJob >( pictureCopy, mTargetSize );
201 connect( mJob.get(), &QgsPictureRasterizerJob::finished, this, &QgsPictureRasterizer::onRenderJobFinished );
202}
203
204void QgsPictureRasterizer::onRenderJobFinished()
205{
206 if ( !mJob )
207 return;
208
209 if ( !mJob->isCanceled() )
210 {
211 const QImage result = mJob->result();
212 if ( !result.isNull() )
213 {
214 mCachedImage = result;
215 }
216 }
217
218 mJob.reset();
219 emit rendered();
220}
221
222void QgsPictureRasterizer::cancelRunningJob( bool block )
223{
224 if ( !mJob )
225 return;
226
227 disconnect( mJob.get(), &QgsPictureRasterizerJob::finished, this, &QgsPictureRasterizer::onRenderJobFinished );
228 if ( block )
229 {
230 mJob->cancelAndWait();
231 mJob.reset();
232 }
233 else
234 {
235 QgsPictureRasterizerJob *oldJob = mJob.release();
236 connect( oldJob, &QgsPictureRasterizerJob::finished, oldJob, &QObject::deleteLater );
237 oldJob->cancelWithoutBlocking();
238 }
239}
Base class for feedback objects to be used for cancellation of something running in a worker thread.
Definition qgsfeedback.h:44
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
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.