1 /***************************************************************************
2                              qgsgloweffect.h
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 #ifndef QGSGLOWEFFECT_H
18 #define QGSGLOWEFFECT_H
19 
20 #include "qgis_core.h"
21 #include "qgis.h"
22 #include "qgspainteffect.h"
23 #include "qgssymbol.h"
24 #include "qgscolorramp.h"
25 
26 #include <QPainter>
27 
28 
29 /**
30  * \ingroup core
31  * \class QgsGlowEffect
32  * \brief Base class for paint effect which draw a glow inside or outside a
33  * picture.
34  *
35  * \since QGIS 2.9
36  */
37 
38 class CORE_EXPORT QgsGlowEffect : public QgsPaintEffect
39 {
40 
41   public:
42 
43     //! Color sources for the glow
44     enum GlowColorType
45     {
46       SingleColor, //!< Use a single color and fade the color to totally transparent
47       ColorRamp //!< Use colors from a color ramp
48     };
49 
50     QgsGlowEffect();
51     QgsGlowEffect( const QgsGlowEffect &other );
52     ~QgsGlowEffect() override;
53 
54     QgsStringMap properties() const override;
55     void readProperties( const QgsStringMap &props ) override;
56 
57     /**
58      * Sets the spread distance for drawing the glow effect.
59      * \param spread spread distance. Units are specified via setSpreadUnit()
60      * \see spread
61      * \see setSpreadUnit
62      * \see setSpreadMapUnitScale
63      */
setSpread(const double spread)64     void setSpread( const double spread ) { mSpread = spread; }
65 
66     /**
67      * Returns the spread distance used for drawing the glow effect.
68      * \returns spread distance. Units are retrieved via spreadUnit()
69      * \see setSpread
70      * \see spreadUnit
71      * \see spreadMapUnitScale
72      */
spread()73     double spread() const { return mSpread; }
74 
75     /**
76      * Sets the units used for the glow spread distance.
77      * \param unit units for spread distance
78      * \see spreadUnit
79      * \see setSpread
80      * \see setSpreadMapUnitScale
81      */
setSpreadUnit(const QgsUnitTypes::RenderUnit unit)82     void setSpreadUnit( const QgsUnitTypes::RenderUnit unit ) { mSpreadUnit = unit; }
83 
84     /**
85      * Returns the units used for the glow spread distance.
86      * \returns units for spread distance
87      * \see setSpreadUnit
88      * \see spread
89      * \see spreadMapUnitScale
90      */
spreadUnit()91     QgsUnitTypes::RenderUnit spreadUnit() const { return mSpreadUnit; }
92 
93     /**
94      * Sets the map unit scale used for the spread distance.
95      * \param scale map unit scale for spread distance
96      * \see spreadMapUnitScale
97      * \see setSpread
98      * \see setSpreadUnit
99      */
setSpreadMapUnitScale(const QgsMapUnitScale & scale)100     void setSpreadMapUnitScale( const QgsMapUnitScale &scale ) { mSpreadMapUnitScale = scale; }
101 
102     /**
103      * Returns the map unit scale used for the spread distance.
104      * \returns map unit scale for spread distance
105      * \see setSpreadMapUnitScale
106      * \see spread
107      * \see spreadUnit
108      */
spreadMapUnitScale()109     const QgsMapUnitScale &spreadMapUnitScale() const { return mSpreadMapUnitScale; }
110 
111     /**
112      * Sets blur level (radius) for the glow. This can be used to smooth the
113      * output from the glow effect.
114      * \param level blur level.
115      * \see blurLevel
116      * \see setBlurUnit
117      * \see setBlurMapUnitScale
118      */
setBlurLevel(const double level)119     void setBlurLevel( const double level ) { mBlurLevel = level; }
120 
121     /**
122      * Returns the blur level (radius) for the glow.
123      * \returns blur level.
124      * \see setBlurLevel
125      * \see blurUnit
126      * \see blurMapUnitScale
127      */
blurLevel()128     double blurLevel() const { return mBlurLevel; }
129 
130     /**
131      * Sets the units used for the glow blur level (radius).
132      * \param unit units for blur level
133      * \see blurUnit
134      * \see setBlurLevel
135      * \see setBlurMapUnitScale
136      * \since QGIS 3.4.9
137      */
setBlurUnit(const QgsUnitTypes::RenderUnit unit)138     void setBlurUnit( const QgsUnitTypes::RenderUnit unit ) { mBlurUnit = unit; }
139 
140     /**
141      * Returns the units used for the glow blur level (radius).
142      * \returns units for blur level
143      * \see setBlurUnit
144      * \see blurLevel
145      * \see blurMapUnitScale
146      * \since QGIS 3.4.9
147      */
blurUnit()148     QgsUnitTypes::RenderUnit blurUnit() const { return mBlurUnit; }
149 
150     /**
151      * Sets the map unit scale used for the glow blur strength (radius).
152      * \param scale map unit scale for blur strength
153      * \see blurMapUnitScale
154      * \see setBlurLevel
155      * \see setBlurUnit
156      * \since QGIS 3.4.9
157      */
setBlurMapUnitScale(const QgsMapUnitScale & scale)158     void setBlurMapUnitScale( const QgsMapUnitScale &scale ) { mBlurMapUnitScale = scale; }
159 
160     /**
161      * Returns the map unit scale used for the glow blur strength (radius).
162      * \returns map unit scale for blur strength
163      * \see setBlurMapUnitScale
164      * \see blurLevel
165      * \see blurUnit
166      * \since QGIS 3.4.9
167      */
blurMapUnitScale()168     const QgsMapUnitScale &blurMapUnitScale() const { return mBlurMapUnitScale; }
169 
170     /**
171      * Sets the \a opacity for the effect.
172      * \param opacity double between 0 and 1 inclusive, where 0 is fully transparent
173      * and 1 is fully opaque
174      * \see opacity()
175      */
setOpacity(const double opacity)176     void setOpacity( const double opacity ) { mOpacity = opacity; }
177 
178     /**
179      * Returns the opacity for the effect.
180      * \returns opacity value between 0 and 1 inclusive, where 0 is fully transparent
181      * and 1 is fully opaque
182      * \see setOpacity().
183      */
opacity()184     double opacity() const { return mOpacity; }
185 
186     /**
187      * Sets the color for the glow. This only applies if the colorType()
188      * is set to SingleColor. The glow will fade between the specified color and
189      * a totally transparent version of the color.
190      * \param color glow color
191      * \see color
192      * \see setColorType
193      */
setColor(const QColor & color)194     void setColor( const QColor &color ) { mColor = color; }
195 
196     /**
197      * Returns the color for the glow. This only applies if the colorType()
198      * is set to SingleColor. The glow will fade between the specified color and
199      * a totally transparent version of the color.
200      * \returns glow color
201      * \see setColor
202      * \see colorType
203      */
color()204     QColor color() const { return mColor; }
205 
206     /**
207      * Sets the color ramp for the glow. This only applies if the colorType()
208      * is set to ColorRamp. The glow will utilize colors from the ramp.
209      * \param ramp color ramp for glow. Ownership of the ramp is transferred to the effect.
210      * \see ramp
211      * \see setColorType
212      */
213     void setRamp( QgsColorRamp *ramp SIP_TRANSFER );
214 
215     /**
216      * Returns the color ramp used for the glow. This only applies if the colorType()
217      * is set to ColorRamp. The glow will utilize colors from the ramp.
218      * \returns color ramp for glow
219      * \see setRamp
220      * \see colorType
221      */
ramp()222     QgsColorRamp *ramp() const { return mRamp; }
223 
224     /**
225      * Sets the blend mode for the effect
226      * \param mode blend mode used for drawing the effect on to a destination
227      * paint device
228      * \see blendMode
229      */
setBlendMode(const QPainter::CompositionMode mode)230     void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; }
231 
232     /**
233      * Returns the blend mode for the effect
234      * \returns blend mode used for drawing the effect on to a destination
235      * paint device
236      * \see setBlendMode
237      */
blendMode()238     QPainter::CompositionMode blendMode() const { return mBlendMode; }
239 
240     /**
241      * Sets the color mode to use for the glow. The glow can either be drawn using a QgsColorRamp
242      * color ramp or by simply specificing a single color. setColorType is used to specify which mode to use
243      * for the glow.
244      * \param colorType color type to use for glow
245      * \see colorType
246      * \see setColor
247      * \see setRamp
248      */
setColorType(GlowColorType colorType)249     void setColorType( GlowColorType colorType ) { mColorType = colorType; }
250 
251     /**
252      * Returns the color mode used for the glow. The glow can either be drawn using a QgsColorRamp
253      * color ramp or by specificing a single color.
254      * \returns current color mode used for the glow
255      * \see setColorType
256      * \see color
257      * \see ramp
258      */
colorType()259     GlowColorType colorType() const { return mColorType; }
260 
261     QgsGlowEffect &operator=( const QgsGlowEffect &rhs );
262 
263   protected:
264 
265     QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const override;
266     void draw( QgsRenderContext &context ) override;
267 
268     /**
269      * Specifies whether the glow is drawn outside the picture or within
270      * the picture.
271      * \returns TRUE if glow is to be drawn outside the picture, or FALSE
272      * to draw glow within the picture
273      */
274     virtual bool shadeExterior() const = 0;
275 
276     double mSpread = 2.0;
277     QgsUnitTypes::RenderUnit mSpreadUnit = QgsUnitTypes::RenderMillimeters;
278     QgsMapUnitScale mSpreadMapUnitScale;
279     QgsColorRamp *mRamp = nullptr;
280     double mBlurLevel = 2.645;
281     QgsUnitTypes::RenderUnit mBlurUnit = QgsUnitTypes::RenderMillimeters;
282     QgsMapUnitScale mBlurMapUnitScale;
283     double mOpacity = 0.5;
284     QColor mColor;
285     QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_SourceOver;
286     GlowColorType mColorType = SingleColor;
287 
288 };
289 
290 
291 /**
292  * \ingroup core
293  * \class QgsOuterGlowEffect
294  * \brief A paint effect which draws a glow outside of a picture.
295  *
296  * \since QGIS 2.9
297  */
298 
299 class CORE_EXPORT QgsOuterGlowEffect : public QgsGlowEffect
300 {
301 
302   public:
303 
304     /**
305      * Creates a new QgsOuterGlowEffect effect from a properties string map.
306      * \param map encoded properties string map
307      * \returns new QgsOuterGlowEffect
308      */
309     static QgsPaintEffect *create( const QgsStringMap &map ) SIP_FACTORY;
310 
311     QgsOuterGlowEffect();
312 
type()313     QString type() const override { return QStringLiteral( "outerGlow" ); }
314     QgsOuterGlowEffect *clone() const override SIP_FACTORY;
315 
316   protected:
317 
shadeExterior()318     bool shadeExterior() const override { return true; }
319 
320 };
321 
322 
323 /**
324  * \ingroup core
325  * \class QgsInnerGlowEffect
326  * \brief A paint effect which draws a glow within a picture.
327  *
328  * \since QGIS 2.9
329  */
330 
331 class CORE_EXPORT QgsInnerGlowEffect : public QgsGlowEffect
332 {
333 
334   public:
335 
336     /**
337      * Creates a new QgsInnerGlowEffect effect from a properties string map.
338      * \param map encoded properties string map
339      * \returns new QgsInnerGlowEffect
340      */
341     static QgsPaintEffect *create( const QgsStringMap &map ) SIP_FACTORY;
342 
343     QgsInnerGlowEffect();
344 
type()345     QString type() const override { return QStringLiteral( "innerGlow" ); }
346     QgsInnerGlowEffect *clone() const override SIP_FACTORY;
347 
348   protected:
349 
shadeExterior()350     bool shadeExterior() const override { return false; }
351 
352 };
353 
354 #endif // QGSGLOWEFFECT_H
355 
356