QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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  if ( d->enabled != other.enabled()
43  || d->type != other.type()
44  || d->size != other.size()
45  || d->sizeUnit != other.sizeUnit()
46  || d->sizeMapUnitScale != other.sizeMapUnitScale()
47  || d->joinStyle != other.joinStyle()
48  || d->opacity != other.opacity()
49  || d->maskedSymbolLayers != other.maskedSymbolLayers() )
50  return false;
51 
52  if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
53  || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
54  return false;
55 
56  return true;
57 }
58 
60 {
61  return !( *this == other );
62 }
63 
65 {
66  return d->enabled;
67 }
68 
69 void QgsTextMaskSettings::setEnabled( bool enabled )
70 {
71  d->enabled = enabled;
72 }
73 
75 {
76  return d->type;
77 }
78 
80 {
81  d->type = type;
82 }
83 
84 
86 {
87  return d->size;
88 }
89 
90 void QgsTextMaskSettings::setSize( double size )
91 {
92  d->size = size;
93 }
94 
96 {
97  return d->sizeUnit;
98 }
99 
101 {
102  d->sizeUnit = unit;
103 }
104 
106 {
107  return d->sizeMapUnitScale;
108 }
109 
111 {
112  d->sizeMapUnitScale = scale;
113 }
114 
115 Qt::PenJoinStyle QgsTextMaskSettings::joinStyle() const
116 {
117  return d->joinStyle;
118 }
119 
120 void QgsTextMaskSettings::setJoinStyle( Qt::PenJoinStyle style )
121 {
122  d->joinStyle = style;
123 }
124 
126 {
127  return d->opacity;
128 }
129 
130 void QgsTextMaskSettings::setOpacity( double opacity )
131 {
132  d->opacity = opacity;
133 }
134 
136 {
137  return d->paintEffect.get();
138 }
139 
141 {
142  d->paintEffect.reset( effect );
143 }
144 
146 {
147  if ( properties.isActive( QgsPalLayerSettings::MaskEnabled ) )
148  {
149  context.expressionContext().setOriginalValueVariable( d->enabled );
150  d->enabled = properties.valueAsBool( QgsPalLayerSettings::MaskEnabled, context.expressionContext(), d->enabled );
151  }
152 
153  if ( properties.isActive( QgsPalLayerSettings::MaskBufferSize ) )
154  {
155  context.expressionContext().setOriginalValueVariable( d->size );
156  d->size = properties.valueAsDouble( QgsPalLayerSettings::MaskBufferSize, context.expressionContext(), d->size );
157  }
158 
159  if ( properties.isActive( QgsPalLayerSettings::MaskBufferUnit ) )
160  {
161  const QVariant exprVal = properties.value( QgsPalLayerSettings::MaskBufferUnit, context.expressionContext() );
162  if ( !exprVal.isNull() )
163  {
164  const QString units = exprVal.toString();
165  if ( !units.isEmpty() )
166  {
167  bool ok;
169  if ( ok )
170  d->sizeUnit = res;
171  }
172  }
173  }
174 
175  if ( properties.isActive( QgsPalLayerSettings::MaskOpacity ) )
176  {
177  context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
178  const QVariant val = properties.value( QgsPalLayerSettings::MaskOpacity, context.expressionContext(), d->opacity * 100 );
179  if ( !val.isNull() )
180  {
181  d->opacity = val.toDouble() / 100.0;
182  }
183  }
184 
185  if ( properties.isActive( QgsPalLayerSettings::MaskJoinStyle ) )
186  {
187  const QVariant exprVal = properties.value( QgsPalLayerSettings::MaskJoinStyle, context.expressionContext() );
188  const QString joinstr = exprVal.toString().trimmed();
189  if ( !joinstr.isEmpty() )
190  {
191  d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
192  }
193  }
194 }
195 
197 {
198  return QSet< QString >(); // nothing for now
199 }
200 
201 void QgsTextMaskSettings::readXml( const QDomElement &elem )
202 {
203  const QDomElement textMaskElem = elem.firstChildElement( QStringLiteral( "text-mask" ) );
204  d->enabled = textMaskElem.attribute( QStringLiteral( "maskEnabled" ), QStringLiteral( "0" ) ).toInt();
205  d->type = static_cast<QgsTextMaskSettings::MaskType>( textMaskElem.attribute( QStringLiteral( "maskType" ), QStringLiteral( "0" ) ).toInt() );
206  d->size = textMaskElem.attribute( QStringLiteral( "maskSize" ), QStringLiteral( "0" ) ).toDouble();
207  d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textMaskElem.attribute( QStringLiteral( "maskSizeUnits" ) ) );
208  d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textMaskElem.attribute( QStringLiteral( "maskSizeMapUnitScale" ) ) );
209  d->joinStyle = static_cast< Qt::PenJoinStyle >( textMaskElem.attribute( QStringLiteral( "maskJoinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
210  d->opacity = textMaskElem.attribute( QStringLiteral( "maskOpacity" ), QStringLiteral( "1.0" ) ).toDouble();
211  const QDomElement effectElem = textMaskElem.firstChildElement( QStringLiteral( "effect" ) );
212  if ( !effectElem.isNull() )
214  else
215  setPaintEffect( nullptr );
216  d->maskedSymbolLayers = stringToSymbolLayerReferenceList( textMaskElem.attribute( QStringLiteral( "maskedSymbolLayers" ) ) );
217 }
218 
219 QDomElement QgsTextMaskSettings::writeXml( QDomDocument &doc ) const
220 {
221  QDomElement textMaskElem = doc.createElement( QStringLiteral( "text-mask" ) );
222  textMaskElem.setAttribute( QStringLiteral( "maskEnabled" ), d->enabled );
223  textMaskElem.setAttribute( QStringLiteral( "maskType" ), d->type );
224  textMaskElem.setAttribute( QStringLiteral( "maskSize" ), d->size );
225  textMaskElem.setAttribute( QStringLiteral( "maskSizeUnits" ), QgsUnitTypes::encodeUnit( d->sizeUnit ) );
226  textMaskElem.setAttribute( QStringLiteral( "maskSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
227  textMaskElem.setAttribute( QStringLiteral( "maskJoinStyle" ), static_cast< unsigned int >( d->joinStyle ) );
228  textMaskElem.setAttribute( QStringLiteral( "maskOpacity" ), d->opacity );
229  if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
230  d->paintEffect->saveProperties( doc, textMaskElem );
231  textMaskElem.setAttribute( QStringLiteral( "maskedSymbolLayers" ), symbolLayerReferenceListToString( d->maskedSymbolLayers ) );
232  return textMaskElem;
233 }
234 
235 QList<QgsSymbolLayerReference> QgsTextMaskSettings::maskedSymbolLayers() const
236 {
237  return d->maskedSymbolLayers;
238 }
239 
240 void QgsTextMaskSettings::setMaskedSymbolLayers( const QList<QgsSymbolLayerReference> &maskedSymbols )
241 {
242  d->maskedSymbolLayers = maskedSymbols;
243 }
qgstextmasksettings.h
QgsTextMaskSettings::setSize
void setSize(double size)
Sets the size of the buffer.
Definition: qgstextmasksettings.cpp:90
QgsTextMaskSettings::enabled
bool enabled() const
Returns whether the mask is enabled.
Definition: qgstextmasksettings.cpp:64
QgsPalLayerSettings::MaskEnabled
@ MaskEnabled
Whether the mask is enabled.
Definition: qgspallabeling.h:190
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:191
qgspallabeling.h
QgsTextMaskSettings::QgsTextMaskSettings
QgsTextMaskSettings()
Definition: qgstextmasksettings.cpp:22
QgsUnitTypes::RenderUnit
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
QgsRenderContext::expressionContext
QgsExpressionContext & expressionContext()
Gets the expression context.
Definition: qgsrendercontext.h:625
QgsSymbolLayerUtils::encodeMapUnitScale
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
Definition: qgssymbollayerutils.cpp:666
QgsTextMaskSettings::setType
void setType(MaskType type)
Sets the type of mask shape.
Definition: qgstextmasksettings.cpp:79
qgstextrenderer_p.h
QgsExpressionContext::setOriginalValueVariable
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Definition: qgsexpressioncontext.cpp:600
qgssymbollayerutils.h
QgsTextMaskSettings::MaskType
MaskType
Mask shape types.
Definition: qgstextmasksettings.h:48
QgsTextMaskSettings::operator!=
bool operator!=(const QgsTextMaskSettings &other) const
Definition: qgstextmasksettings.cpp:59
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsTextMaskSettings::setSizeUnit
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units used for the buffer size.
Definition: qgstextmasksettings.cpp:100
qgspainteffectregistry.h
QgsTextMaskSettings::operator==
bool operator==(const QgsTextMaskSettings &other) const
Definition: qgstextmasksettings.cpp:40
QgsPaintEffectRegistry::createEffect
QgsPaintEffect * createEffect(const QString &name, const QVariantMap &properties=QVariantMap()) const
Creates a new paint effect given the effect name and properties map.
Definition: qgspainteffectregistry.cpp:77
QgsTextMaskSettings::size
double size() const
Returns the size of the buffer.
Definition: qgstextmasksettings.cpp:85
QgsTextMaskSettings::setPaintEffect
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the mask.
Definition: qgstextmasksettings.cpp:140
QgsUnitTypes::decodeRenderUnit
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
Definition: qgsunittypes.cpp:2948
QgsTextMaskSettings::updateDataDefinedProperties
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
Definition: qgstextmasksettings.cpp:145
QgsTextMaskSettings::setMaskedSymbolLayers
void setMaskedSymbolLayers(const QList< QgsSymbolLayerReference > &maskedLayers)
Sets the symbol layers that will be masked by this buffer.
Definition: qgstextmasksettings.cpp:240
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:201
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
Container for settings relating to a selective masking around a text. A selective masking only makes ...
Definition: qgstextmasksettings.h:41
QgsTextMaskSettings::setJoinStyle
void setJoinStyle(Qt::PenJoinStyle style)
Sets the join style used for drawing the buffer.
Definition: qgstextmasksettings.cpp:120
QgsTextMaskSettings::maskedSymbolLayers
QList< QgsSymbolLayerReference > maskedSymbolLayers() const
Returns a list of references to symbol layers that are masked by this buffer.
Definition: qgstextmasksettings.cpp:235
QgsPalLayerSettings::MaskOpacity
@ MaskOpacity
Mask opacity.
Definition: qgspallabeling.h:193
QgsTextMaskSettings::setSizeMapUnitScale
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the buffer size.
Definition: qgstextmasksettings.cpp:110
QgsPalLayerSettings::MaskBufferUnit
@ MaskBufferUnit
Mask buffer size unit.
Definition: qgspallabeling.h:192
QgsTextMaskSettings::opacity
double opacity() const
Returns the mask's opacity.
Definition: qgstextmasksettings.cpp:125
stringToSymbolLayerReferenceList
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utilitary function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbo...
Definition: qgssymbollayerreference.cpp:40
QgsTextMaskSettings::sizeMapUnitScale
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
Definition: qgstextmasksettings.cpp:105
QgsMapUnitScale
Struct for storing maximum and minimum scales for measurements in map units.
Definition: qgsmapunitscale.h:36
QgsPalLayerSettings::MaskJoinStyle
@ MaskJoinStyle
Mask join style.
Definition: qgspallabeling.h:194
symbolLayerReferenceListToString
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utilitary function to turn a QgsSymbolLayerReferenceList into a string.
Definition: qgssymbollayerreference.cpp:20
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:2315
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:228
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
QgsTextMaskSettings::~QgsTextMaskSettings
~QgsTextMaskSettings()
QgsTextMaskSettings::setEnabled
void setEnabled(bool)
Returns whether the mask is enabled.
Definition: qgstextmasksettings.cpp:69
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:52
QgsTextMaskSettings::paintEffect
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the mask.
Definition: qgstextmasksettings.cpp:135
QgsTextMaskSettings::sizeUnit
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
Definition: qgstextmasksettings.cpp:95
QgsTextMaskSettings::setOpacity
void setOpacity(double opacity)
Sets the mask's opacity.
Definition: qgstextmasksettings.cpp:130
QgsSymbolLayerUtils::decodePenJoinStyle
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
Definition: qgssymbollayerutils.cpp:200
QgsTextMaskSettings::referencedFields
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Definition: qgstextmasksettings.cpp:196
QgsTextMaskSettings::writeXml
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
Definition: qgstextmasksettings.cpp:219
QgsPaintEffect::properties
virtual QVariantMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
QgsTextMaskSettings::type
MaskType type() const
Returns the type of mask shape.
Definition: qgstextmasksettings.cpp:74
QgsPropertyCollection::isActive
bool isActive(int key) const override
Returns true if the collection contains an active property with the specified key.
Definition: qgspropertycollection.cpp:268
QgsSymbolLayerUtils::decodeMapUnitScale
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
Definition: qgssymbollayerutils.cpp:676
QgsTextMaskSettings::joinStyle
Qt::PenJoinStyle joinStyle() const
Returns the buffer join style.
Definition: qgstextmasksettings.cpp:115