QGIS API Documentation  3.6.0-Noosa (5873452)
qgsblureffect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsblureffect.cpp
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 
18 #include "qgsblureffect.h"
19 #include "qgsimageoperation.h"
20 #include "qgsrendercontext.h"
21 
23 {
24  QgsBlurEffect *newEffect = new QgsBlurEffect();
25  newEffect->readProperties( map );
26  return newEffect;
27 }
28 
30 {
31  if ( !source() || !enabled() || !context.painter() )
32  return;
33 
34  switch ( mBlurMethod )
35  {
36  case StackBlur:
37  drawStackBlur( context );
38  break;
39  case GaussianBlur:
40  drawGaussianBlur( context );
41  break;
42  }
43 }
44 
45 void QgsBlurEffect::drawStackBlur( QgsRenderContext &context )
46 {
47  QImage im = sourceAsImage( context )->copy();
48  QgsImageOperation::stackBlur( im, mBlurLevel );
49  drawBlurredImage( context, im );
50 }
51 
52 void QgsBlurEffect::drawGaussianBlur( QgsRenderContext &context )
53 {
54  QImage *im = QgsImageOperation::gaussianBlur( *sourceAsImage( context ), mBlurLevel );
55  drawBlurredImage( context, *im );
56  delete im;
57 }
58 
59 void QgsBlurEffect::drawBlurredImage( QgsRenderContext &context, QImage &image )
60 {
61  //opacity
62  QgsImageOperation::multiplyOpacity( image, mOpacity );
63 
64  QPainter *painter = context.painter();
65  painter->save();
66  painter->setCompositionMode( mBlendMode );
67  painter->drawImage( imageOffset( context ), image );
68  painter->restore();
69 }
70 
72 {
73  QgsStringMap props;
74  props.insert( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
75  props.insert( QStringLiteral( "draw_mode" ), QString::number( static_cast< int >( mDrawMode ) ) );
76  props.insert( QStringLiteral( "blend_mode" ), QString::number( static_cast< int >( mBlendMode ) ) );
77  props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
78  props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
79  props.insert( QStringLiteral( "blur_method" ), QString::number( static_cast< int >( mBlurMethod ) ) );
80  return props;
81 }
82 
84 {
85  bool ok;
86  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
87  if ( ok )
88  {
89  mBlendMode = mode;
90  }
91  if ( props.contains( QStringLiteral( "transparency" ) ) )
92  {
93  double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
94  if ( ok )
95  {
96  mOpacity = 1.0 - transparency;
97  }
98  }
99  else
100  {
101  double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
102  if ( ok )
103  {
104  mOpacity = opacity;
105  }
106  }
107 
108  mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
109  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
110  int level = props.value( QStringLiteral( "blur_level" ) ).toInt( &ok );
111  if ( ok )
112  {
113  mBlurLevel = level;
114  }
115  QgsBlurEffect::BlurMethod method = static_cast< QgsBlurEffect::BlurMethod >( props.value( QStringLiteral( "blur_method" ) ).toInt( &ok ) );
116  if ( ok )
117  {
118  mBlurMethod = method;
119  }
120 }
121 
123 {
124  QgsBlurEffect *newEffect = new QgsBlurEffect( *this );
125  return newEffect;
126 }
127 
128 QRectF QgsBlurEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
129 {
130  Q_UNUSED( context );
131 
132  //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
133  double spread = mBlurLevel * 2.0 + 10;
134  return rect.adjusted( -spread, -spread, spread, spread );
135 }
double opacity() const
Returns the opacity for the effect.
static void multiplyOpacity(QImage &image, double factor)
Multiplies opacity of image pixel values by a factor.
DrawMode mDrawMode
QgsStringMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QgsBlurEffect()=default
Constructor for QgsBlurEffect.
Base class for visual effects which can be applied to QPicture drawings.
Gaussian blur, a slower but high quality blur. Blur level values are the distance in pixels for the b...
Definition: qgsblureffect.h:43
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
Stack blur, a fast but low quality blur. Valid blur level values are between 0 - 16.
Definition: qgsblureffect.h:42
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
BlurMethod
Available blur methods (algorithms)
Definition: qgsblureffect.h:40
bool enabled() const
Returns whether the effect is enabled.
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:34
DrawMode
Drawing modes for effects.
static QImage * gaussianBlur(QImage &image, int radius)
Performs a gaussian blur on an image.
Contains information about the context of a rendering operation.
void draw(QgsRenderContext &context) override
Handles drawing of the effect&#39;s result on to the specified render context.
QPainter * painter()
Returns the destination QPainter for the render operation.
static void stackBlur(QImage &image, int radius, bool alphaOnly=false)
Performs a stack blur on an image.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
const QPicture * source() const
Returns the source QPicture.
void readProperties(const QgsStringMap &props) override
Reads a string map of an effect&#39;s properties and restores the effect to the state described by the pr...
QgsBlurEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.