QGIS API Documentation  2.18.3-Las Palmas (77b8c3d)
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 
22 {
23  QgsBlurEffect* newEffect = new QgsBlurEffect();
24  newEffect->readProperties( map );
25  return newEffect;
26 }
27 
29  : QgsPaintEffect()
30  , mBlurLevel( 10 )
31  , mBlurMethod( StackBlur )
32  , mTransparency( 0.0 )
33  , mBlendMode( QPainter::CompositionMode_SourceOver )
34 {
35 
36 }
37 
39 {
40 
41 }
42 
44 {
45  if ( !source() || !enabled() || !context.painter() )
46  return;
47 
48  switch ( mBlurMethod )
49  {
50  case StackBlur:
51  drawStackBlur( context );
52  break;
53  case GaussianBlur:
54  drawGaussianBlur( context );
55  break;
56  }
57 }
58 
59 void QgsBlurEffect::drawStackBlur( QgsRenderContext& context )
60 {
61  QImage im = sourceAsImage( context )->copy();
62  QgsImageOperation::stackBlur( im, mBlurLevel );
63  drawBlurredImage( context, im );
64 }
65 
66 void QgsBlurEffect::drawGaussianBlur( QgsRenderContext &context )
67 {
68  QImage* im = QgsImageOperation::gaussianBlur( *sourceAsImage( context ), mBlurLevel );
69  drawBlurredImage( context, *im );
70  delete im;
71 }
72 
73 void QgsBlurEffect::drawBlurredImage( QgsRenderContext& context, QImage& image )
74 {
75  //transparency
76  QgsImageOperation::multiplyOpacity( image, 1.0 - mTransparency );
77 
78  QPainter* painter = context.painter();
79  painter->save();
80  painter->setCompositionMode( mBlendMode );
81  painter->drawImage( imageOffset( context ), image );
82  painter->restore();
83 }
84 
86 {
87  QgsStringMap props;
88  props.insert( "enabled", mEnabled ? "1" : "0" );
89  props.insert( "draw_mode", QString::number( static_cast< int >( mDrawMode ) ) );
90  props.insert( "blend_mode", QString::number( static_cast< int >( mBlendMode ) ) );
91  props.insert( "transparency", QString::number( mTransparency ) );
92  props.insert( "blur_level", QString::number( mBlurLevel ) );
93  props.insert( "blur_method", QString::number( static_cast< int >( mBlurMethod ) ) );
94  return props;
95 }
96 
98 {
99  bool ok;
100  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( "blend_mode" ).toInt( &ok ) );
101  if ( ok )
102  {
103  mBlendMode = mode;
104  }
105  double transparency = props.value( "transparency" ).toDouble( &ok );
106  if ( ok )
107  {
108  mTransparency = transparency;
109  }
110  mEnabled = props.value( "enabled", "1" ).toInt();
111  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( "draw_mode", "2" ).toInt() );
112  int level = props.value( "blur_level" ).toInt( &ok );
113  if ( ok )
114  {
115  mBlurLevel = level;
116  }
117  QgsBlurEffect::BlurMethod method = static_cast< QgsBlurEffect::BlurMethod >( props.value( "blur_method" ).toInt( &ok ) );
118  if ( ok )
119  {
120  mBlurMethod = method;
121  }
122 }
123 
125 {
126  QgsBlurEffect* newEffect = new QgsBlurEffect( *this );
127  return newEffect;
128 }
129 
130 QRectF QgsBlurEffect::boundingRect( const QRectF &rect, const QgsRenderContext& context ) const
131 {
132  Q_UNUSED( context );
133 
134  //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
135  double spread = mBlurLevel * 2.0 + 10;
136  return rect.adjusted( -spread, -spread, spread, spread );
137 }
void setCompositionMode(CompositionMode mode)
DrawMode mDrawMode
static void multiplyOpacity(QImage &image, const double factor)
Multiplies opacity of image pixel values by a factor.
virtual QgsStringMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
bool enabled() const
Returns whether the effect is enabled.
Base class for visual effects which can be applied to QPicture drawings.
void save()
QImage copy(const QRect &rectangle) const
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
BlurMethod
Available blur methods (algorithms)
Definition: qgsblureffect.h:38
QString number(int n, int base)
static QImage * gaussianBlur(QImage &image, const int radius)
Performs a gaussian blur on an image.
double transparency() const
Returns the transparency for the effect.
Definition: qgsblureffect.h:98
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:32
DrawMode
Drawing modes for effects.
void restore()
Contains information about the context of a rendering operation.
virtual void draw(QgsRenderContext &context) override
Handles drawing of the effect&#39;s result on to the specified render context.
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
QPainter * painter()
static void stackBlur(QImage &image, const int radius, const 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.
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const
iterator insert(const Key &key, const T &value)
const QPicture * source() const
Returns the source QPicture.
virtual ~QgsBlurEffect()
virtual 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...
virtual QgsBlurEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
const T value(const Key &key) const
virtual QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.