QGIS API Documentation  2.99.0-Master (b8fd1fd)
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 <QtConcurrentRun>
21 #include <QFutureWatcher>
22 #include <QImage>
23 #include <QPainter>
24 #include <QObject>
25 #include <QTime>
26 
27 #include "qgsrendercontext.h"
28 
29 #include "qgsmapsettings.h"
30 
31 #include "qgsgeometrycache.h"
32 
33 class QgsLabelingEngine;
34 class QgsLabelingResults;
37 class QgsPalLabeling;
39 
41 
45 struct LayerRenderJob
46 {
47  QgsRenderContext context;
48  QImage *img; // may be null if it is not necessary to draw to separate image (e.g. sequential rendering)
49  QgsMapLayerRenderer *renderer; // must be deleted
50  QPainter::CompositionMode blendMode;
51  double opacity;
52  bool cached; // if true, img already contains cached image from previous rendering
54  int renderingTime;
55 };
56 
57 typedef QList<LayerRenderJob> LayerRenderJobs;
58 
62 struct LabelRenderJob
63 {
64  QgsRenderContext context;
65 
70  QImage *img = nullptr;
72  bool cached = false;
74  bool canUseCache = false;
76  bool complete = false;
78  int renderingTime = -1;
80  QList< QPointer< QgsMapLayer > > participatingLayers;
81 };
82 
84 
109 class CORE_EXPORT QgsMapRendererJob : public QObject
110 {
111  Q_OBJECT
112  public:
113 
114  QgsMapRendererJob( const QgsMapSettings &settings );
115 
118  virtual void start() = 0;
119 
122  virtual void cancel() = 0;
123 
129  virtual void cancelWithoutBlocking() = 0;
130 
132  virtual void waitForFinished() = 0;
133 
135  virtual bool isActive() const = 0;
136 
144  virtual bool usedCachedLabels() const = 0;
145 
151  virtual QgsLabelingResults *takeLabelingResults() = 0;
152 
158  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
159 
163  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
164 
165  struct Error
166  {
167  Error( const QString &lid, const QString &msg )
168  : layerID( lid )
169  , message( msg )
170  {}
171 
172  QString layerID;
173  QString message;
174  };
175 
176  typedef QList<Error> Errors;
177 
179  Errors errors() const;
180 
181 
184  void setCache( QgsMapRendererCache *cache );
185 
188  void setRequestedGeometryCacheForLayers( const QStringList &layerIds ) { mRequestedGeomCacheForLayers = layerIds; }
189 
191  int renderingTime() const { return mRenderingTime; }
192 
198  const QgsMapSettings &mapSettings() const;
199 
204  static const QString LABEL_CACHE_ID;
205 
206  signals:
207 
215  void renderingLayersFinished();
216 
218  void finished();
219 
220  protected:
221 
224  Errors mErrors;
225 
226  QgsMapRendererCache *mCache = nullptr;
227 
228  int mRenderingTime = 0;
229 
235  bool prepareLabelCache() const;
236 
238  LayerRenderJobs prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2 );
239 
245  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true );
246 
248  static QImage composeImage( const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob );
249 
251  void logRenderingTime( const LayerRenderJobs &jobs, const LabelRenderJob &labelJob );
252 
254  void cleanupJobs( LayerRenderJobs &jobs );
255 
262  void cleanupLabelJob( LabelRenderJob &job );
263 
265  static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter );
266 
267  private:
268 
275  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
276 
277  bool needTemporaryImage( QgsMapLayer *ml );
278 
280  void updateLayerGeometryCaches();
281 
283  QStringList mRequestedGeomCacheForLayers;
285  QMap<QString, QgsGeometryCache> mGeometryCaches;
286 
287  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
288 };
289 
290 
297 class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
298 {
299  Q_OBJECT
300 
301  public:
302  QgsMapRendererQImageJob( const QgsMapSettings &settings );
303 
305  virtual QImage renderedImage() = 0;
306 
307 };
308 
309 
310 #endif // QGSMAPRENDERERJOB_H
void setRequestedGeometryCacheForLayers(const QStringList &layerIds)
Set which vector layers should be cached while rendering.
A rectangle specified with double values.
Definition: qgsrectangle.h:36
Base class for all map layer types.
Definition: qgsmaplayer.h:52
Abstract base class for map rendering implementations.
QList< Error > Errors
static const QString LABEL_CACHE_ID
QgsMapRendererCache ID string for cached label image.
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:976
int renderingTime() const
Find out how long it took to finish the job (in milliseconds)
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
The QgsMapSettings class contains configuration for rendering of the map.
const QgsFeatureFilterProvider * featureFilterProvider() const
Abstract interface for use by classes that filter the features of a layer.
QgsMapSettings mSettings
The QgsLabelingEngine class provides map labeling functionality.
Contains information about the context of a rendering operation.
Intermediate base class adding functionality that allows client to query the rendered image...
Class for doing transforms between two map coordinate systems.
Base class for utility classes that encapsulate information necessary for rendering of map layers...
Class that stores computed placement from labeling engine.
This class is responsible for keeping cache of rendered images resulting from a map rendering job...
Error(const QString &lid, const QString &msg)