QGIS API Documentation 3.99.0-Master (a26b91b364d)
qgspainteffect.h
Go to the documentation of this file.
1/***************************************************************************
2 qgspainteffect.h
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#ifndef QGSPAINTEFFECT_H
18#define QGSPAINTEFFECT_H
19
20#include "qgis_core.h"
21#include "qgis_sip.h"
22#include "qgis.h"
23#include <QPainter>
24#include <QDomDocument>
25#include <QDomElement>
26#include <QPicture>
27
29
54{
55
56#ifdef SIP_RUN
58 if ( sipCpp->type() == "drawSource" && dynamic_cast<QgsDrawSourceEffect *>( sipCpp ) != NULL )
59 {
60 sipType = sipType_QgsDrawSourceEffect;
61 }
62 else if ( sipCpp->type() == "effectStack" && dynamic_cast<QgsEffectStack *>( sipCpp ) != NULL )
63 {
64 sipType = sipType_QgsEffectStack;
65 }
66 else if ( sipCpp->type() == "blur" && dynamic_cast<QgsBlurEffect *>( sipCpp ) != NULL )
67 {
68 sipType = sipType_QgsBlurEffect;
69 }
70 else if ( sipCpp->type() == "dropShadow" && dynamic_cast<QgsDropShadowEffect *>( sipCpp ) != NULL )
71 {
72 sipType = sipType_QgsDropShadowEffect;
73 }
74 else if ( sipCpp->type() == "outerGlow" && dynamic_cast<QgsOuterGlowEffect *>( sipCpp ) != NULL )
75 {
76 sipType = sipType_QgsOuterGlowEffect;
77 }
78 else if ( sipCpp->type() == "innerGlow" && dynamic_cast<QgsInnerGlowEffect *>( sipCpp ) != NULL )
79 {
80 sipType = sipType_QgsInnerGlowEffect;
81 }
82 else if ( sipCpp->type() == "transform" && dynamic_cast<QgsTransformEffect *>( sipCpp ) != NULL )
83 {
84 sipType = sipType_QgsTransformEffect;
85 }
86 else if ( sipCpp->type() == "color" && dynamic_cast<QgsColorEffect *>( sipCpp ) != NULL )
87 {
88 sipType = sipType_QgsColorEffect;
89 }
90 else
91 {
92 sipType = 0;
93 }
95#endif
96
97 public:
98
105 {
108 ModifyAndRender
109 };
110
111 QgsPaintEffect() = default;
112
113 QgsPaintEffect( const QgsPaintEffect &other );
114 virtual ~QgsPaintEffect();
115
120 virtual QString type() const = 0;
121
126 virtual QgsPaintEffect *clone() const = 0 SIP_FACTORY;
127
133 virtual Qgis::PaintEffectFlags flags() const;
134
142 virtual QVariantMap properties() const = 0;
143
150 virtual void readProperties( const QVariantMap &props ) = 0;
151
161 virtual bool saveProperties( QDomDocument &doc, QDomElement &element ) const;
162
169 virtual bool readProperties( const QDomElement &element );
170
177 virtual void render( const QPicture &picture, QgsRenderContext &context );
178
187 virtual void begin( QgsRenderContext &context );
188
195 virtual void end( QgsRenderContext &context );
196
202 bool enabled() const { return mEnabled; }
203
209 void setEnabled( bool enabled );
210
217 DrawMode drawMode() const { return mDrawMode; }
218
225 void setDrawMode( DrawMode drawMode );
226
227 protected:
228
229 bool mEnabled = true;
230 DrawMode mDrawMode = ModifyAndRender;
231 bool requiresQPainterDpiFix = true;
232
240 virtual void draw( QgsRenderContext &context ) = 0;
241
249 void drawSource( QPainter &painter );
250
258 const QPicture &source() const { return mPicture; }
259
271 QImage sourceAsImage( QgsRenderContext &context );
272
280 QPointF imageOffset( const QgsRenderContext &context ) const;
281
291 virtual QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const;
292
301 Q_DECL_DEPRECATED void fixQPictureDpi( QPainter *painter ) const SIP_DEPRECATED;
302
303 private:
304
305 QPicture mPicture;
306 QImage mSourceImage;
307
308 QPainter *mPrevPainter = nullptr;
309 std::unique_ptr< QPainter > mEffectPainter;
310 std::unique_ptr< QPicture > mTempPicture;
311
312 QRectF imageBoundingRect( const QgsRenderContext &context ) const;
313
314 friend class QgsEffectStack;
315
316 QgsPaintEffect &operator= ( const QgsPaintEffect & ) = delete;
317
318};
319
333{
334 public:
335
337
343 static QgsPaintEffect *create( const QVariantMap &map ) SIP_FACTORY;
344
345 Qgis::PaintEffectFlags flags() const override;
346 QString type() const override { return QStringLiteral( "drawSource" ); }
347 QgsDrawSourceEffect *clone() const override SIP_FACTORY;
348 QVariantMap properties() const override;
349 void readProperties( const QVariantMap &props ) override;
350
357 void setOpacity( const double opacity ) { mOpacity = opacity; }
358
365 double opacity() const { return mOpacity; }
366
373 void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
374
381 QPainter::CompositionMode blendMode() const { return mBlendMode; }
382
383 protected:
384
385 void draw( QgsRenderContext &context ) override;
386
387 private:
388
389 double mOpacity = 1.0;
390 QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
391};
392
399class CORE_EXPORT QgsEffectPainter
400{
401 public:
402
408 QgsEffectPainter( QgsRenderContext &renderContext );
409
416 QgsEffectPainter( QgsRenderContext &renderContext, QgsPaintEffect *effect );
418
424 void setEffect( QgsPaintEffect *effect );
425
427
432 QPainter *operator->() { return mPainter; }
434
435 private:
436#ifdef SIP_RUN
437 const QgsEffectPainter &operator=( const QgsEffectPainter & );
438#endif
439
440 QgsRenderContext &mRenderContext;
441 QPainter *mPainter = nullptr;
442 QgsPaintEffect *mEffect = nullptr;
443};
444
445#endif // QGSPAINTEFFECT_H
446
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2765
A paint effect which blurs a source picture, using a number of different blur methods.
A paint effect which alters the colors (e.g., brightness, contrast) in a source picture.
A paint effect which draws the source picture with minor or no alterations.
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blend mode for the effect.
QgsDrawSourceEffect()=default
double opacity() const
Returns the opacity for the effect.
QString type() const override
Returns the effect type.
A paint effect which draws an offset and optionally blurred drop shadow.
Manages painter saving and restoring required for effect drawing.
A paint effect which consists of a stack of other chained paint effects.
A paint effect which draws a glow within a picture.
A paint effect which draws a glow outside of a picture.
Base class for visual effects which can be applied to QPicture drawings.
QgsPaintEffect()=default
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...
const QPicture & source() const
Returns the source QPicture.
DrawMode drawMode() const
Returns the draw mode for the effect.
bool enabled() const
Returns whether the effect is enabled.
virtual QgsPaintEffect * clone() const =0
Duplicates an effect by creating a deep copy of the effect.
virtual QVariantMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
DrawMode
Drawing modes for effects.
@ Render
The result of the effect is rendered on the destination, but does not affect subsequent effects in th...
@ Modifier
The result of the effect is not rendered, but is passed on to following effects in the stack.
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.
Contains information about the context of a rendering operation.
A paint effect which applies transformations (such as move, scale and rotate) to a picture.
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition qgis_sip.h:191
#define SIP_DEPRECATED
Definition qgis_sip.h:106
#define SIP_FACTORY
Definition qgis_sip.h:76
#define SIP_NODEFAULTCTORS
Definition qgis_sip.h:101
#define SIP_END
Definition qgis_sip.h:208