QGIS API Documentation 3.99.0-Master (08dd318614d)
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#include "qgsimageoperation.h"
20#include "qgsrendercontext.h"
21#include "qgssymbollayerutils.h"
22#include "qgspainting.h"
23
24#include <QPicture>
25
27 : mEnabled( other.enabled() )
28 , mDrawMode( other.drawMode() )
29{
30
31}
32
34{
35 // ensure painter is destroyed before picture it may be drawing on, just in case
36 mEffectPainter.reset();
37}
38
43
44void QgsPaintEffect::setEnabled( const bool enabled )
45{
47}
48
53
54bool QgsPaintEffect::saveProperties( QDomDocument &doc, QDomElement &element ) const
55{
56 if ( element.isNull() )
57 {
58 return false;
59 }
60 QDomElement effectElement = doc.createElement( QStringLiteral( "effect" ) );
61 effectElement.setAttribute( QStringLiteral( "type" ), type() );
62 QgsSymbolLayerUtils::saveProperties( properties(), doc, effectElement );
63 element.appendChild( effectElement );
64 return true;
65}
66
67bool QgsPaintEffect::readProperties( const QDomElement &element )
68{
69 if ( element.isNull() )
70 {
71 return false;
72 }
73
74 const QVariantMap props = QgsSymbolLayerUtils::parseProperties( element );
75 readProperties( props );
76 return true;
77}
78
79void QgsPaintEffect::render( const QPicture &picture, QgsRenderContext &context )
80{
81 //set source picture
82 mPicture = picture;
83 mSourceImage = QImage();
84
85 draw( context );
86}
87
89{
90 //temporarily replace painter and direct paint operations for context to a QPicture
91 mPrevPainter = context.painter();
92
93 mTempPicture = std::make_unique< QPicture >();
94
95 mEffectPainter = std::make_unique< QPainter >();
96 mEffectPainter->begin( mTempPicture.get() );
97
98 context.setPainterFlagsUsingContext( mEffectPainter.get() );
99 context.setPainter( mEffectPainter.get() );
100}
101
103{
104 if ( !mEffectPainter )
105 return;
106
107 mEffectPainter->end();
108 mEffectPainter.reset();
109
110 //restore previous painter for context
111 context.setPainter( mPrevPainter );
112 mPrevPainter = nullptr;
113
114 // clear any existing pen/brush - sometimes these are not correctly restored when restoring a painter
115 // with a QPicture destination - see #15696
116 context.painter()->setPen( Qt::NoPen );
117 context.painter()->setBrush( Qt::NoBrush );
118
119 //draw using effect
120 render( *mTempPicture, context );
121
122 //clean up
123 mTempPicture.reset();
124}
125
126void QgsPaintEffect::drawSource( QPainter &painter )
127{
129 {
130 QgsPainting::drawPicture( &painter, QPointF( 0, 0 ), mPicture );
131 }
132 else
133 {
134 painter.drawPicture( 0, 0, mPicture );
135 }
136}
137
139{
140 //have we already created a source image? if so, return it
141 if ( !mSourceImage.isNull() )
142 {
143 return mSourceImage;
144 }
145
146 if ( mPicture.isNull() )
147 return QImage();
148
149 //else create it
150 //TODO - test with premultiplied image for speed
151 const QRectF bounds = imageBoundingRect( context );
152 mSourceImage = QImage( static_cast< int >( std::ceil( bounds.width() ) ),
153 static_cast< int >( std::ceil( bounds.height() ) ), QImage::Format_ARGB32 );
154 mSourceImage.fill( Qt::transparent );
155 QPainter imagePainter( &mSourceImage );
156 imagePainter.setRenderHint( QPainter::Antialiasing );
157 imagePainter.translate( -bounds.left(), -bounds.top() );
158 imagePainter.drawPicture( 0, 0, mPicture );
159 imagePainter.end();
160 return mSourceImage;
161}
162
163QPointF QgsPaintEffect::imageOffset( const QgsRenderContext &context ) const
164{
165 return imageBoundingRect( context ).topLeft();
166}
167
168QRectF QgsPaintEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
169{
170 Q_UNUSED( context )
171 return rect;
172}
173
174void QgsPaintEffect::fixQPictureDpi( QPainter *painter ) const
175{
177}
178
179QRectF QgsPaintEffect::imageBoundingRect( const QgsRenderContext &context ) const
180{
181 return boundingRect( mPicture.boundingRect(), context );
182}
183
184
185//
186// QgsDrawSourceEffect
187//
188
190{
192 effect->readProperties( map );
193 return effect;
194}
195
197{
199 if ( mBlendMode != QPainter::CompositionMode_SourceOver || !qgsDoubleNear( mOpacity, 1.0 ) )
200 {
202 }
203 return res;
204}
205
207{
208 if ( !enabled() || !context.painter() )
209 return;
210
211 QPainter *painter = context.painter();
212
213 if ( mBlendMode == QPainter::CompositionMode_SourceOver && qgsDoubleNear( mOpacity, 1.0 ) )
214 {
215 //just draw unmodified source
216 drawSource( *painter );
217 }
218 else
219 {
220 //rasterize source and apply modifications
221 QImage image = sourceAsImage( context ).copy();
222 QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
223 const QgsScopedQPainterState painterState( painter );
224 painter->setCompositionMode( mBlendMode );
225 painter->drawImage( imageOffset( context ), image );
226 }
227}
228
230{
231 return new QgsDrawSourceEffect( *this );
232}
233
235{
236 QVariantMap props;
237 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
238 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
239 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
240 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
241 return props;
242}
243
244void QgsDrawSourceEffect::readProperties( const QVariantMap &props )
245{
246 bool ok;
247 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
248 if ( ok )
249 {
250 mBlendMode = mode;
251 }
252 if ( props.contains( QStringLiteral( "transparency" ) ) )
253 {
254 const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
255 if ( ok )
256 {
257 mOpacity = 1.0 - transparency;
258 }
259 }
260 else
261 {
262 const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
263 if ( ok )
264 {
265 mOpacity = opacity;
266 }
267 }
268 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
269 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
270}
271
272
273//
274// QgsEffectPainter
275//
276
278 : mRenderContext( renderContext )
279
280{
281 mPainter = renderContext.painter();
282 mPainter->save();
283}
284
286 : mRenderContext( renderContext )
287 , mEffect( effect )
288{
289 mPainter = mRenderContext.painter();
290 mPainter->save();
291 mEffect->begin( mRenderContext );
292}
293
295{
296 Q_ASSERT( !mEffect );
297
298 mEffect = effect;
299 mEffect->begin( mRenderContext );
300}
301
303{
304 Q_ASSERT( mEffect );
305
306 mEffect->end( mRenderContext );
307 mPainter->restore();
308}
@ RequiresRasterization
The effect requires raster-based rendering.
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2765
A paint effect which draws the source picture with minor or no alterations.
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:6388