Skip to content

Commit fe39578

Browse files
committed
Added python bindings for QgsMapRendererJob + subclasses and QgsMapRendererCache
1 parent 530b5fd commit fe39578

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

python/core/core.sip

+2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
%Include qgsmaplayerregistry.sip
4949
%Include qgsmaplayerrenderer.sip
5050
%Include qgsmaprenderer.sip
51+
%Include qgsmaprenderercache.sip
52+
%Include qgsmaprendererjob.sip
5153
%Include qgsmapsettings.sip
5254
%Include qgsmaptopixel.sip
5355
%Include qgsmessagelog.sip

python/core/qgsmaprenderercache.sip

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
/**
3+
* This class is responsible for keeping cache of rendered images of individual layers.
4+
*
5+
* Once a layer has rendered image stored in the cache (using setCacheImage(...)),
6+
* the cache listens to repaintRequested() signals from layer. If triggered, the cache
7+
* removes the rendered image (and disconnects from the layer).
8+
*
9+
* The class is thread-safe (multiple classes can access the same instance safely).
10+
*
11+
* @note added in 2.4
12+
*/
13+
class QgsMapRendererCache : QObject
14+
{
15+
%TypeHeaderCode
16+
#include <qgsmaprenderercache.h>
17+
%End
18+
19+
public:
20+
21+
QgsMapRendererCache();
22+
23+
//! invalidate the cache contents
24+
void clear();
25+
26+
//! initialize cache: set new parameters and erase cache if parameters have changed
27+
//! @return flag whether the parameters are the same as last time
28+
bool init( QgsRectangle extent, double scale );
29+
30+
//! set cached image for the specified layer ID
31+
void setCacheImage( QString layerId, const QImage& img );
32+
33+
//! get cached image for the specified layer ID. Returns null image if it is not cached.
34+
QImage cacheImage( QString layerId );
35+
36+
//! remove layer from the cache
37+
void clearCacheImage( QString layerId );
38+
39+
};

python/core/qgsmaprendererjob.sip

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
2+
/** abstract base class renderer jobs that asynchronously start map rendering */
3+
class QgsMapRendererJob : QObject
4+
{
5+
%TypeHeaderCode
6+
#include <qgsmaprendererjob.h>
7+
%End
8+
9+
public:
10+
11+
QgsMapRendererJob( const QgsMapSettings& settings );
12+
13+
virtual ~QgsMapRendererJob();
14+
15+
//! Start the rendering job and immediately return.
16+
//! Does nothing if the rendering is already in progress.
17+
virtual void start() = 0;
18+
19+
//! Stop the rendering job - does not return until the job has terminated.
20+
//! Does nothing if the rendering is not active.
21+
virtual void cancel() = 0;
22+
23+
//! Block until the job has finished.
24+
virtual void waitForFinished() = 0;
25+
26+
//! Tell whether the rendering job is currently running in background.
27+
virtual bool isActive() const = 0;
28+
29+
//! Get pointer to internal labeling engine (in order to get access to the results)
30+
virtual QgsLabelingResults* takeLabelingResults() = 0 /Transfer/;
31+
32+
struct Error
33+
{
34+
Error( const QString& lid, const QString& msg );
35+
36+
QString layerID;
37+
QString message;
38+
};
39+
40+
typedef QList<QgsMapRendererJob::Error> Errors;
41+
42+
//! List of errors that happened during the rendering job - available when the rendering has been finished
43+
Errors errors() const;
44+
45+
46+
//! Assign a cache to be used for reading and storing rendered images of individual layers.
47+
//! Does not take ownership of the object.
48+
void setCache( QgsMapRendererCache* cache );
49+
50+
//! Set which vector layers should be cached while rendering
51+
//! @note The way how geometries are cached is really suboptimal - this method may be removed in future releases
52+
void setRequestedGeometryCacheForLayers( const QStringList& layerIds );
53+
54+
//! Find out how log it took to finish the job (in miliseconds)
55+
int renderingTime() const;
56+
57+
signals:
58+
59+
//! emitted when asynchronous rendering is finished (or canceled).
60+
void finished();
61+
62+
};
63+
64+
65+
/** Intermediate base class adding functionality that allows client to query the rendered image.
66+
* The image can be queried even while the rendering is still in progress to get intermediate result
67+
*/
68+
class QgsMapRendererQImageJob : QgsMapRendererJob
69+
{
70+
%TypeHeaderCode
71+
#include <qgsmaprendererjob.h>
72+
%End
73+
74+
public:
75+
QgsMapRendererQImageJob( const QgsMapSettings& settings );
76+
77+
//! Get a preview/resulting image
78+
virtual QImage renderedImage() = 0;
79+
};
80+
81+
82+
83+
/** job implementation that renders everything sequentially in one thread */
84+
class QgsMapRendererSequentialJob : QgsMapRendererQImageJob
85+
{
86+
%TypeHeaderCode
87+
#include <qgsmaprendererjob.h>
88+
%End
89+
90+
public:
91+
QgsMapRendererSequentialJob( const QgsMapSettings& settings );
92+
~QgsMapRendererSequentialJob();
93+
94+
virtual void start();
95+
virtual void cancel();
96+
virtual void waitForFinished();
97+
virtual bool isActive() const;
98+
99+
virtual QgsLabelingResults* takeLabelingResults() /Transfer/;
100+
101+
// from QgsMapRendererJobWithPreview
102+
virtual QImage renderedImage();
103+
104+
public slots:
105+
106+
void internalFinished();
107+
};
108+
109+
110+
111+
112+
/** job implementation that renders all layers in parallel */
113+
class QgsMapRendererParallelJob : QgsMapRendererQImageJob
114+
{
115+
%TypeHeaderCode
116+
#include <qgsmaprendererjob.h>
117+
%End
118+
119+
public:
120+
QgsMapRendererParallelJob( const QgsMapSettings& settings );
121+
~QgsMapRendererParallelJob();
122+
123+
virtual void start();
124+
virtual void cancel();
125+
virtual void waitForFinished();
126+
virtual bool isActive() const;
127+
128+
virtual QgsLabelingResults* takeLabelingResults() /Transfer/;
129+
130+
// from QgsMapRendererJobWithPreview
131+
virtual QImage renderedImage();
132+
};
133+
134+
135+
136+
/** job implementation that renders everything sequentially using a custom painter.
137+
* The returned image is always invalid (because there is none available).
138+
*/
139+
class QgsMapRendererCustomPainterJob : QgsMapRendererJob
140+
{
141+
%TypeHeaderCode
142+
#include <qgsmaprendererjob.h>
143+
%End
144+
145+
public:
146+
QgsMapRendererCustomPainterJob( const QgsMapSettings& settings, QPainter* painter );
147+
~QgsMapRendererCustomPainterJob();
148+
149+
virtual void start();
150+
virtual void cancel();
151+
virtual void waitForFinished();
152+
virtual bool isActive() const;
153+
virtual QgsLabelingResults* takeLabelingResults() /Transfer/;
154+
155+
// TODO wrap??? const LayerRenderJobs& jobs() const;
156+
};

0 commit comments

Comments
 (0)