QGIS API Documentation 3.36.0-Maidenhead (09951dc0acf)
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 <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
105 QImage *previewRenderImage = nullptr;
106
108 bool imageInitialized = false;
109
115 bool previewRenderImageInitialized = false;
116
117 bool imageCanBeComposed() const;
118
119 QgsMapLayerRenderer *renderer = nullptr; // must be deleted
120
121 QPainter::CompositionMode blendMode = QPainter::CompositionMode_SourceOver;
122
123 double opacity = 1.0;
124
126 bool cached = false;
127
129 bool renderAboveLabels = false;
130
132
139 bool completed = false;
140
142 int renderingTime = -1;
143
153 int estimatedRenderingTime = 0;
154
155 QStringList errors;
156
165 QString layerId;
166
183 std::unique_ptr<QPainter> maskPainter;
184
185
187 std::unique_ptr<QPaintDevice> maskPaintDevice;
188
193 bool maskRequiresLayerRasterization = false;
194
199 LayerRenderJob *firstPassJob = nullptr;
200
204 std::unique_ptr<QPicture> picture;
205
212 QList<QPair<LayerRenderJob *, int>> maskJobs;
213
214 private:
215 std::unique_ptr< QgsRenderContext > mContext;
216
217};
218
223struct LabelRenderJob
224{
225 QgsRenderContext context;
226
231 QImage *img = nullptr;
232
234 std::unique_ptr<QPicture> picture;
235
237 std::vector< std::unique_ptr<QPainter> > maskPainters;
238
249 std::vector< std::unique_ptr<QPaintDevice> > maskPaintDevices;
250
255 QgsMaskIdProvider maskIdProvider;
256
258 bool cached = false;
260 bool canUseCache = false;
262 bool complete = false;
264 int renderingTime = -1;
266 QList< QPointer< QgsMapLayer > > participatingLayers;
267};
268
270#endif
271
298class CORE_EXPORT QgsMapRendererJob : public QObject SIP_ABSTRACT
299{
300 Q_OBJECT
301 public:
302
304
306
311 void start();
312
317 virtual void cancel() = 0;
318
324 virtual void cancelWithoutBlocking() = 0;
325
327 virtual void waitForFinished() = 0;
328
330 virtual bool isActive() const = 0;
331
338 virtual bool usedCachedLabels() const = 0;
339
348 QStringList layersRedrawnFromCache() const;
349
356
365
372 void setFeatureFilterProvider( const QgsFeatureFilterProvider *f ) { mFeatureFilterProvider = f; }
373
378 const QgsFeatureFilterProvider *featureFilterProvider() const { return mFeatureFilterProvider; }
379
380 struct Error
381 {
382 Error( const QString &lid, const QString &msg )
383 : layerID( lid )
384 , message( msg )
385 {}
386
387 QString layerID;
388 QString message;
389 };
390
391 typedef QList<QgsMapRendererJob::Error> Errors;
392
394 Errors errors() const;
395
396
402
408 QgsLabelSink *labelSink() const { return mLabelSink; } SIP_SKIP
409
417 void setLabelSink( QgsLabelSink *sink ) { mLabelSink = sink; } SIP_SKIP
418
428
433 int renderingTime() const { return mRenderingTime; }
434
439 QHash< QgsMapLayer *, int > perLayerRenderingTime() const SIP_SKIP;
440
453 void setLayerRenderingTimeHints( const QHash< QString, int > &hints ) SIP_SKIP;
454
459 const QgsMapSettings &mapSettings() const;
460
465 static const QString LABEL_CACHE_ID SIP_SKIP;
466
472 static const QString LABEL_PREVIEW_CACHE_ID SIP_SKIP;
473
479 static const QString ELEVATION_MAP_CACHE_PREFIX SIP_SKIP;
480
481#ifndef SIP_RUN
484#endif
485
486 signals:
487
495
496
504 void layerRenderingStarted( const QString &layerId );
505
513 void layerRendered( const QString &layerId );
514
516 void finished();
517
518 protected:
519
521 QElapsedTimer mRenderingStart;
523
524 QgsMapRendererCache *mCache = nullptr;
525
526 int mRenderingTime = 0;
527
529 QHash< QgsWeakMapLayerPointer, int > mPerLayerRenderingTime;
530
536 QHash< QString, int > mLayerRenderingTimeHints;
537
541 bool mRecordRenderingTime = true;
542
543#ifndef SIP_RUN
544 std::unique_ptr< QgsRenderedItemResults > mRenderedItemResults;
545#endif
546
548
555
567 std::vector< LayerRenderJob > prepareJobs( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool deferredPainterSet = false ) SIP_SKIP;
568
573 LabelRenderJob prepareLabelingJob( QPainter *painter, QgsLabelingEngine *labelingEngine2, bool canUseLabelCache = true ) SIP_SKIP;
574
583 std::vector< LayerRenderJob > prepareSecondPassJobs( std::vector< LayerRenderJob > &firstPassJobs, LabelRenderJob &labelJob ) SIP_SKIP;
584
589 void initSecondPassJobs( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob ) const SIP_SKIP;
590
592 static QImage composeImage( const QgsMapSettings &settings,
593 const std::vector< LayerRenderJob > &jobs,
594 const LabelRenderJob &labelJob,
595 const QgsMapRendererCache *cache = nullptr ) SIP_SKIP;
596
598 static QImage layerImageToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
599
601 static QgsElevationMap layerElevationToBeComposed( const QgsMapSettings &settings, const LayerRenderJob &job, const QgsMapRendererCache *cache ) SIP_SKIP;
602
609 static void composeSecondPass( std::vector< LayerRenderJob > &secondPassJobs, LabelRenderJob &labelJob, bool forceVector = false ) SIP_SKIP;
610
612 void logRenderingTime( const std::vector< LayerRenderJob > &jobs, const std::vector< LayerRenderJob > &secondPassJobs, const LabelRenderJob &labelJob ) SIP_SKIP;
613
615 void cleanupJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
616
618 void cleanupSecondPassJobs( std::vector< LayerRenderJob > &jobs ) SIP_SKIP;
619
625 void cleanupLabelJob( LabelRenderJob &job ) SIP_SKIP;
626
631 Q_DECL_DEPRECATED static void drawLabeling( const QgsMapSettings &settings, QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
632
634 static void drawLabeling( QgsRenderContext &renderContext, QgsLabelingEngine *labelingEngine2, QPainter *painter ) SIP_SKIP;
635
636 private:
637
649 static bool reprojectToLayerExtent( const QgsMapLayer *ml, const QgsCoordinateTransform &ct, QgsRectangle &extent, QgsRectangle &r2 );
650
651 const QgsFeatureFilterProvider *mFeatureFilterProvider = nullptr;
652
654 QImage *allocateImage( QString layerId );
655
657 QgsElevationMap *allocateElevationMap( QString layerId );
658
660 QPainter *allocateImageAndPainter( QString layerId, QImage *&image, const QgsRenderContext *context );
661
667 virtual void startPrivate() = 0;
668
669 QgsLabelSink *mLabelSink = nullptr;
670 QgsLabelingEngineFeedback *mLabelingEngineFeedback = nullptr;
671
672 typedef std::pair<std::unique_ptr<QPicture>, QPainter * > PictureAndPainter;
673
675 PictureAndPainter allocatePictureAndPainter( const QgsRenderContext *context );
676};
677
678
687{
688 Q_OBJECT
689
690 public:
692
694 virtual QImage renderedImage() = 0;
695
696};
697
698
699#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.
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:75
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.
Contains information about the context of a rendering operation.
Stores collated details of rendered items during a map rendering operation.
A boolean settings entry.
#define SIP_SKIP
Definition qgis_sip.h:126
#define SIP_TRANSFER
Definition qgis_sip.h:36
#define SIP_ABSTRACT
Definition qgis_sip.h:213
QPointer< QgsMapLayer > QgsWeakMapLayerPointer
Weak pointer for QgsMapLayer.
Error(const QString &lid, const QString &msg)