QGIS API Documentation  2.12.0-Lyon
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 
25  : mName( name )
26  , mVisibleName( visibleName )
27 {
28 
29 }
30 
32 {
33  //init registry with known effects
34  addEffectType( new QgsPaintEffectMetadata( "blur", QObject::tr( "Blur" ),
35  QgsBlurEffect::create, NULL ) );
36  addEffectType( new QgsPaintEffectMetadata( "dropShadow", QObject::tr( "Drop Shadow" ),
38  addEffectType( new QgsPaintEffectMetadata( "innerShadow", QObject::tr( "Inner Shadow" ),
40  addEffectType( new QgsPaintEffectMetadata( "effectStack", QObject::tr( "Stack" ),
41  QgsEffectStack::create, NULL ) );
42  addEffectType( new QgsPaintEffectMetadata( "outerGlow", QObject::tr( "Outer Glow" ),
44  addEffectType( new QgsPaintEffectMetadata( "innerGlow", QObject::tr( "Inner Glow" ),
46  addEffectType( new QgsPaintEffectMetadata( "drawSource", QObject::tr( "Source" ),
48  addEffectType( new QgsPaintEffectMetadata( "transform", QObject::tr( "Transform" ),
50  addEffectType( new QgsPaintEffectMetadata( "color", QObject::tr( "Colorise" ),
51  QgsColorEffect::create, NULL ) );
52 }
53 
55 {
56  Q_FOREACH ( const QString& name, mMetadata.keys() )
57  {
58  delete mMetadata[name];
59  }
60  mMetadata.clear();
61 }
62 
64 {
65  static QgsPaintEffectRegistry sInstance;
66  return &sInstance;
67 }
68 
70 {
71  if ( mMetadata.contains( name ) )
72  return mMetadata.value( name );
73  else
74  return NULL;
75 }
76 
78 {
79  if ( metadata == NULL || mMetadata.contains( metadata->name() ) )
80  return false;
81 
82  mMetadata[metadata->name()] = metadata;
83  return true;
84 }
85 
87 {
88  if ( !mMetadata.contains( name ) )
89  return NULL;
90 
91  QgsPaintEffect* effect = mMetadata[name]->createPaintEffect( properties );
92  return effect;
93 }
94 
96 {
97  if ( element.isNull() )
98  {
99  return NULL;
100  }
101 
102  QString type = element.attribute( QString( "type" ) );
103 
104  QgsPaintEffect* effect = instance()->createEffect( type );
105  if ( !effect )
106  return NULL;
107 
108  effect->readProperties( element );
109  return effect;
110 }
111 
113 {
114  QStringList lst;
116  for ( ; it != mMetadata.end(); ++it )
117  {
118  lst.append( it.key() );
119  }
120  return lst;
121 }
122 
124 {
125  //NOTE - also remember to update isDefaultStack below if making changes to this list
126  QgsEffectStack* stack = new QgsEffectStack();
127  QgsDropShadowEffect* dropShadow = new QgsDropShadowEffect();
128  dropShadow->setEnabled( false );
129  stack->appendEffect( dropShadow );
130  QgsOuterGlowEffect* outerGlow = new QgsOuterGlowEffect();
131  outerGlow->setEnabled( false );
132  stack->appendEffect( outerGlow );
133  stack->appendEffect( new QgsDrawSourceEffect() );
134  QgsInnerShadowEffect* innerShadow = new QgsInnerShadowEffect();
135  innerShadow->setEnabled( false );
136  stack->appendEffect( innerShadow );
137  QgsInnerGlowEffect* innerGlow = new QgsInnerGlowEffect();
138  innerGlow->setEnabled( false );
139  stack->appendEffect( innerGlow );
140  return stack;
141 }
142 
144 {
145  QgsEffectStack* effectStack = dynamic_cast< QgsEffectStack* >( effect );
146  if ( !effectStack )
147  return false;
148 
149  if ( effectStack->count() != 5 )
150  return false;
151 
152  for ( int i = 0; i < 5; ++i )
153  {
154  //only the third effect should be enabled
155  if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
156  return false;
157  }
158 
159  if ( !dynamic_cast< QgsDropShadowEffect* >( effectStack->effect( 0 ) ) )
160  return false;
161  if ( !dynamic_cast< QgsOuterGlowEffect* >( effectStack->effect( 1 ) ) )
162  return false;
163  if ( !dynamic_cast< QgsDrawSourceEffect* >( effectStack->effect( 2 ) ) )
164  return false;
165  if ( !dynamic_cast< QgsInnerShadowEffect* >( effectStack->effect( 3 ) ) )
166  return false;
167  if ( !dynamic_cast< QgsInnerGlowEffect* >( effectStack->effect( 4 ) ) )
168  return false;
169 
170  //we don't go as far as to check the individual effect's properties
171  return true;
172 }
Convenience metadata class that uses static functions to create an effect and its widget...
void setEnabled(const bool enabled)
Sets whether the effect is enabled.
bool contains(const Key &key) const
QString attribute(const QString &name, const QString &defValue) const
static QgsPaintEffect * create(const QgsStringMap &)
Creates a new QgsTransformEffect effect from a properties string map.
bool addEffectType(QgsPaintEffectAbstractMetadata *metadata)
Registers a new effect type.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
bool enabled() const
Returns whether the effect is enabled.
Base class for visual effects which can be applied to QPicture drawings.
Stores metadata about a paint effect class.
void clear()
QString tr(const char *sourceText, const char *disambiguation, int n)
QStringList effects() const
Returns a list of known paint effects.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsEffectStack effect.
QList< Key > keys() const
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDrawSource effect from a properties string map.
void append(const T &value)
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. ...
static QgsPaintEffectRegistry * instance()
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsOuterGlowEffect effect from a properties string map.
A paint effect which consists of a stack of other chained paint effects.
iterator end()
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
iterator begin()
A paint effect which draws an offset and optionally blurred drop shadow.
A paint effect which draws a glow outside of a picture.
bool isNull() const
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...
const Key key(const T &value) const
A paint effect which draws an offset and optionally blurred drop shadow within a picture.
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.
QMap< QString, QgsPaintEffectAbstractMetadata * > mMetadata
QgsPaintEffectAbstractMetadata(const QString &name, const QString &visibleName)
Construct a new QgsPaintEffectAbstractMetadata.
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
static QgsPaintEffect * create(const QgsStringMap &)
Creates a new QgsBlurEffect effect from a properties string map.
QString name() const
Returns the unique string representing the paint effect class.
static QgsPaintEffect * create(const QgsStringMap &)
Creates a new QgsColorEffect effect from a properties string map.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsInnerGlowEffect effect from a properties string map.
QgsPaintEffect * createEffect(const QString &name, const QgsStringMap &properties=QgsStringMap()) const
Creates a new paint effect given the effect name and properties map.
Singleton registry of available paint effects.
const T value(const Key &key) const
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.