QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgsrendercontext.h"
25
26#include <QElapsedTimer>
27#include <QFutureWatcher>
28#include <QImage>
29#include <QObject>
30#include <QPainter>
31#include <QPicture>
32#include <QTime>
33
34class QPicture;
35
42class QgsElevationMap;
45
46#ifndef SIP_RUN
48
53class LayerRenderJob
54{
55 public:
56 LayerRenderJob() = default;
57
58 LayerRenderJob( const LayerRenderJob & ) = delete;
59 LayerRenderJob &operator=( const LayerRenderJob & ) = delete;
60
61 LayerRenderJob( LayerRenderJob && );
62 LayerRenderJob &operator=( LayerRenderJob && );
63
70 void setContext( std::unique_ptr< QgsRenderContext > context ) { mContext = std::move( context ); }
71
77 QgsRenderContext *context() { return mContext.get(); }
78
84 QImage *img = nullptr;
85
93 QgsElevationMap *elevationMap = nullptr;
94
102 QImage *previewRenderImage = nullptr;
103
105 bool imageInitialized = false;
106
112 bool previewRenderImageInitialized = false;
113
114 bool imageCanBeComposed() const;
115
116 QgsMapLayerRenderer *renderer = nullptr; // must be deleted
117
118 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
119
120 double opacity = 1.0;
121
123 bool cached = false;
124
126 bool renderAboveLabels = false;
127
129
136 bool completed = false;
137
139 int renderingTime = -1;
140
150 int estimatedRenderingTime = 0;
151
152 QStringList errors;
153
162 QString layerId;
163
178
180 std::unique_ptr<QPainter> maskPainter;
181
182
184 std::unique_ptr<QPaintDevice> maskPaintDevice;
185
190 bool maskRequiresLayerRasterization = false;
191
196 LayerRenderJob *firstPassJob = nullptr;
197
201 std::unique_ptr<QPicture> picture;
202
209 QList<QPair<LayerRenderJob *, int>> maskJobs;
210
211 private:
212 std::unique_ptr< QgsRenderContext > mContext;
213};
214
219struct LabelRenderJob
220{
221 QgsRenderContext context;
222
227 QImage *img = nullptr;
228
230 std::unique_ptr<QPicture> picture;
231
233 std::vector< std::unique_ptr<QPainter> > maskPainters;
234
245 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
246
251 QgsMaskIdProvider maskIdProvider;
252
254 bool cached = false;
256 bool canUseCache = false;
258 bool complete = false;
260 int renderingTime = -1;
262 QList< QPointer< QgsMapLayer > > participatingLayers;
263};
264
266#endif
267
294class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
295{
296 Q_OBJECT
297 public:
299
301
306 void start();
307
312 virtual void cancel() = 0;
313
319 virtual void cancelWithoutBlocking() = 0;
320
322 virtual void waitForFinished() = 0;
323
325 virtual bool isActive() const = 0;
326
333 virtual bool usedCachedLabels() const = 0;
334
343 QStringList layersRedrawnFromCache() const;
344
351
360
367 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
368
373 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
374
375 struct Error
376 {
377 Error( const QString &lid, const QString &msg )
378 : layerID( lid )
379 , message( msg )
380 {}
381
382 QString layerID;
383 QString message;
384 };
385
386 typedef QList<QgsMapRendererJob::Error> Errors;
387
389 Errors errors() const;
390
391
397
403 QgsLabelSink *labelSink() const SIP_SKIP { return mLabelSink; }
404
412 void setLabelSink( QgsLabelSink *sink ) SIP_SKIP { mLabelSink = sink; }
413
423
428 int renderingTime() const { return mRenderingTime; }
429
434 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
435
448 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
449
455
460 static const QString LABEL_CACHE_ID SIP_SKIP;
461
467 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
468
475
476#ifndef SIP_RUN
479
486#endif
487
488 signals:
489
497
498
506 void layerRenderingStarted( const QString &layerId );
507
515 void layerRendered( const QString &layerId );
516
518 void finished();
519
520 protected:
522 QElapsedTimer mRenderingStart;
524
526
528
530 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
531
537 QHash< QString, int > mLayerRenderingTimeHints;
538
542 QList< QPointer< QgsMapLayer > > mAdditionalLabelLayers;
543
548
549#ifndef SIP_RUN
550 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
551#endif
552
554
561
574
586 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
587
592 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
593
602 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
603
612
617 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
618
620 static QImage composeImage( const QgsMapSettings &settings, const std::vector< LayerRenderJob > &jobs, const LabelRenderJob &labelJob, const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
621
623 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
624
626 static QgsElevationMap layerElevationToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
627
634 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
635
637 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
638
640 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
641
643 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
644
650 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
651
656 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
657
659 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
660
661 private:
673 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
674
675 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
676
678 QImage *allocateImage( QString layerId );
679
681 QgsElevationMap *allocateElevationMap( QString layerId );
682
684 QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
685
691 virtual void startPrivate() = 0;
692
693 QgsLabelSink *mLabelSink = nullptr;
694 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
695
696 typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
697
699 PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
700};
701
702
711{
712 Q_OBJECT
713
714 public:
716
718 virtual QImage renderedImage() = 0;
719};
720
721
722#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 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:133
#define SIP_TRANSFER
Definition qgis_sip.h:35
#define SIP_ABSTRACT
Definition qgis_sip.h:220
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Error(const QString &lid, const QString &msg)