QGIS API Documentation 3.41.0-Master (fda2aa46e9a)
Loading...
Searching...
No Matches
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"
21#include "qgsapplication.h"
22#include "qgsunittypes.h"
23
25{
26 d = new QgsTextMaskSettingsPrivate();
27}
28
30
32 : d( other.d )
33{
34}
35
37{
38 d = other.d;
39 return *this;
40}
41
43{
44 if ( d->enabled != other.enabled()
45 || d->type != other.type()
46 || d->size != other.size()
47 || d->sizeUnit != other.sizeUnit()
48 || d->sizeMapUnitScale != other.sizeMapUnitScale()
49 || d->joinStyle != other.joinStyle()
50 || d->opacity != other.opacity()
51 || d->maskedSymbolLayers != other.maskedSymbolLayers() )
52 return false;
53
54 if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
55 || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
56 return false;
57
58 return true;
59}
60
62{
63 return !( *this == other );
64}
65
67{
68 return d->enabled;
69}
70
72{
73 d->enabled = enabled;
74}
75
80
85
86
88{
89 return d->size;
90}
91
93{
94 d->size = size;
95}
96
98{
99 return d->sizeUnit;
100}
101
103{
104 d->sizeUnit = unit;
105}
106
108{
109 return d->sizeMapUnitScale;
110}
111
113{
114 d->sizeMapUnitScale = scale;
115}
116
117Qt::PenJoinStyle QgsTextMaskSettings::joinStyle() const
118{
119 return d->joinStyle;
120}
121
122void QgsTextMaskSettings::setJoinStyle( Qt::PenJoinStyle style )
123{
124 d->joinStyle = style;
125}
126
128{
129 return d->opacity;
130}
131
133{
134 d->opacity = opacity;
135}
136
138{
139 return d->paintEffect.get();
140}
141
143{
144 d->paintEffect.reset( effect );
145}
146
148{
150 {
151 context.expressionContext().setOriginalValueVariable( d->enabled );
152 d->enabled = properties.valueAsBool( QgsPalLayerSettings::Property::MaskEnabled, context.expressionContext(), d->enabled );
153 }
154
156 {
157 context.expressionContext().setOriginalValueVariable( d->size );
158 d->size = properties.valueAsDouble( QgsPalLayerSettings::Property::MaskBufferSize, context.expressionContext(), d->size );
159 }
160
162 {
163 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskBufferUnit, context.expressionContext() );
164 if ( !QgsVariantUtils::isNull( exprVal ) )
165 {
166 const QString units = exprVal.toString();
167 if ( !units.isEmpty() )
168 {
169 bool ok;
170 const Qgis::RenderUnit res = QgsUnitTypes::decodeRenderUnit( units, &ok );
171 if ( ok )
172 d->sizeUnit = res;
173 }
174 }
175 }
176
178 {
179 context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
180 const QVariant val = properties.value( QgsPalLayerSettings::Property::MaskOpacity, context.expressionContext(), d->opacity * 100 );
181 if ( !QgsVariantUtils::isNull( val ) )
182 {
183 d->opacity = val.toDouble() / 100.0;
184 }
185 }
186
188 {
189 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskJoinStyle, context.expressionContext() );
190 const QString joinstr = exprVal.toString().trimmed();
191 if ( !joinstr.isEmpty() )
192 {
193 d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
194 }
195 }
196}
197
199{
200 return QSet< QString >(); // nothing for now
201}
202
203void QgsTextMaskSettings::readXml( const QDomElement &elem )
204{
205 const QDomElement textMaskElem = elem.firstChildElement( QStringLiteral( "text-mask" ) );
206 d->enabled = textMaskElem.attribute( QStringLiteral( "maskEnabled" ), QStringLiteral( "0" ) ).toInt();
207 d->type = static_cast<QgsTextMaskSettings::MaskType>( textMaskElem.attribute( QStringLiteral( "maskType" ), QStringLiteral( "0" ) ).toInt() );
208 if ( textMaskElem.hasAttribute( QStringLiteral( "maskSize2" ) ) )
209 {
210 d->size = textMaskElem.attribute( QStringLiteral( "maskSize2" ), QStringLiteral( "1.5" ) ).toDouble();
211 }
212 else
213 {
214 // the older maskSize attribute used to be treated as 0 instead of the default 1.5 mm size when no mask
215 // settings were available, and then when the project was saved this incorrect 0 value would become an
216 // actual valid attribute in the XML. Since we can't now differentiate a valid 0 value from an accidental
217 // 0 value in older projects, we instead assume "0" as a mistake and reset it to 1.5.
218 // when the project is saved the newer maskSize2 attribute will be used and we know that a "0" value
219 // WAS an explicit user choice.
220 d->size = textMaskElem.attribute( QStringLiteral( "maskSize" ) ).toDouble();
221 if ( d->size == 0 )
222 d->size = 1.5;
223 }
224 d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textMaskElem.attribute( QStringLiteral( "maskSizeUnits" ) ) );
225 d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textMaskElem.attribute( QStringLiteral( "maskSizeMapUnitScale" ) ) );
226 d->joinStyle = static_cast< Qt::PenJoinStyle >( textMaskElem.attribute( QStringLiteral( "maskJoinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
227 d->opacity = textMaskElem.attribute( QStringLiteral( "maskOpacity" ), QStringLiteral( "1.0" ) ).toDouble();
228 const QDomElement effectElem = textMaskElem.firstChildElement( QStringLiteral( "effect" ) );
229 if ( !effectElem.isNull() )
231 else
232 setPaintEffect( nullptr );
233 d->maskedSymbolLayers = stringToSymbolLayerReferenceList( textMaskElem.attribute( QStringLiteral( "maskedSymbolLayers" ) ) );
234}
235
236QDomElement QgsTextMaskSettings::writeXml( QDomDocument &doc ) const
237{
238 QDomElement textMaskElem = doc.createElement( QStringLiteral( "text-mask" ) );
239 textMaskElem.setAttribute( QStringLiteral( "maskEnabled" ), d->enabled );
240 textMaskElem.setAttribute( QStringLiteral( "maskType" ), d->type );
241 textMaskElem.setAttribute( QStringLiteral( "maskSize" ), d->size );
242 // deliberate -- see comment in readXml
243 textMaskElem.setAttribute( QStringLiteral( "maskSize2" ), d->size );
244 textMaskElem.setAttribute( QStringLiteral( "maskSizeUnits" ), QgsUnitTypes::encodeUnit( d->sizeUnit ) );
245 textMaskElem.setAttribute( QStringLiteral( "maskSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
246 textMaskElem.setAttribute( QStringLiteral( "maskJoinStyle" ), static_cast< unsigned int >( d->joinStyle ) );
247 textMaskElem.setAttribute( QStringLiteral( "maskOpacity" ), d->opacity );
248 if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
249 d->paintEffect->saveProperties( doc, textMaskElem );
250 textMaskElem.setAttribute( QStringLiteral( "maskedSymbolLayers" ), symbolLayerReferenceListToString( d->maskedSymbolLayers ) );
251 return textMaskElem;
252}
253
254QList<QgsSymbolLayerReference> QgsTextMaskSettings::maskedSymbolLayers() const
255{
256 return d->maskedSymbolLayers;
257}
258
259void QgsTextMaskSettings::setMaskedSymbolLayers( const QList<QgsSymbolLayerReference> &maskedSymbols )
260{
261 d->maskedSymbolLayers = maskedSymbols;
262}
RenderUnit
Rendering size units.
Definition qgis.h:4839
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.
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.
static QgsPaintEffectRegistry * paintEffectRegistry()
Returns the application's paint effect registry, used for managing paint effects.
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Struct for storing maximum and minimum scales for measurements in map units.
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.
Base class for visual effects which can be applied to QPicture drawings.
virtual QVariantMap properties() const =0
Returns the properties describing the paint effect encoded in a string format.
@ MaskEnabled
Whether the mask is enabled.
@ MaskJoinStyle
Mask join style.
@ MaskBufferUnit
Mask buffer size unit.
@ MaskBufferSize
Mask buffer size.
A grouped map of multiple QgsProperty objects, each referenced by a integer key value.
QVariant value(int key, const QgsExpressionContext &context, const QVariant &defaultValue=QVariant()) const final
Returns the calculated value of the property with the specified key from within the collection.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
Contains information about the context of a rendering operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
static Qt::PenJoinStyle decodePenJoinStyle(const QString &str)
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
Container for settings relating to a selective masking around a text.
void setEnabled(bool)
Returns whether the mask is enabled.
void setMaskedSymbolLayers(const QList< QgsSymbolLayerReference > &maskedLayers)
Sets the symbol layers that will be masked by this buffer.
MaskType
Mask shape types.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
QList< QgsSymbolLayerReference > maskedSymbolLayers() const
Returns a list of references to symbol layers that are masked by this buffer.
void setSize(double size)
Sets the size of the buffer.
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
double size() const
Returns the size of the buffer.
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the buffer size.
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
void readXml(const QDomElement &elem)
Read settings from a DOM element.
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
QgsPaintEffect * paintEffect() const
Returns the current paint effect for the mask.
void setJoinStyle(Qt::PenJoinStyle style)
Sets the join style used for drawing the buffer.
double opacity() const
Returns the mask's opacity.
bool enabled() const
Returns whether the mask is enabled.
bool operator==(const QgsTextMaskSettings &other) const
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the buffer size.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the mask.
Qt::PenJoinStyle joinStyle() const
Returns the buffer join style.
void setType(MaskType type)
Sets the type of mask shape.
MaskType type() const
Returns the type of mask shape.
bool operator!=(const QgsTextMaskSettings &other) const
QgsTextMaskSettings & operator=(const QgsTextMaskSettings &other)
void setOpacity(double opacity)
Sets the mask's opacity.
static Q_INVOKABLE Qgis::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
static Q_INVOKABLE QString encodeUnit(Qgis::DistanceUnit unit)
Encodes a distance unit to a string.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
QString symbolLayerReferenceListToString(const QgsSymbolLayerReferenceList &lst)
Utilitary function to turn a QgsSymbolLayerReferenceList into a string.
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utilitary function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbo...