QGIS API Documentation 4.3.0-Master (35968c35f7e)
Loading...
Searching...
No Matches
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.h"
21#include "qgis_core.h"
22#include "qgis_sip.h"
23#include "qgspainting.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27#include <QPainter>
28#include <QPicture>
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
34
57
59{
60#ifdef SIP_RUN
62 if ( sipCpp->type() == "drawSource" && dynamic_cast<QgsDrawSourceEffect *>( sipCpp ) != NULL )
63 {
64 sipType = sipType_QgsDrawSourceEffect;
65 }
66 else if ( sipCpp->type() == "effectStack" && dynamic_cast<QgsEffectStack *>( sipCpp ) != NULL )
67 {
68 sipType = sipType_QgsEffectStack;
69 }
70 else if ( sipCpp->type() == "blur" && dynamic_cast<QgsBlurEffect *>( sipCpp ) != NULL )
71 {
72 sipType = sipType_QgsBlurEffect;
73 }
74 else if ( sipCpp->type() == "dropShadow" && dynamic_cast<QgsDropShadowEffect *>( sipCpp ) != NULL )
75 {
76 sipType = sipType_QgsDropShadowEffect;
77 }
78 else if ( sipCpp->type() == "outerGlow" && dynamic_cast<QgsOuterGlowEffect *>( sipCpp ) != NULL )
79 {
80 sipType = sipType_QgsOuterGlowEffect;
81 }
82 else if ( sipCpp->type() == "innerGlow" && dynamic_cast<QgsInnerGlowEffect *>( sipCpp ) != NULL )
83 {
84 sipType = sipType_QgsInnerGlowEffect;
85 }
86 else if ( sipCpp->type() == "transform" && dynamic_cast<QgsTransformEffect *>( sipCpp ) != NULL )
87 {
88 sipType = sipType_QgsTransformEffect;
89 }
90 else if ( sipCpp->type() == "color" && dynamic_cast<QgsColorEffect *>( sipCpp ) != NULL )
91 {
92 sipType = sipType_QgsColorEffect;
93 }
94 else
95 {
96 sipType = 0;
97 }
99#endif
100
101 public:
113
114 QgsPaintEffect() = default;
115
116 QgsPaintEffect( const QgsPaintEffect &other );
117 virtual ~QgsPaintEffect();
118
123 virtual QString type() const = 0;
124
129 virtual QgsPaintEffect *clone() const = 0 SIP_FACTORY;
130
136 virtual Qgis::PaintEffectFlags flags() const;
137
145 virtual QVariantMap properties() const = 0;
146
153 virtual void readProperties( const QVariantMap &props ) = 0;
154
164 virtual bool saveProperties( QDomDocument &doc, QDomElement &element ) const;
165
172 virtual bool readProperties( const QDomElement &element );
173
180 virtual void render( const QPicture &picture, QgsRenderContext &context );
181
190 virtual void begin( QgsRenderContext &context );
191
198 virtual void end( QgsRenderContext &context );
199
205 bool enabled() const { return mEnabled; }
206
212 void setEnabled( bool enabled );
213
220 DrawMode drawMode() const { return mDrawMode; }
221
228 void setDrawMode( DrawMode drawMode );
229
230 protected:
231 bool mEnabled = true;
234
242 virtual void draw( QgsRenderContext &context ) = 0;
243
251 void drawSource( QPainter &painter );
252
260 const QPicture &source() const { return mPicture; }
261
273 QImage sourceAsImage( QgsRenderContext &context );
274
282 QPointF imageOffset( const QgsRenderContext &context ) const;
283
293 virtual QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const;
294
303 Q_DECL_DEPRECATED void fixQPictureDpi( QPainter *painter ) const SIP_DEPRECATED;
304
305 private:
306 QPicture mPicture;
307 QImage mSourceImage;
308
309 QPainter *mPrevPainter = nullptr;
310 std::unique_ptr< QPainter > mEffectPainter;
311 std::unique_ptr< QPicture > mTempPicture;
312
313 QRectF imageBoundingRect( const QgsRenderContext &context ) const;
314
315 friend class QgsEffectStack;
316
317 QgsPaintEffect &operator=( const QgsPaintEffect & ) = delete;
318};
319
331
333{
334 public:
336
342 static std::unique_ptr<QgsPaintEffect> create( const QVariantMap &map );
343
344 Qgis::PaintEffectFlags flags() const override;
345 QString type() const override { return u"drawSource"_s; }
346 QgsDrawSourceEffect *clone() const override SIP_FACTORY;
347 QVariantMap properties() const override;
348
349 using QgsPaintEffect::readProperties;
350 void readProperties( const QVariantMap &props ) override;
351
358 void setOpacity( const double opacity ) { mOpacity = opacity; }
359
366 double opacity() const { return mOpacity; }
367
374 void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
375
382 QPainter::CompositionMode blendMode() const { return mBlendMode; }
383
384 protected:
385 void draw( QgsRenderContext &context ) override;
386
387 private:
388 double mOpacity = 1.0;
389 QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
390};
391
398class CORE_EXPORT QgsEffectPainter
399{
400 public:
406 QgsEffectPainter( QgsRenderContext &renderContext );
407
414 QgsEffectPainter( QgsRenderContext &renderContext, QgsPaintEffect *effect );
416
421
427 void setEffect( QgsPaintEffect *effect );
428
430
435 QPainter *operator->() { return mPainter; }
437
438 private:
439#ifdef SIP_RUN
441 const QgsEffectPainter &operator=( const QgsEffectPainter & );
442#endif
443
444 QgsRenderContext &mRenderContext;
445 QPainter *mPainter = nullptr;
446 QgsScopedQPainterState mPainterState;
447 QgsPaintEffect *mEffect = nullptr;
448};
449
450#endif // QGSPAINTEFFECT_H
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:3032
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.
void setOpacity(const double opacity)
Sets the opacity for the effect.
static std::unique_ptr< QgsPaintEffect > create(const QVariantMap &map)
Creates a new QgsDrawSource effect from a properties string map.
QString type() const override
Returns the effect type.
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
A paint effect which draws an offset and optionally blurred drop shadow.
Manages painter saving and restoring required for effect drawing.
QgsEffectPainter & operator=(const QgsEffectPainter &)=delete
QgsEffectPainter(QgsRenderContext &renderContext)
QgsEffectPainter constructor.
QgsEffectPainter(QgsEffectPainter &&)=delete
void setEffect(QgsPaintEffect *effect)
Sets the effect to be painted.
QgsEffectPainter & operator=(QgsEffectPainter &&)=delete
QgsEffectPainter(const QgsEffectPainter &)=delete
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 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.
const QPicture & source() const
Returns the source QPicture.
friend class QgsEffectStack
DrawMode drawMode() const
Returns the draw mode for the effect.
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 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.
@ ModifyAndRender
The result of the effect is both rendered and passed on to subsequent 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.
Scoped object for saving and restoring a QPainter object's state.
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:203
#define SIP_DEPRECATED
Definition qgis_sip.h:113
#define SIP_FACTORY
Definition qgis_sip.h:83
#define SIP_NODEFAULTCTORS
Definition qgis_sip.h:108
#define SIP_END
Definition qgis_sip.h:220