QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgstextmasksettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextmasksettings.cpp
3  -----------------
4  begin : May 2020
5  copyright : (C) 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 "qgstextmasksettings.h"
17 #include "qgstextrenderer_p.h"
18 #include "qgspallabeling.h"
19 #include "qgssymbollayerutils.h"
20 #include "qgspainteffectregistry.h"
21 
23 {
24  d = new QgsTextMaskSettingsPrivate();
25 }
26 
28 
30  : d( other.d )
31 {
32 }
33 
35 {
36  d = other.d;
37  return *this;
38 }
39 
41 {
42  return d->enabled;
43 }
44 
45 void QgsTextMaskSettings::setEnabled( bool enabled )
46 {
47  d->enabled = enabled;
48 }
49 
51 {
52  return d->type;
53 }
54 
56 {
57  d->type = type;
58 }
59 
60 
62 {
63  return d->size;
64 }
65 
66 void QgsTextMaskSettings::setSize( double size )
67 {
68  d->size = size;
69 }
70 
72 {
73  return d->sizeUnit;
74 }
75 
77 {
78  d->sizeUnit = unit;
79 }
80 
82 {
83  return d->sizeMapUnitScale;
84 }
85 
87 {
88  d->sizeMapUnitScale = scale;
89 }
90 
91 Qt::PenJoinStyle QgsTextMaskSettings::joinStyle() const
92 {
93  return d->joinStyle;
94 }
95 
96 void QgsTextMaskSettings::setJoinStyle( Qt::PenJoinStyle style )
97 {
98  d->joinStyle = style;
99 }
100 
102 {
103  return d->opacity;
104 }
105 
106 void QgsTextMaskSettings::setOpacity( double opacity )
107 {
108  d->opacity = opacity;
109 }
110 
112 {
113  return d->paintEffect.get();
114 }
115 
117 {
118  d->paintEffect.reset( effect );
119 }
120 
122 {
123  if ( properties.isActive( QgsPalLayerSettings::MaskEnabled ) )
124  {
125  context.expressionContext().setOriginalValueVariable( d->enabled );
126  d->enabled = properties.valueAsBool( QgsPalLayerSettings::MaskEnabled, context.expressionContext(), d->enabled );
127  }
128 
129  if ( properties.isActive( QgsPalLayerSettings::MaskBufferSize ) )
130  {
131  context.expressionContext().setOriginalValueVariable( d->size );
132  d->size = properties.valueAsDouble( QgsPalLayerSettings::MaskBufferSize, context.expressionContext(), d->size );
133  }
134 
135  if ( properties.isActive( QgsPalLayerSettings::MaskBufferUnit ) )
136  {
137  QVariant exprVal = properties.value( QgsPalLayerSettings::MaskBufferUnit, context.expressionContext() );
138  if ( exprVal.isValid() )
139  {
140  QString units = exprVal.toString();
141  if ( !units.isEmpty() )
142  {
143  bool ok;
145  if ( ok )
146  d->sizeUnit = res;
147  }
148  }
149  }
150 
151  if ( properties.isActive( QgsPalLayerSettings::MaskOpacity ) )
152  {
153  context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
154  d->opacity = properties.value( QgsPalLayerSettings::MaskOpacity, context.expressionContext(), d->opacity * 100 ).toDouble() / 100.0;
155  }
156 
157  if ( properties.isActive( QgsPalLayerSettings::MaskJoinStyle ) )
158  {
159  QVariant exprVal = properties.value( QgsPalLayerSettings::MaskJoinStyle, context.expressionContext() );
160  QString joinstr = exprVal.toString().trimmed();
161  if ( !joinstr.isEmpty() )
162  {
163  d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
164  }
165  }
166 }
167 
169 {
170  return QSet< QString >(); // nothing for now
171 }
172 
173 void QgsTextMaskSettings::readXml( const QDomElement &elem )
174 {
175  QDomElement textMaskElem = elem.firstChildElement( QStringLiteral( "text-mask" ) );
176  d->enabled = textMaskElem.attribute( QStringLiteral( "maskEnabled" ), QStringLiteral( "0" ) ).toInt();
177  d->type = static_cast<QgsTextMaskSettings::MaskType>( textMaskElem.attribute( QStringLiteral( "maskType" ), QStringLiteral( "0" ) ).toInt() );
178  d->size = textMaskElem.attribute( QStringLiteral( "maskSize" ), QStringLiteral( "0" ) ).toDouble();
179  d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textMaskElem.attribute( QStringLiteral( "maskSizeUnits" ) ) );
180  d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textMaskElem.attribute( QStringLiteral( "maskSizeMapUnitScale" ) ) );
181  d->joinStyle = static_cast< Qt::PenJoinStyle >( textMaskElem.attribute( QStringLiteral( "maskJoinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
182  d->opacity = textMaskElem.attribute( QStringLiteral( "maskOpacity" ), QStringLiteral( "1.0" ) ).toDouble();
183  QDomElement effectElem = textMaskElem.firstChildElement( QStringLiteral( "effect" ) );
184  if ( !effectElem.isNull() )
186  else
187  setPaintEffect( nullptr );
188  d->maskedSymbolLayers = stringToSymbolLayerReferenceList( textMaskElem.attribute( QStringLiteral( "maskedSymbolLayers" ) ) );
189 }
190 
191 QDomElement QgsTextMaskSettings::writeXml( QDomDocument &doc ) const
192 {
193  QDomElement textMaskElem = doc.createElement( QStringLiteral( "text-mask" ) );
194  textMaskElem.setAttribute( QStringLiteral( "maskEnabled" ), d->enabled );
195  textMaskElem.setAttribute( QStringLiteral( "maskType" ), d->type );
196  textMaskElem.setAttribute( QStringLiteral( "maskSize" ), d->size );
197  textMaskElem.setAttribute( QStringLiteral( "maskSizeUnits" ), QgsUnitTypes::encodeUnit( d->sizeUnit ) );
198  textMaskElem.setAttribute( QStringLiteral( "maskSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
199  textMaskElem.setAttribute( QStringLiteral( "maskJoinStyle" ), static_cast< unsigned int >( d->joinStyle ) );
200  textMaskElem.setAttribute( QStringLiteral( "maskOpacity" ), d->opacity );
201  if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
202  d->paintEffect->saveProperties( doc, textMaskElem );
203  textMaskElem.setAttribute( QStringLiteral( "maskedSymbolLayers" ), symbolLayerReferenceListToString( d->maskedSymbolLayers ) );
204  return textMaskElem;
205 }
206 
208 {
209  return d->maskedSymbolLayers;
210 }
211 
213 {
214  d->maskedSymbolLayers = maskedSymbols;
215 }
qgstextmasksettings.h
QgsTextMaskSettings::setSize
void setSize(double size)
Sets the size of the buffer.
Definition: qgstextmasksettings.cpp:66
QgsTextMaskSettings::enabled
bool enabled() const
Returns whether the mask is enabled.
Definition: qgstextmasksettings.cpp:40
QgsPalLayerSettings::MaskEnabled
@ MaskEnabled
Whether the mask is enabled.
Definition: qgspallabeling.h:382
QgsAbstractPropertyCollection::valueAsDouble
double valueAsDouble(int key, const QgsExpressionContext &context, double defaultValue=0.0, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a double.
Definition: qgspropertycollection.cpp:66
QgsPalLayerSettings::MaskBufferSize
@ MaskBufferSize
Mask buffer size.
Definition: qgspallabeling.h:383
qgspallabeling.h
QgsTextMaskSettings::QgsTextMaskSettings
QgsTextMaskSettings()
Definition: qgstextmasksettings.cpp:22
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:166
QgsRenderContext::expressionContext
QgsExpressionContext & expressionContext()
Gets the expression context.
Definition: qgsrendercontext.h:580
QgsSymbolLayerUtils::encodeMapUnitScale
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
Definition: qgssymbollayerutils.cpp:558
QgsTextMaskSettings::setType
void setType(MaskType type)
Sets the type of mask shape.
Definition: qgstextmasksettings.cpp:55
qgstextrenderer_p.h
QgsPaintEffectRegistry::createEffect
QgsPaintEffect * createEffect(const QString &name, const QgsStringMap &properties=QgsStringMap()) const
Creates a new paint effect given the effect name and properties map.
Definition: qgspainteffectregistry.cpp:77
QgsExpressionContext::setOriginalValueVariable
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Definition: qgsexpressioncontext.cpp:566
qgssymbollayerutils.h
QgsTextMaskSettings::MaskType
MaskType
Mask shape types.
Definition: qgstextmasksettings.h:48
QgsRenderContext
Definition: qgsrendercontext.h:57
QgsTextMaskSettings::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units used for the buffer size.
Definition: qgstextmasksettings.cpp:76
qgspainteffectregistry.h
QgsTextMaskSettings::size
double size() const
Returns the size of the buffer.
Definition: qgstextmasksettings.cpp:61
QgsTextMaskSettings::setPaintEffect
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the mask.
Definition: qgstextmasksettings.cpp:116
QgsUnitTypes::decodeRenderUnit
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
Definition: qgsunittypes.cpp:2900
QgsTextMaskSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextmasksettings.cpp:121
QgsTextMaskSettings::operator=
QgsTextMaskSettings & operator=(const QgsTextMaskSettings &other)
Copy constructor.
Definition: qgstextmasksettings.cpp:34
QgsTextMaskSettings::readXml
void readXml(const QDomElement &elem)
Read settings from a DOM element.
Definition: qgstextmasksettings.cpp:173
QgsPaintEffectRegistry::isDefaultStack
static bool isDefaultStack(QgsPaintEffect *effect)
Tests whether a paint effect matches the default effects stack.
Definition: qgspainteffectregistry.cpp:134
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsTextMaskSettings
Definition: qgstextmasksettings.h:41
QgsTextMaskSettings::setJoinStyle
void setJoinStyle(Qt::PenJoinStyle style)
Sets the join style used for drawing the buffer.
Definition: qgstextmasksettings.cpp:96
QgsPalLayerSettings::MaskOpacity
@ MaskOpacity
Mask opacity.
Definition: qgspallabeling.h:385
QgsTextMaskSettings::setSizeMapUnitScale
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the buffer size.
Definition: qgstextmasksettings.cpp:86
QgsPalLayerSettings::MaskBufferUnit
@ MaskBufferUnit
Mask buffer size unit.
Definition: qgspallabeling.h:384
QgsTextMaskSettings::opacity
double opacity() const
Returns the mask's opacity.
Definition: qgstextmasksettings.cpp:101
stringToSymbolLayerReferenceList
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utilitary function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbo...
Definition: qgssymbollayerreference.cpp:34
QgsTextMaskSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Definition: qgstextmasksettings.cpp:81
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:37
QgsPalLayerSettings::MaskJoinStyle
@ MaskJoinStyle
Mask join style.
Definition: qgspallabeling.h:386
QgsTextMaskSettings::maskedSymbolLayers
QgsSymbolLayerReferenceList maskedSymbolLayers() const
Returns a list of references to symbol layers that are masked by this buffer.
Definition: qgstextmasksettings.cpp:207
symbolLayerReferenceListToString
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utilitary function to turn a QgsSymbolLayerReferenceList into a string.
Definition: qgssymbollayerreference.cpp:19
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
Definition: qgspropertycollection.h:318
QgsApplication::paintEffectRegistry
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
Definition: qgsapplication.cpp:2094
QgsTextMaskSettings::setMaskedSymbolLayers
void setMaskedSymbolLayers(QgsSymbolLayerReferenceList maskedLayers)
Sets the symbol layers that will be masked by this buffer.
Definition: qgstextmasksettings.cpp:212
QgsPropertyCollection::value
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const override
Returns the calculated value of the property with the specified key from within the collection.
Definition: qgspropertycollection.cpp:218
QgsAbstractPropertyCollection::valueAsBool
bool valueAsBool(int key, const QgsExpressionContext &context, bool defaultValue=false, bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as an boolean.
Definition: qgspropertycollection.cpp:88
QgsSymbolLayerReferenceList
QList< QgsSymbolLayerReference > QgsSymbolLayerReferenceList
Definition: qgssymbollayerreference.h:160
QgsTextMaskSettings::~QgsTextMaskSettings
~QgsTextMaskSettings()
QgsTextMaskSettings::setEnabled
void setEnabled(bool)
Returns whether the mask is enabled.
Definition: qgstextmasksettings.cpp:45
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:53
QgsTextMaskSettings::paintEffect
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the mask.
Definition: qgstextmasksettings.cpp:111
QgsTextMaskSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Definition: qgstextmasksettings.cpp:71
QgsTextMaskSettings::setOpacity
void setOpacity(double opacity)
Sets the mask's opacity.
Definition: qgstextmasksettings.cpp:106
QgsSymbolLayerUtils::decodePenJoinStyle
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
Definition: qgssymbollayerutils.cpp:188
QgsTextMaskSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextmasksettings.cpp:168
QgsTextMaskSettings::writeXml
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
Definition: qgstextmasksettings.cpp:191
QgsTextMaskSettings::type
MaskType type() const
Returns the type of mask shape.
Definition: qgstextmasksettings.cpp:50
QgsPropertyCollection::isActive
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Definition: qgspropertycollection.cpp:258
QgsSymbolLayerUtils::decodeMapUnitScale
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
Definition: qgssymbollayerutils.cpp:568
QgsTextMaskSettings::joinStyle
Qt::PenJoinStyle joinStyle() const
Returns the buffer join style.
Definition: qgstextmasksettings.cpp:91