QGIS API Documentation  2.99.0-Master (b8fd1fd)
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  , mInternalJob( nullptr )
25  , mPainter( nullptr )
26 {
27  QgsDebugMsg( "SEQUENTIAL construct" );
28 
29  mImage = QImage( mSettings.outputSize(), mSettings.outputImageFormat() );
30  mImage.setDotsPerMeterX( 1000 * settings.outputDpi() / 25.4 );
31  mImage.setDotsPerMeterY( 1000 * settings.outputDpi() / 25.4 );
32  mImage.fill( Qt::transparent );
33 }
34 
36 {
37  QgsDebugMsg( "SEQUENTIAL destruct" );
38  if ( isActive() )
39  {
40  // still running!
41  QgsDebugMsg( "SEQUENTIAL destruct -- still running! (canceling)" );
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  QgsDebugMsg( "SEQUENTIAL START" );
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  QgsDebugMsg( "sequential - cancel internal" );
81  mInternalJob->cancel();
82 
83  Q_ASSERT( !mInternalJob && !mPainter );
84 }
85 
87 {
88  if ( !isActive() )
89  return;
90 
91  QgsDebugMsg( "sequential - cancel internal" );
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  QgsDebugMsg( "SEQUENTIAL finished" );
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.
virtual void start() override
Start the rendering job and immediately return.
#define QgsDebugMsg(str)
Definition: qgslogger.h:36
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
virtual void cancel() override
Stop the rendering job - does not return until the job has terminated.
virtual void cancel() override
Stop the rendering job - does not return until the job has terminated.
virtual QgsLabelingResults * takeLabelingResults() override
Get 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)
virtual void start() override
Start the rendering job and immediately return.
QgsMapRendererSequentialJob(const QgsMapSettings &settings)
QImage::Format outputImageFormat() const
format of internal QImage, default QImage::Format_ARGB32_Premultiplied
virtual bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
QgsMapSettings mSettings
virtual void cancelWithoutBlocking() override
Triggers cancelation of the rendering job without blocking.
double outputDpi() const
Return DPI used for conversion between real world units (e.g.
virtual QImage renderedImage() override
Get a preview/resulting image.
Intermediate base class adding functionality that allows client to query the rendered image...
virtual void cancelWithoutBlocking() override
Triggers cancelation of the rendering job without blocking.
Class that stores computed placement from labeling engine.
virtual bool isActive() const override
Tell whether the rendering job is currently running in background.
virtual void waitForFinished() override
Block until the job has finished.
virtual QgsLabelingResults * takeLabelingResults() override
Get pointer to internal labeling engine (in order to get access to the results).
virtual bool usedCachedLabels() const override
Returns true if the render job was able to use a cached labeling solution.
virtual void waitForFinished() override
Block until the job has finished.
QgsMapRendererCache * mCache
QSize outputSize() const
Return the size of the resulting map image.