QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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( mSettings.devicePixelRatio() * 1000 * settings.outputDpi() / 25.4 );
33  mImage.setDotsPerMeterY( mSettings.devicePixelRatio() * 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->setCache( mCache );
70 
72 
73  mInternalJob->start();
74 }
75 
76 
78 {
79  if ( !isActive() )
80  return;
81 
82  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
83  mInternalJob->cancel();
84 
85  Q_ASSERT( !mInternalJob && !mPainter );
86 }
87 
89 {
90  if ( !isActive() )
91  return;
92 
93  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
94  mInternalJob->cancelWithoutBlocking();
95 }
96 
98 {
99  if ( !isActive() )
100  return;
101 
102  mInternalJob->waitForFinished();
103 }
104 
106 {
107  return nullptr != mInternalJob;
108 }
109 
111 {
112  return mUsedCachedLabels;
113 }
114 
116 {
117  return mLabelingResults.release();
118 }
119 
120 
122 {
123  if ( isActive() && mCache )
124  // this will allow immediate display of cached layers and at the same time updates of the layer being rendered
125  return composeImage( mSettings, mInternalJob->jobs(), LabelRenderJob() );
126  else
127  return mImage;
128 }
129 
130 
132 {
133  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL finished" ), 5 );
134 
135  mPainter->end();
136  delete mPainter;
137  mPainter = nullptr;
138 
139  mLabelingResults.reset( mInternalJob->takeLabelingResults() );
140  mUsedCachedLabels = mInternalJob->usedCachedLabels();
142 
143  mRenderedItemResults.reset( mInternalJob->takeRenderedItemResults() );
144 
145  mErrors = mInternalJob->errors();
146 
147  // now we are in a slot called from mInternalJob - do not delete it immediately
148  // so the class is still valid when the execution returns to the class
149  mInternalJob->deleteLater();
150  mInternalJob = nullptr;
151 
152  mRenderingTime = mRenderingStart.elapsed();
153 
154  emit finished();
155 }
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.
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
QElapsedTimer mRenderingStart
QgsMapRendererCache * mCache
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
void start()
Start the rendering job and immediately return.
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