QGIS API Documentation  3.6.0-Noosa (5873452)
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 }
void finished()
emitted when asynchronous rendering is finished (or canceled).
const LayerRenderJobs & jobs() const
Job implementation that renders everything sequentially using a custom painter.
void start() override
Start the rendering job and immediately return.
QSize deviceOutputSize() const
Returns the device output size of the map canvas This is equivalent to the output size multiplicated ...
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
void cancel() override
Stop the rendering job - does not return until the job has terminated.
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). ...
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
The QgsMapSettings class contains configuration for rendering of the map.
static QImage composeImage(const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob)
void start() override
Start the rendering job and immediately return.
#define QgsDebugMsgLevel(str, level)
Definition: qgslogger.h:39
QgsMapRendererSequentialJob(const QgsMapSettings &settings)
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
float devicePixelRatio() const
Returns device pixel ratio Common values are 1 for normal-dpi displays and 2 for high-dpi "retina" di...
QgsMapSettings mSettings
void cancelWithoutBlocking() override
Triggers cancelation of the rendering job without blocking.
double outputDpi() const
Returns DPI used for conversion between real world units (e.g.
QImage renderedImage() override
Gets a preview/resulting image.
Intermediate base class adding functionality that allows client to query the rendered image...
void cancelWithoutBlocking() override
Triggers cancelation of the rendering job without blocking.
Class that stores computed placement from labeling engine.
bool isActive() const override
Tell whether the rendering job is currently running in background.
void waitForFinished() override
Block until the job has finished.
QgsLabelingResults * takeLabelingResults() override
Gets pointer to internal labeling engine (in order to get access to the results). ...
bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
void waitForFinished() override
Block until the job has finished.
QgsMapRendererCache * mCache