QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
24  : QgsMapRendererQImageJob( settings )
25 
26 {
27  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL construct" ), 5 );
28 
29  mImage = QImage( mSettings.deviceOutputSize(), mSettings.outputImageFormat() );
30  mImage.setDevicePixelRatio( mSettings.devicePixelRatio() );
31  mImage.setDotsPerMeterX( mSettings.devicePixelRatio() * 1000 * settings.outputDpi() / 25.4 );
32  mImage.setDotsPerMeterY( mSettings.devicePixelRatio() * 1000 * settings.outputDpi() / 25.4 );
33  mImage.fill( Qt::transparent );
34 }
35 
37 {
38  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct" ), 5 );
39  if ( isActive() )
40  {
41  // still running!
42  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct -- still running! (canceling)" ), 5 );
43  cancel();
44  }
45 
46  Q_ASSERT( !mInternalJob && !mPainter );
47 }
48 
49 
50 void QgsMapRendererSequentialJob::startPrivate()
51 {
52  if ( isActive() )
53  return; // do nothing if we are already running
54 
55  mLabelingResults.reset();
56 
57  mRenderingStart.start();
58 
59  mErrors.clear();
60 
61  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL START" ), 5 );
62 
63  Q_ASSERT( !mInternalJob && !mPainter );
64 
65  mPainter = new QPainter( &mImage );
66 
67  mInternalJob = new QgsMapRendererCustomPainterJob( mSettings, mPainter );
68  mInternalJob->setCache( mCache );
69 
71 
72  mInternalJob->start();
73 }
74 
75 
77 {
78  if ( !isActive() )
79  return;
80 
81  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
82  mInternalJob->cancel();
83 
84  Q_ASSERT( !mInternalJob && !mPainter );
85 }
86 
88 {
89  if ( !isActive() )
90  return;
91 
92  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
93  mInternalJob->cancelWithoutBlocking();
94 }
95 
97 {
98  if ( !isActive() )
99  return;
100 
101  mInternalJob->waitForFinished();
102 }
103 
105 {
106  return nullptr != mInternalJob;
107 }
108 
110 {
111  return mUsedCachedLabels;
112 }
113 
115 {
116  return mLabelingResults.release();
117 }
118 
119 
121 {
122  if ( isActive() && mCache )
123  // this will allow immediate display of cached layers and at the same time updates of the layer being rendered
124  return composeImage( mSettings, mInternalJob->jobs(), LabelRenderJob() );
125  else
126  return mImage;
127 }
128 
129 
131 {
132  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL finished" ), 5 );
133 
134  mPainter->end();
135  delete mPainter;
136  mPainter = nullptr;
137 
138  mLabelingResults.reset( mInternalJob->takeLabelingResults() );
139  mUsedCachedLabels = mInternalJob->usedCachedLabels();
140 
141  mErrors = mInternalJob->errors();
142 
143  // now we are in a slot called from mInternalJob - do not delete it immediately
144  // so the class is still valid when the execution returns to the class
145  mInternalJob->deleteLater();
146  mInternalJob = nullptr;
147 
148  mRenderingTime = mRenderingStart.elapsed();
149 
150  emit finished();
151 }
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 LayerRenderJobs & 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.
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
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).
static QImage composeImage(const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache=nullptr)
QgsMapSettings mSettings
void start()
Start the rendering job and immediately return.
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