QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsshadoweffect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsshadoweffect.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 "qgsshadoweffect.h"
19 #include "qgsimageoperation.h"
20 #include "qgssymbollayerutils.h"
21 #include "qgsunittypes.h"
22 
24  : mColor( Qt::black )
25 {
26 
27 }
28 
30 {
31  if ( !source() || !enabled() || !context.painter() )
32  return;
33 
34  QImage colorisedIm = sourceAsImage( context )->copy();
35 
36  QPainter *painter = context.painter();
37  QgsScopedQPainterState painterState( painter );
38  painter->setCompositionMode( mBlendMode );
39 
40  if ( !exteriorShadow() )
41  {
42  //inner shadow, first invert the opacity. The color does not matter since we will
43  //be replacing it anyway
44  colorisedIm.invertPixels( QImage::InvertRgba );
45  }
46 
48 
49  int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale ) );
50  if ( blurLevel <= 16 )
51  {
53  }
54  else
55  {
56  QImage *imb = QgsImageOperation::gaussianBlur( colorisedIm, blurLevel );
57  colorisedIm = QImage( *imb );
58  delete imb;
59  }
60 
61  double offsetDist = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );
62 
63  double angleRad = mOffsetAngle * M_PI / 180; // to radians
64  QPointF transPt( -offsetDist * std::cos( angleRad + M_PI_2 ),
65  -offsetDist * std::sin( angleRad + M_PI_2 ) );
66 
67  //transparency, scale
69 
70  if ( !exteriorShadow() )
71  {
72  //inner shadow, do a bit of painter juggling
73  QImage innerShadowIm( colorisedIm.width(), colorisedIm.height(), QImage::Format_ARGB32 );
74  innerShadowIm.fill( Qt::transparent );
75  QPainter imPainter( &innerShadowIm );
76 
77  //draw shadow at offset
78  imPainter.drawImage( transPt.x(), transPt.y(), colorisedIm );
79 
80  //restrict shadow so it's only drawn on top of original image
81  imPainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
82  imPainter.drawImage( 0, 0, *sourceAsImage( context ) );
83  imPainter.end();
84 
85  painter->drawImage( imageOffset( context ), innerShadowIm );
86  }
87  else
88  {
89  painter->drawImage( imageOffset( context ) + transPt, colorisedIm );
90  }
91 }
92 
93 QVariantMap QgsShadowEffect::properties() const
94 {
95  QVariantMap props;
96  props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
97  props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
98  props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
99  props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
100  props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
101  props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
102  props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
103  props.insert( QStringLiteral( "offset_angle" ), QString::number( mOffsetAngle ) );
104  props.insert( QStringLiteral( "offset_distance" ), QString::number( mOffsetDist ) );
105  props.insert( QStringLiteral( "offset_unit" ), QgsUnitTypes::encodeUnit( mOffsetUnit ) );
106  props.insert( QStringLiteral( "offset_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale ) );
107  props.insert( QStringLiteral( "color" ), QgsSymbolLayerUtils::encodeColor( mColor ) );
108  return props;
109 }
110 
111 void QgsShadowEffect::readProperties( const QVariantMap &props )
112 {
113  bool ok;
114  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
115  if ( ok )
116  {
117  mBlendMode = mode;
118  }
119  if ( props.contains( QStringLiteral( "transparency" ) ) )
120  {
121  double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
122  if ( ok )
123  {
124  mOpacity = 1.0 - transparency;
125  }
126  }
127  else
128  {
129  double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
130  if ( ok )
131  {
132  mOpacity = opacity;
133  }
134  }
135  mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
136  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
137  double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
138  if ( ok )
139  {
140  mBlurLevel = level;
141  if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
142  {
143  // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
144  mBlurLevel *= 0.2645;
145  }
146  }
147  mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ).toString() );
148  mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ).toString() );
149  int angle = props.value( QStringLiteral( "offset_angle" ) ).toInt( &ok );
150  if ( ok )
151  {
153  }
154  double distance = props.value( QStringLiteral( "offset_distance" ) ).toDouble( &ok );
155  if ( ok )
156  {
157  mOffsetDist = distance;
158  }
159  mOffsetUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "offset_unit" ) ).toString() );
160  mOffsetMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "offset_unit_scale" ) ).toString() );
161  if ( props.contains( QStringLiteral( "color" ) ) )
162  {
163  mColor = QgsSymbolLayerUtils::decodeColor( props.value( QStringLiteral( "color" ) ).toString() );
164  }
165 }
166 
167 QRectF QgsShadowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
168 {
169  //blur radius and offset distance
170  int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale ) );
172  //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
173  spread += blurLevel * 2 + 10;
174  return rect.adjusted( -spread, -spread, spread, spread );
175 }
176 
177 
178 //
179 // QgsDropShadowEffect
180 //
181 
182 QgsPaintEffect *QgsDropShadowEffect::create( const QVariantMap &map )
183 {
185  effect->readProperties( map );
186  return effect;
187 }
188 
190  : QgsShadowEffect()
191 {
192 
193 }
194 
196 {
197  return QStringLiteral( "dropShadow" );
198 }
199 
201 {
202  return new QgsDropShadowEffect( *this );
203 }
204 
206 {
207  return true;
208 }
209 
210 
211 //
212 // QgsInnerShadowEffect
213 //
214 
216 {
218  effect->readProperties( map );
219  return effect;
220 }
221 
223  : QgsShadowEffect()
224 {
225 
226 }
227 
229 {
230  return QStringLiteral( "innerShadow" );
231 }
232 
234 {
235  return new QgsInnerShadowEffect( *this );
236 }
237 
239 {
240  return false;
241 }
A paint effect which draws an offset and optionally blurred drop shadow.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.
QString type() const override
Returns the effect type.
QgsDropShadowEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
bool exteriorShadow() const override
Specifies whether the shadow is drawn outside the picture or within the picture.
static void overlayColor(QImage &image, const QColor &color)
Overlays a color onto an image.
static void multiplyOpacity(QImage &image, double factor)
Multiplies opacity of image pixel values by a factor.
static void stackBlur(QImage &image, int radius, bool alphaOnly=false)
Performs a stack blur on an image.
static QImage * gaussianBlur(QImage &image, int radius)
Performs a gaussian blur on an image.
A paint effect which draws an offset and optionally blurred drop shadow within a picture.
bool exteriorShadow() const override
Specifies whether the shadow is drawn outside the picture or within the picture.
QString type() const override
Returns the effect type.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsInnerShadowEffect effect from a properties string map.
QgsInnerShadowEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
Base class for visual effects which can be applied to QPicture drawings.
DrawMode mDrawMode
const QPicture * source() const
Returns the source QPicture.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
bool enabled() const
Returns whether the effect is enabled.
DrawMode
Drawing modes for effects.
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
Scoped object for saving and restoring a QPainter object's state.
Base class for paint effects which offset, blurred shadows.
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
double opacity() const
Returns the opacity for the effect.
QgsUnitTypes::RenderUnit mOffsetUnit
QgsUnitTypes::RenderUnit mBlurUnit
virtual bool exteriorShadow() const =0
Specifies whether the shadow is drawn outside the picture or within the picture.
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
QgsMapUnitScale mOffsetMapUnitScale
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.
double blurLevel() const
Returns the blur level (radius) for the shadow.
QPainter::CompositionMode mBlendMode
void readProperties(const QVariantMap &props) override
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
QgsMapUnitScale mBlurMapUnitScale
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QColor decodeColor(const QString &str)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QString encodeColor(const QColor &color)
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786