1 /*************************************************************************** 2 qgsshadoweffect.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 QGSSHADOWEFFECT_H 18 #define QGSSHADOWEFFECT_H 19 20 #include "qgis_core.h" 21 #include "qgspainteffect.h" 22 #include "qgis_sip.h" 23 #include "qgssymbol.h" 24 #include <QPainter> 25 26 /** 27 * \ingroup core 28 * \class QgsShadowEffect 29 * \brief Base class for paint effects which offset, blurred shadows 30 * 31 * \since QGIS 2.9 32 */ 33 34 class CORE_EXPORT QgsShadowEffect : public QgsPaintEffect SIP_NODEFAULTCTORS 35 { 36 37 public: 38 39 QgsShadowEffect(); 40 41 QgsStringMap properties() const override; 42 void readProperties( const QgsStringMap &props ) override; 43 44 /** 45 * Sets blur level (radius) for the shadow. 46 * \param level blur level. 47 * values indicating greater blur strength. 48 * \see blurLevel 49 * \see setBlurUnit 50 * \see setBlurMapUnitScale 51 */ setBlurLevel(const double level)52 void setBlurLevel( const double level ) { mBlurLevel = level; } 53 54 /** 55 * Returns the blur level (radius) for the shadow. 56 * \returns blur level. 57 * values indicating greater blur strength. 58 * \see setBlurLevel 59 * \see blurUnit 60 * \see blurMapUnitScale 61 */ blurLevel()62 double blurLevel() const { return mBlurLevel; } 63 64 /** 65 * Sets the units used for the shadow blur level (radius). 66 * \param unit units for blur level 67 * \see blurUnit 68 * \see setBlurLevel 69 * \see setBlurMapUnitScale 70 * \since QGIS 3.4.9 71 */ setBlurUnit(const QgsUnitTypes::RenderUnit unit)72 void setBlurUnit( const QgsUnitTypes::RenderUnit unit ) { mBlurUnit = unit; } 73 74 /** 75 * Returns the units used for the shadow blur level (radius). 76 * \returns units for blur level 77 * \see setBlurUnit 78 * \see blurLevel 79 * \see blurMapUnitScale 80 * \since QGIS 3.4.9 81 */ blurUnit()82 QgsUnitTypes::RenderUnit blurUnit() const { return mBlurUnit; } 83 84 /** 85 * Sets the map unit scale used for the shadow blur strength (radius). 86 * \param scale map unit scale for blur strength 87 * \see blurMapUnitScale 88 * \see setBlurLevel 89 * \see setBlurUnit 90 * \since QGIS 3.4.9 91 */ setBlurMapUnitScale(const QgsMapUnitScale & scale)92 void setBlurMapUnitScale( const QgsMapUnitScale &scale ) { mBlurMapUnitScale = scale; } 93 94 /** 95 * Returns the map unit scale used for the shadow blur strength (radius). 96 * \returns map unit scale for blur strength 97 * \see setBlurMapUnitScale 98 * \see blurLevel 99 * \see blurUnit 100 * \since QGIS 3.4.9 101 */ blurMapUnitScale()102 const QgsMapUnitScale &blurMapUnitScale() const { return mBlurMapUnitScale; } 103 104 /** 105 * Sets the angle for offsetting the shadow. 106 * \param angle offset angle in degrees clockwise from North 107 * \see offsetAngle 108 * \see setOffsetDistance 109 */ setOffsetAngle(const int angle)110 void setOffsetAngle( const int angle ) { mOffsetAngle = angle; } 111 112 /** 113 * Returns the angle used for offsetting the shadow. 114 * \returns offset angle in degrees clockwise from North 115 * \see setOffsetAngle 116 * \see offsetDistance 117 */ offsetAngle()118 int offsetAngle() const { return mOffsetAngle; } 119 120 /** 121 * Sets the distance for offsetting the shadow. 122 * \param distance offset distance. Units are specified via setOffsetUnit() 123 * \see offsetDistance 124 * \see setOffsetUnit 125 * \see setOffsetMapUnitScale 126 */ setOffsetDistance(const double distance)127 void setOffsetDistance( const double distance ) { mOffsetDist = distance; } 128 129 /** 130 * Returns the distance used for offsetting the shadow. 131 * \returns offset distance. Distance units are retrieved via offsetUnit() 132 * \see setOffsetDistance 133 * \see offsetUnit 134 * \see offsetMapUnitScale 135 */ offsetDistance()136 double offsetDistance() const { return mOffsetDist; } 137 138 /** 139 * Sets the units used for the shadow offset distance. 140 * \param unit units for offset distance 141 * \see offsetUnit 142 * \see setOffsetDistance 143 * \see setOffsetMapUnitScale 144 */ setOffsetUnit(const QgsUnitTypes::RenderUnit unit)145 void setOffsetUnit( const QgsUnitTypes::RenderUnit unit ) { mOffsetUnit = unit; } 146 147 /** 148 * Returns the units used for the shadow offset distance. 149 * \returns units for offset distance 150 * \see setOffsetUnit 151 * \see offsetDistance 152 * \see offsetMapUnitScale 153 */ offsetUnit()154 QgsUnitTypes::RenderUnit offsetUnit() const { return mOffsetUnit; } 155 156 /** 157 * Sets the map unit scale used for the shadow offset distance. 158 * \param scale map unit scale for offset distance 159 * \see offsetMapUnitScale 160 * \see setOffsetDistance 161 * \see setOffsetUnit 162 */ setOffsetMapUnitScale(const QgsMapUnitScale & scale)163 void setOffsetMapUnitScale( const QgsMapUnitScale &scale ) { mOffsetMapUnitScale = scale; } 164 165 /** 166 * Returns the map unit scale used for the shadow offset distance. 167 * \returns map unit scale for offset distance 168 * \see setOffsetMapUnitScale 169 * \see offsetDistance 170 * \see offsetUnit 171 */ offsetMapUnitScale()172 const QgsMapUnitScale &offsetMapUnitScale() const { return mOffsetMapUnitScale; } 173 174 /** 175 * Sets the color for the shadow. 176 * \param color shadow color 177 * \see color 178 */ setColor(const QColor & color)179 void setColor( const QColor &color ) { mColor = color; } 180 181 /** 182 * Returns the color used for the shadow. 183 * \returns shadow color 184 * \see setColor 185 */ color()186 QColor color() const { return mColor; } 187 188 /** 189 * Sets the \a opacity for the effect. 190 * \param opacity double between 0 and 1 inclusive, where 0 is fully transparent 191 * and 1 is fully opaque 192 * \see opacity() 193 */ setOpacity(const double opacity)194 void setOpacity( const double opacity ) { mOpacity = opacity; } 195 196 /** 197 * Returns the opacity for the effect. 198 * \returns opacity value between 0 and 1 inclusive, where 0 is fully transparent 199 * and 1 is fully opaque 200 * \see setOpacity() 201 */ opacity()202 double opacity() const { return mOpacity; } 203 204 /** 205 * Sets the blend mode for the effect 206 * \param mode blend mode used for drawing the effect on to a destination 207 * paint device 208 * \see blendMode 209 */ setBlendMode(const QPainter::CompositionMode mode)210 void setBlendMode( const QPainter::CompositionMode mode ) { mBlendMode = mode; } 211 212 /** 213 * Returns the blend mode for the effect 214 * \returns blend mode used for drawing the effect on to a destination 215 * paint device 216 * \see setBlendMode 217 */ blendMode()218 QPainter::CompositionMode blendMode() const { return mBlendMode; } 219 220 protected: 221 222 QRectF boundingRect( const QRectF &rect, const QgsRenderContext &context ) const override; 223 void draw( QgsRenderContext &context ) override; 224 225 /** 226 * Specifies whether the shadow is drawn outside the picture or within 227 * the picture. 228 * \returns TRUE if shadow is to be drawn outside the picture, or FALSE 229 * to draw shadow within the picture 230 */ 231 virtual bool exteriorShadow() const = 0; 232 233 double mBlurLevel = 2.645; 234 QgsUnitTypes::RenderUnit mBlurUnit = QgsUnitTypes::RenderMillimeters; 235 QgsMapUnitScale mBlurMapUnitScale; 236 int mOffsetAngle = 135; 237 double mOffsetDist = 2.0; 238 QgsUnitTypes::RenderUnit mOffsetUnit = QgsUnitTypes::RenderMillimeters; 239 QgsMapUnitScale mOffsetMapUnitScale; 240 double mOpacity = 1.0; 241 QColor mColor; 242 QPainter::CompositionMode mBlendMode = QPainter::CompositionMode_Multiply; 243 244 }; 245 246 247 /** 248 * \ingroup core 249 * \class QgsDropShadowEffect 250 * \brief A paint effect which draws an offset and optionally blurred drop shadow 251 * 252 * \since QGIS 2.9 253 */ 254 class CORE_EXPORT QgsDropShadowEffect : public QgsShadowEffect SIP_NODEFAULTCTORS 255 { 256 257 public: 258 259 /** 260 * Creates a new QgsDropShadowEffect effect from a properties string map. 261 * \param map encoded properties string map 262 * \returns new QgsDropShadowEffect 263 */ 264 static QgsPaintEffect *create( const QgsStringMap &map ) SIP_FACTORY; 265 266 QgsDropShadowEffect(); 267 268 QString type() const override; 269 QgsDropShadowEffect *clone() const override SIP_FACTORY; 270 271 protected: 272 273 bool exteriorShadow() const override; 274 275 }; 276 277 /** 278 * \ingroup core 279 * \class QgsInnerShadowEffect 280 * \brief A paint effect which draws an offset and optionally blurred drop shadow 281 * within a picture. 282 * 283 * \since QGIS 2.9 284 */ 285 class CORE_EXPORT QgsInnerShadowEffect : public QgsShadowEffect SIP_NODEFAULTCTORS 286 { 287 288 public: 289 290 /** 291 * Creates a new QgsInnerShadowEffect effect from a properties string map. 292 * \param map encoded properties string map 293 * \returns new QgsInnerShadowEffect 294 */ 295 static QgsPaintEffect *create( const QgsStringMap &map ) SIP_FACTORY; 296 297 QgsInnerShadowEffect(); 298 299 QString type() const override; 300 QgsInnerShadowEffect *clone() const override SIP_FACTORY; 301 302 protected: 303 304 bool exteriorShadow() const override; 305 306 }; 307 308 #endif // QGSSHADOWEFFECT_H 309 310