QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
18#include "qgsapplication.h"
20#include "qgspallabeling.h"
21#include "qgssymbollayerutils.h"
22#include "qgstextrenderer_p.h"
23#include "qgsunittypes.h"
24
26{
27 d = new QgsTextMaskSettingsPrivate();
28}
29
31
33 : d( other.d )
34{
35}
36
38 : d( std::move( other.d ) )
39{
40}
41
43{
44 if ( &other == this )
45 return *this;
46
47 d = other.d;
48 return *this;
49}
50
52{
53 if ( &other == this )
54 return *this;
55
56 d = std::move( other.d );
57 return *this;
58}
59
61{
62 if ( d->enabled != other.enabled()
63 || d->type != other.type()
64 || d->size != other.size()
65 || d->sizeUnit != other.sizeUnit()
66 || d->sizeMapUnitScale != other.sizeMapUnitScale()
67 || d->joinStyle != other.joinStyle()
68 || d->opacity != other.opacity()
69 || d->maskedSymbolLayers != other.maskedSymbolLayers() )
70 return false;
71
72 if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
73 || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
74 return false;
75
76 return true;
77}
78
80{
81 return !( *this == other );
82}
83
85{
86 return d->enabled;
87}
88
90{
91 d->enabled = enabled;
92}
93
98
103
104
106{
107 return d->size;
108}
109
111{
112 d->size = size;
113}
114
116{
117 return d->sizeUnit;
118}
119
121{
122 d->sizeUnit = unit;
123}
124
126{
127 return d->sizeMapUnitScale;
128}
129
131{
132 d->sizeMapUnitScale = scale;
133}
134
135Qt::PenJoinStyle QgsTextMaskSettings::joinStyle() const
136{
137 return d->joinStyle;
138}
139
140void QgsTextMaskSettings::setJoinStyle( Qt::PenJoinStyle style )
141{
142 d->joinStyle = style;
143}
144
146{
147 return d->opacity;
148}
149
151{
152 d->opacity = opacity;
153}
154
156{
157 return d->paintEffect.get();
158}
159
161{
162 d->paintEffect.reset( effect );
163}
164
166{
168 {
169 context.expressionContext().setOriginalValueVariable( d->enabled );
170 d->enabled = properties.valueAsBool( QgsPalLayerSettings::Property::MaskEnabled, context.expressionContext(), d->enabled );
171 }
172
174 {
175 context.expressionContext().setOriginalValueVariable( d->size );
176 d->size = properties.valueAsDouble( QgsPalLayerSettings::Property::MaskBufferSize, context.expressionContext(), d->size );
177 }
178
180 {
181 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskBufferUnit, context.expressionContext() );
182 if ( !QgsVariantUtils::isNull( exprVal ) )
183 {
184 const QString units = exprVal.toString();
185 if ( !units.isEmpty() )
186 {
187 bool ok;
188 const Qgis::RenderUnit res = QgsUnitTypes::decodeRenderUnit( units, &ok );
189 if ( ok )
190 d->sizeUnit = res;
191 }
192 }
193 }
194
196 {
197 context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
198 const QVariant val = properties.value( QgsPalLayerSettings::Property::MaskOpacity, context.expressionContext(), d->opacity * 100 );
199 if ( !QgsVariantUtils::isNull( val ) )
200 {
201 d->opacity = val.toDouble() / 100.0;
202 }
203 }
204
206 {
207 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskJoinStyle, context.expressionContext() );
208 const QString joinstr = exprVal.toString().trimmed();
209 if ( !joinstr.isEmpty() )
210 {
211 d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
212 }
213 }
214}
215
217{
218 return QSet< QString >(); // nothing for now
219}
220
221void QgsTextMaskSettings::readXml( const QDomElement &elem )
222{
223 const QDomElement textMaskElem = elem.firstChildElement( QStringLiteral( "text-mask" ) );
224 d->enabled = textMaskElem.attribute( QStringLiteral( "maskEnabled" ), QStringLiteral( "0" ) ).toInt();
225 d->type = static_cast<QgsTextMaskSettings::MaskType>( textMaskElem.attribute( QStringLiteral( "maskType" ), QStringLiteral( "0" ) ).toInt() );
226 if ( textMaskElem.hasAttribute( QStringLiteral( "maskSize2" ) ) )
227 {
228 d->size = textMaskElem.attribute( QStringLiteral( "maskSize2" ), QStringLiteral( "1.5" ) ).toDouble();
229 }
230 else
231 {
232 // the older maskSize attribute used to be treated as 0 instead of the default 1.5 mm size when no mask
233 // settings were available, and then when the project was saved this incorrect 0 value would become an
234 // actual valid attribute in the XML. Since we can't now differentiate a valid 0 value from an accidental
235 // 0 value in older projects, we instead assume "0" as a mistake and reset it to 1.5.
236 // when the project is saved the newer maskSize2 attribute will be used and we know that a "0" value
237 // WAS an explicit user choice.
238 d->size = textMaskElem.attribute( QStringLiteral( "maskSize" ) ).toDouble();
239 if ( d->size == 0 )
240 d->size = 1.5;
241 }
242 d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textMaskElem.attribute( QStringLiteral( "maskSizeUnits" ) ) );
243 d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textMaskElem.attribute( QStringLiteral( "maskSizeMapUnitScale" ) ) );
244 d->joinStyle = static_cast< Qt::PenJoinStyle >( textMaskElem.attribute( QStringLiteral( "maskJoinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
245 d->opacity = textMaskElem.attribute( QStringLiteral( "maskOpacity" ), QStringLiteral( "1.0" ) ).toDouble();
246 const QDomElement effectElem = textMaskElem.firstChildElement( QStringLiteral( "effect" ) );
247 if ( !effectElem.isNull() )
248 setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
249 else
250 setPaintEffect( nullptr );
251 d->maskedSymbolLayers = stringToSymbolLayerReferenceList( textMaskElem.attribute( QStringLiteral( "maskedSymbolLayers" ) ) );
252}
253
254QDomElement QgsTextMaskSettings::writeXml( QDomDocument &doc ) const
255{
256 QDomElement textMaskElem = doc.createElement( QStringLiteral( "text-mask" ) );
257 textMaskElem.setAttribute( QStringLiteral( "maskEnabled" ), d->enabled );
258 textMaskElem.setAttribute( QStringLiteral( "maskType" ), d->type );
259 textMaskElem.setAttribute( QStringLiteral( "maskSize" ), d->size );
260 // deliberate -- see comment in readXml
261 textMaskElem.setAttribute( QStringLiteral( "maskSize2" ), d->size );
262 textMaskElem.setAttribute( QStringLiteral( "maskSizeUnits" ), QgsUnitTypes::encodeUnit( d->sizeUnit ) );
263 textMaskElem.setAttribute( QStringLiteral( "maskSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
264 textMaskElem.setAttribute( QStringLiteral( "maskJoinStyle" ), static_cast< unsigned int >( d->joinStyle ) );
265 textMaskElem.setAttribute( QStringLiteral( "maskOpacity" ), d->opacity );
266 if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
267 d->paintEffect->saveProperties( doc, textMaskElem );
268 textMaskElem.setAttribute( QStringLiteral( "maskedSymbolLayers" ), symbolLayerReferenceListToString( d->maskedSymbolLayers ) );
269 return textMaskElem;
270}
271
272QList<QgsSymbolLayerReference> QgsTextMaskSettings::maskedSymbolLayers() const
273{
274 return d->maskedSymbolLayers;
275}
276
277void QgsTextMaskSettings::setMaskedSymbolLayers( const QList<QgsSymbolLayerReference> &maskedSymbols )
278{
279 d->maskedSymbolLayers = maskedSymbols;
280}
RenderUnit
Rendering size units.
Definition qgis.h:5183
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.
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.
@ MaskBufferUnit
Mask buffer size unit.
@ MaskBufferSize
Mask buffer size.
A grouped map of multiple QgsProperty objects, each referenced by an 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)
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)
Utility function to turn a QgsSymbolLayerReferenceList into a string.
QgsSymbolLayerReferenceList stringToSymbolLayerReferenceList(const QString &str)
Utility function to parse a string originated from symbolLayerReferenceListToString into a QgsSymbolL...