QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
qgsmaprendererjob.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaprendererjob.h
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 
16 #ifndef QGSMAPRENDERERJOB_H
17 #define QGSMAPRENDERERJOB_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include <QFutureWatcher>
22 #include <QImage>
23 #include <QPainter>
24 #include <QObject>
25 #include <QTime>
26 
27 #include "qgsrendercontext.h"
28 
29 #include "qgsmapsettings.h"
30 #include "qgsmaskidprovider.h"
31 
32 
33 class QgsLabelingEngine;
34 class QgsLabelingResults;
38 
39 #ifndef SIP_RUN
40 
46 struct LayerRenderJob
47 {
48  QgsRenderContext context;
49 
55  QImage *img;
57  bool imageInitialized = false;
58  QgsMapLayerRenderer *renderer; // must be deleted
59  QPainter::CompositionMode blendMode;
60  double opacity;
62  bool cached;
64  int renderingTime;
65  QStringList errors;
66 
75  QString layerId;
76 
92  QImage *maskImage = nullptr;
94 
99  LayerRenderJob *firstPassJob = nullptr;
100 
107  QList<QPair<LayerRenderJob *, int>> maskJobs;
108 };
109 
110 typedef QList<LayerRenderJob> LayerRenderJobs;
111 
116 struct LabelRenderJob
117 {
118  QgsRenderContext context;
119 
124  QImage *img = nullptr;
125 
136  QVector<QImage *> maskImages;
137 
142  QgsMaskIdProvider maskIdProvider;
143 
145  bool cached = false;
147  bool canUseCache = false;
149  bool complete = false;
151  int renderingTime = -1;
153  QList< QPointer< QgsMapLayer > > participatingLayers;
154 };
155 
157 #endif
158 
186 class CORE_EXPORT QgsMapRendererJob : public QObject
187 {
188  Q_OBJECT
189  public:
190 
191  QgsMapRendererJob( const QgsMapSettings &settings );
192 
197  virtual void start() = 0;
198 
203  virtual void cancel() = 0;
204 
210  virtual void cancelWithoutBlocking() = 0;
211 
213  virtual void waitForFinished() = 0;
214 
216  virtual bool isActive() const = 0;
217 
225  virtual bool usedCachedLabels() const = 0;
226 
233 
241  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
242 
248  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
249 
250  struct Error
251  {
252  Error( const QString &lid, const QString &msg )
253  : layerID( lid )
254  , message( msg )
255  {}
256 
257  QString layerID;
258  QString message;
259  };
260 
261  typedef QList<QgsMapRendererJob::Error> Errors;
262 
264  Errors errors() const;
265 
266 
272 
277  int renderingTime() const { return mRenderingTime; }
278 
284  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
285 
291  const QgsMapSettings &mapSettings() const;
292 
297  static const QString LABEL_CACHE_ID SIP_SKIP;
298 
299  signals:
300 
308  void renderingLayersFinished();
309 
311  void finished();
312 
313  protected:
314 
315  QgsMapSettings mSettings;
316  QElapsedTimer mRenderingStart;
317  Errors mErrors;
318 
319  QgsMapRendererCache *mCache = nullptr;
320 
321  int mRenderingTime = 0;
322 
324  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
325 
329  bool mRecordRenderingTime = true;
330 
336  bool prepareLabelCache() const SIP_SKIP;
337 
349  LayerRenderJobs prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
350 
356  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
357 
366  LayerRenderJobs prepareSecondPassJobs( LayerRenderJobs &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
367 
369  static QImage composeImage( const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob ) SIP_SKIP;
370 
377  static void composeSecondPass( LayerRenderJobs &secondPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
378 
380  void logRenderingTime( const LayerRenderJobs &jobs, const LayerRenderJobs &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
381 
383  void cleanupJobs( LayerRenderJobs &jobs ) SIP_SKIP;
384 
386  void cleanupSecondPassJobs( LayerRenderJobs &jobs ) SIP_SKIP;
387 
394  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
395 
400  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
401 
403  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
404 
405  private:
406 
418  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
419 
420  bool needTemporaryImage( QgsMapLayer *ml );
421 
422  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
423 
425  QImage *allocateImage( QString layerId );
426 
428  QPainter *allocateImageAndPainter( QString layerId, QImage *&image );
429 };
430 
431 
439 class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
440 {
441  Q_OBJECT
442 
443  public:
445 
447  virtual QImage renderedImage() = 0;
448 
449 };
450 
451 
452 #endif // QGSMAPRENDERERJOB_H
QgsMapRendererJob::Error::Error
Error(const QString &lid, const QString &msg)
Definition: qgsmaprendererjob.h:252
QgsMapRendererJob::usedCachedLabels
virtual bool usedCachedLabels() const =0
Returns true if the render job was able to use a cached labeling solution.
QgsMapRendererJob::Error
Definition: qgsmaprendererjob.h:251
QgsMapRendererJob::isActive
virtual bool isActive() const =0
Tell whether the rendering job is currently running in background.
QgsMapRendererJob::QgsMapRendererJob
QgsMapRendererJob(const QgsMapSettings &settings)
QgsMapRendererQImageJob::renderedImage
virtual QImage renderedImage()=0
Gets a preview/resulting image.
qgsmaskidprovider.h
QgsLabelingResults
Class that stores computed placement from labeling engine.
Definition: qgspallabeling.h:1224
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:58
qgsmapsettings.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:42
QgsMapRendererCache
This class is responsible for keeping cache of rendered images resulting from a map rendering job.
Definition: qgsmaprenderercache.h:43
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:51
QgsMapRendererJob::renderingTime
int renderingTime() const
Returns the total time it took to finish the job (in milliseconds).
Definition: qgsmaprendererjob.h:277
QgsMapRendererJob::setFeatureFilterProvider
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
Definition: qgsmaprendererjob.h:241
QgsMapRendererJob::errors
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsMapRendererJob::perLayerRenderingTime
QHash< QgsMapLayer *, int > perLayerRenderingTime() const
Returns the render time (in ms) per layer.
QgsMapRendererJob::waitForFinished
virtual void waitForFinished()=0
Block until the job has finished.
QgsMapRendererJob::setCache
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgsrendercontext.h
QgsMapRendererJob::featureFilterProvider
const QgsFeatureFilterProvider * featureFilterProvider() const
Returns the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
Definition: qgsmaprendererjob.h:248
QgsWeakMapLayerPointer
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1688
QgsMapRendererJob::start
virtual void start()=0
Start the rendering job and immediately return.
QgsMapRendererJob::Error::message
QString message
Definition: qgsmaprendererjob.h:258
QgsLabelingEngine
The QgsLabelingEngine class provides map labeling functionality.
Definition: qgslabelingengine.h:215
QgsMapRendererQImageJob
Intermediate base class adding functionality that allows client to query the rendered image.
Definition: qgsmaprendererjob.h:440
QgsMaskIdProvider
Some rendering operations may need multiple mask images.
Definition: qgsmaskidprovider.h:38
QgsMapLayer
Base class for all map layer types.
Definition: qgsmaplayer.h:83
QgsMapRendererJob::cancelWithoutBlocking
virtual void cancelWithoutBlocking()=0
Triggers cancellation of the rendering job without blocking.
QgsMapRendererJob
Abstract base class for map rendering implementations.
Definition: qgsmaprendererjob.h:187
QgsFeatureFilterProvider
Abstract interface for use by classes that filter the features of a layer.
Definition: qgsfeaturefilterprovider.h:41
QgsMapRendererJob::takeLabelingResults
virtual QgsLabelingResults * takeLabelingResults()=0
Gets pointer to internal labeling engine (in order to get access to the results).
QgsMapRendererJob::Errors
QList< QgsMapRendererJob::Error > Errors
Definition: qgsmaprendererjob.h:261
QgsMapRendererJob::cancel
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
QgsMapRendererQImageJob::QgsMapRendererQImageJob
QgsMapRendererQImageJob(const QgsMapSettings &settings)
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map.
Definition: qgsmapsettings.h:88
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:53
QgsMapRendererJob::Error::layerID
QString layerID
Definition: qgsmaprendererjob.h:257