QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgspainteffectregistry.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgspainteffectregistry.cpp
3  ------------------------
4  begin : January 2015
5  copyright : (C) 2015 Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "qgspainteffectregistry.h"
17 #include "qgsblureffect.h"
18 #include "qgsshadoweffect.h"
19 #include "qgseffectstack.h"
20 #include "qgsgloweffect.h"
21 #include "qgstransformeffect.h"
22 #include "qgscoloreffect.h"
23 #include "qgsapplication.h"
24 
25 QgsPaintEffectAbstractMetadata::QgsPaintEffectAbstractMetadata( const QString &name, const QString &visibleName )
26  : mName( name )
27  , mVisibleName( visibleName )
28 {
29 
30 }
31 
33 {
34  //init registry with known effects
35  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "blur" ), QObject::tr( "Blur" ),
36  QgsBlurEffect::create, nullptr ) );
37  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "dropShadow" ), QObject::tr( "Drop Shadow" ),
38  QgsDropShadowEffect::create, nullptr ) );
39  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerShadow" ), QObject::tr( "Inner Shadow" ),
40  QgsInnerShadowEffect::create, nullptr ) );
41  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "effectStack" ), QObject::tr( "Stack" ),
42  QgsEffectStack::create, nullptr ) );
43  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "outerGlow" ), QObject::tr( "Outer Glow" ),
44  QgsOuterGlowEffect::create, nullptr ) );
45  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerGlow" ), QObject::tr( "Inner Glow" ),
46  QgsInnerGlowEffect::create, nullptr ) );
47  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "drawSource" ), QObject::tr( "Source" ),
48  QgsDrawSourceEffect::create, nullptr ) );
49  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "transform" ), QObject::tr( "Transform" ),
50  QgsTransformEffect::create, nullptr ) );
51  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "color" ), QObject::tr( "Colorise" ),
52  QgsColorEffect::create, nullptr ) );
53 }
54 
56 {
57  qDeleteAll( mMetadata );
58 }
59 
61 {
62  if ( mMetadata.contains( name ) )
63  return mMetadata.value( name );
64  else
65  return nullptr;
66 }
67 
69 {
70  if ( !metadata || mMetadata.contains( metadata->name() ) )
71  return false;
72 
73  mMetadata[metadata->name()] = metadata;
74  return true;
75 }
76 
77 QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QString &name, const QgsStringMap &properties ) const
78 {
79  if ( !mMetadata.contains( name ) )
80  return nullptr;
81 
82  QgsPaintEffect *effect = mMetadata[name]->createPaintEffect( properties );
83  return effect;
84 }
85 
86 QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QDomElement &element ) const
87 {
88  if ( element.isNull() )
89  {
90  return nullptr;
91  }
92 
93  QString type = element.attribute( QStringLiteral( "type" ) );
94 
96  if ( !effect )
97  return nullptr;
98 
99  effect->readProperties( element );
100  return effect;
101 }
102 
104 {
105  QStringList lst;
106  QMap<QString, QgsPaintEffectAbstractMetadata *>::ConstIterator it = mMetadata.begin();
107  for ( ; it != mMetadata.end(); ++it )
108  {
109  lst.append( it.key() );
110  }
111  return lst;
112 }
113 
115 {
116  //NOTE - also remember to update isDefaultStack below if making changes to this list
117  QgsEffectStack *stack = new QgsEffectStack();
118  QgsDropShadowEffect *dropShadow = new QgsDropShadowEffect();
119  dropShadow->setEnabled( false );
120  stack->appendEffect( dropShadow );
121  QgsOuterGlowEffect *outerGlow = new QgsOuterGlowEffect();
122  outerGlow->setEnabled( false );
123  stack->appendEffect( outerGlow );
124  stack->appendEffect( new QgsDrawSourceEffect() );
125  QgsInnerShadowEffect *innerShadow = new QgsInnerShadowEffect();
126  innerShadow->setEnabled( false );
127  stack->appendEffect( innerShadow );
128  QgsInnerGlowEffect *innerGlow = new QgsInnerGlowEffect();
129  innerGlow->setEnabled( false );
130  stack->appendEffect( innerGlow );
131  return stack;
132 }
133 
135 {
136  QgsEffectStack *effectStack = dynamic_cast< QgsEffectStack * >( effect );
137  if ( !effectStack )
138  return false;
139 
140  if ( effectStack->count() != 5 )
141  return false;
142 
143  for ( int i = 0; i < 5; ++i )
144  {
145  //only the third effect should be enabled
146  if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
147  return false;
148  }
149 
150  if ( !dynamic_cast< QgsDropShadowEffect * >( effectStack->effect( 0 ) ) )
151  return false;
152  if ( !dynamic_cast< QgsOuterGlowEffect * >( effectStack->effect( 1 ) ) )
153  return false;
154  if ( !dynamic_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) ) )
155  return false;
156  if ( !dynamic_cast< QgsInnerShadowEffect * >( effectStack->effect( 3 ) ) )
157  return false;
158  if ( !dynamic_cast< QgsInnerGlowEffect * >( effectStack->effect( 4 ) ) )
159  return false;
160 
161  QgsDrawSourceEffect *sourceEffect = static_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) );
162  if ( !qgsDoubleNear( sourceEffect->opacity(), 1.0 ) )
163  return false;
164  if ( sourceEffect->blendMode() != QPainter::CompositionMode_SourceOver )
165  return false;
166 
167  //we don't go as far as to check disabled effect's properties
168  return true;
169 }
QgsPaintEffectAbstractMetadata::QgsPaintEffectAbstractMetadata
QgsPaintEffectAbstractMetadata(const QString &name, const QString &visibleName)
Construct a new QgsPaintEffectAbstractMetadata.
Definition: qgspainteffectregistry.cpp:25
QgsInnerShadowEffect
A paint effect which draws an offset and optionally blurred drop shadow within a picture.
Definition: qgsshadoweffect.h:285
QgsPaintEffectRegistry::createEffect
QgsPaintEffect * createEffect(const QString &name, const QgsStringMap &properties=QgsStringMap()) const
Creates a new paint effect given the effect name and properties map.
Definition: qgspainteffectregistry.cpp:77
QgsPaintEffectRegistry::addEffectType
bool addEffectType(QgsPaintEffectAbstractMetadata *metadata)
Registers a new effect type.
Definition: qgspainteffectregistry.cpp:68
QgsDrawSourceEffect
A paint effect which draws the source picture with minor or no alterations.
Definition: qgspainteffect.h:328
qgspainteffectregistry.h
qgstransformeffect.h
QgsInnerShadowEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsInnerShadowEffect effect from a properties string map.
Definition: qgsshadoweffect.cpp:216
QgsPaintEffectRegistry::defaultStack
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects.
Definition: qgspainteffectregistry.cpp:114
QgsPaintEffectRegistry::effects
QStringList effects() const
Returns a list of known paint effects.
Definition: qgspainteffectregistry.cpp:103
qgsapplication.h
qgscoloreffect.h
QgsPaintEffectAbstractMetadata::name
QString name() const
Returns the unique string representing the paint effect class.
Definition: qgspainteffectregistry.h:57
QgsInnerGlowEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsInnerGlowEffect effect from a properties string map.
Definition: qgsgloweffect.cpp:265
QgsEffectStack::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsEffectStack effect.
Definition: qgseffectstack.cpp:72
QgsPaintEffectRegistry::isDefaultStack
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Definition: qgspainteffectregistry.cpp:134
QgsPaintEffectAbstractMetadata
Stores metadata about a paint effect class.
Definition: qgspainteffectregistry.h:39
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsDropShadowEffect
A paint effect which draws an offset and optionally blurred drop shadow.
Definition: qgsshadoweffect.h:254
QgsEffectStack::effect
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
Definition: qgseffectstack.cpp:258
qgsgloweffect.h
QgsPaintEffect::readProperties
virtual void readProperties(const QgsStringMap &props)=0
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
QgsPaintEffectRegistry::effectMetadata
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
Definition: qgspainteffectregistry.cpp:60
QgsPaintEffectRegistry::QgsPaintEffectRegistry
QgsPaintEffectRegistry()
Definition: qgspainteffectregistry.cpp:32
qgseffectstack.h
QgsPaintEffect::setEnabled
void setEnabled(bool enabled)
Sets whether the effect is enabled.
Definition: qgspainteffect.cpp:44
QgsInnerGlowEffect
A paint effect which draws a glow within a picture.
Definition: qgsgloweffect.h:331
QgsOuterGlowEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsOuterGlowEffect effect from a properties string map.
Definition: qgsgloweffect.cpp:241
QgsDrawSourceEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDrawSource effect from a properties string map.
Definition: qgspainteffect.cpp:229
QgsStringMap
QMap< QString, QString > QgsStringMap
Definition: qgis.h:714
QgsPaintEffectRegistry::~QgsPaintEffectRegistry
~QgsPaintEffectRegistry()
Definition: qgspainteffectregistry.cpp:55
QgsBlurEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
Definition: qgsblureffect.cpp:23
QgsEffectStack::count
int count() const
Returns count of effects contained by the stack.
Definition: qgseffectstack.h:136
QgsApplication::paintEffectRegistry
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
Definition: qgsapplication.cpp:2094
qgsshadoweffect.h
QgsDrawSourceEffect::blendMode
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
Definition: qgspainteffect.h:377
QgsEffectStack::appendEffect
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
Definition: qgseffectstack.cpp:217
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:53
QgsOuterGlowEffect
A paint effect which draws a glow outside of a picture.
Definition: qgsgloweffect.h:299
QgsDropShadowEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.
Definition: qgsshadoweffect.cpp:183
QgsPaintEffect::enabled
bool enabled() const
Returns whether the effect is enabled.
Definition: qgspainteffect.h:198
QgsColorEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsColorEffect effect from a properties string map.
Definition: qgscoloreffect.cpp:22
qgsblureffect.h
QgsDrawSourceEffect::opacity
double opacity() const
Returns the opacity for the effect.
Definition: qgspainteffect.h:361
QgsTransformEffect::create
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsTransformEffect effect from a properties string map.
Definition: qgstransformeffect.cpp:24
QgsPaintEffectMetadata
Convenience metadata class that uses static functions to create an effect and its widget.
Definition: qgspainteffectregistry.h:97
QgsEffectStack
A paint effect which consists of a stack of other chained paint effects.
Definition: qgseffectstack.h:44