QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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, nullptr ) );
36  addEffectType( new QgsPaintEffectMetadata( "dropShadow", QObject::tr( "Drop Shadow" ),
37  QgsDropShadowEffect::create, nullptr ) );
38  addEffectType( new QgsPaintEffectMetadata( "innerShadow", QObject::tr( "Inner Shadow" ),
39  QgsInnerShadowEffect::create, nullptr ) );
40  addEffectType( new QgsPaintEffectMetadata( "effectStack", QObject::tr( "Stack" ),
41  QgsEffectStack::create, nullptr ) );
42  addEffectType( new QgsPaintEffectMetadata( "outerGlow", QObject::tr( "Outer Glow" ),
43  QgsOuterGlowEffect::create, nullptr ) );
44  addEffectType( new QgsPaintEffectMetadata( "innerGlow", QObject::tr( "Inner Glow" ),
45  QgsInnerGlowEffect::create, nullptr ) );
46  addEffectType( new QgsPaintEffectMetadata( "drawSource", QObject::tr( "Source" ),
47  QgsDrawSourceEffect::create, nullptr ) );
48  addEffectType( new QgsPaintEffectMetadata( "transform", QObject::tr( "Transform" ),
49  QgsTransformEffect::create, nullptr ) );
50  addEffectType( new QgsPaintEffectMetadata( "color", QObject::tr( "Colorise" ),
51  QgsColorEffect::create, nullptr ) );
52 }
53 
55 {
56  qDeleteAll( mMetadata );
57 }
58 
60 {
61  static QgsPaintEffectRegistry sInstance;
62  return &sInstance;
63 }
64 
66 {
67  if ( mMetadata.contains( name ) )
68  return mMetadata.value( name );
69  else
70  return nullptr;
71 }
72 
74 {
75  if ( !metadata || mMetadata.contains( metadata->name() ) )
76  return false;
77 
78  mMetadata[metadata->name()] = metadata;
79  return true;
80 }
81 
83 {
84  if ( !mMetadata.contains( name ) )
85  return nullptr;
86 
87  QgsPaintEffect* effect = mMetadata[name]->createPaintEffect( properties );
88  return effect;
89 }
90 
92 {
93  if ( element.isNull() )
94  {
95  return nullptr;
96  }
97 
98  QString type = element.attribute( QString( "type" ) );
99 
100  QgsPaintEffect* effect = instance()->createEffect( type );
101  if ( !effect )
102  return nullptr;
103 
104  effect->readProperties( element );
105  return effect;
106 }
107 
109 {
110  QStringList lst;
112  for ( ; it != mMetadata.end(); ++it )
113  {
114  lst.append( it.key() );
115  }
116  return lst;
117 }
118 
120 {
121  //NOTE - also remember to update isDefaultStack below if making changes to this list
122  QgsEffectStack* stack = new QgsEffectStack();
123  QgsDropShadowEffect* dropShadow = new QgsDropShadowEffect();
124  dropShadow->setEnabled( false );
125  stack->appendEffect( dropShadow );
126  QgsOuterGlowEffect* outerGlow = new QgsOuterGlowEffect();
127  outerGlow->setEnabled( false );
128  stack->appendEffect( outerGlow );
129  stack->appendEffect( new QgsDrawSourceEffect() );
130  QgsInnerShadowEffect* innerShadow = new QgsInnerShadowEffect();
131  innerShadow->setEnabled( false );
132  stack->appendEffect( innerShadow );
133  QgsInnerGlowEffect* innerGlow = new QgsInnerGlowEffect();
134  innerGlow->setEnabled( false );
135  stack->appendEffect( innerGlow );
136  return stack;
137 }
138 
140 {
141  QgsEffectStack* effectStack = dynamic_cast< QgsEffectStack* >( effect );
142  if ( !effectStack )
143  return false;
144 
145  if ( effectStack->count() != 5 )
146  return false;
147 
148  for ( int i = 0; i < 5; ++i )
149  {
150  //only the third effect should be enabled
151  if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
152  return false;
153  }
154 
155  if ( !dynamic_cast< QgsDropShadowEffect* >( effectStack->effect( 0 ) ) )
156  return false;
157  if ( !dynamic_cast< QgsOuterGlowEffect* >( effectStack->effect( 1 ) ) )
158  return false;
159  if ( !dynamic_cast< QgsDrawSourceEffect* >( effectStack->effect( 2 ) ) )
160  return false;
161  if ( !dynamic_cast< QgsInnerShadowEffect* >( effectStack->effect( 3 ) ) )
162  return false;
163  if ( !dynamic_cast< QgsInnerGlowEffect* >( effectStack->effect( 4 ) ) )
164  return false;
165 
166  //we don't go as far as to check the individual effect's properties
167  return true;
168 }
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.
QString attribute(const QString &name, const QString &defValue) const
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.
QString tr(const char *sourceText, const char *disambiguation, int n)
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.
int count() const
Returns count of effects contained by the stack.
void append(const T &value)
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects. ...
static QgsPaintEffectRegistry * instance()
Returns a reference to the singleton instance of the paint effect registry.
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.
iterator end()
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.
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.
bool isNull() const
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...
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 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.
Singleton registry of available paint effects.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.