QGIS API Documentation  2.99.0-Master (cb63e82)
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;
38 
40 
44 struct LayerRenderJob
45 {
46  QgsRenderContext context;
47  QImage *img; // may be null if it is not necessary to draw to separate image (e.g. sequential rendering)
49  bool imageInitialized = false;
50  QgsMapLayerRenderer *renderer; // must be deleted
51  QPainter::CompositionMode blendMode;
52  double opacity;
53  bool cached; // if true, img already contains cached image from previous rendering
55  int renderingTime;
56 };
57 
58 typedef QList<LayerRenderJob> LayerRenderJobs;
59 
63 struct LabelRenderJob
64 {
65  QgsRenderContext context;
66 
71  QImage *img = nullptr;
73  bool cached = false;
75  bool canUseCache = false;
77  bool complete = false;
79  int renderingTime = -1;
81  QList< QPointer< QgsMapLayer > > participatingLayers;
82 };
83 
85 
110 class CORE_EXPORT QgsMapRendererJob : public QObject
111 {
112  Q_OBJECT
113  public:
114 
115  QgsMapRendererJob( const QgsMapSettings &settings );
116 
119  virtual void start() = 0;
120 
123  virtual void cancel() = 0;
124 
130  virtual void cancelWithoutBlocking() = 0;
131 
133  virtual void waitForFinished() = 0;
134 
136  virtual bool isActive() const = 0;
137 
145  virtual bool usedCachedLabels() const = 0;
146 
152  virtual QgsLabelingResults *takeLabelingResults() = 0;
153 
159  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
160 
164  const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
165 
166  struct Error
167  {
168  Error( const QString &lid, const QString &msg )
169  : layerID( lid )
170  , message( msg )
171  {}
172 
173  QString layerID;
174  QString message;
175  };
176 
177  typedef QList<Error> Errors;
178 
180  Errors errors() const;
181 
182 
185  void setCache( QgsMapRendererCache *cache );
186 
189  void setRequestedGeometryCacheForLayers( const QStringList &layerIds ) { mRequestedGeomCacheForLayers = layerIds; }
190 
192  int renderingTime() const { return mRenderingTime; }
193 
199  const QgsMapSettings &mapSettings() const;
200 
205  static const QString LABEL_CACHE_ID;
206 
207  signals:
208 
216  void renderingLayersFinished();
217 
219  void finished();
220 
221  protected:
222 
225  Errors mErrors;
226 
227  QgsMapRendererCache *mCache = nullptr;
228 
229  int mRenderingTime = 0;
230 
236  bool prepareLabelCache() const;
237 
239  LayerRenderJobs prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2 );
240 
246  LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true );
247 
249  static QImage composeImage( const QgsMapSettings &settings, const LayerRenderJobs &jobs, const LabelRenderJob &labelJob );
250 
252  void logRenderingTime( const LayerRenderJobs &jobs, const LabelRenderJob &labelJob );
253 
255  void cleanupJobs( LayerRenderJobs &jobs );
256 
263  void cleanupLabelJob( LabelRenderJob &job );
264 
266  static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter );
267 
268  private:
269 
276  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
277 
278  bool needTemporaryImage( QgsMapLayer *ml );
279 
281  void updateLayerGeometryCaches();
282 
284  QStringList mRequestedGeomCacheForLayers;
286  QMap<QString, QgsGeometryCache> mGeometryCaches;
287 
288  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
289 };
290 
291 
298 class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
299 {
300  Q_OBJECT
301 
302  public:
303  QgsMapRendererQImageJob( const QgsMapSettings &settings );
304 
306  virtual QImage renderedImage() = 0;
307 
308 };
309 
310 
311 #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:38
Base class for all map layer types.
Definition: qgsmaplayer.h:53
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:1033
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)