QGIS API Documentation  3.2.0-Bonn (bc43194)
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( "SEQUENTIAL construct", 5 );
27 
28  mImage = QImage( mSettings.outputSize(), mSettings.outputImageFormat() );
29  mImage.setDotsPerMeterX( 1000 * settings.outputDpi() / 25.4 );
30  mImage.setDotsPerMeterY( 1000 * settings.outputDpi() / 25.4 );
31  mImage.fill( Qt::transparent );
32 }
33 
35 {
36  QgsDebugMsgLevel( "SEQUENTIAL destruct", 5 );
37  if ( isActive() )
38  {
39  // still running!
40  QgsDebugMsgLevel( "SEQUENTIAL destruct -- still running! (canceling)", 5 );
41  cancel();
42  }
43 
44  Q_ASSERT( !mInternalJob && !mPainter );
45 }
46 
47 
49 {
50  if ( isActive() )
51  return; // do nothing if we are already running
52 
53  mLabelingResults.reset();
54 
55  mRenderingStart.start();
56 
57  mErrors.clear();
58 
59  QgsDebugMsgLevel( "SEQUENTIAL START", 5 );
60 
61  Q_ASSERT( !mInternalJob && !mPainter );
62 
63  mPainter = new QPainter( &mImage );
64 
65  mInternalJob = new QgsMapRendererCustomPainterJob( mSettings, mPainter );
66  mInternalJob->setCache( mCache );
67 
69 
70  mInternalJob->start();
71 }
72 
73 
75 {
76  if ( !isActive() )
77  return;
78 
79  QgsDebugMsgLevel( "sequential - cancel internal", 5 );
80  mInternalJob->cancel();
81 
82  Q_ASSERT( !mInternalJob && !mPainter );
83 }
84 
86 {
87  if ( !isActive() )
88  return;
89 
90  QgsDebugMsgLevel( "sequential - cancel internal", 5 );
91  mInternalJob->cancelWithoutBlocking();
92 }
93 
95 {
96  if ( !isActive() )
97  return;
98 
99  mInternalJob->waitForFinished();
100 }
101 
103 {
104  return nullptr != mInternalJob;
105 }
106 
108 {
109  return mUsedCachedLabels;
110 }
111 
113 {
114  return mLabelingResults.release();
115 }
116 
117 
119 {
120  if ( isActive() && mCache )
121  // this will allow immediate display of cached layers and at the same time updates of the layer being rendered
122  return composeImage( mSettings, mInternalJob->jobs(), LabelRenderJob() );
123  else
124  return mImage;
125 }
126 
127 
129 {
130  QgsDebugMsgLevel( "SEQUENTIAL finished", 5 );
131 
132  mPainter->end();
133  delete mPainter;
134  mPainter = nullptr;
135 
136  mLabelingResults.reset( mInternalJob->takeLabelingResults() );
137  mUsedCachedLabels = mInternalJob->usedCachedLabels();
138 
139  mErrors = mInternalJob->errors();
140 
141  // now we are in a slot called from mInternalJob - do not delete it immediately
142  // so the class is still valid when the execution returns to the class
143  mInternalJob->deleteLater();
144  mInternalJob = nullptr;
145 
146  mRenderingTime = mRenderingStart.elapsed();
147 
148  emit finished();
149 }
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.
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.
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
QSize outputSize() const
Returns the size of the resulting map image.