QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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
37 : QgsPaintEffect( other )
38{
39 operator=( other );
40}
41
46
51
53{
54 if ( !enabled() || !context.painter() || source().isNull() )
55 return;
56
58 {
59 //just draw unmodified source, we can't render this effect when forcing vectors
60 drawSource( *context.painter() );
61 return;
62 }
63
64 QImage im = sourceAsImage( context ).copy();
65
66 QgsColorRamp *ramp = nullptr;
67 std::unique_ptr< QgsGradientColorRamp > tempRamp;
68 if ( mColorType == ColorRamp && mRamp )
69 {
70 ramp = mRamp;
71 }
72 else
73 {
74 //create a temporary ramp
75 QColor transparentColor = mColor;
76 transparentColor.setAlpha( 0 );
77 tempRamp = std::make_unique<QgsGradientColorRamp>( mColor, transparentColor );
78 ramp = tempRamp.get();
79 }
80
83 dtProps.useMaxDistance = false;
84 dtProps.shadeExterior = shadeExterior();
85 dtProps.ramp = ramp;
86 QgsImageOperation::distanceTransform( im, dtProps, context.feedback() );
87
88 if ( context.feedback() && context.feedback()->isCanceled() )
89 return;
90
92 if ( blurLevel <= 16 )
93 {
94 QgsImageOperation::stackBlur( im, blurLevel, false, context.feedback() );
95 }
96 else
97 {
98 QImage *imb = QgsImageOperation::gaussianBlur( im, blurLevel, context.feedback() );
99 if ( !imb->isNull() )
100 im = QImage( *imb );
101 delete imb;
102 }
103
104 if ( context.feedback() && context.feedback()->isCanceled() )
105 return;
106
108
109 if ( context.feedback() && context.feedback()->isCanceled() )
110 return;
111
112 if ( !shadeExterior() )
113 {
114 //only keep interior portion
115 QPainter p( &im );
116 p.setRenderHint( QPainter::Antialiasing );
117 p.setCompositionMode( QPainter::CompositionMode_DestinationIn );
118 p.drawImage( 0, 0, sourceAsImage( context ) );
119 p.end();
120 }
121
122 QPainter *painter = context.painter();
123 const QgsScopedQPainterState painterState( painter );
124 painter->setCompositionMode( mBlendMode );
125 painter->drawImage( imageOffset( context ), im );
126}
127
128QVariantMap QgsGlowEffect::properties() const
129{
130 QVariantMap props;
131 props.insert( u"enabled"_s, mEnabled ? "1" : "0" );
132 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
133 props.insert( u"blend_mode"_s, QString::number( int( mBlendMode ) ) );
134 props.insert( u"opacity"_s, QString::number( mOpacity ) );
135 props.insert( u"blur_level"_s, QString::number( mBlurLevel ) );
136 props.insert( u"blur_unit"_s, QgsUnitTypes::encodeUnit( mBlurUnit ) );
137 props.insert( u"blur_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
138 props.insert( u"spread"_s, QString::number( mSpread ) );
139 props.insert( u"spread_unit"_s, QgsUnitTypes::encodeUnit( mSpreadUnit ) );
140 props.insert( u"spread_unit_scale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( mSpreadMapUnitScale ) );
141 props.insert( u"color_type"_s, QString::number( static_cast< int >( mColorType ) ) );
142 props.insert( u"single_color"_s, QgsColorUtils::colorToString( mColor ) );
143
144 if ( mRamp )
145 {
146 props.insert( mRamp->properties() );
147 }
148
149 return props;
150}
151
152void QgsGlowEffect::readProperties( const QVariantMap &props )
153{
154 bool ok;
155 const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( u"blend_mode"_s ).toInt( &ok ) );
156 if ( ok )
157 {
158 mBlendMode = mode;
159 }
160 if ( props.contains( u"transparency"_s ) )
161 {
162 const double transparency = props.value( u"transparency"_s ).toDouble( &ok );
163 if ( ok )
164 {
165 mOpacity = 1.0 - transparency;
166 }
167 }
168 else
169 {
170 const double opacity = props.value( u"opacity"_s ).toDouble( &ok );
171 if ( ok )
172 {
174 }
175 }
176 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
177 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
178 const double level = props.value( u"blur_level"_s ).toDouble( &ok );
179 if ( ok )
180 {
181 mBlurLevel = level;
182 if ( !props.contains( u"blur_unit"_s ) )
183 {
184 // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
185 mBlurLevel *= 0.2645;
186 }
187 }
188 mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"blur_unit"_s ).toString() );
189 mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"blur_unit_scale"_s ).toString() );
190 const double spread = props.value( u"spread"_s ).toDouble( &ok );
191 if ( ok )
192 {
193 mSpread = spread;
194 }
195 mSpreadUnit = QgsUnitTypes::decodeRenderUnit( props.value( u"spread_unit"_s ).toString() );
196 mSpreadMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( u"spread_unit_scale"_s ).toString() );
197 const QgsGlowEffect::GlowColorType type = static_cast< QgsGlowEffect::GlowColorType >( props.value( u"color_type"_s ).toInt( &ok ) );
198 if ( ok )
199 {
201 }
202 if ( props.contains( u"single_color"_s ) )
203 {
204 mColor = QgsColorUtils::colorFromString( props.value( u"single_color"_s ).toString() );
205 }
206
207 //attempt to create color ramp from props
208 delete mRamp;
209 if ( props.contains( u"rampType"_s ) && props[u"rampType"_s] == QgsCptCityColorRamp::typeString() )
210 {
212 }
213 else
214 {
216 }
217}
218
220{
221 delete mRamp;
222 mRamp = ramp;
223}
224
226{
227 if ( &rhs == this )
228 return *this;
229
230 delete mRamp;
231
232 mSpread = rhs.spread();
233 mSpreadUnit = rhs.spreadUnit();
235 mRamp = rhs.ramp() ? rhs.ramp()->clone() : nullptr;
236 mBlurLevel = rhs.blurLevel();
237 mBlurUnit = rhs.mBlurUnit;
239 mOpacity = rhs.opacity();
240 mColor = rhs.color();
241 mBlendMode = rhs.blendMode();
242 mColorType = rhs.colorType();
243
244 return *this;
245}
246
247QRectF QgsGlowEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
248{
249 //blur radius and spread size
252
253 //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
254 spread += blurLevel * 2 + 10;
255 return rect.adjusted( -spread, -spread, spread, spread );
256}
257
258
259//
260// QgsOuterGlowEffect
261//
262
266
268{
270 effect->readProperties( map );
271 return effect;
272}
273
275{
276 QgsOuterGlowEffect *newEffect = new QgsOuterGlowEffect( *this );
277 return newEffect;
278}
279
280
281//
282// QgsInnerGlowEffect
283//
284
288
290{
292 effect->readProperties( map );
293 return effect;
294}
295
297{
298 QgsInnerGlowEffect *newEffect = new QgsInnerGlowEffect( *this );
299 return newEffect;
300}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2801
@ RequiresRasterization
The effect requires raster-based rendering.
Definition qgis.h:2903
@ GlowSpread
Glow spread size.
Definition qgis.h:3158
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2912
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:56
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.