QGIS API Documentation 4.3.0-Master (0cfde48c85b)
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 QImage *img = nullptr;
86
94 QgsElevationMap *elevationMap = nullptr;
95
103 QImage *previewRenderImage = nullptr;
104
106 bool imageInitialized = false;
107
113 bool previewRenderImageInitialized = false;
114
115 bool imageCanBeComposed() const;
116
117 QgsMapLayerRenderer *renderer = nullptr; // must be deleted
118
119 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
120
121 double opacity = 1.0;
122
124 bool cached = false;
125
127 bool renderAboveLabels = false;
128
130
137 bool completed = false;
138
140 int renderingTime = -1;
141
151 int estimatedRenderingTime = 0;
152
153 QStringList errors;
154
163 QString layerId;
164
179
181 std::unique_ptr<QPainter> maskPainter;
182
183
185 std::unique_ptr<QPaintDevice> maskPaintDevice;
186
191 bool maskRequiresLayerRasterization = false;
192
197 LayerRenderJob *firstPassJob = nullptr;
198
202 std::unique_ptr<QPicture> picture;
203
210 QList<QPair<LayerRenderJob *, int>> maskJobs;
211
212 private:
213 std::unique_ptr< QgsRenderContext > mContext;
214};
215
220struct LabelRenderJob
221{
222 QgsRenderContext context;
223
228 QImage *img = nullptr;
229
231 std::unique_ptr<QPicture> picture;
232
234 std::vector< std::unique_ptr<QPainter> > maskPainters;
235
246 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
247
252 QgsMaskIdProvider maskIdProvider;
253
255 bool cached = false;
257 bool canUseCache = false;
259 bool complete = false;
261 int renderingTime = -1;
263 QList< QPointer< QgsMapLayer > > participatingLayers;
264};
265
267#endif
268
295class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
296{
297 Q_OBJECT
298 public:
300
302
307 void start();
308
313 virtual void cancel() = 0;
314
320 virtual void cancelWithoutBlocking() = 0;
321
323 virtual void waitForFinished() = 0;
324
326 virtual bool isActive() const = 0;
327
334 virtual bool usedCachedLabels() const = 0;
335
344 QStringList layersRedrawnFromCache() const;
345
352
361
368 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
369
374 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
375
383 void setPerLayerTemporalRange( QHash<QgsMapLayer *, QgsDateTimeRange> perLayerTemporalRange ) SIP_SKIP { mPerLayerTemporalRange = perLayerTemporalRange; }
384
385 struct Error
386 {
387 Error( const QString &lid, const QString &msg )
388 : layerID( lid )
389 , message( msg )
390 {}
391
392 QString layerID;
393 QString message;
394 };
395
396 typedef QList<QgsMapRendererJob::Error> Errors;
397
399 Errors errors() const;
400
401
407
413 QgsLabelSink *labelSink() const SIP_SKIP { return mLabelSink; }
414
422 void setLabelSink( QgsLabelSink *sink ) SIP_SKIP { mLabelSink = sink; }
423
433
438 int renderingTime() const { return mRenderingTime; }
439
444 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
445
458 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
459
465
470 static const QString LABEL_CACHE_ID SIP_SKIP;
471
477 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
478
485
486#ifndef SIP_RUN
489
496#endif
497
498 signals:
499
507
508
516 void layerRenderingStarted( const QString &layerId );
517
525 void layerRendered( const QString &layerId );
526
528 void finished();
529
530 protected:
532 QElapsedTimer mRenderingStart;
534
536
538
540 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
541
547 QHash< QString, int > mLayerRenderingTimeHints;
548
552 QList< QPointer< QgsMapLayer > > mAdditionalLabelLayers;
553
558
559#ifndef SIP_RUN
560 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
561#endif
562
564
571
584
596 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
597
602 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
603
612 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
613
622
627 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
628
630 static QImage composeImage( const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
631
633 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
634
636 static QgsElevationMap layerElevationToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
637
644 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
645
647 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
648
650 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
651
653 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
654
660 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
661
666 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
667
669 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
670
671 private:
683 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
684
685 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
686
687 QHash<QgsMapLayer *, QgsDateTimeRange> mPerLayerTemporalRange;
688
690 QImage *allocateImage( QString layerId );
691
693 QgsElevationMap *allocateElevationMap( QString layerId );
694
696 QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
697
703 virtual void startPrivate() = 0;
704
705 QgsLabelSink *mLabelSink = nullptr;
706 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
707
708 typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
709
711 PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
712};
713
714
723{
724 Q_OBJECT
725
726 public:
728
730 virtual QImage renderedImage() = 0;
731};
732
733
734#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)