QGIS API Documentation 3.41.0-Master (3c143d501a8)
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#include "qgsimageoperation.h"
20#include "qgslogger.h"
21#include "qgsrendercontext.h"
22#include "qgssymbollayerutils.h"
23#include "qgspainting.h"
24
25#include <QPicture>
26
28 : mEnabled( other.enabled() )
29 , mDrawMode( other.drawMode() )
30{
31
32}
33
35{
36 if ( mOwnsImage )
37 {
38 delete mSourceImage;
39 }
40 delete mEffectPainter;
41 delete mTempPicture;
42}
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( QPicture &picture, QgsRenderContext &context )
80{
81 //set source picture
82 mPicture = &picture;
83 delete mSourceImage;
84 mSourceImage = nullptr;
85
86 draw( context );
87}
88
90{
91 //temporarily replace painter and direct paint operations for context to a QPicture
92 mPrevPainter = context.painter();
93
94 delete mTempPicture;
95 mTempPicture = new QPicture();
96
97 delete mEffectPainter;
98 mEffectPainter = new QPainter();
99 mEffectPainter->begin( mTempPicture );
100
101 context.setPainter( mEffectPainter );
102}
103
105{
106 if ( !mEffectPainter )
107 return;
108
109 mEffectPainter->end();
110 delete mEffectPainter;
111 mEffectPainter = nullptr;
112
113 //restore previous painter for context
114 context.setPainter( mPrevPainter );
115 mPrevPainter = nullptr;
116
117 // clear any existing pen/brush - sometimes these are not correctly restored when restoring a painter
118 // with a QPicture destination - see #15696
119 context.painter()->setPen( Qt::NoPen );
120 context.painter()->setBrush( Qt::NoBrush );
121
122 //draw using effect
123 render( *mTempPicture, context );
124
125 //clean up
126 delete mTempPicture;
127 mTempPicture = nullptr;
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 )
146 {
147 return mSourceImage;
148 }
149
150 if ( !mPicture )
151 return nullptr;
152
153 //else create it
154 //TODO - test with premultiplied image for speed
155 const QRectF bounds = imageBoundingRect( context );
156 mSourceImage = new QImage( bounds.width(), bounds.height(), QImage::Format_ARGB32 );
157 mSourceImage->fill( Qt::transparent );
158 QPainter imagePainter( mSourceImage );
159 imagePainter.setRenderHint( QPainter::Antialiasing );
160 imagePainter.translate( -bounds.left(), -bounds.top() );
161 imagePainter.drawPicture( 0, 0, *mPicture );
162 imagePainter.end();
163 mOwnsImage = true;
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
173{
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{
202 if ( !enabled() || !context.painter() )
203 return;
204
205 QPainter *painter = context.painter();
206
207 if ( mBlendMode == QPainter::CompositionMode_SourceOver && qgsDoubleNear( mOpacity, 1.0 ) )
208 {
209 //just draw unmodified source
210 drawSource( *painter );
211 }
212 else
213 {
214 //rasterize source and apply modifications
215 QImage image = sourceAsImage( context )->copy();
216 QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
217 const QgsScopedQPainterState painterState( painter );
218 painter->setCompositionMode( mBlendMode );
219 painter->drawImage( imageOffset( context ), image );
220 }
221}
222
224{
225 return new QgsDrawSourceEffect( *this );
226}
227
229{
230 QVariantMap props;
231 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
232 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
233 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
234 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
235 return props;
236}
237
238void QgsDrawSourceEffect::readProperties( const QVariantMap &props )
239{
240 bool ok;
241 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
242 if ( ok )
243 {
244 mBlendMode = mode;
245 }
246 if ( props.contains( QStringLiteral( "transparency" ) ) )
247 {
248 const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
249 if ( ok )
250 {
251 mOpacity = 1.0 - transparency;
252 }
253 }
254 else
255 {
256 const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
257 if ( ok )
258 {
259 mOpacity = opacity;
260 }
261 }
262 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
263 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
264}
265
266
267//
268// QgsEffectPainter
269//
270
272 : mRenderContext( renderContext )
273
274{
275 mPainter = renderContext.painter();
276 mPainter->save();
277}
278
280 : mRenderContext( renderContext )
281 , mEffect( effect )
282{
283 mPainter = mRenderContext.painter();
284 mPainter->save();
285 mEffect->begin( mRenderContext );
286}
287
289{
290 Q_ASSERT( !mEffect );
291
292 mEffect = effect;
293 mEffect->begin( mRenderContext );
294}
295
297{
298 Q_ASSERT( mEffect );
299
300 mEffect->end( mRenderContext );
301 mPainter->restore();
302}
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...
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.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
virtual void render(QPicture &picture, QgsRenderContext &context)
Renders a picture using the effect.
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 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.
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
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.
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:6024