QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  foreach ( 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  QgsEffectStack* stack = new QgsEffectStack();
126  QgsDropShadowEffect* dropShadow = new QgsDropShadowEffect();
127  dropShadow->setEnabled( false );
128  stack->appendEffect( dropShadow );
129  QgsOuterGlowEffect* outerGlow = new QgsOuterGlowEffect();
130  outerGlow->setEnabled( false );
131  stack->appendEffect( outerGlow );
132  stack->appendEffect( new QgsDrawSourceEffect() );
133  QgsInnerShadowEffect* innerShadow = new QgsInnerShadowEffect();
134  innerShadow->setEnabled( false );
135  stack->appendEffect( innerShadow );
136  QgsInnerGlowEffect* innerGlow = new QgsInnerGlowEffect();
137  innerGlow->setEnabled( false );
138  stack->appendEffect( innerGlow );
139  return stack;
140 }
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.
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)
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.
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.