QGIS API Documentation 4.3.0-Master (d835ed0a563)
Loading...
Searching...
No Matches
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 "qgslabelsink.h"
22#include "qgsmapsettings.h"
23#include "qgsmaskidprovider.h"
24#include "qgsrange.h"
25#include "qgsrendercontext.h"
26
27#include <QElapsedTimer>
28#include <QFutureWatcher>
29#include <QImage>
30#include <QObject>
31#include <QPainter>
32#include <QPicture>
33#include <QTime>
34
35class QPicture;
36
43class QgsElevationMap;
46
47#ifndef SIP_RUN
49
54class LayerRenderJob
55{
56 public:
57 LayerRenderJob() = default;
58
59 LayerRenderJob( const LayerRenderJob & ) = delete;
60 LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
61
62 LayerRenderJob( LayerRenderJob && );
63 LayerRenderJob &operator=( LayerRenderJob && );
64
71 void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
72
78 QgsRenderContext *context() { return mContext.get(); }
79
85 std::unique_ptr< QImage > destinationImage;
86
87 std::unique_ptr< QPainter > destinationPainter;
88
96 std::unique_ptr< QgsElevationMap > elevationMap;
97
105 std::unique_ptr< QImage > previewRenderImage;
106
107 std::unique_ptr< QPainter > previewRenderPainter;
108
110 bool imageInitialized = false;
111
117 bool previewRenderImageInitialized = false;
118
119 bool imageCanBeComposed() const;
120
122 std::unique_ptr< QgsMapLayerRenderer > renderer;
123
124 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
125
126 double opacity = 1.0;
127
129 bool cached = false;
130
132 bool renderAboveLabels = false;
133
135
142 bool completed = false;
143
145 int renderingTime = -1;
146
156 int estimatedRenderingTime = 0;
157
158 QStringList errors;
159
168 QString layerId;
169
184
186 std::unique_ptr<QPaintDevice> maskPaintDevice;
187
189 std::unique_ptr<QPainter> maskPainter;
190
195 bool maskRequiresLayerRasterization = false;
196
201 LayerRenderJob *firstPassJob = nullptr;
202
206 std::unique_ptr<QPicture> picture;
207
208 struct MaskJob
209 {
211 LayerRenderJob *layerRenderJob = nullptr;
212
214 int maskPaintDeviceId = -1;
215 };
216
220 std::vector<MaskJob> maskJobs;
221
222 private:
223 std::unique_ptr< QgsRenderContext > mContext;
224};
225
230struct LabelRenderJob
231{
232 QgsRenderContext context;
233
238 std::unique_ptr< QImage > img;
239
241 std::unique_ptr<QPicture> picture;
242
244 std::vector< std::unique_ptr<QPainter> > maskPainters;
245
256 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
257
262 QgsMaskIdProvider maskIdProvider;
263
265 bool cached = false;
267 bool canUseCache = false;
269 bool complete = false;
271 int renderingTime = -1;
273 QList< QPointer< QgsMapLayer > > participatingLayers;
274};
275
277#endif
278
305class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
306{
307 Q_OBJECT
308 public:
310
312
317 void start();
318
323 virtual void cancel() = 0;
324
330 virtual void cancelWithoutBlocking() = 0;
331
333 virtual void waitForFinished() = 0;
334
336 virtual bool isActive() const = 0;
337
344 virtual bool usedCachedLabels() const = 0;
345
354 QStringList layersRedrawnFromCache() const;
355
362
371
378 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
379
384 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
385
393 void setPerLayerTemporalRange( QHash<QgsMapLayer *, QgsDateTimeRange> perLayerTemporalRange ) SIP_SKIP { mPerLayerTemporalRange = perLayerTemporalRange; }
394
395 struct Error
396 {
397 Error( const QString &lid, const QString &msg )
398 : layerID( lid )
399 , message( msg )
400 {}
401
402 QString layerID;
403 QString message;
404 };
405
406 typedef QList<QgsMapRendererJob::Error> Errors;
407
409 Errors errors() const;
410
411
417
423 QgsLabelSink *labelSink() const SIP_SKIP { return mLabelSink; }
424
432 void setLabelSink( QgsLabelSink *sink ) SIP_SKIP { mLabelSink = sink; }
433
443
448 int renderingTime() const { return mRenderingTime; }
449
454 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
455
468 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
469
475
480 static const QString LABEL_CACHE_ID SIP_SKIP;
481
487 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
488
495
496#ifndef SIP_RUN
499
506#endif
507
508 signals:
509
517
518
526 void layerRenderingStarted( const QString &layerId );
527
535 void layerRendered( const QString &layerId );
536
538 void finished();
539
540 protected:
542 QElapsedTimer mRenderingStart;
544
546
548
550 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
551
557 QHash< QString, int > mLayerRenderingTimeHints;
558
562 QList< QPointer< QgsMapLayer > > mAdditionalLabelLayers;
563
568
569#ifndef SIP_RUN
570 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
571#endif
572
574
581
594
606 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
607
612 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
613
622 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
623
632
637 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
638
640 static QImage composeImage( const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
641
643 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
644
646 static QgsElevationMap layerElevationToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
647
654 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
655
657 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
658
660 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
661
663 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
664
670 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
671
676 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
677
679 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
680
681 private:
693 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
694
695 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
696
697 QHash<QgsMapLayer *, QgsDateTimeRange> mPerLayerTemporalRange;
698
700 std::unique_ptr< QImage > allocateImage( const QString &layerId );
701
703 std::unique_ptr< QgsElevationMap > allocateElevationMap( const QString &layerId );
704
706 std::tuple< std::unique_ptr< QImage >, std::unique_ptr< QPainter > > allocateImageAndPainter( const QString &layerId, const QgsRenderContext &context );
707
713 virtual void startPrivate() = 0;
714
715 QgsLabelSink *mLabelSink = nullptr;
716 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
717
719 std::tuple<std::unique_ptr<QPicture>, std::unique_ptr< QPainter > > allocatePictureAndPainter( const QgsRenderContext &context );
720};
721
722
731{
732 Q_OBJECT
733
734 public:
736
738 virtual QImage renderedImage() = 0;
739};
740
741
742#endif // QGSMAPRENDERERJOB_H
Handles coordinate transforms between two coordinate systems.
Stores a digital elevation model in a raster image which may get updated as a part of the map layer r...
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.
QgsFeedback subclass for granular reporting of labeling engine progress.
Provides map labeling functionality.
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:83
Responsible for keeping a cache of rendered images resulting from a map rendering job.
void logRenderingTime(const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob)
QList< QPointer< QgsMapLayer > > participatingLabelLayers(QgsLabelingEngine *engine)
Returns a list of the layers participating in the map labeling.
static QgsElevationMap layerElevationToBeComposed(const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache)
virtual void waitForFinished()=0
Block until the job has finished.
static QImage composeImage(const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache=nullptr)
void cleanupSecondPassJobs(std::vector< LayerRenderJob > &jobs)
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...
void initSecondPassJobs(std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob) const
Initialize secondPassJobs according to what have been rendered (mask clipping path e....
static QImage layerImageToBeComposed(const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache)
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...
static Q_DECL_DEPRECATED void drawLabeling(const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter)
void layerRendered(const QString &layerId)
Emitted when a layer has completed rendering.
static const QString LABEL_PREVIEW_CACHE_ID
QgsMapRendererCache ID string for cached label image during preview compositions only.
QList< QPointer< QgsMapLayer > > mAdditionalLabelLayers
Additional layers participating in labeling problem.
std::vector< LayerRenderJob > prepareJobs(QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet=false)
Creates a list of layer rendering jobs and prepares them for later render.
void renderingLayersFinished()
Emitted when the layers are rendered.
void cleanupJobs(std::vector< LayerRenderJob > &jobs)
QElapsedTimer mRenderingStart
const QgsMapSettings & mapSettings() const
Returns map settings with which this job was started.
QgsMapRendererCache * mCache
void setPerLayerTemporalRange(QHash< QgsMapLayer *, QgsDateTimeRange > perLayerTemporalRange)
Set a temporal range used by the QgsRenderContext of each LayerRenderJob If set this temporal range o...
void finished()
emitted when asynchronous rendering is finished (or canceled).
bool labelingHasNonDefaultCompositionModes() const
Returns true if any component of the map labeling requires non-default composition modes.
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.
bool mRecordRenderingTime
true if layer rendering time should be recorded.
int renderingTime() const
Returns the total time it took to finish the job (in milliseconds).
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
static const QString LABEL_CACHE_ID
QgsMapRendererCache ID string for cached label image.
static const QString ELEVATION_MAP_CACHE_PREFIX
QgsMapRendererCache prefix string for cached elevation map image.
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.
static void composeSecondPass(std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector=false)
Compose second pass images into first pass images.
virtual QgsLabelingResults * takeLabelingResults()=0
Gets pointer to internal labeling engine (in order to get access to the results).
void setFeatureFilterProvider(const QgsFeatureFilterProvider *f)
Set the feature filter provider used by the QgsRenderContext of each LayerRenderJob.
virtual void cancel()=0
Stop the rendering job - does not return until the job has terminated.
std::vector< LayerRenderJob > prepareSecondPassJobs(std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob)
Prepares jobs for a second pass, if selective masks exist (from labels or symbol layers).
static const QgsSettingsEntryString * settingsMaskBackend
Settings entry for mask painting backend engine.
LabelRenderJob prepareLabelingJob(QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache=true)
Prepares a labeling job.
void setLayerRenderingTimeHints(const QHash< QString, int > &hints)
Sets approximate render times (in ms) for map layers.
void cleanupLabelJob(LabelRenderJob &job)
Handles clean up tasks for a label job, including deletion of images and storing cached label results...
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.
QgsMapRendererQImageJob(const QgsMapSettings &settings)
virtual QImage renderedImage()=0
Gets a preview/resulting image.
Contains configuration for rendering maps.
A rectangle specified with double values.
Contains information about the context of a rendering operation.
Stores collated details of rendered items during a map rendering operation.
A boolean settings entry.
A string settings entry.
#define SIP_SKIP
Definition qgis_sip.h:138
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_ABSTRACT
Definition qgis_sip.h:225
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
QgsTemporalRange< QDateTime > QgsDateTimeRange
QgsRange which stores a range of date times.
Definition qgsrange.h:764
Error(const QString &lid, const QString &msg)