QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
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"
33
34class QPicture;
35
42class QgsElevationMap;
44
45#ifndef SIP_RUN
47
52class LayerRenderJob
53{
54 public:
55
56 LayerRenderJob() = default;
57
59 LayerRenderJob( const LayerRenderJob & ) = delete;
60
62 LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
63
64 LayerRenderJob( LayerRenderJob && );
65 LayerRenderJob &operator=( LayerRenderJob && );
66
73 void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
74
80 QgsRenderContext *context() { return mContext.get(); }
81
87 QImage *img = nullptr;
88
96 QgsElevationMap *elevationMap = nullptr;
97
99 bool imageInitialized = false;
100
101 bool imageCanBeComposed() const;
102
103 QgsMapLayerRenderer *renderer = nullptr; // must be deleted
104
105 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
106
107 double opacity = 1.0;
108
110 bool cached = false;
111
113 bool renderAboveLabels = false;
114
116
123 bool completed = false;
124
126 int renderingTime = -1;
127
137 int estimatedRenderingTime = 0;
138
139 QStringList errors;
140
149 QString layerId;
150
167 std::unique_ptr<QPainter> maskPainter;
168
169
171 std::unique_ptr<QPaintDevice> maskPaintDevice;
172
177 bool maskRequiresLayerRasterization = false;
178
183 LayerRenderJob *firstPassJob = nullptr;
184
188 std::unique_ptr<QPicture> picture;
189
196 QList<QPair<LayerRenderJob *, int>> maskJobs;
197
198 private:
199 std::unique_ptr< QgsRenderContext > mContext;
200
201};
202
207struct LabelRenderJob
208{
209 QgsRenderContext context;
210
215 QImage *img = nullptr;
216
218 std::unique_ptr<QPicture> picture;
219
221 std::vector< std::unique_ptr<QPainter> > maskPainters;
222
233 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
234
239 QgsMaskIdProvider maskIdProvider;
240
242 bool cached = false;
244 bool canUseCache = false;
246 bool complete = false;
248 int renderingTime = -1;
250 QList< QPointer< QgsMapLayer > > participatingLayers;
251};
252
254#endif
255
283class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
284{
285 Q_OBJECT
286 public:
287
289
291
296 void start();
297
302 virtual void cancel() = 0;
303
309 virtual void cancelWithoutBlocking() = 0;
310
312 virtual void waitForFinished() = 0;
313
315 virtual bool isActive() const = 0;
316
324 virtual bool usedCachedLabels() const = 0;
325
334 QStringList layersRedrawnFromCache() const;
335
342
351
359 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
360
366 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
367
368 struct Error
369 {
370 Error( const QString &lid, const QString &msg )
371 : layerID( lid )
372 , message( msg )
373 {}
374
375 QString layerID;
376 QString message;
377 };
378
379 typedef QList<QgsMapRendererJob::Error> Errors;
380
382 Errors errors() const;
383
384
390
396 QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
397
405 void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
406
416
421 int renderingTime() const { return mRenderingTime; }
422
428 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
429
442 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
443
449 const QgsMapSettings &mapSettings() const;
450
455 static const QString LABEL_CACHE_ID SIP_SKIP;
456
462 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
463
469 static const QString ELEVATION_MAP_CACHE_PREFIX SIP_SKIP;
470
471#ifndef SIP_RUN
474#endif
475
476 signals:
477
486
487
495 void layerRenderingStarted( const QString &layerId );
496
504 void layerRendered( const QString &layerId );
505
507 void finished();
508
509 protected:
510
512 QElapsedTimer mRenderingStart;
514
515 QgsMapRendererCache *mCache = nullptr;
516
517 int mRenderingTime = 0;
518
520 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
521
527 QHash< QString, int > mLayerRenderingTimeHints;
528
532 bool mRecordRenderingTime = true;
533
534#ifndef SIP_RUN
535 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
536#endif
537
539
546
558 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
559
565 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
566
575 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
576
581 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
582
584 static QImage composeImage( const QgsMapSettings &settings,
585 const std::vector< LayerRenderJob > &jobs,
586 const LabelRenderJob &labelJob,
587 const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
588
590 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
591
593 static QgsElevationMap layerElevationToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
594
601 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
602
604 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
605
607 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
608
610 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
611
618 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
619
624 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
625
627 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
628
629 private:
630
642 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
643
644 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
645
647 QImage *allocateImage( QString layerId );
648
650 QgsElevationMap *allocateElevationMap( QString layerId );
651
653 QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
654
660 virtual void startPrivate() = 0;
661
662 QgsLabelSink *mLabelSink = nullptr;
663 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
664
665 typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
666
668 PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
669};
670
671
681{
682 Q_OBJECT
683
684 public:
686
688 virtual QImage renderedImage() = 0;
689
690};
691
692
693#endif // QGSMAPRENDERERJOB_H
Class for doing transforms between two map coordinate systems.
Stores digital elevation model in a raster image which may get updated as a part of map layer renderi...
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.
static const QgsSettingsEntryBool * settingsLogCanvasRefreshEvent
Settings entry log canvas refresh event.
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.
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:2206
Error(const QString &lid, const QString &msg)