QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
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 
359  int renderingTime() const { return mRenderingTime; }
360 
366  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
367 
380  void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
381 
387  const QgsMapSettings &mapSettings() const;
388 
393  static const QString LABEL_CACHE_ID SIP_SKIP;
394 
400  static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
401 
402 #ifndef SIP_RUN
404  static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "Map/logCanvasRefreshEvent" ), QgsSettings::NoSection, false );
405 #endif
406 
407  signals:
408 
417 
419  void finished();
420 
421  protected:
422 
424  QElapsedTimer mRenderingStart;
426 
427  QgsMapRendererCache *mCache = nullptr;
428 
429  int mRenderingTime = 0;
430 
432  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
433 
439  QHash< QString, int > mLayerRenderingTimeHints;
440 
444  bool mRecordRenderingTime = true;
445 
446 #ifndef SIP_RUN
447  std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
448 #endif
449 
451 
458 
470  std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
471 
477  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
478 
487  std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
488 
490  static QImage composeImage( const QgsMapSettings &settings,
491  const std::vector< LayerRenderJob > &jobs,
492  const LabelRenderJob &labelJob,
493  const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
494 
496  static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
497 
504  static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
505 
507  void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
508 
510  void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
511 
513  void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
514 
521  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
522 
527  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
528 
530  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
531 
532  private:
533 
545  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
546 
547  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
548 
550  QImage *allocateImage( QString layerId );
551 
553  QPainter *allocateImageAndPainter( QString layerId, QImage *&image );
554 
560  virtual void startPrivate() = 0;
561 
562 };
563 
564 
574 {
575  Q_OBJECT
576 
577  public:
579 
581  virtual QImage renderedImage() = 0;
582 
583 };
584 
585 
586 #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.
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.
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 renderingLayersFinished()
Emitted when the layers are rendered.
QElapsedTimer mRenderingStart
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
QgsMapRendererJob(const QgsMapSettings &settings)
~QgsMapRendererJob() override
void start()
Start the rendering job and immediately return.
int renderingTime() const
Returns the total time it took to finish the job (in milliseconds).
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
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:2130
Error(const QString &lid, const QString &msg)