QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 "qgssettingsentry.h"
33 
34 
35 class QgsLabelingEngine;
36 class QgsLabelingResults;
41 
42 #ifndef SIP_RUN
44 
49 class LayerRenderJob
50 {
51  public:
52 
53  LayerRenderJob() = default;
54 
56  LayerRenderJob( const LayerRenderJob & ) = delete;
57 
59  LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
60 
61  LayerRenderJob( LayerRenderJob && );
62  LayerRenderJob &operator=( LayerRenderJob && );
63 
70  void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
71 
77  QgsRenderContext *context() { return mContext.get(); }
78 
84  QImage *img = nullptr;
85 
87  bool imageInitialized = false;
88 
89  bool imageCanBeComposed() const;
90 
91  QgsMapLayerRenderer *renderer = nullptr; // must be deleted
92 
93  QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
94 
95  double opacity = 1.0;
96 
98  bool cached = false;
99 
101 
108  bool completed = false;
109 
111  int renderingTime = -1;
112 
122  int estimatedRenderingTime = 0;
123 
124  QStringList errors;
125 
134  QString layerId;
135 
152  QImage *maskImage = nullptr;
153 
158  LayerRenderJob *firstPassJob = nullptr;
159 
166  QList<QPair<LayerRenderJob *, int>> maskJobs;
167 
168  private:
169  std::unique_ptr< QgsRenderContext > mContext;
170 
171 };
172 
177 struct LabelRenderJob
178 {
179  QgsRenderContext context;
180 
185  QImage *img = nullptr;
186 
197  QVector<QImage *> maskImages;
198 
203  QgsMaskIdProvider maskIdProvider;
204 
206  bool cached = false;
208  bool canUseCache = false;
210  bool complete = false;
212  int renderingTime = -1;
214  QList< QPointer< QgsMapLayer > > participatingLayers;
215 };
216 
218 #endif
219 
247 class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
248 {
249  Q_OBJECT
250  public:
251 
252  QgsMapRendererJob( const QgsMapSettings &settings );
253 
254  ~QgsMapRendererJob() override;
255 
260  void start();
261 
266  virtual void cancel() = 0;
267 
273  virtual void cancelWithoutBlocking() = 0;
274 
276  virtual void waitForFinished() = 0;
277 
279  virtual bool isActive() const = 0;
280 
288  virtual bool usedCachedLabels() const = 0;
289 
298  QStringList layersRedrawnFromCache() const;
299 
306 
315 
323  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
324 
330  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
331 
332  struct Error
333  {
334  Error( const QString &lid, const QString &msg )
335  : layerID( lid )
336  , message( msg )
337  {}
338 
339  QString layerID;
340  QString message;
341  };
342 
343  typedef QList<QgsMapRendererJob::Error> Errors;
344 
346  Errors errors() const;
347 
348 
354 
360  QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
361 
369  void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
370 
380 
385  int renderingTime() const { return mRenderingTime; }
386 
392  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
393 
406  void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
407 
413  const QgsMapSettings &mapSettings() const;
414 
419  static const QString LABEL_CACHE_ID SIP_SKIP;
420 
426  static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
427 
428 #ifndef SIP_RUN
430  static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "Map/logCanvasRefreshEvent" ), QgsSettings::NoSection, false );
431 #endif
432 
433  signals:
434 
443 
444 
452  void layerRenderingStarted( const QString &layerId );
453 
461  void layerRendered( const QString &layerId );
462 
464  void finished();
465 
466  protected:
467 
469  QElapsedTimer mRenderingStart;
471 
472  QgsMapRendererCache *mCache = nullptr;
473 
474  int mRenderingTime = 0;
475 
477  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
478 
484  QHash< QString, int > mLayerRenderingTimeHints;
485 
489  bool mRecordRenderingTime = true;
490 
491 #ifndef SIP_RUN
492  std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
493 #endif
494 
496 
503 
515  std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
516 
522  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
523 
532  std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
533 
535  static QImage composeImage( const QgsMapSettings &settings,
536  const std::vector< LayerRenderJob > &jobs,
537  const LabelRenderJob &labelJob,
538  const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
539 
541  static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
542 
549  static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
550 
552  void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
553 
555  void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
556 
558  void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
559 
566  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
567 
572  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
573 
575  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
576 
577  private:
578 
590  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
591 
592  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
593 
595  QImage *allocateImage( QString layerId );
596 
598  QPainter *allocateImageAndPainter( QString layerId, QImage *&image );
599 
605  virtual void startPrivate() = 0;
606 
607  QgsLabelSink *mLabelSink = nullptr;
608  QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
609 
610 };
611 
612 
622 {
623  Q_OBJECT
624 
625  public:
627 
629  virtual QImage renderedImage() = 0;
630 
631 };
632 
633 
634 #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.
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_ABSTRACT
Definition: qgis_sip.h:199
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2133
Error(const QString &lid, const QString &msg)