QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
23  : QgsMapRendererQImageJob( settings )
24 
25 {
26  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL construct" ), 5 );
27 
28  mImage = QImage( mSettings.deviceOutputSize(), mSettings.outputImageFormat() );
29  mImage.setDevicePixelRatio( mSettings.devicePixelRatio() );
30  mImage.setDotsPerMeterX( mSettings.devicePixelRatio() * 1000 * settings.outputDpi() / 25.4 );
31  mImage.setDotsPerMeterY( mSettings.devicePixelRatio() * 1000 * settings.outputDpi() / 25.4 );
32  mImage.fill( Qt::transparent );
33 }
34 
36 {
37  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct" ), 5 );
38  if ( isActive() )
39  {
40  // still running!
41  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL destruct -- still running! (canceling)" ), 5 );
42  cancel();
43  }
44 
45  Q_ASSERT( !mInternalJob && !mPainter );
46 }
47 
48 
50 {
51  if ( isActive() )
52  return; // do nothing if we are already running
53 
54  mLabelingResults.reset();
55 
56  mRenderingStart.start();
57 
58  mErrors.clear();
59 
60  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL START" ), 5 );
61 
62  Q_ASSERT( !mInternalJob && !mPainter );
63 
64  mPainter = new QPainter( &mImage );
65 
66  mInternalJob = new QgsMapRendererCustomPainterJob( mSettings, mPainter );
67  mInternalJob->setCache( mCache );
68 
70 
71  mInternalJob->start();
72 }
73 
74 
76 {
77  if ( !isActive() )
78  return;
79 
80  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
81  mInternalJob->cancel();
82 
83  Q_ASSERT( !mInternalJob && !mPainter );
84 }
85 
87 {
88  if ( !isActive() )
89  return;
90 
91  QgsDebugMsgLevel( QStringLiteral( "sequential - cancel internal" ), 5 );
92  mInternalJob->cancelWithoutBlocking();
93 }
94 
96 {
97  if ( !isActive() )
98  return;
99 
100  mInternalJob->waitForFinished();
101 }
102 
104 {
105  return nullptr != mInternalJob;
106 }
107 
109 {
110  return mUsedCachedLabels;
111 }
112 
114 {
115  return mLabelingResults.release();
116 }
117 
118 
120 {
121  if ( isActive() && mCache )
122  // this will allow immediate display of cached layers and at the same time updates of the layer being rendered
123  return composeImage( mSettings, mInternalJob->jobs(), LabelRenderJob() );
124  else
125  return mImage;
126 }
127 
128 
130 {
131  QgsDebugMsgLevel( QStringLiteral( "SEQUENTIAL finished" ), 5 );
132 
133  mPainter->end();
134  delete mPainter;
135  mPainter = nullptr;
136 
137  mLabelingResults.reset( mInternalJob->takeLabelingResults() );
138  mUsedCachedLabels = mInternalJob->usedCachedLabels();
139 
140  mErrors = mInternalJob->errors();
141 
142  // now we are in a slot called from mInternalJob - do not delete it immediately
143  // so the class is still valid when the execution returns to the class
144  mInternalJob->deleteLater();
145  mInternalJob = nullptr;
146 
147  mRenderingTime = mRenderingStart.elapsed();
148 
149  emit finished();
150 }
QgsMapRendererSequentialJob::cancel
void cancel() override
Stop the rendering job - does not return until the job has terminated.
Definition: qgsmaprenderersequentialjob.cpp:75
qgspallabeling.h
QgsMapRendererJob::mCache
QgsMapRendererCache * mCache
Definition: qgsmaprendererjob.h:319
QgsMapRendererCustomPainterJob::waitForFinished
void waitForFinished() override
Block until the job has finished.
Definition: qgsmaprenderercustompainterjob.cpp:169
QgsMapRendererJob::mErrors
Errors mErrors
Definition: qgsmaprendererjob.h:317
QgsMapRendererSequentialJob::cancelWithoutBlocking
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
Definition: qgsmaprenderersequentialjob.cpp:86
QgsDebugMsgLevel
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsMapRendererSequentialJob::~QgsMapRendererSequentialJob
~QgsMapRendererSequentialJob() override
Definition: qgsmaprenderersequentialjob.cpp:35
QgsMapSettings::devicePixelRatio
float devicePixelRatio() const
Returns device pixel ratio Common values are 1 for normal-dpi displays and 2 for high-dpi "retina" di...
Definition: qgsmapsettings.cpp:246
QgsLabelingResults
Definition: qgspallabeling.h:1224
QgsMapRendererJob::mRenderingStart
QElapsedTimer mRenderingStart
Definition: qgsmaprendererjob.h:316
qgsmaprenderercustompainterjob.h
QgsMapSettings::outputImageFormat
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
Definition: qgsmapsettings.h:358
QgsMapRendererCustomPainterJob::cancel
void cancel() override
Stop the rendering job - does not return until the job has terminated.
Definition: qgsmaprenderercustompainterjob.cpp:128
QgsMapRendererCustomPainterJob::start
void start() override
Start the rendering job and immediately return.
Definition: qgsmaprenderercustompainterjob.cpp:75
QgsMapRendererCustomPainterJob::takeLabelingResults
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
Definition: qgsmaprenderercustompainterjob.cpp:196
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:321
QgsMapRendererCustomPainterJob::cancelWithoutBlocking
void cancelWithoutBlocking() override
Triggers cancellation of the rendering job without blocking.
Definition: qgsmaprenderercustompainterjob.cpp:152
QgsMapRendererCustomPainterJob::usedCachedLabels
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
Definition: qgsmaprenderercustompainterjob.cpp:191
QgsMapRendererSequentialJob::takeLabelingResults
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results).
Definition: qgsmaprenderersequentialjob.cpp:113
QgsMapRendererJob::setCache
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
QgsMapRendererJob::composeImage
static QImage composeImage(const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob)
QgsMapRendererSequentialJob::usedCachedLabels
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
Definition: qgsmaprenderersequentialjob.cpp:108
qgsmaprenderersequentialjob.h
QgsMapRendererCustomPainterJob
Definition: qgsmaprenderercustompainterjob.h:63
QgsMapSettings::deviceOutputSize
QSize deviceOutputSize() const
Returns the device output size of the map canvas This is equivalent to the output size multiplicated ...
Definition: qgsmapsettings.cpp:257
QgsMapRendererQImageJob
Definition: qgsmaprendererjob.h:435
QgsMapRendererSequentialJob::QgsMapRendererSequentialJob
QgsMapRendererSequentialJob(const QgsMapSettings &settings)
Definition: qgsmaprenderersequentialjob.cpp:22
QgsMapRendererSequentialJob::waitForFinished
void waitForFinished() override
Block until the job has finished.
Definition: qgsmaprenderersequentialjob.cpp:95
QgsMapRendererJob::mSettings
QgsMapSettings mSettings
Definition: qgsmaprendererjob.h:315
QgsMapRendererCustomPainterJob::jobs
const LayerRenderJobs & jobs() const
Definition: qgsmaprenderercustompainterjob.h:79
QgsMapRendererSequentialJob::isActive
bool isActive() const override
Tell whether the rendering job is currently running in background.
Definition: qgsmaprenderersequentialjob.cpp:103
QgsMapRendererSequentialJob::internalFinished
void internalFinished()
Definition: qgsmaprenderersequentialjob.cpp:129
QgsMapRendererSequentialJob::start
void start() override
Start the rendering job and immediately return.
Definition: qgsmaprenderersequentialjob.cpp:49
qgslogger.h
QgsMapSettings::outputDpi
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
Definition: qgsmapsettings.cpp:262
QgsMapSettings
Definition: qgsmapsettings.h:86
QgsMapRendererSequentialJob::renderedImage
QImage renderedImage() override
Gets a preview/resulting image.
Definition: qgsmaprenderersequentialjob.cpp:119
QgsMapRendererJob::finished
void finished()
emitted when asynchronous rendering is finished (or canceled).