QGIS API Documentation 3.99.0-Master (d270888f95f)
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
24#include <QDomDocument>
25#include <QDomElement>
26#include <QPainter>
27#include <QPicture>
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
33
56
58{
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:
102
114
115 QgsPaintEffect() = default;
116
117 QgsPaintEffect( const QgsPaintEffect &other );
118 virtual ~QgsPaintEffect();
119
124 virtual QString type() const = 0;
125
130 virtual QgsPaintEffect *clone() const = 0 SIP_FACTORY;
131
137 virtual Qgis::PaintEffectFlags flags() const;
138
146 virtual QVariantMap properties() const = 0;
147
154 virtual void readProperties( const QVariantMap &props ) = 0;
155
165 virtual bool saveProperties( QDomDocument &doc, QDomElement &element ) const;
166
173 virtual bool readProperties( const QDomElement &element );
174
181 virtual void render( const QPicture &picture, QgsRenderContext &context );
182
191 virtual void begin( QgsRenderContext &context );
192
199 virtual void end( QgsRenderContext &context );
200
206 bool enabled() const { return mEnabled; }
207
213 void setEnabled( bool enabled );
214
221 DrawMode drawMode() const { return mDrawMode; }
222
229 void setDrawMode( DrawMode drawMode );
230
231 protected:
232
233 bool mEnabled = true;
236
244 virtual void draw( QgsRenderContext &context ) = 0;
245
253 void drawSource( QPainter &painter );
254
262 const QPicture &source() const { return mPicture; }
263
275 QImage sourceAsImage( QgsRenderContext &context );
276
284 QPointF imageOffset( const QgsRenderContext &context ) const;
285
295 virtual QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const;
296
305 Q_DECL_DEPRECATED void fixQPictureDpi( QPainter *painter ) const SIP_DEPRECATED;
306
307 private:
308
309 QPicture mPicture;
310 QImage mSourceImage;
311
312 QPainter *mPrevPainter = nullptr;
313 std::unique_ptr< QPainter > mEffectPainter;
314 std::unique_ptr< QPicture > mTempPicture;
315
316 QRectF imageBoundingRect( const QgsRenderContext &context ) const;
317
318 friend class QgsEffectStack;
319
320 QgsPaintEffect &operator= ( const QgsPaintEffect & ) = delete;
321
322};
323
335
337{
338 public:
339
341
347 static QgsPaintEffect *create( const QVariantMap &map ) SIP_FACTORY;
348
349 Qgis::PaintEffectFlags flags() const override;
350 QString type() const override { return u"drawSource"_s; }
351 QgsDrawSourceEffect *clone() const override SIP_FACTORY;
352 QVariantMap properties() const override;
353
354 using QgsPaintEffect::readProperties;
355 void readProperties( const QVariantMap &props ) override;
356
363 void setOpacity( const double opacity ) { mOpacity = opacity; }
364
371 double opacity() const { return mOpacity; }
372
379 void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
380
387 QPainter::CompositionMode blendMode() const { return mBlendMode; }
388
389 protected:
390
391 void draw( QgsRenderContext &context ) override;
392
393 private:
394
395 double mOpacity = 1.0;
396 QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
397};
398
405class CORE_EXPORT QgsEffectPainter
406{
407 public:
408
414 QgsEffectPainter( QgsRenderContext &renderContext );
415
422 QgsEffectPainter( QgsRenderContext &renderContext, QgsPaintEffect *effect );
424
430 void setEffect( QgsPaintEffect *effect );
431
433
438 QPainter *operator->() { return mPainter; }
440
441 private:
442#ifdef SIP_RUN
443 const QgsEffectPainter &operator=( const QgsEffectPainter & );
444#endif
445
446 QgsRenderContext &mRenderContext;
447 QPainter *mPainter = nullptr;
448 QgsPaintEffect *mEffect = nullptr;
449};
450
451#endif // QGSPAINTEFFECT_H
452
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2873
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.
QString type() const override
Returns the effect type.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsDrawSource effect from a properties string map.
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.
QgsEffectPainter(QgsRenderContext &renderContext)
QgsEffectPainter constructor.
void setEffect(QgsPaintEffect *effect)
Sets the effect to be painted.
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.
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:199
#define SIP_DEPRECATED
Definition qgis_sip.h:114
#define SIP_FACTORY
Definition qgis_sip.h:84
#define SIP_NODEFAULTCTORS
Definition qgis_sip.h:109
#define SIP_END
Definition qgis_sip.h:216