QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgspainteffect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgspainteffect.cpp
3 -------------------
4 begin : December 2014
5 copyright : (C) 2014 Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgspainteffect.h"
19
20#include "qgsimageoperation.h"
21#include "qgspainting.h"
22#include "qgsrendercontext.h"
23#include "qgssymbollayerutils.h"
24
25#include <QPicture>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31 : mEnabled( other.enabled() )
32 , mDrawMode( other.drawMode() )
33{
34
35}
36
38{
39 // ensure painter is destroyed before picture it may be drawing on, just in case
40 mEffectPainter.reset();
41}
42
47
49{
51}
52
57
58bool QgsPaintEffect::saveProperties( QDomDocument &doc, QDomElement &element ) const
59{
60 if ( element.isNull() )
61 {
62 return false;
63 }
64 QDomElement effectElement = doc.createElement( u"effect"_s );
65 effectElement.setAttribute( u"type"_s, type() );
66 QgsSymbolLayerUtils::saveProperties( properties(), doc, effectElement );
67 element.appendChild( effectElement );
68 return true;
69}
70
71bool QgsPaintEffect::readProperties( const QDomElement &element )
72{
73 if ( element.isNull() )
74 {
75 return false;
76 }
77
78 const QVariantMap props = QgsSymbolLayerUtils::parseProperties( element );
79 readProperties( props );
80 return true;
81}
82
83void QgsPaintEffect::render( const QPicture &picture, QgsRenderContext &context )
84{
85 //set source picture
86 mPicture = picture;
87 mSourceImage = QImage();
88
89 draw( context );
90}
91
93{
94 //temporarily replace painter and direct paint operations for context to a QPicture
95 mPrevPainter = context.painter();
96
97 mTempPicture = std::make_unique< QPicture >();
98
99 mEffectPainter = std::make_unique< QPainter >();
100 mEffectPainter->begin( mTempPicture.get() );
101
102 context.setPainterFlagsUsingContext( mEffectPainter.get() );
103 context.setPainter( mEffectPainter.get() );
104}
105
107{
108 if ( !mEffectPainter )
109 return;
110
111 mEffectPainter->end();
112 mEffectPainter.reset();
113
114 //restore previous painter for context
115 context.setPainter( mPrevPainter );
116 mPrevPainter = nullptr;
117
118 // clear any existing pen/brush - sometimes these are not correctly restored when restoring a painter
119 // with a QPicture destination - see #15696
120 context.painter()->setPen( Qt::NoPen );
121 context.painter()->setBrush( Qt::NoBrush );
122
123 //draw using effect
124 render( *mTempPicture, context );
125
126 //clean up
127 mTempPicture.reset();
128}
129
130void QgsPaintEffect::drawSource( QPainter &painter )
131{
133 {
134 QgsPainting::drawPicture( &painter, QPointF( 0, 0 ), mPicture );
135 }
136 else
137 {
138 painter.drawPicture( 0, 0, mPicture );
139 }
140}
141
143{
144 //have we already created a source image? if so, return it
145 if ( !mSourceImage.isNull() )
146 {
147 return mSourceImage;
148 }
149
150 if ( mPicture.isNull() )
151 return QImage();
152
153 //else create it
154 //TODO - test with premultiplied image for speed
155 const QRectF bounds = imageBoundingRect( context );
156 mSourceImage = QImage( static_cast< int >( std::ceil( bounds.width() ) ),
157 static_cast< int >( std::ceil( bounds.height() ) ), QImage::Format_ARGB32 );
158 mSourceImage.fill( Qt::transparent );
159 QPainter imagePainter( &mSourceImage );
160 imagePainter.setRenderHint( QPainter::Antialiasing );
161 imagePainter.translate( -bounds.left(), -bounds.top() );
162 imagePainter.drawPicture( 0, 0, mPicture );
163 imagePainter.end();
164 return mSourceImage;
165}
166
167QPointF QgsPaintEffect::imageOffset( const QgsRenderContext &context ) const
168{
169 return imageBoundingRect( context ).topLeft();
170}
171
172QRectF QgsPaintEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
174 Q_UNUSED( context )
175 return rect;
176}
177
178void QgsPaintEffect::fixQPictureDpi( QPainter *painter ) const
179{
181}
182
183QRectF QgsPaintEffect::imageBoundingRect( const QgsRenderContext &context ) const
184{
185 return boundingRect( mPicture.boundingRect(), context );
186}
187
188
189//
190// QgsDrawSourceEffect
191//
192
194{
196 effect->readProperties( map );
197 return effect;
198}
199
201{
203 if ( mBlendMode != QPainter::CompositionMode_SourceOver || !qgsDoubleNear( mOpacity, 1.0 ) )
204 {
206 }
207 return res;
208}
209
211{
212 if ( !enabled() || !context.painter() )
213 return;
214
215 QPainter *painter = context.painter();
216
217 if ( mBlendMode == QPainter::CompositionMode_SourceOver && qgsDoubleNear( mOpacity, 1.0 ) )
218 {
219 //just draw unmodified source
220 drawSource( *painter );
221 }
222 else
223 {
224 //rasterize source and apply modifications
225 QImage image = sourceAsImage( context ).copy();
226 QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
227 const QgsScopedQPainterState painterState( painter );
228 painter->setCompositionMode( mBlendMode );
229 painter->drawImage( imageOffset( context ), image );
230 }
231}
232
234{
235 return new QgsDrawSourceEffect( *this );
236}
237
239{
240 QVariantMap props;
241 props.insert( u"enabled"_s, mEnabled ? "1" : "0" );
242 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
243 props.insert( u"blend_mode"_s, QString::number( int( mBlendMode ) ) );
244 props.insert( u"opacity"_s, QString::number( mOpacity ) );
245 return props;
246}
247
248void QgsDrawSourceEffect::readProperties( const QVariantMap &props )
249{
250 bool ok;
251 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( u"blend_mode"_s ).toInt( &ok ) );
252 if ( ok )
253 {
254 mBlendMode = mode;
255 }
256 if ( props.contains( u"transparency"_s ) )
257 {
258 const double transparency = props.value( u"transparency"_s ).toDouble( &ok );
259 if ( ok )
260 {
261 mOpacity = 1.0 - transparency;
262 }
263 }
264 else
265 {
266 const double opacity = props.value( u"opacity"_s ).toDouble( &ok );
267 if ( ok )
268 {
269 mOpacity = opacity;
270 }
271 }
272 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
273 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
274}
275
276
277//
278// QgsEffectPainter
279//
280
282 : mRenderContext( renderContext )
283
284{
285 mPainter = renderContext.painter();
286 mPainter->save();
287}
288
290 : mRenderContext( renderContext )
291 , mEffect( effect )
292{
293 mPainter = mRenderContext.painter();
294 mPainter->save();
295 mEffect->begin( mRenderContext );
296}
297
299{
300 Q_ASSERT( !mEffect );
301
302 mEffect = effect;
303 mEffect->begin( mRenderContext );
304}
305
307{
308 Q_ASSERT( mEffect );
309
310 mEffect->end( mRenderContext );
311 mPainter->restore();
312}
@ RequiresRasterization
The effect requires raster-based rendering.
Definition qgis.h:2864
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2873
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QgsDrawSourceEffect()=default
double opacity() const
Returns the opacity for the effect.
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
QgsDrawSourceEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsDrawSource effect from a properties string map.
void readProperties(const QVariantMap &props) override
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
QgsEffectPainter(QgsRenderContext &renderContext)
QgsEffectPainter constructor.
void setEffect(QgsPaintEffect *effect)
Sets the effect to be painted.
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
Base class for visual effects which can be applied to QPicture drawings.
QgsPaintEffect()=default
void setDrawMode(DrawMode drawMode)
Sets the draw mode for the effect.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
virtual bool saveProperties(QDomDocument &doc, QDomElement &element) const
Saves the current state of the effect to a DOM element.
void drawSource(QPainter &painter)
Draws the source QPicture onto the specified painter.
virtual void readProperties(const QVariantMap &props)=0
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
QImage sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
DrawMode drawMode() const
Returns the draw mode for the effect.
virtual ~QgsPaintEffect()
virtual void end(QgsRenderContext &context)
Ends interception of paint operations to a render context, and draws the result to the render context...
bool enabled() const
Returns whether the effect is enabled.
virtual Qgis::PaintEffectFlags flags() const
Returns flags which specify how the paint effect behaves.
virtual void render(const QPicture &picture, QgsRenderContext &context)
Renders a picture using the effect.
virtual QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const
Returns the bounding rect required for drawing the effect.
virtual QVariantMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
Q_DECL_DEPRECATED void fixQPictureDpi(QPainter *painter) const
Applies a workaround to a QPainter to avoid an issue with incorrect scaling when drawing QPictures.
DrawMode
Drawing modes for effects.
virtual void draw(QgsRenderContext &context)=0
Handles drawing of the effect's result on to the specified render context.
virtual QString type() const =0
Returns the effect type.
static void applyScaleFixForQPictureDpi(QPainter *painter)
Applies a workaround to a painter to avoid an issue with incorrect scaling when drawing QPictures.
static void drawPicture(QPainter *painter, const QPointF &point, const QPicture &picture)
Draws a picture onto a painter, correctly applying workarounds to avoid issues with incorrect scaling...
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
void setPainter(QPainter *p)
Sets the destination QPainter for the render operation.
Scoped object for saving and restoring a QPainter object's state.
static void saveProperties(QVariantMap props, QDomDocument &doc, QDomElement &element)
Saves the map of properties to XML.
static QVariantMap parseProperties(const QDomElement &element)
Parses the properties from XML and returns a map.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6900