QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
43{
44 if ( !source() || !enabled() || !context.painter() )
45 return;
46
47 QImage im = sourceAsImage( context )->copy();
48
49 QgsColorRamp *ramp = nullptr;
50 std::unique_ptr< QgsGradientColorRamp > tempRamp;
51 if ( mColorType == ColorRamp && mRamp )
52 {
53 ramp = mRamp;
54 }
55 else
56 {
57 //create a temporary ramp
58 QColor transparentColor = mColor;
59 transparentColor.setAlpha( 0 );
60 tempRamp.reset( new QgsGradientColorRamp( mColor, transparentColor ) );
61 ramp = tempRamp.get();
62 }
63
66 dtProps.useMaxDistance = false;
67 dtProps.shadeExterior = shadeExterior();
68 dtProps.ramp = ramp;
69 QgsImageOperation::distanceTransform( im, dtProps, context.feedback() );
70
71 if ( context.feedback() && context.feedback()->isCanceled() )
72 return;
73
75 if ( blurLevel <= 16 )
76 {
77 QgsImageOperation::stackBlur( im, blurLevel, false, context.feedback() );
78 }
79 else
80 {
81 QImage *imb = QgsImageOperation::gaussianBlur( im, blurLevel, context.feedback() );
82 if ( !imb->isNull() )
83 im = QImage( *imb );
84 delete imb;
85 }
86
87 if ( context.feedback() && context.feedback()->isCanceled() )
88 return;
89
91
92 if ( context.feedback() && context.feedback()->isCanceled() )
93 return;
94
95 if ( !shadeExterior() )
96 {
97 //only keep interior portion
98 QPainter p( &im );
99 p.setRenderHint( QPainter::Antialiasing );
100 p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
101 p.drawImage( 0, 0, *sourceAsImage( context ) );
102 p.end();
103 }
104
105 QPainter *painter = context.painter();
106 const QgsScopedQPainterState painterState( painter );
107 painter->setCompositionMode( mBlendMode );
108 painter->drawImage( imageOffset( context ), im );
109}
110
111QVariantMap QgsGlowEffect::properties() const
112{
113 QVariantMap props;
114 props.insert( QStringLiteral( "enabled" ), mEnabled ? "1" : "0" );
115 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
116 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
117 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
118 props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
119 props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
120 props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
121 props.insert( QStringLiteral( "spread" ), QString::number( mSpread ) );
122 props.insert( QStringLiteral( "spread_unit" ), QgsUnitTypes::encodeUnit( mSpreadUnit ) );
123 props.insert( QStringLiteral( "spread_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mSpreadMapUnitScale ) );
124 props.insert( QStringLiteral( "color_type" ), QString::number( static_cast< int >( mColorType ) ) );
125 props.insert( QStringLiteral( "single_color" ), QgsColorUtils::colorToString( mColor ) );
126
127 if ( mRamp )
128 {
129 props.insert( mRamp->properties() );
130 }
131
132 return props;
133}
134
135void QgsGlowEffect::readProperties( const QVariantMap &props )
136{
137 bool ok;
138 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
139 if ( ok )
140 {
141 mBlendMode = mode;
142 }
143 if ( props.contains( QStringLiteral( "transparency" ) ) )
144 {
145 const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
146 if ( ok )
147 {
148 mOpacity = 1.0 - transparency;
149 }
150 }
151 else
152 {
153 const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
154 if ( ok )
155 {
157 }
158 }
159 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
160 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
161 const double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
162 if ( ok )
163 {
164 mBlurLevel = level;
165 if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
166 {
167 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
168 mBlurLevel *= 0.2645;
169 }
170 }
171 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ).toString() );
172 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ).toString() );
173 const double spread = props.value( QStringLiteral( "spread" ) ).toDouble( &ok );
174 if ( ok )
175 {
176 mSpread = spread;
177 }
178 mSpreadUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "spread_unit" ) ).toString() );
179 mSpreadMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "spread_unit_scale" ) ).toString() );
180 const QgsGlowEffect::GlowColorType type = static_cast< QgsGlowEffect::GlowColorType >( props.value( QStringLiteral( "color_type" ) ).toInt( &ok ) );
181 if ( ok )
182 {
184 }
185 if ( props.contains( QStringLiteral( "single_color" ) ) )
186 {
187 mColor = QgsColorUtils::colorFromString( props.value( QStringLiteral( "single_color" ) ).toString() );
188 }
189
190 //attempt to create color ramp from props
191 delete mRamp;
192 if ( props.contains( QStringLiteral( "rampType" ) ) && props[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
193 {
195 }
196 else
197 {
199 }
200}
201
203{
204 delete mRamp;
205 mRamp = ramp;
206}
207
209{
210 if ( &rhs == this )
211 return *this;
212
213 delete mRamp;
214
215 mSpread = rhs.spread();
216 mSpreadUnit = rhs.spreadUnit();
218 mRamp = rhs.ramp() ? rhs.ramp()->clone() : nullptr;
219 mBlurLevel = rhs.blurLevel();
220 mBlurUnit = rhs.mBlurUnit;
222 mOpacity = rhs.opacity();
223 mColor = rhs.color();
224 mBlendMode = rhs.blendMode();
225 mColorType = rhs.colorType();
226
227 return *this;
228}
229
230QRectF QgsGlowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
231{
232 //blur radius and spread size
235
236 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
237 spread += blurLevel * 2 + 10;
238 return rect.adjusted( -spread, -spread, spread, spread );
239}
240
241
242//
243// QgsOuterGlowEffect
244//
245
251
253{
255 effect->readProperties( map );
256 return effect;
257}
258
260{
261 QgsOuterGlowEffect *newEffect = new QgsOuterGlowEffect( *this );
262 return newEffect;
263}
264
265
266//
267// QgsInnerGlowEffect
268//
269
275
277{
279 effect->readProperties( map );
280 return effect;
281}
282
284{
285 QgsInnerGlowEffect *newEffect = new QgsInnerGlowEffect( *this );
286 return newEffect;
287}
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 effect 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.
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.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
const QPicture * source() const
Returns the source QPicture.
bool enabled() const
Returns whether the effect is enabled.
DrawMode
Drawing modes for effects.
virtual QString type() const =0
Returns the effect type.
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
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...
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.