QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
44 
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 
152  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 
327  virtual QgsLabelingResults *takeLabelingResults() = 0 SIP_TRANSFER;
328 
336  QgsRenderedItemResults *takeRenderedItemResults() SIP_TRANSFER;
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 
375  void setCache( QgsMapRendererCache *cache );
376 
382  QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
383 
391  void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
392 
401  QgsLabelingEngineFeedback *labelingEngineFeedback() SIP_SKIP;
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
451  static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "logCanvasRefreshEvent" ), QgsSettings::Prefix::MAP, false );
453 #endif
454 
455  signals:
456 
464  void renderingLayersFinished();
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 
524  bool prepareLabelCache() const SIP_SKIP;
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:
658  QgsMapRendererQImageJob( const QgsMapSettings &settings );
659 
661  virtual QImage renderedImage() = 0;
662 
663 };
664 
665 
666 #endif // QGSMAPRENDERERJOB_H
QgsMapRendererJob::mLayersRedrawnFromCache
QStringList mLayersRedrawnFromCache
Definition: qgsmaprendererjob.h:517
QgsMapRendererJob::Error::Error
Error(const QString &lid, const QString &msg)
Definition: qgsmaprendererjob.h:356
QgsMapRendererJob::Error
Definition: qgsmaprendererjob.h:354
QgsSettings::Prefix::MAP
static const char * MAP
Definition: qgssettings.h:98
QgsMapRendererJob::mErrors
Errors mErrors
Definition: qgsmaprendererjob.h:492
QgsLabelSink
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Definition: qgslabelsink.h:37
qgsmaskidprovider.h
QgsLabelingResults
Class that stores computed placement from labeling engine.
Definition: qgslabelingresults.h:32
QgsMapRendererJob::mRenderedItemResults
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Definition: qgsmaprendererjob.h:514
QgsMapRendererJob::mRenderingStart
QElapsedTimer mRenderingStart
Definition: qgsmaprendererjob.h:491
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsMapRendererJob::mPerLayerRenderingTime
QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime
Render time (in ms) per layer, by layer ID.
Definition: qgsmaprendererjob.h:499
qgsmapsettings.h
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsMapRendererCache
This class is responsible for keeping cache of rendered images resulting from a map rendering job.
Definition: qgsmaprenderercache.h:47
QgsMapLayerRenderer
Base class for utility classes that encapsulate information necessary for rendering of map layers.
Definition: qgsmaplayerrenderer.h:54
QgsMapRendererJob::setFeatureFilterProvider
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
Definition: qgsmaprendererjob.h:345
geos::unique_ptr
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:79
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
qgslabelsink.h
qgis_sip.h
SIP_TRANSFER
#define SIP_TRANSFER
Definition: qgis_sip.h:36
qgsrendercontext.h
QgsMapRendererJob::labelSink
QgsLabelSink * labelSink() const
Returns the label sink associated to this rendering job.
Definition: qgsmaprendererjob.h:382
QgsMapRendererJob::featureFilterProvider
const QgsFeatureFilterProvider * featureFilterProvider() const
Returns the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
Definition: qgsmaprendererjob.h:352
QgsWeakMapLayerPointer
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2146
qgssettingsentryimpl.h
QgsMapRendererJob::mLayerRenderingTimeHints
QHash< QString, int > mLayerRenderingTimeHints
Approximate expected layer rendering time per layer, by layer ID.
Definition: qgsmaprendererjob.h:506
QgsMapRendererJob::Error::message
QString message
Definition: qgsmaprendererjob.h:362
QgsLabelingEngine
The QgsLabelingEngine class provides map labeling functionality. The input for the engine is a list o...
Definition: qgslabelingengine.h:343
QgsMapRendererQImageJob
Intermediate base class adding functionality that allows client to query the rendered image.
Definition: qgsmaprendererjob.h:653
QgsMaskIdProvider
This class allows the creation of mask ids based on the different label layers and to give a mask id ...
Definition: qgsmaskidprovider.h:39
QgsMapLayer
Base class for all map layer types. This is the base class for all map layer types (vector,...
Definition: qgsmaplayer.h:72
QgsMapRendererJob::mSettings
QgsMapSettings mSettings
Definition: qgsmaprendererjob.h:490
QgsMapRendererJob
Abstract base class for map rendering implementations.
Definition: qgsmaprendererjob.h:269
QgsFeatureFilterProvider
Abstract interface for use by classes that filter the features or attributes of a layer.
Definition: qgsfeaturefilterprovider.h:43
QgsMapRendererJob::Errors
QList< QgsMapRendererJob::Error > Errors
Definition: qgsmaprendererjob.h:365
QgsRenderedItemResults
Stores collated details of rendered items during a map rendering operation.
Definition: qgsrendereditemresults.h:42
QgsMapSettings
The QgsMapSettings class contains configuration for rendering of the map. The rendering itself is don...
Definition: qgsmapsettings.h:88
QgsCoordinateTransform
Class for doing transforms between two map coordinate systems.
Definition: qgscoordinatetransform.h:57
QgsLabelingEngineFeedback
QgsFeedback subclass for granular reporting of labeling engine progress.
Definition: qgslabelingengine.h:214
QgsMapRendererJob::Error::layerID
QString layerID
Definition: qgsmaprendererjob.h:361
QgsSettingsEntryBool
A boolean settings entry.
Definition: qgssettingsentryimpl.h:241
SIP_ABSTRACT
#define SIP_ABSTRACT
Definition: qgis_sip.h:208
QgsMapRendererJob::setLabelSink
void setLabelSink(QgsLabelSink *sink)
Assigns the label sink which will take over responsibility for handling labels during the rendering j...
Definition: qgsmaprendererjob.h:391