QGIS API Documentation 3.99.0-Master (21b3aa880ba)
Loading...
Searching...
No Matches
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
18#include "qgsapplication.h"
19#include "qgsblureffect.h"
20#include "qgscoloreffect.h"
21#include "qgseffectstack.h"
22#include "qgsgloweffect.h"
23#include "qgsshadoweffect.h"
24#include "qgstransformeffect.h"
25
32
34{
35 //init registry with known effects
36 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "blur" ), QObject::tr( "Blur" ),
37 QgsBlurEffect::create, nullptr ) );
38 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "dropShadow" ), QObject::tr( "Drop Shadow" ),
39 QgsDropShadowEffect::create, nullptr ) );
40 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerShadow" ), QObject::tr( "Inner Shadow" ),
42 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "effectStack" ), QObject::tr( "Stack" ),
43 QgsEffectStack::create, nullptr ) );
44 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "outerGlow" ), QObject::tr( "Outer Glow" ),
45 QgsOuterGlowEffect::create, nullptr ) );
46 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "innerGlow" ), QObject::tr( "Inner Glow" ),
47 QgsInnerGlowEffect::create, nullptr ) );
48 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "drawSource" ), QObject::tr( "Source" ),
49 QgsDrawSourceEffect::create, nullptr ) );
50 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "transform" ), QObject::tr( "Transform" ),
51 QgsTransformEffect::create, nullptr ) );
52 addEffectType( new QgsPaintEffectMetadata( QStringLiteral( "color" ), QObject::tr( "Colorise" ),
53 QgsColorEffect::create, nullptr ) );
54}
55
57{
58 qDeleteAll( mMetadata );
59}
60
62{
63 if ( mMetadata.contains( name ) )
64 return mMetadata.value( name );
65 else
66 return nullptr;
67}
68
70{
71 if ( !metadata || mMetadata.contains( metadata->name() ) )
72 return false;
73
74 mMetadata[metadata->name()] = metadata;
75 return true;
76}
77
78QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QString &name, const QVariantMap &properties ) const
79{
80 if ( !mMetadata.contains( name ) )
81 return nullptr;
82
83 QgsPaintEffect *effect = mMetadata[name]->createPaintEffect( properties );
84 return effect;
85}
86
87QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QDomElement &element ) const
88{
89 if ( element.isNull() )
90 {
91 return nullptr;
92 }
93
94 const QString type = element.attribute( QStringLiteral( "type" ) );
95
97 if ( !effect )
98 return nullptr;
99
100 effect->readProperties( element );
101 return effect;
102}
103
105{
106 QStringList lst;
107 QMap<QString, QgsPaintEffectAbstractMetadata *>::ConstIterator it = mMetadata.begin();
108 for ( ; it != mMetadata.end(); ++it )
109 {
110 lst.append( it.key() );
111 }
112 return lst;
113}
114
116{
117 //NOTE - also remember to update isDefaultStack below if making changes to this list
118 QgsEffectStack *stack = new QgsEffectStack();
119 QgsDropShadowEffect *dropShadow = new QgsDropShadowEffect();
120 dropShadow->setEnabled( false );
121 stack->appendEffect( dropShadow );
122 QgsOuterGlowEffect *outerGlow = new QgsOuterGlowEffect();
123 outerGlow->setEnabled( false );
124 stack->appendEffect( outerGlow );
125 stack->appendEffect( new QgsDrawSourceEffect() );
126 QgsInnerShadowEffect *innerShadow = new QgsInnerShadowEffect();
127 innerShadow->setEnabled( false );
128 stack->appendEffect( innerShadow );
129 QgsInnerGlowEffect *innerGlow = new QgsInnerGlowEffect();
130 innerGlow->setEnabled( false );
131 stack->appendEffect( innerGlow );
132 return stack;
133}
134
136{
137 QgsEffectStack *effectStack = dynamic_cast< QgsEffectStack * >( effect );
138 if ( !effectStack )
139 return false;
140
141 if ( effectStack->count() != 5 )
142 return false;
143
144 for ( int i = 0; i < 5; ++i )
145 {
146 //only the third effect should be enabled
147 if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
148 return false;
149 }
150
151 if ( !dynamic_cast< QgsDropShadowEffect * >( effectStack->effect( 0 ) ) )
152 return false;
153 if ( !dynamic_cast< QgsOuterGlowEffect * >( effectStack->effect( 1 ) ) )
154 return false;
155 if ( !dynamic_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) ) )
156 return false;
157 if ( !dynamic_cast< QgsInnerShadowEffect * >( effectStack->effect( 3 ) ) )
158 return false;
159 if ( !dynamic_cast< QgsInnerGlowEffect * >( effectStack->effect( 4 ) ) )
160 return false;
161
162 QgsDrawSourceEffect *sourceEffect = static_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) );
163 if ( !qgsDoubleNear( sourceEffect->opacity(), 1.0 ) )
164 return false;
165 if ( sourceEffect->blendMode() != QPainter::CompositionMode_SourceOver )
166 return false;
167
168 //we don't go as far as to check disabled effect's properties
169 return true;
170}
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 visibleName() const
Returns the user visible string representing the 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...
virtual void begin(QgsRenderContext &context)
Begins intercepting paint operations to a render context.
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:6607