QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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#include <QPicture>
28
29#include "qgsrendercontext.h"
30#include "qgslabelsink.h"
31#include "qgsmapsettings.h"
32#include "qgsmaskidprovider.h"
34
35class QPicture;
36
43
44#ifndef SIP_RUN
46
51class LayerRenderJob
52{
53 public:
54
55 LayerRenderJob() = default;
56
58 LayerRenderJob( const LayerRenderJob & ) = delete;
59
61 LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
62
63 LayerRenderJob( LayerRenderJob && );
64 LayerRenderJob &operator=( LayerRenderJob && );
65
72 void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
73
79 QgsRenderContext *context() { return mContext.get(); }
80
86 QImage *img = nullptr;
87
89 bool imageInitialized = false;
90
91 bool imageCanBeComposed() const;
92
93 QgsMapLayerRenderer *renderer = nullptr; // must be deleted
94
95 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
96
97 double opacity = 1.0;
98
100 bool cached = false;
101
103
110 bool completed = false;
111
113 int renderingTime = -1;
114
124 int estimatedRenderingTime = 0;
125
126 QStringList errors;
127
136 QString layerId;
137
154 std::unique_ptr<QPainter> maskPainter;
155
156
158 std::unique_ptr<QPaintDevice> maskPaintDevice;
159
164 bool maskRequiresLayerRasterization = false;
165
170 LayerRenderJob *firstPassJob = nullptr;
171
175 std::unique_ptr<QPicture> picture;
176
183 QList<QPair<LayerRenderJob *, int>> maskJobs;
184
185 private:
186 std::unique_ptr< QgsRenderContext > mContext;
187
188};
189
194struct LabelRenderJob
195{
196 QgsRenderContext context;
197
202 QImage *img = nullptr;
203
205 std::unique_ptr<QPicture> picture;
206
208 std::vector< std::unique_ptr<QPainter> > maskPainters;
209
220 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
221
226 QgsMaskIdProvider maskIdProvider;
227
229 bool cached = false;
231 bool canUseCache = false;
233 bool complete = false;
235 int renderingTime = -1;
237 QList< QPointer< QgsMapLayer > > participatingLayers;
238};
239
241#endif
242
270class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
271{
272 Q_OBJECT
273 public:
274
276
278
283 void start();
284
289 virtual void cancel() = 0;
290
296 virtual void cancelWithoutBlocking() = 0;
297
299 virtual void waitForFinished() = 0;
300
302 virtual bool isActive() const = 0;
303
311 virtual bool usedCachedLabels() const = 0;
312
321 QStringList layersRedrawnFromCache() const;
322
329
338
346 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
347
353 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
354
355 struct Error
356 {
357 Error( const QString &lid, const QString &msg )
358 : layerID( lid )
359 , message( msg )
360 {}
361
362 QString layerID;
363 QString message;
364 };
365
366 typedef QList<QgsMapRendererJob::Error> Errors;
367
369 Errors errors() const;
370
371
377
383 QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
384
392 void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
393
403
408 int renderingTime() const { return mRenderingTime; }
409
415 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
416
429 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
430
436 const QgsMapSettings &mapSettings() const;
437
442 static const QString LABEL_CACHE_ID SIP_SKIP;
443
449 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
450
451#ifndef SIP_RUN
453 static const inline QgsSettingsEntryBool settingsLogCanvasRefreshEvent = QgsSettingsEntryBool( QStringLiteral( "logCanvasRefreshEvent" ), QgsSettings::Prefix::MAP, false );
454#endif
455
456 signals:
457
466
467
475 void layerRenderingStarted( const QString &layerId );
476
484 void layerRendered( const QString &layerId );
485
487 void finished();
488
489 protected:
490
492 QElapsedTimer mRenderingStart;
494
495 QgsMapRendererCache *mCache = nullptr;
496
497 int mRenderingTime = 0;
498
500 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
501
507 QHash< QString, int > mLayerRenderingTimeHints;
508
512 bool mRecordRenderingTime = true;
513
514#ifndef SIP_RUN
515 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
516#endif
517
519
526
538 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
539
545 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
546
555 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
556
561 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
562
564 static QImage composeImage( const QgsMapSettings &settings,
565 const std::vector< LayerRenderJob > &jobs,
566 const LabelRenderJob &labelJob,
567 const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
568
570 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
571
578 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
579
581 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
582
584 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
585
587 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
588
595 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
596
601 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
602
604 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
605
606 private:
607
619 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
620
621 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
622
624 QImage *allocateImage( QString layerId );
625
627 QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
628
634 virtual void startPrivate() = 0;
635
636 QgsLabelSink *mLabelSink = nullptr;
637 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
638
639 typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
640
642 PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
643};
644
645
655{
656 Q_OBJECT
657
658 public:
660
662 virtual QImage renderedImage() = 0;
663
664};
665
666
667#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.
Abstract base class that can be used to intercept rendered labels from a labeling / rendering job.
Definition: qgslabelsink.h:38
QgsFeedback subclass for granular reporting of labeling engine progress.
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:73
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.
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.
void setLabelSink(QgsLabelSink *sink)
Assigns the label sink which will take over responsibility for handling labels during the rendering j...
QHash< QString, int > mLayerRenderingTimeHints
Approximate expected layer rendering time per layer, by layer ID.
std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults
Errors errors() const
List of errors that happened during the rendering job - available when the rendering has been finishe...
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
void renderingLayersFinished()
Emitted when the layers are rendered.
QElapsedTimer mRenderingStart
void finished()
emitted when asynchronous rendering is finished (or canceled).
QgsMapSettings mSettings
const QgsFeatureFilterProvider * featureFilterProvider() const
Returns the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
QgsMapRendererJob(const QgsMapSettings &settings)
~QgsMapRendererJob() override
void start()
Start the rendering job and immediately return.
void layerRenderingStarted(const QString &layerId)
Emitted just before rendering starts for a particular layer.
QStringList mLayersRedrawnFromCache
QStringList layersRedrawnFromCache() const
Returns a list of the layer IDs for all layers which were redrawn from cached images.
QList< QgsMapRendererJob::Error > Errors
QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime
Render time (in ms) per layer, by layer ID.
QgsRenderedItemResults * takeRenderedItemResults()
Takes the rendered item results from the map render job and returns them.
virtual bool isActive() const =0
Tell whether the rendering job is currently running in background.
QgsLabelingEngineFeedback * labelingEngineFeedback()
Returns the associated labeling engine feedback object.
virtual QgsLabelingResults * takeLabelingResults()=0
Gets pointer to internal labeling engine (in order to get access to the results).
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.
QgsLabelSink * labelSink() const
Returns the label sink associated to this rendering job.
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.
Stores collated details of rendered items during a map rendering operation.
A boolean settings entry.
static const char * MAP
Definition: qgssettings.h:98
std::unique_ptr< GEOSGeometry, GeosDeleter > unique_ptr
Scoped GEOS pointer.
Definition: qgsgeos.h:74
#define SIP_SKIP
Definition: qgis_sip.h:126
#define SIP_TRANSFER
Definition: qgis_sip.h:36
#define SIP_ABSTRACT
Definition: qgis_sip.h:208
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Definition: qgsmaplayer.h:2147
Error(const QString &lid, const QString &msg)