QGIS API Documentation 3.37.0-Master (fdefdf9c27f)
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
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
25QgsPaintEffectAbstractMetadata::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" ),
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
77QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QString &name, const QVariantMap &properties ) const
78{
79 if ( !mMetadata.contains( name ) )
80 return nullptr;
81
82 QgsPaintEffect *effect = mMetadata[name]->createPaintEffect( properties );
83 return effect;
84}
85
86QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QDomElement &element ) const
87{
88 if ( element.isNull() )
89 {
90 return nullptr;
91 }
92
93 const 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 QgsDrawSourceEffect *sourceEffect = static_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) );
162 if ( !qgsDoubleNear( sourceEffect->opacity(), 1.0 ) )
163 return false;
164 if ( sourceEffect->blendMode() != QPainter::CompositionMode_SourceOver )
165 return false;
166
167 //we don't go as far as to check disabled effect's properties
168 return true;
169}
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsColorEffect effect from a properties string map.
A paint effect which draws the source picture with minor or no alterations.
QPainter::CompositionMode blendMode() const
Returns the blend mode for the effect.
double opacity() const
Returns the opacity for the effect.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsDrawSource effect from a properties string map.
A paint effect which draws an offset and optionally blurred drop shadow.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsDropShadowEffect effect from a properties string map.
A paint effect which consists of a stack of other chained paint effects.
void appendEffect(QgsPaintEffect *effect)
Appends an effect to the end of the stack.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsEffectStack effect.
int count() const
Returns count of effects contained by the stack.
QgsPaintEffect * effect(int index) const
Returns a pointer to the effect at a specified index within the stack.
A paint effect which draws a glow within a picture.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsInnerGlowEffect effect from a properties string map.
A paint effect which draws an offset and optionally blurred drop shadow within a picture.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsInnerShadowEffect effect from a properties string map.
A paint effect which draws a glow outside of a picture.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsOuterGlowEffect effect from a properties string map.
Stores metadata about a paint effect class.
QString name() const
Returns the unique string representing the paint effect class.
QgsPaintEffectAbstractMetadata(const QString &name, const QString &visibleName)
Construct a new QgsPaintEffectAbstractMetadata.
Convenience metadata class that uses static functions to create an effect and its widget.
QgsPaintEffectAbstractMetadata * effectMetadata(const QString &name) const
Returns the metadata for a specific effect.
QStringList effects() const
Returns a list of known paint effects.
static QgsPaintEffect * defaultStack()
Returns a new effect stack consisting of a sensible selection of default effects.
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
QgsPaintEffect * createEffect(const QString &name, const QVariantMap &properties=QVariantMap()) const
Creates a new paint effect given the effect name and properties map.
bool addEffectType(QgsPaintEffectAbstractMetadata *metadata)
Registers a new effect type.
Base class for visual effects which can be applied to QPicture drawings.
void setEnabled(bool enabled)
Sets whether the effect is enabled.
virtual void readProperties(const QVariantMap &props)=0
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
bool enabled() const
Returns whether the effect is enabled.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsTransformEffect effect from a properties string map.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:5207