QGIS API Documentation 3.99.0-Master (d270888f95f)
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
20#include <memory>
21
22#include "qgscolorrampimpl.h"
23#include "qgscolorutils.h"
24#include "qgsimageoperation.h"
25#include "qgssymbollayerutils.h"
26#include "qgsunittypes.h"
27
28#include <QString>
29
30using namespace Qt::StringLiterals;
31
33 : mColor( Qt::white )
34{
35
36}
37
39 : QgsPaintEffect( other )
40{
41 operator=( other );
42}
43
48
53
55{
56 if ( !enabled() || !context.painter() || source().isNull() )
57 return;
58
60 {
61 //just draw unmodified source, we can't render this effect when forcing vectors
62 drawSource( *context.painter() );
63 return;
64 }
65
66 QImage im = sourceAsImage( context ).copy();
67
68 QgsColorRamp *ramp = nullptr;
69 std::unique_ptr< QgsGradientColorRamp > tempRamp;
70 if ( mColorType == ColorRamp && mRamp )
71 {
72 ramp = mRamp;
73 }
74 else
75 {
76 //create a temporary ramp
77 QColor transparentColor = mColor;
78 transparentColor.setAlpha( 0 );
79 tempRamp = std::make_unique<QgsGradientColorRamp>( mColor, transparentColor );
80 ramp = tempRamp.get();
81 }
82
85 dtProps.useMaxDistance = false;
86 dtProps.shadeExterior = shadeExterior();
87 dtProps.ramp = ramp;
88 QgsImageOperation::distanceTransform( im, dtProps, context.feedback() );
89
90 if ( context.feedback() && context.feedback()->isCanceled() )
91 return;
92
94 if ( blurLevel <= 16 )
95 {
96 QgsImageOperation::stackBlur( im, blurLevel, false, context.feedback() );
97 }
98 else
99 {
100 QImage *imb = QgsImageOperation::gaussianBlur( im, blurLevel, context.feedback() );
101 if ( !imb->isNull() )
102 im = QImage( *imb );
103 delete imb;
104 }
105
106 if ( context.feedback() && context.feedback()->isCanceled() )
107 return;
108
110
111 if ( context.feedback() && context.feedback()->isCanceled() )
112 return;
113
114 if ( !shadeExterior() )
115 {
116 //only keep interior portion
117 QPainter p( &im );
118 p.setRenderHint( QPainter::Antialiasing );
119 p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
120 p.drawImage( 0, 0, sourceAsImage( context ) );
121 p.end();
122 }
123
124 QPainter *painter = context.painter();
125 const QgsScopedQPainterState painterState( painter );
126 painter->setCompositionMode( mBlendMode );
127 painter->drawImage( imageOffset( context ), im );
128}
129
130QVariantMap QgsGlowEffect::properties() const
131{
132 QVariantMap props;
133 props.insert( u"enabled"_s, mEnabled ? "1" : "0" );
134 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
135 props.insert( u"blend_mode"_s, QString::number( int( mBlendMode ) ) );
136 props.insert( u"opacity"_s, QString::number( mOpacity ) );
137 props.insert( u"blur_level"_s, QString::number( mBlurLevel ) );
138 props.insert( u"blur_unit"_s, QgsUnitTypes::encodeUnit( mBlurUnit ) );
139 props.insert( u"blur_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
140 props.insert( u"spread"_s, QString::number( mSpread ) );
141 props.insert( u"spread_unit"_s, QgsUnitTypes::encodeUnit( mSpreadUnit ) );
142 props.insert( u"spread_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mSpreadMapUnitScale ) );
143 props.insert( u"color_type"_s, QString::number( static_cast< int >( mColorType ) ) );
144 props.insert( u"single_color"_s, QgsColorUtils::colorToString( mColor ) );
145
146 if ( mRamp )
147 {
148 props.insert( mRamp->properties() );
149 }
150
151 return props;
152}
153
154void QgsGlowEffect::readProperties( const QVariantMap &props )
155{
156 bool ok;
157 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( u"blend_mode"_s ).toInt( &ok ) );
158 if ( ok )
159 {
160 mBlendMode = mode;
161 }
162 if ( props.contains( u"transparency"_s ) )
163 {
164 const double transparency = props.value( u"transparency"_s ).toDouble( &ok );
165 if ( ok )
166 {
167 mOpacity = 1.0 - transparency;
168 }
169 }
170 else
171 {
172 const double opacity = props.value( u"opacity"_s ).toDouble( &ok );
173 if ( ok )
174 {
176 }
177 }
178 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
179 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
180 const double level = props.value( u"blur_level"_s ).toDouble( &ok );
181 if ( ok )
182 {
183 mBlurLevel = level;
184 if ( !props.contains( u"blur_unit"_s ) )
185 {
186 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
187 mBlurLevel *= 0.2645;
188 }
189 }
190 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"blur_unit"_s ).toString() );
191 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"blur_unit_scale"_s ).toString() );
192 const double spread = props.value( u"spread"_s ).toDouble( &ok );
193 if ( ok )
194 {
195 mSpread = spread;
196 }
197 mSpreadUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"spread_unit"_s ).toString() );
198 mSpreadMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"spread_unit_scale"_s ).toString() );
199 const QgsGlowEffect::GlowColorType type = static_cast< QgsGlowEffect::GlowColorType >( props.value( u"color_type"_s ).toInt( &ok ) );
200 if ( ok )
201 {
203 }
204 if ( props.contains( u"single_color"_s ) )
205 {
206 mColor = QgsColorUtils::colorFromString( props.value( u"single_color"_s ).toString() );
207 }
208
209 //attempt to create color ramp from props
210 delete mRamp;
211 if ( props.contains( u"rampType"_s ) && props[u"rampType"_s] == QgsCptCityColorRamp::typeString() )
212 {
214 }
215 else
216 {
218 }
219}
220
222{
223 delete mRamp;
224 mRamp = ramp;
225}
226
228{
229 if ( &rhs == this )
230 return *this;
231
232 delete mRamp;
233
234 mSpread = rhs.spread();
235 mSpreadUnit = rhs.spreadUnit();
237 mRamp = rhs.ramp() ? rhs.ramp()->clone() : nullptr;
238 mBlurLevel = rhs.blurLevel();
239 mBlurUnit = rhs.mBlurUnit;
241 mOpacity = rhs.opacity();
242 mColor = rhs.color();
243 mBlendMode = rhs.blendMode();
244 mColorType = rhs.colorType();
245
246 return *this;
247}
248
249QRectF QgsGlowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
250{
251 //blur radius and spread size
254
255 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
256 spread += blurLevel * 2 + 10;
257 return rect.adjusted( -spread, -spread, spread, spread );
258}
259
260
261//
262// QgsOuterGlowEffect
263//
264
270
272{
274 effect->readProperties( map );
275 return effect;
276}
277
279{
280 QgsOuterGlowEffect *newEffect = new QgsOuterGlowEffect( *this );
281 return newEffect;
282}
283
284
285//
286// QgsInnerGlowEffect
287//
288
294
296{
298 effect->readProperties( map );
299 return effect;
300}
301
303{
304 QgsInnerGlowEffect *newEffect = new QgsInnerGlowEffect( *this );
305 return newEffect;
306}
@ 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
@ GlowSpread
Glow spread size.
Definition qgis.h:3115
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2873
Abstract base class for color ramps.
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:55
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.
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.
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.
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.
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.
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.