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