QGIS API Documentation  3.20.0-Odense (decaadbb31)
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;
40 
41 #ifndef SIP_RUN
43 
48 struct LayerRenderJob
49 {
50  QgsRenderContext context;
51 
57  QImage *img;
59  bool imageInitialized = false;
60 
61  bool imageCanBeComposed() const;
62 
63  QgsMapLayerRenderer *renderer; // must be deleted
64 
65  QPainter::CompositionMode blendMode;
66  double opacity;
68  bool cached;
70 
77  bool completed = false;
78 
79  int renderingTime;
80 
90  int estimatedRenderingTime = 0;
91 
92  QStringList errors;
93 
102  QString layerId;
103 
120  QImage *maskImage = nullptr;
121 
126  LayerRenderJob *firstPassJob = nullptr;
127 
134  QList<QPair<LayerRenderJob *, int>> maskJobs;
135 };
136 
137 typedef QList<LayerRenderJob> LayerRenderJobs;
138 
143 struct LabelRenderJob
144 {
145  QgsRenderContext context;
146 
151  QImage *img = nullptr;
152 
163  QVector<QImage *> maskImages;
164 
169  QgsMaskIdProvider maskIdProvider;
170 
172  bool cached = false;
174  bool canUseCache = false;
176  bool complete = false;
178  int renderingTime = -1;
180  QList< QPointer< QgsMapLayer > > participatingLayers;
181 };
182 
184 #endif
185 
213 class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
214 {
215  Q_OBJECT
216  public:
217 
218  QgsMapRendererJob( const QgsMapSettings &settings );
219 
224  void start();
225 
230  virtual void cancel() = 0;
231 
237  virtual void cancelWithoutBlocking() = 0;
238 
240  virtual void waitForFinished() = 0;
241 
243  virtual bool isActive() const = 0;
244 
252  virtual bool usedCachedLabels() const = 0;
253 
260 
268  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
269 
275  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
276 
277  struct Error
278  {
279  Error( const QString &lid, const QString &msg )
280  : layerID( lid )
281  , message( msg )
282  {}
283 
284  QString layerID;
285  QString message;
286  };
287 
288  typedef QList<QgsMapRendererJob::Error> Errors;
289 
291  Errors errors() const;
292 
293 
299 
304  int renderingTime() const { return mRenderingTime; }
305 
311  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
312 
325  void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
326 
332  const QgsMapSettings &mapSettings() const;
333 
338  static const QString LABEL_CACHE_ID SIP_SKIP;
339 
345  static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
346 
347 #ifndef SIP_RUN
349  static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "Map/logCanvasRefreshEvent" ), QgsSettings::NoSection, false );
350 #endif
351 
352  signals:
353 
362 
364  void finished();
365 
366  protected:
367 
369  QElapsedTimer mRenderingStart;
371 
372  QgsMapRendererCache *mCache = nullptr;
373 
374  int mRenderingTime = 0;
375 
377  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
378 
384  QHash< QString, int > mLayerRenderingTimeHints;
385 
389  bool mRecordRenderingTime = true;
390 
397 
409  LayerRenderJobs prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
410 
416  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
417 
426  LayerRenderJobs prepareSecondPassJobs( LayerRenderJobs &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
427 
429  static QImage composeImage( const QgsMapSettings &settings,
430  const LayerRenderJobs &jobs,
431  const LabelRenderJob &labelJob,
432  const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
433 
435  static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
436 
443  static void composeSecondPass( LayerRenderJobs &secondPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
444 
446  void logRenderingTime( const LayerRenderJobs &jobs, const LayerRenderJobs &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
447 
449  void cleanupJobs( LayerRenderJobs &jobs ) SIP_SKIP;
450 
452  void cleanupSecondPassJobs( LayerRenderJobs &jobs ) SIP_SKIP;
453 
460  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
461 
466  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
467 
469  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
470 
471  private:
472 
484  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
485 
486  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
487 
489  QImage *allocateImage( QString layerId );
490 
492  QPainter *allocateImageAndPainter( QString layerId, QImage *&image );
493 
499  virtual void startPrivate() = 0;
500 
501 };
502 
503 
513 {
514  Q_OBJECT
515 
516  public:
518 
520  virtual QImage renderedImage() = 0;
521 
522 };
523 
524 
525 #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:70
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.
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)
void start()
Start the rendering job and immediately return.
int renderingTime() const
Returns the total time it took to finish the job (in milliseconds).
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.
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
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.
#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:1853
Error(const QString &lid, const QString &msg)