QGIS API Documentation 4.1.0-Master (60fea48833c)
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
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
34
36{
37 //init registry with known effects
38 addEffectType( new QgsPaintEffectMetadata( u"blur"_s, QObject::tr( "Blur" ), QgsBlurEffect::create, nullptr ) );
39 addEffectType( new QgsPaintEffectMetadata( u"dropShadow"_s, QObject::tr( "Drop Shadow" ), QgsDropShadowEffect::create, nullptr ) );
40 addEffectType( new QgsPaintEffectMetadata( u"innerShadow"_s, QObject::tr( "Inner Shadow" ), QgsInnerShadowEffect::create, nullptr ) );
41 addEffectType( new QgsPaintEffectMetadata( u"effectStack"_s, QObject::tr( "Stack" ), QgsEffectStack::create, nullptr ) );
42 addEffectType( new QgsPaintEffectMetadata( u"outerGlow"_s, QObject::tr( "Outer Glow" ), QgsOuterGlowEffect::create, nullptr ) );
43 addEffectType( new QgsPaintEffectMetadata( u"innerGlow"_s, QObject::tr( "Inner Glow" ), QgsInnerGlowEffect::create, nullptr ) );
44 addEffectType( new QgsPaintEffectMetadata( u"drawSource"_s, QObject::tr( "Source" ), QgsDrawSourceEffect::create, nullptr ) );
45 addEffectType( new QgsPaintEffectMetadata( u"transform"_s, QObject::tr( "Transform" ), QgsTransformEffect::create, nullptr ) );
46 addEffectType( new QgsPaintEffectMetadata( u"color"_s, QObject::tr( "Colorise" ), QgsColorEffect::create, nullptr ) );
47}
48
50{
51 qDeleteAll( mMetadata );
52}
53
55{
56 if ( mMetadata.contains( name ) )
57 return mMetadata.value( name );
58 else
59 return nullptr;
60}
61
63{
64 if ( !metadata || mMetadata.contains( metadata->name() ) )
65 return false;
66
67 mMetadata[metadata->name()] = metadata;
68 return true;
69}
70
71QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QString &name, const QVariantMap &properties ) const
72{
73 if ( !mMetadata.contains( name ) )
74 return nullptr;
75
76 QgsPaintEffect *effect = mMetadata[name]->createPaintEffect( properties );
77 return effect;
78}
79
80QgsPaintEffect *QgsPaintEffectRegistry::createEffect( const QDomElement &element ) const
81{
82 if ( element.isNull() )
83 {
84 return nullptr;
85 }
86
87 const QString type = element.attribute( u"type"_s );
88
90 if ( !effect )
91 return nullptr;
92
93 effect->readProperties( element );
94 return effect;
95}
96
98{
99 QStringList lst;
100 QMap<QString, QgsPaintEffectAbstractMetadata *>::ConstIterator it = mMetadata.begin();
101 for ( ; it != mMetadata.end(); ++it )
102 {
103 lst.append( it.key() );
104 }
105 return lst;
106}
107
109{
110 //NOTE - also remember to update isDefaultStack below if making changes to this list
111 QgsEffectStack *stack = new QgsEffectStack();
112 QgsDropShadowEffect *dropShadow = new QgsDropShadowEffect();
113 dropShadow->setEnabled( false );
114 stack->appendEffect( dropShadow );
115 QgsOuterGlowEffect *outerGlow = new QgsOuterGlowEffect();
116 outerGlow->setEnabled( false );
117 stack->appendEffect( outerGlow );
118 stack->appendEffect( new QgsDrawSourceEffect() );
119 QgsInnerShadowEffect *innerShadow = new QgsInnerShadowEffect();
120 innerShadow->setEnabled( false );
121 stack->appendEffect( innerShadow );
122 QgsInnerGlowEffect *innerGlow = new QgsInnerGlowEffect();
123 innerGlow->setEnabled( false );
124 stack->appendEffect( innerGlow );
125 return stack;
126}
127
129{
130 QgsEffectStack *effectStack = dynamic_cast< QgsEffectStack * >( effect );
131 if ( !effectStack )
132 return false;
133
134 if ( effectStack->count() != 5 )
135 return false;
136
137 for ( int i = 0; i < 5; ++i )
138 {
139 //only the third effect should be enabled
140 if ( effectStack->effect( i )->enabled() != ( i == 2 ) )
141 return false;
142 }
143
144 if ( !dynamic_cast< QgsDropShadowEffect * >( effectStack->effect( 0 ) ) )
145 return false;
146 if ( !dynamic_cast< QgsOuterGlowEffect * >( effectStack->effect( 1 ) ) )
147 return false;
148 if ( !dynamic_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) ) )
149 return false;
150 if ( !dynamic_cast< QgsInnerShadowEffect * >( effectStack->effect( 3 ) ) )
151 return false;
152 if ( !dynamic_cast< QgsInnerGlowEffect * >( effectStack->effect( 4 ) ) )
153 return false;
154
155 QgsDrawSourceEffect *sourceEffect = static_cast< QgsDrawSourceEffect * >( effectStack->effect( 2 ) );
156 if ( !qgsDoubleNear( sourceEffect->opacity(), 1.0 ) )
157 return false;
158 if ( sourceEffect->blendMode() != QPainter::CompositionMode_SourceOver )
159 return false;
160
161 //we don't go as far as to check disabled effect's properties
162 return true;
163}
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:6975