QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 }
Class that stores computed placement from labeling engine.
Job implementation that renders everything sequentially using a custom painter.
void cancel() override
Stop the rendering job - does not return until the job has terminated.
const std::vector< LayerRenderJob > & jobs() const
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
void waitForFinished() override
Block until the job has finished.
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
static QImage composeImage(const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache=nullptr)
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
void setLabelSink(QgsLabelSink *sink)
Assigns the label sink which will take over responsibility for handling labels during the rendering j...
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
QElapsedTimer mRenderingStart
QgsMapRendererCache * mCache
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
QgsLabelSink * labelSink() const
Returns the label sink associated to this rendering job.
void start()
Start the rendering job and immediately return.
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
QStringList mLayersRedrawnFromCache
QStringList layersRedrawnFromCache() const
Returns a list of the layer IDs for all layers which were redrawn from cached images.
QgsRenderedItemResults * takeRenderedItemResults()
Takes the rendered item results from the map render job and returns them.
Intermediate base class adding functionality that allows client to query the rendered image.
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
QImage renderedImage() override
Gets a preview/resulting image.
void waitForFinished() override
Block until the job has finished.
void cancel() override
Stop the rendering job - does not return until the job has terminated.
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
QgsMapRendererSequentialJob(const QgsMapSettings &settings)
bool isActive() const override
Tell whether the rendering job is currently running in background.
The QgsMapSettings class contains configuration for rendering of the map.
QSize deviceOutputSize() const
Returns the device output size of the map render.
float devicePixelRatio() const
Returns the device pixel ratio.
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
double outputDpi() const
Returns the DPI (dots per inch) used for conversion between real world units (e.g.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39