QGIS API Documentation 3.99.0-Master (8e76e220402)
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
25#include <QString>
26
27using namespace Qt::StringLiterals;
28
30{
31 d = new QgsTextMaskSettingsPrivate();
32}
33
35
37 : d( other.d )
38{
39}
40
42 : d( std::move( other.d ) )
43{
44}
45
47{
48 if ( &other == this )
49 return *this;
50
51 d = other.d;
52 return *this;
53}
54
56{
57 if ( &other == this )
58 return *this;
59
60 d = std::move( other.d );
61 return *this;
62}
63
65{
66 if ( d->enabled != other.enabled()
67 || d->type != other.type()
68 || d->size != other.size()
69 || d->sizeUnit != other.sizeUnit()
70 || d->sizeMapUnitScale != other.sizeMapUnitScale()
71 || d->joinStyle != other.joinStyle()
72 || d->opacity != other.opacity()
73 || d->maskedSymbolLayers != other.maskedSymbolLayers() )
74 return false;
75
76 if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
77 || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
78 return false;
79
80 return true;
81}
82
84{
85 return !( *this == other );
86}
87
89{
90 return d->enabled;
91}
92
94{
95 d->enabled = enabled;
96}
97
99{
100 return d->type;
101}
102
107
108
110{
111 return d->size;
112}
113
115{
116 d->size = size;
117}
118
120{
121 return d->sizeUnit;
122}
123
125{
126 d->sizeUnit = unit;
127}
128
130{
131 return d->sizeMapUnitScale;
132}
133
135{
136 d->sizeMapUnitScale = scale;
137}
138
139Qt::PenJoinStyle QgsTextMaskSettings::joinStyle() const
140{
141 return d->joinStyle;
142}
143
144void QgsTextMaskSettings::setJoinStyle( Qt::PenJoinStyle style )
145{
146 d->joinStyle = style;
147}
148
150{
151 return d->opacity;
152}
153
155{
156 d->opacity = opacity;
157}
158
160{
161 return d->paintEffect.get();
162}
163
165{
166 d->paintEffect.reset( effect );
167}
168
170{
172 {
173 context.expressionContext().setOriginalValueVariable( d->enabled );
174 d->enabled = properties.valueAsBool( QgsPalLayerSettings::Property::MaskEnabled, context.expressionContext(), d->enabled );
175 }
176
178 {
179 context.expressionContext().setOriginalValueVariable( d->size );
180 d->size = properties.valueAsDouble( QgsPalLayerSettings::Property::MaskBufferSize, context.expressionContext(), d->size );
181 }
182
184 {
185 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskBufferUnit, context.expressionContext() );
186 if ( !QgsVariantUtils::isNull( exprVal ) )
187 {
188 const QString units = exprVal.toString();
189 if ( !units.isEmpty() )
190 {
191 bool ok;
192 const Qgis::RenderUnit res = QgsUnitTypes::decodeRenderUnit( units, &ok );
193 if ( ok )
194 d->sizeUnit = res;
195 }
196 }
197 }
198
200 {
201 context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
202 const QVariant val = properties.value( QgsPalLayerSettings::Property::MaskOpacity, context.expressionContext(), d->opacity * 100 );
203 if ( !QgsVariantUtils::isNull( val ) )
204 {
205 d->opacity = val.toDouble() / 100.0;
206 }
207 }
208
210 {
211 const QVariant exprVal = properties.value( QgsPalLayerSettings::Property::MaskJoinStyle, context.expressionContext() );
212 const QString joinstr = exprVal.toString().trimmed();
213 if ( !joinstr.isEmpty() )
214 {
215 d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
216 }
217 }
218}
219
221{
222 return QSet< QString >(); // nothing for now
223}
224
225void QgsTextMaskSettings::readXml( const QDomElement &elem )
226{
227 const QDomElement textMaskElem = elem.firstChildElement( u"text-mask"_s );
228 d->enabled = textMaskElem.attribute( u"maskEnabled"_s, u"0"_s ).toInt();
229 d->type = static_cast<QgsTextMaskSettings::MaskType>( textMaskElem.attribute( u"maskType"_s, u"0"_s ).toInt() );
230 if ( textMaskElem.hasAttribute( u"maskSize2"_s ) )
231 {
232 d->size = textMaskElem.attribute( u"maskSize2"_s, u"1.5"_s ).toDouble();
233 }
234 else
235 {
236 // the older maskSize attribute used to be treated as 0 instead of the default 1.5 mm size when no mask
237 // settings were available, and then when the project was saved this incorrect 0 value would become an
238 // actual valid attribute in the XML. Since we can't now differentiate a valid 0 value from an accidental
239 // 0 value in older projects, we instead assume "0" as a mistake and reset it to 1.5.
240 // when the project is saved the newer maskSize2 attribute will be used and we know that a "0" value
241 // WAS an explicit user choice.
242 d->size = textMaskElem.attribute( u"maskSize"_s ).toDouble();
243 if ( d->size == 0 )
244 d->size = 1.5;
245 }
246 d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textMaskElem.attribute( u"maskSizeUnits"_s ) );
247 d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textMaskElem.attribute( u"maskSizeMapUnitScale"_s ) );
248 d->joinStyle = static_cast< Qt::PenJoinStyle >( textMaskElem.attribute( u"maskJoinStyle"_s, QString::number( Qt::RoundJoin ) ).toUInt() );
249 d->opacity = textMaskElem.attribute( u"maskOpacity"_s, u"1.0"_s ).toDouble();
250 const QDomElement effectElem = textMaskElem.firstChildElement( u"effect"_s );
251 if ( !effectElem.isNull() )
252 setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
253 else
254 setPaintEffect( nullptr );
255 d->maskedSymbolLayers = stringToSymbolLayerReferenceList( textMaskElem.attribute( u"maskedSymbolLayers"_s ) );
256}
257
258QDomElement QgsTextMaskSettings::writeXml( QDomDocument &doc ) const
259{
260 QDomElement textMaskElem = doc.createElement( u"text-mask"_s );
261 textMaskElem.setAttribute( u"maskEnabled"_s, d->enabled );
262 textMaskElem.setAttribute( u"maskType"_s, d->type );
263 textMaskElem.setAttribute( u"maskSize"_s, d->size );
264 // deliberate -- see comment in readXml
265 textMaskElem.setAttribute( u"maskSize2"_s, d->size );
266 textMaskElem.setAttribute( u"maskSizeUnits"_s, QgsUnitTypes::encodeUnit( d->sizeUnit ) );
267 textMaskElem.setAttribute( u"maskSizeMapUnitScale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
268 textMaskElem.setAttribute( u"maskJoinStyle"_s, static_cast< unsigned int >( d->joinStyle ) );
269 textMaskElem.setAttribute( u"maskOpacity"_s, d->opacity );
270 if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
271 d->paintEffect->saveProperties( doc, textMaskElem );
272 textMaskElem.setAttribute( u"maskedSymbolLayers"_s, symbolLayerReferenceListToString( d->maskedSymbolLayers ) );
273 return textMaskElem;
274}
275
276QList<QgsSymbolLayerReference> QgsTextMaskSettings::maskedSymbolLayers() const
277{
278 return d->maskedSymbolLayers;
279}
280
281void QgsTextMaskSettings::setMaskedSymbolLayers( const QList<QgsSymbolLayerReference> &maskedSymbols )
282{
283 d->maskedSymbolLayers = maskedSymbols;
284}
RenderUnit
Rendering size units.
Definition qgis.h:5290
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...