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