QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
33 
34 class QgsLabelingEngine;
35 class QgsLabelingResults;
39 
40 #ifndef SIP_RUN
42 
47 struct LayerRenderJob
48 {
49  QgsRenderContext context;
50 
56  QImage *img;
58  bool imageInitialized = false;
59 
60  bool imageCanBeComposed() const;
61 
62  QgsMapLayerRenderer *renderer; // must be deleted
63 
64  QPainter::CompositionMode blendMode;
65  double opacity;
67  bool cached;
69 
76  bool completed = false;
77 
78  int renderingTime;
79 
89  int estimatedRenderingTime = 0;
90 
91  QStringList errors;
92 
101  QString layerId;
102 
119  QImage *maskImage = nullptr;
120 
125  LayerRenderJob *firstPassJob = nullptr;
126 
133  QList<QPair<LayerRenderJob *, int>> maskJobs;
134 };
135 
136 typedef QList<LayerRenderJob> LayerRenderJobs;
137 
142 struct LabelRenderJob
143 {
144  QgsRenderContext context;
145 
150  QImage *img = nullptr;
151 
162  QVector<QImage *> maskImages;
163 
168  QgsMaskIdProvider maskIdProvider;
169 
171  bool cached = false;
173  bool canUseCache = false;
175  bool complete = false;
177  int renderingTime = -1;
179  QList< QPointer< QgsMapLayer > > participatingLayers;
180 };
181 
183 #endif
184 
212 class CORE_EXPORT QgsMapRendererJob : public QObject
213 {
214  Q_OBJECT
215  public:
216 
217  QgsMapRendererJob( const QgsMapSettings &settings );
218 
223  virtual void start() = 0;
224 
229  virtual void cancel() = 0;
230 
236  virtual void cancelWithoutBlocking() = 0;
237 
239  virtual void waitForFinished() = 0;
240 
242  virtual bool isActive() const = 0;
243 
251  virtual bool usedCachedLabels() const = 0;
252 
259 
267  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
268 
274  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
275 
276  struct Error
277  {
278  Error( const QString &lid, const QString &msg )
279  : layerID( lid )
280  , message( msg )
281  {}
282 
283  QString layerID;
284  QString message;
285  };
286 
287  typedef QList<QgsMapRendererJob::Error> Errors;
288 
290  Errors errors() const;
291 
292 
298 
303  int renderingTime() const { return mRenderingTime; }
304 
310  QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
311 
324  void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
325 
331  const QgsMapSettings &mapSettings() const;
332 
337  static const QString LABEL_CACHE_ID SIP_SKIP;
338 
344  static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
345 
346  signals:
347 
355  void renderingLayersFinished();
356 
358  void finished();
359 
360  protected:
361 
362  QgsMapSettings mSettings;
363  QElapsedTimer mRenderingStart;
364  Errors mErrors;
365 
366  QgsMapRendererCache *mCache = nullptr;
367 
368  int mRenderingTime = 0;
369 
371  QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
372 
378  QHash< QString, int > mLayerRenderingTimeHints;
379 
383  bool mRecordRenderingTime = true;
384 
390  bool prepareLabelCache() const SIP_SKIP;
391 
403  LayerRenderJobs prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
404 
410  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
411 
420  LayerRenderJobs prepareSecondPassJobs( LayerRenderJobs &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
421 
423  static QImage composeImage( const QgsMapSettings &settings,
424  const LayerRenderJobs &jobs,
425  const LabelRenderJob &labelJob,
426  const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
427 
429  static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
430 
437  static void composeSecondPass( LayerRenderJobs &secondPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
438 
440  void logRenderingTime( const LayerRenderJobs &jobs, const LayerRenderJobs &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
441 
443  void cleanupJobs( LayerRenderJobs &jobs ) SIP_SKIP;
444 
446  void cleanupSecondPassJobs( LayerRenderJobs &jobs ) SIP_SKIP;
447 
454  void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
455 
460  Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
461 
463  static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
464 
465  private:
466 
478  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
479 
480  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
481 
483  QImage *allocateImage( QString layerId );
484 
486  QPainter *allocateImageAndPainter( QString layerId, QImage *&image );
487 };
488 
489 
498 class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
499 {
500  Q_OBJECT
501 
502  public:
504 
506  virtual QImage renderedImage() = 0;
507 
508 };
509 
510 
511 #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:85
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.
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...
QgsMapRendererJob(const QgsMapSettings &settings)
int renderingTime() const
Returns the total time it took to finish the job (in milliseconds).
virtual void start()=0
Start the rendering job and immediately return.
QList< QgsMapRendererJob::Error > Errors
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.
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
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:1769
Error(const QString &lid, const QString &msg)