QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsmaprenderersequentialjob.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaprenderersequentialjob.cpp
3  --------------------------------------
4  Date : December 2013
5  Copyright : (C) 2013 by Martin Dobias
6  Email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
17 
18 #include "qgslogger.h"
20 #include "qgspallabeling.h"
21 #include "qgslabelingresults.h"
22 #include "qgsrendereditemresults.h"
23 
25  : QgsMapRendererQImageJob( settings )
26 
27 {
28  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL construct" ), 5 );
29 
30  mImage = QImage( mSettings.deviceOutputSize(), mSettings.outputImageFormat() );
31  mImage.setDevicePixelRatio( mSettings.devicePixelRatio() );
32  mImage.setDotsPerMeterX( 1000 * settings.outputDpi() / 25.4 );
33  mImage.setDotsPerMeterY( 1000 * settings.outputDpi() / 25.4 );
34  mImage.fill( Qt::transparent );
35 }
36 
38 {
39  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct" ), 5 );
40  if ( isActive() )
41  {
42  // still running!
43  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct -- still running! (canceling)" ), 5 );
44  cancel();
45  }
46 
47  Q_ASSERT( !mInternalJob && !mPainter );
48 }
49 
50 
51 void QgsMapRendererSequentialJob::startPrivate()
52 {
53  if ( isActive() )
54  return; // do nothing if we are already running
55 
56  mLabelingResults.reset();
57 
58  mRenderingStart.start();
59 
60  mErrors.clear();
61 
62  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL START" ), 5 );
63 
64  Q_ASSERT( !mInternalJob && !mPainter );
65 
66  mPainter = new QPainter( &mImage );
67 
68  mInternalJob = new QgsMapRendererCustomPainterJob( mSettings, mPainter );
69  mInternalJob->setLabelSink( labelSink() );
70  mInternalJob->setCache( mCache );
71 
75 
76  mInternalJob->start();
77 }
78 
79 
81 {
82  if ( !isActive() )
83  return;
84 
85  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
86  mInternalJob->cancel();
87 
88  Q_ASSERT( !mInternalJob && !mPainter );
89 }
90 
92 {
93  if ( !isActive() )
94  return;
95 
96  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
97  mInternalJob->cancelWithoutBlocking();
98 }
99 
101 {
102  if ( !isActive() )
103  return;
104 
105  mInternalJob->waitForFinished();
106 }
107 
109 {
110  return nullptr != mInternalJob;
111 }
112 
114 {
115  return mUsedCachedLabels;
116 }
117 
119 {
120  return mLabelingResults.release();
121 }
122 
123 
125 {
126  if ( isActive() && mCache )
127  // this will allow immediate display of cached layers and at the same time updates of the layer being rendered
128  return composeImage( mSettings, mInternalJob->jobs(), LabelRenderJob() );
129  else
130  return mImage;
131 }
132 
133 
135 {
136  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL finished" ), 5 );
137 
138  mPainter->end();
139  delete mPainter;
140  mPainter = nullptr;
141 
142  mLabelingResults.reset( mInternalJob->takeLabelingResults() );
143  mUsedCachedLabels = mInternalJob->usedCachedLabels();
145 
146  mRenderedItemResults.reset( mInternalJob->takeRenderedItemResults() );
147 
148  mErrors = mInternalJob->errors();
149 
150  // now we are in a slot called from mInternalJob - do not delete it immediately
151  // so the class is still valid when the execution returns to the class
152  mInternalJob->deleteLater();
153  mInternalJob = nullptr;
154 
155  mRenderingTime = mRenderingStart.elapsed();
156 
157  emit finished();
158 }
QgsMapRendererJob::mLayersRedrawnFromCache
QStringList mLayersRedrawnFromCache
Definition: qgsmaprendererjob.h:517
QgsMapRendererSequentialJob::cancel
void cancel() override
Stop the rendering job - does not return until the job has terminated.
Definition: qgsmaprenderersequentialjob.cpp:80
QgsMapRendererJob::layerRenderingStarted
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
qgspallabeling.h
QgsMapRendererJob::mCache
QgsMapRendererCache * mCache
Definition: qgsmaprendererjob.h:494
QgsMapRendererJob::composeImage
static QImage composeImage(const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache=nullptr)
QgsMapRendererCustomPainterJob::waitForFinished
void waitForFinished() override
Block until the job has finished.
Definition: qgsmaprenderercustompainterjob.cpp:187
QgsMapRendererCustomPainterJob::jobs
const std::vector< LayerRenderJob > & jobs() const
Definition: qgsmaprenderercustompainterjob.h:78
QgsMapRendererJob::mErrors
Errors mErrors
Definition: qgsmaprendererjob.h:492
QgsMapRendererSequentialJob::cancelWithoutBlocking
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
Definition: qgsmaprenderersequentialjob.cpp:91
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob
~QgsMapRendererSequentialJob() override
Definition: qgsmaprenderersequentialjob.cpp:37
QgsMapSettings::devicePixelRatio
float devicePixelRatio() const
Returns the device pixel ratio.
Definition: qgsmapsettings.cpp:251
QgsLabelingResults
Class that stores computed placement from labeling engine.
Definition: qgslabelingresults.h:32
QgsMapRendererJob::mRenderedItemResults
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Definition: qgsmaprendererjob.h:514
QgsMapRendererJob::mRenderingStart
QElapsedTimer mRenderingStart
Definition: qgsmaprendererjob.h:491
qgsmaprenderercustompainterjob.h
QgsMapSettings::outputImageFormat
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
Definition: qgsmapsettings.h:444
QgsMapRendererCustomPainterJob::cancel
void cancel() override
Stop the rendering job - does not return until the job has terminated.
Definition: qgsmaprenderercustompainterjob.cpp:146
QgsMapRendererJob::layerRendered
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
QgsMapRendererCustomPainterJob::takeLabelingResults
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
Definition: qgsmaprenderercustompainterjob.cpp:214
qgslabelingresults.h
QgsMapRendererJob::errors
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
QgsMapRendererJob::mRenderingTime
int mRenderingTime
Definition: qgsmaprendererjob.h:496
QgsMapRendererCustomPainterJob::cancelWithoutBlocking
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
Definition: qgsmaprenderercustompainterjob.cpp:170
QgsMapRendererJob::start
void start()
Start the rendering job and immediately return.
QgsMapRendererCustomPainterJob::usedCachedLabels
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
Definition: qgsmaprenderercustompainterjob.cpp:209
QgsMapRendererSequentialJob::takeLabelingResults
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
Definition: qgsmaprenderersequentialjob.cpp:118
QgsMapRendererJob::setCache
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
QgsMapRendererJob::labelSink
QgsLabelSink * labelSink() const
Returns the label sink associated to this rendering job.
Definition: qgsmaprendererjob.h:382
QgsMapRendererJob::takeRenderedItemResults
QgsRenderedItemResults * takeRenderedItemResults()
Takes the rendered item results from the map render job and returns them.
QgsMapRendererSequentialJob::usedCachedLabels
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
Definition: qgsmaprenderersequentialjob.cpp:113
qgsmaprenderersequentialjob.h
QgsMapRendererCustomPainterJob
Job implementation that renders everything sequentially using a custom painter.
Definition: qgsmaprenderercustompainterjob.h:63
QgsMapSettings::deviceOutputSize
QSize deviceOutputSize() const
Returns the device output size of the map render.
Definition: qgsmapsettings.cpp:262
QgsMapRendererQImageJob
Intermediate base class adding functionality that allows client to query the rendered image.
Definition: qgsmaprendererjob.h:653
QgsMapRendererSequentialJob::QgsMapRendererSequentialJob
QgsMapRendererSequentialJob(const QgsMapSettings &settings)
Definition: qgsmaprenderersequentialjob.cpp:24
QgsMapRendererSequentialJob::waitForFinished
void waitForFinished() override
Block until the job has finished.
Definition: qgsmaprenderersequentialjob.cpp:100
QgsMapRendererJob::layersRedrawnFromCache
QStringList layersRedrawnFromCache() const
Returns a list of the layer IDs for all layers which were redrawn from cached images.
QgsMapRendererJob::mSettings
QgsMapSettings mSettings
Definition: qgsmaprendererjob.h:490
QgsMapRendererSequentialJob::isActive
bool isActive() const override
Tell whether the rendering job is currently running in background.
Definition: qgsmaprenderersequentialjob.cpp:108
QgsMapRendererSequentialJob::internalFinished
void internalFinished()
Definition: qgsmaprenderersequentialjob.cpp:134
qgslogger.h
QgsMapSettings::outputDpi
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
Definition: qgsmapsettings.cpp:267
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
qgsrendereditemresults.h
QgsMapRendererSequentialJob::renderedImage
QImage renderedImage() override
Gets a preview/resulting image.
Definition: qgsmaprenderersequentialjob.cpp:124
QgsMapRendererJob::finished
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapRendererJob::setLabelSink
void setLabelSink(QgsLabelSink *sink)
Assigns the label sink which will take over responsibility for handling labels during the rendering j...
Definition: qgsmaprendererjob.h:391