QGIS API Documentation  3.27.0-Master (aef1b1ec20)
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 #include <QElapsedTimer>
27 
28 #include "qgsrendercontext.h"
29 #include "qgslabelsink.h"
30 #include "qgsmapsettings.h"
31 #include "qgsmaskidprovider.h"
32 #include "qgssettingsentryimpl.h"
33 
34 class QPicture;
35 
36 class QgsLabelingEngine;
37 class QgsLabelingResults;
42 
43 #ifndef SIP_RUN
45 
50 class LayerRenderJob
51 {
52  public:
53 
54  LayerRenderJob() = default;
55 
57  LayerRenderJob( const LayerRenderJob & ) = delete;
58 
60  LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
61 
62  LayerRenderJob( LayerRenderJob && );
63  LayerRenderJob &operator=( LayerRenderJob && );
64 
71  void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
72 
78  QgsRenderContext *context() { return mContext.get(); }
79 
85  QImage *img = nullptr;
86 
88  bool imageInitialized = false;
89 
90  bool imageCanBeComposed() const;
91 
92  QgsMapLayerRenderer *renderer = nullptr; // must be deleted
93 
94  QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
95 
96  double opacity = 1.0;
97 
99  bool cached = false;
100 
102 
109  bool completed = false;
110 
112  int renderingTime = -1;
113 
123  int estimatedRenderingTime = 0;
124 
125  QStringList errors;
126 
135  QString layerId;
136 
153  std::unique_ptr<QPainter> maskPainter;
154 
155 
157  std::unique_ptr<QPaintDevice> maskPaintDevice;
158 
163  bool maskRequiresLayerRasterization = false;
164 
169  LayerRenderJob *firstPassJob = nullptr;
170 
174  std::unique_ptr<QPicture> picture;
175 
182  QList<QPair<LayerRenderJob *, int>> maskJobs;
183 
184  private:
185  std::unique_ptr< QgsRenderContext > mContext;
186 
187 };
188 
193 struct LabelRenderJob
194 {
195  QgsRenderContext context;
196 
201  QImage *img = nullptr;
202 
204  std::unique_ptr<QPicture> picture;
205 
207  std::vector< std::unique_ptr<QPainter> > maskPainters;
208 
219  std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
220 
225  QgsMaskIdProvider maskIdProvider;
226 
228  bool cached = false;
230  bool canUseCache = false;
232  bool complete = false;
234  int renderingTime = -1;
236  QList< QPointer< QgsMapLayer > > participatingLayers;
237 };
238 
240 #endif
241 
269 class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
270 {
271  Q_OBJECT
272  public:
273 
274  QgsMapRendererJob( const QgsMapSettings &settings );
275 
276  ~QgsMapRendererJob() override;
277 
282  void start();
283 
288  virtual void cancel() = 0;
289 
295  virtual void cancelWithoutBlocking() = 0;
296 
298  virtual void waitForFinished() = 0;
299 
301  virtual bool isActive() const = 0;
302 
310  virtual bool usedCachedLabels() const = 0;
311 
320  QStringList layersRedrawnFromCache() const;
321 
328 
337 
345  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
346 
352  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
353 
354  struct Error
355  {
356  Error( const QString &lid, const QString &msg )
357  : layerID( lid )
358  , message( msg )
359  {}
360 
361  QString layerID;
362  QString message;
363  };
364 
365  typedef QList<QgsMapRendererJob::Error> Errors;
366 
368  Errors errors() const;
369 
370 
376 
382  QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
383 
391  void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
392 
402 
407  int renderingTime() const { return mRenderingTime; }
408 
414  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
415 
428  void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
429 
435  const QgsMapSettings &mapSettings() const;
436 
441  static const QString LABEL_CACHE_ID SIP_SKIP;
442 
448  static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
449 
450 #ifndef SIP_RUN
452  static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "logCanvasRefreshEvent" ), QgsSettings::Prefix::MAP, false );
453 #endif
454 
455  signals:
456 
465 
466 
474  void layerRenderingStarted( const QString &layerId );
475 
483  void layerRendered( const QString &layerId );
484 
486  void finished();
487 
488  protected:
489 
491  QElapsedTimer mRenderingStart;
493 
494  QgsMapRendererCache *mCache = nullptr;
495 
496  int mRenderingTime = 0;
497 
499  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
500 
506  QHash< QString, int > mLayerRenderingTimeHints;
507 
511  bool mRecordRenderingTime = true;
512 
513 #ifndef SIP_RUN
514  std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
515 #endif
516 
518 
525 
537  std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
538 
544  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
545 
554  std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
555 
560  void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
561 
563  static QImage composeImage( const QgsMapSettings &settings,
564  const std::vector< LayerRenderJob > &jobs,
565  const LabelRenderJob &labelJob,
566  const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
567 
569  static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
570 
577  static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
578 
580  void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
581 
583  void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
584 
586  void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
587 
594  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
595 
600  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
601 
603  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
604 
605  private:
606 
618  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
619 
620  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
621 
623  QImage *allocateImage( QString layerId );
624 
626  QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
627 
633  virtual void startPrivate() = 0;
634 
635  QgsLabelSink *mLabelSink = nullptr;
636  QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
637 
638  typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
639 
641  PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
642 };
643 
644 
654 {
655  Q_OBJECT
656 
657  public:
659 
661  virtual QImage renderedImage() = 0;
662 
663 };
664 
665 
666 #endif // QGSMAPRENDERERJOB_H
Class for doing transforms between two map coordinate systems.
Abstract interface for use by classes that filter the features or attributes of a layer.
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Definition: qgslabelsink.h:38
QgsFeedback subclass for granular reporting of labeling engine progress.
The QgsLabelingEngine class provides map labeling functionality.
Class that stores computed placement from labeling engine.
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Base class for all map layer types.
Definition: qgsmaplayer.h:73
This class is responsible for keeping cache of rendered images resulting from a map rendering job.
Abstract base class for map rendering implementations.
virtual void waitForFinished()=0
Block until the job has finished.
void setCache(QgsMapRendererCache *cache)
Assign a cache to be used for reading and storing rendered images of individual layers.
virtual QgsLabelingResults * takeLabelingResults()=0
Gets pointer to internal labeling engine (in order to get access to the results).
QHash< QgsMapLayer *, int > perLayerRenderingTime() const
Returns the render time (in ms) per layer.
virtual bool usedCachedLabels() const =0
Returns true if the render job was able to use a cached labeling solution.
void setLabelSink(QgsLabelSink *sink)
Assigns the label sink which will take over responsibility for handling labels during the rendering j...
QHash< QString, int > mLayerRenderingTimeHints
Approximate expected layer rendering time per layer, by layer ID.
const QgsFeatureFilterProvider * featureFilterProvider() const
Returns the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
void renderingLayersFinished()
Emitted when the layers are rendered.
QElapsedTimer mRenderingStart
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
QgsLabelSink * labelSink() const
Returns the label sink associated to this rendering job.
QgsMapRendererJob(const QgsMapSettings &settings)
~QgsMapRendererJob() override
void start()
Start the rendering job and immediately return.
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
QStringList mLayersRedrawnFromCache
QStringList layersRedrawnFromCache() const
Returns a list of the layer IDs for all layers which were redrawn from cached images.
QList< QgsMapRendererJob::Error > Errors
QgsLabelingEngineFeedback * labelingEngineFeedback()
Returns the associated labeling engine feedback object.
QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime
Render time (in ms) per layer, by layer ID.
virtual bool isActive() const =0
Tell whether the rendering job is currently running in background.
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
QgsRenderedItemResults * takeRenderedItemResults()
Takes the rendered item results from the map render job and returns them.
virtual void cancelWithoutBlocking()=0
Triggers cancellation of the rendering job without blocking.
bool prepareLabelCache() const
Prepares the cache for storing the result of labeling.
Intermediate base class adding functionality that allows client to query the rendered image.
QgsMapRendererQImageJob(const QgsMapSettings &settings)
virtual QImage renderedImage()=0
Gets a preview/resulting image.
The QgsMapSettings class contains configuration for rendering of the map.
This class allows the creation of mask ids based on the different label layers and to give a mask id ...
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
Stores collated details of rendered items during a map rendering operation.
A boolean settings entry.
static const char * MAP
Definition: qgssettings.h:98
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_ABSTRACT
Definition: qgis_sip.h:208
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2146
Error(const QString &lid, const QString &msg)