QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
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
20#include "qgscolorutils.h"
21#include "qgsimageoperation.h"
22#include "qgssymbollayerutils.h"
23#include "qgsunittypes.h"
24
26 : mColor( Qt::black )
27{
28
29}
30
35
37{
38 if ( !enabled() || !context.painter() || source().isNull() )
39 return;
40
41 if ( context.feedback() && context.feedback()->isCanceled() )
42 return;
43
45 {
46 //just draw unmodified source, we can't render this effect when forcing vectors
47 drawSource( *context.painter() );
48 return;
49 }
50
51 QImage colorisedIm = sourceAsImage( context ).copy();
52
53 if ( context.feedback() && context.feedback()->isCanceled() )
54 return;
55
56 QPainter *painter = context.painter();
57 const QgsScopedQPainterState painterState( painter );
58 painter->setCompositionMode( mBlendMode );
59
60 if ( !exteriorShadow() )
61 {
62 //inner shadow, first invert the opacity. The color does not matter since we will
63 //be replacing it anyway
64 colorisedIm.invertPixels( QImage::InvertRgba );
65 }
66
68
70 if ( blurLevel <= 16 )
71 {
72 QgsImageOperation::stackBlur( colorisedIm, blurLevel, false, context.feedback() );
73 }
74 else
75 {
76 QImage *imb = QgsImageOperation::gaussianBlur( colorisedIm, blurLevel, context.feedback() );
77 if ( !imb->isNull() )
78 colorisedIm = QImage( *imb );
79 delete imb;
80 }
81
82 const double offsetDist = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );
83
84 const double angleRad = mOffsetAngle * M_PI / 180; // to radians
85 const QPointF transPt( -offsetDist * std::cos( angleRad + M_PI_2 ),
86 -offsetDist * std::sin( angleRad + M_PI_2 ) );
87
88 //transparency, scale
89 QgsImageOperation::multiplyOpacity( colorisedIm, mOpacity, context.feedback() );
90
91 if ( !exteriorShadow() )
92 {
93 //inner shadow, do a bit of painter juggling
94 QImage innerShadowIm( colorisedIm.width(), colorisedIm.height(), QImage::Format_ARGB32 );
95 innerShadowIm.fill( Qt::transparent );
96 QPainter imPainter( &innerShadowIm );
97
98 //draw shadow at offset
99 imPainter.drawImage( transPt.x(), transPt.y(), colorisedIm );
100
101 //restrict shadow so it's only drawn on top of original image
102 imPainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
103 imPainter.drawImage( 0, 0, sourceAsImage( context ) );
104 imPainter.end();
105
106 painter->drawImage( imageOffset( context ), innerShadowIm );
107 }
108 else
109 {
110 painter->drawImage( imageOffset( context ) + transPt, colorisedIm );
111 }
112}
113
115{
116 QVariantMap props;
117 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
118 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
119 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
120 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
121 props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
122 props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
123 props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
124 props.insert( QStringLiteral( "offset_angle" ), QString::number( mOffsetAngle ) );
125 props.insert( QStringLiteral( "offset_distance" ), QString::number( mOffsetDist ) );
126 props.insert( QStringLiteral( "offset_unit" ), QgsUnitTypes::encodeUnit( mOffsetUnit ) );
127 props.insert( QStringLiteral( "offset_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale ) );
128 props.insert( QStringLiteral( "color" ), QgsColorUtils::colorToString( mColor ) );
129 return props;
130}
131
132void QgsShadowEffect::readProperties( const QVariantMap &props )
133{
134 bool ok;
135 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
136 if ( ok )
137 {
138 mBlendMode = mode;
139 }
140 if ( props.contains( QStringLiteral( "transparency" ) ) )
141 {
142 const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
143 if ( ok )
144 {
145 mOpacity = 1.0 - transparency;
146 }
147 }
148 else
149 {
150 const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
151 if ( ok )
152 {
154 }
155 }
156 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
157 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
158 const double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
159 if ( ok )
160 {
161 mBlurLevel = level;
162 if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
163 {
164 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
165 mBlurLevel *= 0.2645;
166 }
167 }
168 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ).toString() );
169 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ).toString() );
170 const int angle = props.value( QStringLiteral( "offset_angle" ) ).toInt( &ok );
171 if ( ok )
172 {
173 mOffsetAngle = angle;
174 }
175 const double distance = props.value( QStringLiteral( "offset_distance" ) ).toDouble( &ok );
176 if ( ok )
177 {
178 mOffsetDist = distance;
179 }
180 mOffsetUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "offset_unit" ) ).toString() );
181 mOffsetMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "offset_unit_scale" ) ).toString() );
182 if ( props.contains( QStringLiteral( "color" ) ) )
183 {
184 mColor = QgsColorUtils::colorFromString( props.value( QStringLiteral( "color" ) ).toString() );
185 }
186}
187
188QRectF QgsShadowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
189{
190 //blur radius and offset distance
192
193 // spread is initially the shadow offset size
195
196 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
197 spread += blurLevel * 2 + 10;
198 return rect.adjusted( -spread, -spread, spread, spread );
199}
200
201
202//
203// QgsDropShadowEffect
204//
205
207{
209 effect->readProperties( map );
210 return effect;
211}
212
218
220{
221 return QStringLiteral( "dropShadow" );
222}
223
225{
226 return new QgsDropShadowEffect( *this );
227}
228
230{
231 return true;
232}
233
234
235//
236// QgsInnerShadowEffect
237//
238
240{
242 effect->readProperties( map );
243 return effect;
244}
245
251
253{
254 return QStringLiteral( "innerShadow" );
255}
256
261
263{
264 return false;
265}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2706
@ RequiresRasterization
The effect requires raster-based rendering.
Definition qgis.h:2806
@ ShadowOffset
Shadow offset.
Definition qgis.h:3055
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2815
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
static void overlayColor(QImage &image, const QColor &color)
Overlays a color onto an image.
static QImage * gaussianBlur(QImage &image, int radius, QgsFeedback *feedback=nullptr)
Performs a gaussian blur on an image.
static void stackBlur(QImage &image, int radius, bool alphaOnly=false, QgsFeedback *feedback=nullptr)
Performs a stack blur on an image.
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.
QgsPaintEffect()=default
void drawSource(QPainter &painter)
Draws the source QPicture onto the specified painter.
QImage sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
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.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
Scoped object for saving and restoring a QPainter object's state.
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.
Qgis::RenderUnit mOffsetUnit
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
Qgis::RenderUnit mBlurUnit
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.
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
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 QgsMapUnitScale decodeMapUnitScale(const QString &str)
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.