QGIS API Documentation  3.21.0-Master (5b68dc587e)
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 <QPainter>
23 #include <QDomDocument>
24 #include <QDomElement>
25 
26 class QgsRenderContext;
27 
53 {
54 
55 #ifdef SIP_RUN
57  if ( sipCpp->type() == "drawSource" && dynamic_cast<QgsDrawSourceEffect *>( sipCpp ) != NULL )
58  {
59  sipType = sipType_QgsDrawSourceEffect;
60  }
61  else if ( sipCpp->type() == "effectStack" && dynamic_cast<QgsEffectStack *>( sipCpp ) != NULL )
62  {
63  sipType = sipType_QgsEffectStack;
64  }
65  else if ( sipCpp->type() == "blur" && dynamic_cast<QgsBlurEffect *>( sipCpp ) != NULL )
66  {
67  sipType = sipType_QgsBlurEffect;
68  }
69  else if ( sipCpp->type() == "dropShadow" && dynamic_cast<QgsDropShadowEffect *>( sipCpp ) != NULL )
70  {
71  sipType = sipType_QgsDropShadowEffect;
72  }
73  else if ( sipCpp->type() == "outerGlow" && dynamic_cast<QgsOuterGlowEffect *>( sipCpp ) != NULL )
74  {
75  sipType = sipType_QgsOuterGlowEffect;
76  }
77  else if ( sipCpp->type() == "innerGlow" && dynamic_cast<QgsInnerGlowEffect *>( sipCpp ) != NULL )
78  {
79  sipType = sipType_QgsInnerGlowEffect;
80  }
81  else if ( sipCpp->type() == "transform" && dynamic_cast<QgsTransformEffect *>( sipCpp ) != NULL )
82  {
83  sipType = sipType_QgsTransformEffect;
84  }
85  else if ( sipCpp->type() == "color" && dynamic_cast<QgsColorEffect *>( sipCpp ) != NULL )
86  {
87  sipType = sipType_QgsColorEffect;
88  }
89  else
90  {
91  sipType = 0;
92  }
93  SIP_END
94 #endif
95 
96  public:
97 
103  enum DrawMode
104  {
107  ModifyAndRender
108  };
109 
113  QgsPaintEffect() = default;
114 
115  QgsPaintEffect( const QgsPaintEffect &other );
116  virtual ~QgsPaintEffect();
117 
122  virtual QString type() const = 0;
123 
128  virtual QgsPaintEffect *clone() const = 0 SIP_FACTORY;
129 
137  virtual QVariantMap properties() const = 0;
138 
145  virtual void readProperties( const QVariantMap &props ) = 0;
146 
156  virtual bool saveProperties( QDomDocument &doc, QDomElement &element ) const;
157 
164  virtual bool readProperties( const QDomElement &element );
165 
172  virtual void render( QPicture &picture, QgsRenderContext &context );
173 
182  virtual void begin( QgsRenderContext &context );
183 
190  virtual void end( QgsRenderContext &context );
191 
197  bool enabled() const { return mEnabled; }
198 
204  void setEnabled( bool enabled );
205 
212  DrawMode drawMode() const { return mDrawMode; }
213 
220  void setDrawMode( DrawMode drawMode );
221 
222  protected:
223 
224  bool mEnabled = true;
225  DrawMode mDrawMode = ModifyAndRender;
226  bool requiresQPainterDpiFix = true;
227 
235  virtual void draw( QgsRenderContext &context ) = 0;
236 
244  void drawSource( QPainter &painter );
245 
253  const QPicture *source() const { return mPicture; }
254 
266  QImage *sourceAsImage( QgsRenderContext &context );
267 
275  QPointF imageOffset( const QgsRenderContext &context ) const;
276 
286  virtual QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const;
287 
294  void fixQPictureDpi( QPainter *painter ) const;
295 
296  private:
297 
298  const QPicture *mPicture = nullptr;
299  QImage *mSourceImage = nullptr;
300  bool mOwnsImage = false;
301 
302  QPainter *mPrevPainter = nullptr;
303  QPainter *mEffectPainter = nullptr;
304  QPicture *mTempPicture = nullptr;
305 
306  QRectF imageBoundingRect( const QgsRenderContext &context ) const;
307 
308  friend class QgsEffectStack;
309 
310  QgsPaintEffect &operator= ( const QgsPaintEffect & ) = delete;
311 
312 };
313 
328 {
329  public:
330 
332  QgsDrawSourceEffect() = default;
333 
339  static QgsPaintEffect *create( const QVariantMap &map ) SIP_FACTORY;
340 
341  QString type() const override { return QStringLiteral( "drawSource" ); }
342  QgsDrawSourceEffect *clone() const override SIP_FACTORY;
343  QVariantMap properties() const override;
344  void readProperties( const QVariantMap &props ) override;
345 
352  void setOpacity( const double opacity ) { mOpacity = opacity; }
353 
360  double opacity() const { return mOpacity; }
361 
368  void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
369 
376  QPainter::CompositionMode blendMode() const { return mBlendMode; }
377 
378  protected:
379 
380  void draw( QgsRenderContext &context ) override;
381 
382  private:
383 
384  double mOpacity = 1.0;
385  QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
386 };
387 
395 class CORE_EXPORT QgsEffectPainter
396 {
397  public:
398 
405  QgsEffectPainter( QgsRenderContext &renderContext );
406 
414  QgsEffectPainter( QgsRenderContext &renderContext, QgsPaintEffect *effect );
415  ~QgsEffectPainter();
416 
422  void setEffect( QgsPaintEffect *effect );
423 
425 
431  QPainter *operator->() { return mPainter; }
433 
434  private:
435 #ifdef SIP_RUN
436  const QgsEffectPainter &operator=( const QgsEffectPainter & );
437 #endif
438 
439  QgsRenderContext &mRenderContext;
440  QPainter *mPainter = nullptr;
441  QgsPaintEffect *mEffect = nullptr;
442 };
443 
444 #endif // QGSPAINTEFFECT_H
445 
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:38
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
Constructor for QgsDrawSourceEffect.
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.
A class to manager painter saving and restoring required for effect drawing.
A paint effect which consists of a stack of other chained paint effects.
QgsEffectStack & operator=(const QgsEffectStack &rhs)
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
Constructor for QgsPaintEffect.
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.
virtual QgsPaintEffect * clone() const =0
Duplicates an effect by creating a deep copy of the effect.
DrawMode drawMode() const
Returns the draw mode for the effect.
bool enabled() const
Returns whether the effect is enabled.
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:177
#define SIP_FACTORY
Definition: qgis_sip.h:76
#define SIP_NODEFAULTCTORS
Definition: qgis_sip.h:101
#define SIP_END
Definition: qgis_sip.h:194