QGIS API Documentation 3.99.0-Master (d270888f95f)
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
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30 : mColor( Qt::black )
31{
32
33}
34
39
41{
42 if ( !enabled() || !context.painter() || source().isNull() )
43 return;
44
45 if ( context.feedback() && context.feedback()->isCanceled() )
46 return;
47
49 {
50 //just draw unmodified source, we can't render this effect when forcing vectors
51 drawSource( *context.painter() );
52 return;
53 }
54
55 QImage colorisedIm = sourceAsImage( context ).copy();
56
57 if ( context.feedback() && context.feedback()->isCanceled() )
58 return;
59
60 QPainter *painter = context.painter();
61 const QgsScopedQPainterState painterState( painter );
62 painter->setCompositionMode( mBlendMode );
63
64 if ( !exteriorShadow() )
65 {
66 //inner shadow, first invert the opacity. The color does not matter since we will
67 //be replacing it anyway
68 colorisedIm.invertPixels( QImage::InvertRgba );
69 }
70
72
74 if ( blurLevel <= 16 )
75 {
76 QgsImageOperation::stackBlur( colorisedIm, blurLevel, false, context.feedback() );
77 }
78 else
79 {
80 QImage *imb = QgsImageOperation::gaussianBlur( colorisedIm, blurLevel, context.feedback() );
81 if ( !imb->isNull() )
82 colorisedIm = QImage( *imb );
83 delete imb;
84 }
85
86 const double offsetDist = context.convertToPainterUnits( mOffsetDist, mOffsetUnit, mOffsetMapUnitScale );
87
88 const double angleRad = mOffsetAngle * M_PI / 180; // to radians
89 const QPointF transPt( -offsetDist * std::cos( angleRad + M_PI_2 ),
90 -offsetDist * std::sin( angleRad + M_PI_2 ) );
91
92 //transparency, scale
93 QgsImageOperation::multiplyOpacity( colorisedIm, mOpacity, context.feedback() );
94
95 if ( !exteriorShadow() )
96 {
97 //inner shadow, do a bit of painter juggling
98 QImage innerShadowIm( colorisedIm.width(), colorisedIm.height(), QImage::Format_ARGB32 );
99 innerShadowIm.fill( Qt::transparent );
100 QPainter imPainter( &innerShadowIm );
101
102 //draw shadow at offset
103 imPainter.drawImage( transPt.x(), transPt.y(), colorisedIm );
104
105 //restrict shadow so it's only drawn on top of original image
106 imPainter.setCompositionMode( QPainter::CompositionMode_DestinationIn );
107 imPainter.drawImage( 0, 0, sourceAsImage( context ) );
108 imPainter.end();
109
110 painter->drawImage( imageOffset( context ), innerShadowIm );
111 }
112 else
113 {
114 painter->drawImage( imageOffset( context ) + transPt, colorisedIm );
115 }
116}
117
119{
120 QVariantMap props;
121 props.insert( u"enabled"_s, mEnabled ? "1" : "0" );
122 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
123 props.insert( u"blend_mode"_s, QString::number( int( mBlendMode ) ) );
124 props.insert( u"opacity"_s, QString::number( mOpacity ) );
125 props.insert( u"blur_level"_s, QString::number( mBlurLevel ) );
126 props.insert( u"blur_unit"_s, QgsUnitTypes::encodeUnit( mBlurUnit ) );
127 props.insert( u"blur_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
128 props.insert( u"offset_angle"_s, QString::number( mOffsetAngle ) );
129 props.insert( u"offset_distance"_s, QString::number( mOffsetDist ) );
130 props.insert( u"offset_unit"_s, QgsUnitTypes::encodeUnit( mOffsetUnit ) );
131 props.insert( u"offset_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mOffsetMapUnitScale ) );
132 props.insert( u"color"_s, QgsColorUtils::colorToString( mColor ) );
133 return props;
134}
135
136void QgsShadowEffect::readProperties( const QVariantMap &props )
137{
138 bool ok;
139 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( u"blend_mode"_s ).toInt( &ok ) );
140 if ( ok )
141 {
142 mBlendMode = mode;
143 }
144 if ( props.contains( u"transparency"_s ) )
145 {
146 const double transparency = props.value( u"transparency"_s ).toDouble( &ok );
147 if ( ok )
148 {
149 mOpacity = 1.0 - transparency;
150 }
151 }
152 else
153 {
154 const double opacity = props.value( u"opacity"_s ).toDouble( &ok );
155 if ( ok )
156 {
158 }
159 }
160 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
161 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
162 const double level = props.value( u"blur_level"_s ).toDouble( &ok );
163 if ( ok )
164 {
165 mBlurLevel = level;
166 if ( !props.contains( u"blur_unit"_s ) )
167 {
168 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
169 mBlurLevel *= 0.2645;
170 }
171 }
172 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"blur_unit"_s ).toString() );
173 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"blur_unit_scale"_s ).toString() );
174 const int angle = props.value( u"offset_angle"_s ).toInt( &ok );
175 if ( ok )
176 {
177 mOffsetAngle = angle;
178 }
179 const double distance = props.value( u"offset_distance"_s ).toDouble( &ok );
180 if ( ok )
181 {
182 mOffsetDist = distance;
183 }
184 mOffsetUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"offset_unit"_s ).toString() );
185 mOffsetMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"offset_unit_scale"_s ).toString() );
186 if ( props.contains( u"color"_s ) )
187 {
188 mColor = QgsColorUtils::colorFromString( props.value( u"color"_s ).toString() );
189 }
190}
191
192QRectF QgsShadowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
193{
194 //blur radius and offset distance
196
197 // spread is initially the shadow offset size
199
200 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
201 spread += blurLevel * 2 + 10;
202 return rect.adjusted( -spread, -spread, spread, spread );
203}
204
205
206//
207// QgsDropShadowEffect
208//
209
211{
213 effect->readProperties( map );
214 return effect;
215}
216
222
224{
225 return u"dropShadow"_s;
226}
227
229{
230 return new QgsDropShadowEffect( *this );
231}
232
234{
235 return true;
236}
237
238
239//
240// QgsInnerShadowEffect
241//
242
244{
246 effect->readProperties( map );
247 return effect;
248}
249
255
257{
258 return u"innerShadow"_s;
259}
260
265
267{
268 return false;
269}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2764
@ RequiresRasterization
The effect requires raster-based rendering.
Definition qgis.h:2864
@ ShadowOffset
Shadow offset.
Definition qgis.h:3113
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2873
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:55
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.