QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgssymbollayer.h"
23 #include <QPainter>
24 #include <QDomDocument>
25 #include <QDomElement>
26 
27 class QgsRenderContext;
28 
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  }
94  SIP_END
95 #endif
96 
97  public:
98 
104  enum DrawMode
105  {
108  ModifyAndRender
109  };
110 
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 
138  virtual QgsStringMap properties() const = 0;
139 
146  virtual void readProperties( const QgsStringMap &props ) = 0;
147 
157  virtual bool saveProperties( QDomDocument &doc, QDomElement &element ) const;
158 
165  virtual bool readProperties( const QDomElement &element );
166 
173  virtual void render( QPicture &picture, QgsRenderContext &context );
174 
183  virtual void begin( QgsRenderContext &context );
184 
191  virtual void end( QgsRenderContext &context );
192 
198  bool enabled() const { return mEnabled; }
199 
205  void setEnabled( bool enabled );
206 
213  DrawMode drawMode() const { return mDrawMode; }
214 
221  void setDrawMode( DrawMode drawMode );
222 
223  protected:
224 
225  bool mEnabled = true;
226  DrawMode mDrawMode = ModifyAndRender;
227  bool requiresQPainterDpiFix = true;
228 
236  virtual void draw( QgsRenderContext &context ) = 0;
237 
245  void drawSource( QPainter &painter );
246 
254  const QPicture *source() const { return mPicture; }
255 
267  QImage *sourceAsImage( QgsRenderContext &context );
268 
276  QPointF imageOffset( const QgsRenderContext &context ) const;
277 
287  virtual QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const;
288 
295  void fixQPictureDpi( QPainter *painter ) const;
296 
297  private:
298 
299  const QPicture *mPicture = nullptr;
300  QImage *mSourceImage = nullptr;
301  bool mOwnsImage = false;
302 
303  QPainter *mPrevPainter = nullptr;
304  QPainter *mEffectPainter = nullptr;
305  QPicture *mTempPicture = nullptr;
306 
307  QRectF imageBoundingRect( const QgsRenderContext &context ) const;
308 
309  friend class QgsEffectStack;
310 
311  QgsPaintEffect &operator= ( const QgsPaintEffect & ) = delete;
312 
313 };
314 
329 {
330  public:
331 
333  QgsDrawSourceEffect() = default;
334 
340  static QgsPaintEffect *create( const QgsStringMap &map ) SIP_FACTORY;
341 
342  QString type() const override { return QStringLiteral( "drawSource" ); }
343  QgsDrawSourceEffect *clone() const override SIP_FACTORY;
344  QgsStringMap properties() const override;
345  void readProperties( const QgsStringMap &props ) override;
346 
353  void setOpacity( const double opacity ) { mOpacity = opacity; }
354 
361  double opacity() const { return mOpacity; }
362 
369  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
370 
377  QPainter::CompositionMode blendMode() const { return mBlendMode; }
378 
379  protected:
380 
381  void draw( QgsRenderContext &context ) override;
382 
383  private:
384 
385  double mOpacity = 1.0;
386  QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
387 };
388 
396 class CORE_EXPORT QgsEffectPainter
397 {
398  public:
399 
406  QgsEffectPainter( QgsRenderContext &renderContext );
407 
415  QgsEffectPainter( QgsRenderContext &renderContext, QgsPaintEffect *effect );
416  ~QgsEffectPainter();
417 
423  void setEffect( QgsPaintEffect *effect );
424 
426 
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 
QgsPaintEffect::DrawMode
DrawMode
Drawing modes for effects.
Definition: qgspainteffect.h:104
QgsBlurEffect
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:34
QgsEffectPainter
A class to manager painter saving and restoring required for effect drawing.
Definition: qgspainteffect.h:396
QgsDrawSourceEffect::setBlendMode
void setBlendMode(const QPainter::CompositionMode mode)
Sets the blend mode for the effect.
Definition: qgspainteffect.h:369
QgsDrawSourceEffect
A paint effect which draws the source picture with minor or no alterations.
Definition: qgspainteffect.h:328
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsColorEffect
A paint effect which alters the colors (e.g., brightness, contrast) in a source picture.
Definition: qgscoloreffect.h:35
SIP_FACTORY
#define SIP_FACTORY
Definition: qgis_sip.h:76
QgsPaintEffect::Modifier
@ Modifier
The result of the effect is not rendered, but is passed on to following effects in the stack.
Definition: qgspainteffect.h:106
QgsPaintEffect::Render
@ Render
The result of the effect is rendered on the destination, but does not affect subsequent effects in th...
Definition: qgspainteffect.h:107
SIP_CONVERT_TO_SUBCLASS_CODE
#define SIP_CONVERT_TO_SUBCLASS_CODE(code)
Definition: qgis_sip.h:172
QgsDropShadowEffect
A paint effect which draws an offset and optionally blurred drop shadow.
Definition: qgsshadoweffect.h:254
QgsPaintEffect::clone
virtual QgsPaintEffect * clone() const =0
Duplicates an effect by creating a deep copy of the effect.
qgis_sip.h
qgssymbollayer.h
QgsPaintEffect::drawMode
DrawMode drawMode() const
Returns the draw mode for the effect.
Definition: qgspainteffect.h:213
QgsInnerGlowEffect
A paint effect which draws a glow within a picture.
Definition: qgsgloweffect.h:331
SIP_NODEFAULTCTORS
#define SIP_NODEFAULTCTORS
Definition: qgis_sip.h:101
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsPaintEffect::source
const QPicture * source() const
Returns the source QPicture.
Definition: qgspainteffect.h:254
QgsTransformEffect
A paint effect which applies transformations (such as move, scale and rotate) to a picture.
Definition: qgstransformeffect.h:36
QgsDrawSourceEffect::blendMode
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
Definition: qgspainteffect.h:377
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:53
QgsOuterGlowEffect
A paint effect which draws a glow outside of a picture.
Definition: qgsgloweffect.h:299
QgsPaintEffect::enabled
bool enabled() const
Returns whether the effect is enabled.
Definition: qgspainteffect.h:198
QgsEffectStack::operator=
QgsEffectStack & operator=(const QgsEffectStack &rhs)
Definition: qgseffectstack.cpp:50
SIP_END
#define SIP_END
Definition: qgis_sip.h:189
QgsDrawSourceEffect::opacity
double opacity() const
Returns the opacity for the effect.
Definition: qgspainteffect.h:361
QgsPaintEffect::draw
virtual void draw(QgsRenderContext &context)=0
Handles drawing of the effect's result on to the specified render context.
QgsDrawSourceEffect::type
QString type() const override
Returns the effect type.
Definition: qgspainteffect.h:342
QgsEffectStack
A paint effect which consists of a stack of other chained paint effects.
Definition: qgseffectstack.h:44