QGIS API Documentation  2.99.0-Master (90ae728)
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 
125  virtual void waitForFinished() = 0;
126 
128  virtual bool isActive() const = 0;
129 
137  virtual bool usedCachedLabels() const = 0;
138 
144  virtual QgsLabelingResults* takeLabelingResults() = 0;
145 
151  void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
152 
156  const QgsFeatureFilterProvider* featureFilterProvider() const { return mFeatureFilterProvider; }
157 
158  struct Error
159  {
160  Error( const QString& lid, const QString& msg )
161  : layerID( lid )
162  , message( msg )
163  {}
164 
165  QString layerID;
166  QString message;
167  };
168 
169  typedef QList<Error> Errors;
170 
172  Errors errors() const;
173 
174 
177  void setCache( QgsMapRendererCache* cache );
178 
181  void setRequestedGeometryCacheForLayers( const QStringList& layerIds ) { mRequestedGeomCacheForLayers = layerIds; }
182 
184  int renderingTime() const { return mRenderingTime; }
185 
191  const QgsMapSettings& mapSettings() const;
192 
197  static const QString LABEL_CACHE_ID;
198 
199  signals:
200 
208  void renderingLayersFinished();
209 
211  void finished();
212 
213  protected:
214 
217  Errors mErrors;
218 
219  QgsMapRendererCache* mCache = nullptr;
220 
221  int mRenderingTime = 0;
222 
228  bool prepareLabelCache() const;
229 
231  LayerRenderJobs prepareJobs( QPainter* painter, QgsLabelingEngine* labelingEngine2 );
232 
238  LabelRenderJob prepareLabelingJob( QPainter* painter, QgsLabelingEngine* labelingEngine2, bool canUseLabelCache = true );
239 
241  static QImage composeImage( const QgsMapSettings& settings, const LayerRenderJobs& jobs, const LabelRenderJob& labelJob );
242 
244  void logRenderingTime( const LayerRenderJobs& jobs, const LabelRenderJob& labelJob );
245 
247  void cleanupJobs( LayerRenderJobs& jobs );
248 
255  void cleanupLabelJob( LabelRenderJob& job );
256 
258  static void drawLabeling( const QgsMapSettings& settings, QgsRenderContext& renderContext, QgsLabelingEngine* labelingEngine2, QPainter* painter );
259 
260  private:
261 
268  static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
269 
270  bool needTemporaryImage( QgsMapLayer* ml );
271 
273  void updateLayerGeometryCaches();
274 
276  QStringList mRequestedGeomCacheForLayers;
278  QMap<QString, QgsGeometryCache> mGeometryCaches;
279 
280  const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
281 };
282 
283 
290 class CORE_EXPORT QgsMapRendererQImageJob : public QgsMapRendererJob
291 {
292  Q_OBJECT
293 
294  public:
295  QgsMapRendererQImageJob( const QgsMapSettings& settings );
296 
298  virtual QImage renderedImage() = 0;
299 };
300 
301 
302 #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)