QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
24 QgsPaintEffectAbstractMetadata::QgsPaintEffectAbstractMetadata( const QString &name, const QString &visibleName )
25  : mName( name )
26  , mVisibleName( visibleName )
27 {
28 
29 }
30 
32 {
33  //init registry with known effects
34  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "blur" ), QObject::tr( "Blur" ),
35  QgsBlurEffect::create, nullptr ) );
36  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "dropShadow" ), QObject::tr( "Drop Shadow" ),
37  QgsDropShadowEffect::create, nullptr ) );
38  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerShadow" ), QObject::tr( "Inner Shadow" ),
39  QgsInnerShadowEffect::create, nullptr ) );
40  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "effectStack" ), QObject::tr( "Stack" ),
41  QgsEffectStack::create, nullptr ) );
42  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "outerGlow" ), QObject::tr( "Outer Glow" ),
43  QgsOuterGlowEffect::create, nullptr ) );
44  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerGlow" ), QObject::tr( "Inner Glow" ),
45  QgsInnerGlowEffect::create, nullptr ) );
46  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "drawSource" ), QObject::tr( "Source" ),
47  QgsDrawSourceEffect::create, nullptr ) );
48  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "transform" ), QObject::tr( "Transform" ),
49  QgsTransformEffect::create, nullptr ) );
50  addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "color" ), QObject::tr( "Colorise" ),
51  QgsColorEffect::create, nullptr ) );
52 }
53 
55 {
56  qDeleteAll( mMetadata );
57 }
58 
60 {
61  if ( mMetadata.contains( name ) )
62  return mMetadata.value( name );
63  else
64  return nullptr;
65 }
66 
68 {
69  if ( !metadata || mMetadata.contains( metadata->name() ) )
70  return false;
71 
72  mMetadata[metadata->name()] = metadata;
73  return true;
74 }
75 
76 QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QString &name, const QgsStringMap &properties ) const
77 {
78  if ( !mMetadata.contains( name ) )
79  return nullptr;
80 
81  QgsPaintEffect *effect = mMetadata[name]->createPaintEffect( properties );
82  return effect;
83 }
84 
85 QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QDomElement &element ) const
86 {
87  if ( element.isNull() )
88  {
89  return nullptr;
90  }
91 
92  QString type = element.attribute( QStringLiteral( "type" ) );
93 
95  if ( !effect )
96  return nullptr;
97 
98  effect->readProperties( element );
99  return effect;
100 }
101 
103 {
104  QStringList lst;
105  QMap<QString, QgsPaintEffectAbstractMetadata *>::ConstIterator it = mMetadata.begin();
106  for ( ; it != mMetadata.end(); ++it )
107  {
108  lst.append( it.key() );
109  }
110  return lst;
111 }
112 
114 {
115  //NOTE - also remember to update isDefaultStack below if making changes to this list
116  QgsEffectStack *stack = new QgsEffectStack();
117  QgsDropShadowEffect *dropShadow = new QgsDropShadowEffect();
118  dropShadow->setEnabled( false );
119  stack->appendEffect( dropShadow );
120  QgsOuterGlowEffect *outerGlow = new QgsOuterGlowEffect();
121  outerGlow->setEnabled( false );
122  stack->appendEffect( outerGlow );
123  stack->appendEffect( new QgsDrawSourceEffect() );
124  QgsInnerShadowEffect *innerShadow = new QgsInnerShadowEffect();
125  innerShadow->setEnabled( false );
126  stack->appendEffect( innerShadow );
127  QgsInnerGlowEffect *innerGlow = new QgsInnerGlowEffect();
128  innerGlow->setEnabled( false );
129  stack->appendEffect( innerGlow );
130  return stack;
131 }
132 
134 {
135  QgsEffectStack *effectStack = dynamic_cast< QgsEffectStack * >( effect );
136  if ( !effectStack )
137  return false;
138 
139  if ( effectStack->count() != 5 )
140  return false;
141 
142  for ( int i = 0; i < 5; ++i )
143  {
144  //only the third effect should be enabled
145  if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
146  return false;
147  }
148 
149  if ( !dynamic_cast< QgsDropShadowEffect * >( effectStack->effect( 0 ) ) )
150  return false;
151  if ( !dynamic_cast< QgsOuterGlowEffect * >( effectStack->effect( 1 ) ) )
152  return false;
153  if ( !dynamic_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) ) )
154  return false;
155  if ( !dynamic_cast< QgsInnerShadowEffect * >( effectStack->effect( 3 ) ) )
156  return false;
157  if ( !dynamic_cast< QgsInnerGlowEffect * >( effectStack->effect( 4 ) ) )
158  return false;
159 
160  QgsDrawSourceEffect *sourceEffect = static_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) );
161  if ( !qgsDoubleNear( sourceEffect->opacity(), 1.0 ) )
162  return false;
163  if ( sourceEffect->blendMode() != QPainter::CompositionMode_SourceOver )
164  return false;
165 
166  //we don't go as far as to check disabled effect's properties
167  return true;
168 }
Convenience metadata class that uses static functions to create an effect and its widget...
bool addEffectType(QgsPaintEffectAbstractMetadata *metadata)
Registers a new effect type.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:278
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.
QMap< QString, QString > QgsStringMap
Definition: qgis.h:577
QStringList effects() const
Returns a list of known paint effects.
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. ...
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.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsTransformEffect effect from a properties string map.
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.
A paint effect which draws an offset and optionally blurred drop shadow.
A paint effect which draws a glow outside of a picture.
double opacity() const
Returns the opacity for the 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.
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.
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
QString name() const
Returns the unique string representing the paint effect class.
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.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.