QGIS API Documentation  3.6.0-Noosa (5873452)
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  //we don't go as far as to check the individual effect's properties
162  return true;
163 }
Convenience metadata class that uses static functions to create an effect and its widget...
bool addEffectType(QgsPaintEffectAbstractMetadata *metadata)
Registers a new effect type.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Base class for visual effects which can be applied to QPicture drawings.
Stores metadata about a paint effect class.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application&#39;s paint effect registry, used for managing paint effects. ...
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsEffectStack effect.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDrawSource effect from a properties string map.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
int count() const
Returns count of effects contained by the stack.
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects. ...
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsOuterGlowEffect effect from a properties string map.
QString name() const
Returns the unique string representing the paint effect class.
A paint effect which consists of a stack of other chained paint effects.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsTransformEffect effect from a properties string map.
bool enabled() const
Returns whether the effect is enabled.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
A paint effect which draws an offset and optionally blurred drop shadow.
QStringList effects() const
Returns a list of known paint effects.
A paint effect which draws a glow outside of a picture.
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
virtual void readProperties(const QgsStringMap &props)=0
Reads a string map of an effect&#39;s properties and restores the effect to the state described by the pr...
A paint effect which draws an offset and optionally blurred drop shadow within a picture.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsColorEffect effect from a properties string map.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsInnerShadowEffect effect from a properties string map.
A paint effect which draws a glow within a picture.
A paint effect which draws the source picture with minor or no alterations.
QgsPaintEffect * createEffect(const QString &name, const QgsStringMap &properties=QgsStringMap()) const
Creates a new paint effect given the effect name and properties map.
QgsPaintEffectAbstractMetadata(const QString &name, const QString &visibleName)
Construct a new QgsPaintEffectAbstractMetadata.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsInnerGlowEffect effect from a properties string map.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.