QGIS API Documentation 3.99.0-Master (e69e8341d6a)
qgsgloweffect.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsgloweffect.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 "qgsgloweffect.h"
19#include "qgssymbollayerutils.h"
20#include "qgscolorutils.h"
21#include "qgsimageoperation.h"
22#include "qgscolorrampimpl.h"
23#include "qgsunittypes.h"
24
26 : mColor( Qt::white )
27{
28
29}
30
32 : QgsPaintEffect( other )
33{
34 operator=( other );
35}
36
41
46
48{
49 if ( !enabled() || !context.painter() || source().isNull() )
50 return;
51
53 {
54 //just draw unmodified source, we can't render this effect when forcing vectors
55 drawSource( *context.painter() );
56 return;
57 }
58
59 QImage im = sourceAsImage( context ).copy();
60
61 QgsColorRamp *ramp = nullptr;
62 std::unique_ptr< QgsGradientColorRamp > tempRamp;
63 if ( mColorType == ColorRamp && mRamp )
64 {
65 ramp = mRamp;
66 }
67 else
68 {
69 //create a temporary ramp
70 QColor transparentColor = mColor;
71 transparentColor.setAlpha( 0 );
72 tempRamp.reset( new QgsGradientColorRamp( mColor, transparentColor ) );
73 ramp = tempRamp.get();
74 }
75
78 dtProps.useMaxDistance = false;
79 dtProps.shadeExterior = shadeExterior();
80 dtProps.ramp = ramp;
81 QgsImageOperation::distanceTransform( im, dtProps, context.feedback() );
82
83 if ( context.feedback() && context.feedback()->isCanceled() )
84 return;
85
87 if ( blurLevel <= 16 )
88 {
89 QgsImageOperation::stackBlur( im, blurLevel, false, context.feedback() );
90 }
91 else
92 {
93 QImage *imb = QgsImageOperation::gaussianBlur( im, blurLevel, context.feedback() );
94 if ( !imb->isNull() )
95 im = QImage( *imb );
96 delete imb;
97 }
98
99 if ( context.feedback() && context.feedback()->isCanceled() )
100 return;
101
103
104 if ( context.feedback() && context.feedback()->isCanceled() )
105 return;
106
107 if ( !shadeExterior() )
108 {
109 //only keep interior portion
110 QPainter p( &im );
111 p.setRenderHint( QPainter::Antialiasing );
112 p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
113 p.drawImage( 0, 0, sourceAsImage( context ) );
114 p.end();
115 }
116
117 QPainter *painter = context.painter();
118 const QgsScopedQPainterState painterState( painter );
119 painter->setCompositionMode( mBlendMode );
120 painter->drawImage( imageOffset( context ), im );
121}
122
123QVariantMap QgsGlowEffect::properties() const
124{
125 QVariantMap props;
126 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
127 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
128 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
129 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
130 props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
131 props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
132 props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
133 props.insert( QStringLiteral( "spread" ), QString::number( mSpread ) );
134 props.insert( QStringLiteral( "spread_unit" ), QgsUnitTypes::encodeUnit( mSpreadUnit ) );
135 props.insert( QStringLiteral( "spread_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mSpreadMapUnitScale ) );
136 props.insert( QStringLiteral( "color_type" ), QString::number( static_cast< int >( mColorType ) ) );
137 props.insert( QStringLiteral( "single_color" ), QgsColorUtils::colorToString( mColor ) );
138
139 if ( mRamp )
140 {
141 props.insert( mRamp->properties() );
142 }
143
144 return props;
145}
146
147void QgsGlowEffect::readProperties( const QVariantMap &props )
148{
149 bool ok;
150 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
151 if ( ok )
152 {
153 mBlendMode = mode;
154 }
155 if ( props.contains( QStringLiteral( "transparency" ) ) )
156 {
157 const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
158 if ( ok )
159 {
160 mOpacity = 1.0 - transparency;
161 }
162 }
163 else
164 {
165 const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
166 if ( ok )
167 {
169 }
170 }
171 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
172 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
173 const double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
174 if ( ok )
175 {
176 mBlurLevel = level;
177 if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
178 {
179 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
180 mBlurLevel *= 0.2645;
181 }
182 }
183 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ).toString() );
184 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ).toString() );
185 const double spread = props.value( QStringLiteral( "spread" ) ).toDouble( &ok );
186 if ( ok )
187 {
188 mSpread = spread;
189 }
190 mSpreadUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "spread_unit" ) ).toString() );
191 mSpreadMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "spread_unit_scale" ) ).toString() );
192 const QgsGlowEffect::GlowColorType type = static_cast< QgsGlowEffect::GlowColorType >( props.value( QStringLiteral( "color_type" ) ).toInt( &ok ) );
193 if ( ok )
194 {
196 }
197 if ( props.contains( QStringLiteral( "single_color" ) ) )
198 {
199 mColor = QgsColorUtils::colorFromString( props.value( QStringLiteral( "single_color" ) ).toString() );
200 }
201
202 //attempt to create color ramp from props
203 delete mRamp;
204 if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
205 {
207 }
208 else
209 {
211 }
212}
213
215{
216 delete mRamp;
217 mRamp = ramp;
218}
219
221{
222 if ( &rhs == this )
223 return *this;
224
225 delete mRamp;
226
227 mSpread = rhs.spread();
228 mSpreadUnit = rhs.spreadUnit();
230 mRamp = rhs.ramp() ? rhs.ramp()->clone() : nullptr;
231 mBlurLevel = rhs.blurLevel();
232 mBlurUnit = rhs.mBlurUnit;
234 mOpacity = rhs.opacity();
235 mColor = rhs.color();
236 mBlendMode = rhs.blendMode();
237 mColorType = rhs.colorType();
238
239 return *this;
240}
241
242QRectF QgsGlowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
243{
244 //blur radius and spread size
247
248 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
249 spread += blurLevel * 2 + 10;
250 return rect.adjusted( -spread, -spread, spread, spread );
251}
252
253
254//
255// QgsOuterGlowEffect
256//
257
263
265{
267 effect->readProperties( map );
268 return effect;
269}
270
272{
273 QgsOuterGlowEffect *newEffect = new QgsOuterGlowEffect( *this );
274 return newEffect;
275}
276
277
278//
279// QgsInnerGlowEffect
280//
281
287
289{
291 effect->readProperties( map );
292 return effect;
293}
294
296{
297 QgsInnerGlowEffect *newEffect = new QgsInnerGlowEffect( *this );
298 return newEffect;
299}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
@ RequiresRasterization
The effect requires raster-based rendering.
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2765
Abstract base class for color ramps.
virtual QVariantMap properties() const =0
Returns a string map containing all the color ramp's properties.
virtual QgsColorRamp * clone() const =0
Creates a clone of the color ramp.
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 QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates the symbol layer.
static QString typeString()
Returns the string identifier for QgsCptCityColorRamp.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:53
Base class for paint effects which draw a glow inside or outside a picture.
QgsGlowEffect & operator=(const QgsGlowEffect &rhs)
QgsMapUnitScale mBlurMapUnitScale
~QgsGlowEffect() override
Qgis::RenderUnit mSpreadUnit
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
double spread() const
Returns the spread distance used for drawing the glow effect.
virtual bool shadeExterior() const =0
Specifies whether the glow is drawn outside the picture or within the picture.
const QgsMapUnitScale & spreadMapUnitScale() const
Returns the map unit scale used for the spread distance.
GlowColorType
Color sources for the glow.
@ ColorRamp
Use colors from a color ramp.
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
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...
QgsColorRamp * mRamp
double blurLevel() const
Returns the blur level (radius) for the glow.
double opacity() const
Returns the opacity for the effect.
Qgis::RenderUnit mBlurUnit
QColor color() const
Returns the color for the glow.
GlowColorType mColorType
QgsColorRamp * ramp() const
Returns the color ramp used for the glow.
Qgis::RenderUnit spreadUnit() const
Returns the units used for the glow spread distance.
QPainter::CompositionMode mBlendMode
QgsMapUnitScale mSpreadMapUnitScale
void setRamp(QgsColorRamp *ramp)
Sets the color ramp for the glow.
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.
GlowColorType colorType() const
Returns the color mode used for the glow.
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
Gradient color ramp, which smoothly interpolates between two colors and also supports optional extra ...
static QgsColorRamp * create(const QVariantMap &properties=QVariantMap())
Creates a new QgsColorRamp from a map of properties.
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
static void distanceTransform(QImage &image, const QgsImageOperation::DistanceTransformProperties &properties, QgsFeedback *feedback=nullptr)
Performs a distance transform on the source image and shades the result using a color ramp.
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.
A paint effect which draws a glow within a picture.
QgsInnerGlowEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsInnerGlowEffect effect from a properties string map.
A paint effect which draws a glow outside of a picture.
QgsOuterGlowEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsOuterGlowEffect effect from a properties string map.
Base class for visual effects which can be applied to QPicture drawings.
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.
virtual QString type() const =0
Returns the effect type.
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.
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.
Struct for storing properties of a distance transform operation.
bool useMaxDistance
Set to true to automatically calculate the maximum distance in the transform to use as the spread val...
bool shadeExterior
Set to true to perform the distance transform on transparent pixels in the source image,...
double spread
Maximum distance (in pixels) for the distance transform shading to spread.
QgsColorRamp * ramp
Color ramp to use for shading the distance transform.