QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 }
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.
void start() override
Start the rendering job and immediately return.
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
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)
void start() override
Start the rendering job and immediately return.
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