QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
qgstextbuffersettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextbuffersettings.cpp
3  -----------------
4  begin : May 2020
5  copyright : (C) Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7 
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgstextbuffersettings.h"
18 #include "qgstextrenderer_p.h"
19 #include "qgsvectorlayer.h"
20 #include "qgspallabeling.h"
21 #include "qgssymbollayerutils.h"
22 #include "qgspainting.h"
23 #include "qgspainteffectregistry.h"
24 #include "qgstextrendererutils.h"
25 
27 {
28  d = new QgsTextBufferSettingsPrivate();
29 }
30 
32  : d( other.d )
33 {
34 }
35 
37 {
38  d = other.d;
39  return *this;
40 }
41 
43 {
44 
45 }
46 
48 {
49  if ( d->enabled != other.enabled()
50  || d->size != other.size()
51  || d->sizeUnit != other.sizeUnit()
52  || d->sizeMapUnitScale != other.sizeMapUnitScale()
53  || d->color != other.color()
54  || d->opacity != other.opacity()
55  || d->fillBufferInterior != other.fillBufferInterior()
56  || d->joinStyle != other.joinStyle()
57  || d->blendMode != other.blendMode() )
58  return false;
59 
60  if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
61  || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
62  return false;
63 
64  return true;
65 }
66 
68 {
69  return !( *this == other );
70 }
71 
73 {
74  return d->enabled;
75 }
76 
78 {
79  d->enabled = enabled;
80 }
81 
83 {
84  return d->size;
85 }
86 
87 void QgsTextBufferSettings::setSize( double size )
88 {
89  d->size = size;
90 }
91 
93 {
94  return d->sizeUnit;
95 }
96 
98 {
99  d->sizeUnit = unit;
100 }
101 
103 {
104  return d->sizeMapUnitScale;
105 }
106 
108 {
109  d->sizeMapUnitScale = scale;
110 }
111 
113 {
114  return d->color;
115 }
116 
117 void QgsTextBufferSettings::setColor( const QColor &color )
118 {
119  d->color = color;
120 }
121 
123 {
124  return d->fillBufferInterior;
125 }
126 
128 {
129  d->fillBufferInterior = fill;
130 }
131 
133 {
134  return d->opacity;
135 }
136 
137 void QgsTextBufferSettings::setOpacity( double opacity )
138 {
139  d->opacity = opacity;
140 }
141 
142 Qt::PenJoinStyle QgsTextBufferSettings::joinStyle() const
143 {
144  return d->joinStyle;
145 }
146 
147 void QgsTextBufferSettings::setJoinStyle( Qt::PenJoinStyle style )
148 {
149  d->joinStyle = style;
150 }
151 
152 QPainter::CompositionMode QgsTextBufferSettings::blendMode() const
153 {
154  return d->blendMode;
155 }
156 
157 void QgsTextBufferSettings::setBlendMode( QPainter::CompositionMode mode )
158 {
159  d->blendMode = mode;
160 }
161 
163 {
164  return d->paintEffect.get();
165 }
166 
168 {
169  d->paintEffect.reset( effect );
170 }
171 
173 {
174  if ( properties.isActive( QgsPalLayerSettings::BufferDraw ) )
175  {
176  context.expressionContext().setOriginalValueVariable( d->enabled );
177  d->enabled = properties.valueAsBool( QgsPalLayerSettings::BufferDraw, context.expressionContext(), d->enabled );
178  }
179 
180  if ( properties.isActive( QgsPalLayerSettings::BufferSize ) )
181  {
182  context.expressionContext().setOriginalValueVariable( d->size );
183  d->size = properties.valueAsDouble( QgsPalLayerSettings::BufferSize, context.expressionContext(), d->size );
184  }
185 
186  QVariant exprVal = properties.value( QgsPalLayerSettings::BufferUnit, context.expressionContext() );
187  if ( exprVal.isValid() )
188  {
189  QString units = exprVal.toString();
190  if ( !units.isEmpty() )
191  {
192  bool ok;
194  if ( ok )
195  d->sizeUnit = res;
196  }
197  }
198 
199  if ( properties.isActive( QgsPalLayerSettings::BufferOpacity ) )
200  {
201  context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
202  d->opacity = properties.value( QgsPalLayerSettings::BufferOpacity, context.expressionContext(), d->opacity * 100 ).toDouble() / 100.0;
203  }
204 
205  if ( properties.isActive( QgsPalLayerSettings::BufferColor ) )
206  {
208  d->color = properties.valueAsColor( QgsPalLayerSettings::BufferColor, context.expressionContext(), d->color );
209  }
210 
212  {
213  exprVal = properties.value( QgsPalLayerSettings::BufferBlendMode, context.expressionContext() );
214  QString blendstr = exprVal.toString().trimmed();
215  if ( !blendstr.isEmpty() )
216  d->blendMode = QgsSymbolLayerUtils::decodeBlendMode( blendstr );
217  }
218 
220  {
221  exprVal = properties.value( QgsPalLayerSettings::BufferJoinStyle, context.expressionContext() );
222  QString joinstr = exprVal.toString().trimmed();
223  if ( !joinstr.isEmpty() )
224  {
225  d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
226  }
227  }
228 }
229 
231 {
232  return QSet< QString >(); // nothing for now
233 }
234 
236 {
237  // text buffer
238  double bufSize = layer->customProperty( QStringLiteral( "labeling/bufferSize" ), QVariant( 0.0 ) ).toDouble();
239 
240  // fix for buffer being keyed off of just its size in the past (<2.0)
241  QVariant drawBuffer = layer->customProperty( QStringLiteral( "labeling/bufferDraw" ), QVariant() );
242  if ( drawBuffer.isValid() )
243  {
244  d->enabled = drawBuffer.toBool();
245  d->size = bufSize;
246  }
247  else if ( bufSize != 0.0 )
248  {
249  d->enabled = true;
250  d->size = bufSize;
251  }
252  else
253  {
254  // keep bufferSize at new 1.0 default
255  d->enabled = false;
256  }
257 
258  if ( layer->customProperty( QStringLiteral( "labeling/bufferSizeUnits" ) ).toString().isEmpty() )
259  {
260  bool bufferSizeInMapUnits = layer->customProperty( QStringLiteral( "labeling/bufferSizeInMapUnits" ) ).toBool();
261  d->sizeUnit = bufferSizeInMapUnits ? QgsUnitTypes::RenderMapUnits : QgsUnitTypes::RenderMillimeters;
262  }
263  else
264  {
265  d->sizeUnit = QgsUnitTypes::decodeRenderUnit( layer->customProperty( QStringLiteral( "labeling/bufferSizeUnits" ) ).toString() );
266  }
267 
268  if ( layer->customProperty( QStringLiteral( "labeling/bufferSizeMapUnitScale" ) ).toString().isEmpty() )
269  {
270  //fallback to older property
271  double oldMin = layer->customProperty( QStringLiteral( "labeling/bufferSizeMapUnitMinScale" ), 0.0 ).toDouble();
272  d->sizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
273  double oldMax = layer->customProperty( QStringLiteral( "labeling/bufferSizeMapUnitMaxScale" ), 0.0 ).toDouble();
274  d->sizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
275  }
276  else
277  {
278  d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( layer->customProperty( QStringLiteral( "labeling/bufferSizeMapUnitScale" ) ).toString() );
279  }
280  d->color = QgsTextRendererUtils::readColor( layer, QStringLiteral( "labeling/bufferColor" ), Qt::white, false );
281  if ( layer->customProperty( QStringLiteral( "labeling/bufferOpacity" ) ).toString().isEmpty() )
282  {
283  d->opacity = ( 1 - layer->customProperty( QStringLiteral( "labeling/bufferTransp" ) ).toInt() / 100.0 ); //0 -100
284  }
285  else
286  {
287  d->opacity = ( layer->customProperty( QStringLiteral( "labeling/bufferOpacity" ) ).toDouble() );
288  }
289  d->blendMode = QgsPainting::getCompositionMode(
290  static_cast< QgsPainting::BlendMode >( layer->customProperty( QStringLiteral( "labeling/bufferBlendMode" ), QVariant( QgsPainting::BlendNormal ) ).toUInt() ) );
291  d->joinStyle = static_cast< Qt::PenJoinStyle >( layer->customProperty( QStringLiteral( "labeling/bufferJoinStyle" ), QVariant( Qt::RoundJoin ) ).toUInt() );
292 
293  d->fillBufferInterior = !layer->customProperty( QStringLiteral( "labeling/bufferNoFill" ), QVariant( false ) ).toBool();
294 
295  if ( layer->customProperty( QStringLiteral( "labeling/bufferEffect" ) ).isValid() )
296  {
297  QDomDocument doc( QStringLiteral( "effect" ) );
298  doc.setContent( layer->customProperty( QStringLiteral( "labeling/bufferEffect" ) ).toString() );
299  QDomElement effectElem = doc.firstChildElement( QStringLiteral( "effect" ) ).firstChildElement( QStringLiteral( "effect" ) );
300  setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
301  }
302  else
303  setPaintEffect( nullptr );
304 }
305 
306 void QgsTextBufferSettings::readXml( const QDomElement &elem )
307 {
308  QDomElement textBufferElem = elem.firstChildElement( QStringLiteral( "text-buffer" ) );
309  double bufSize = textBufferElem.attribute( QStringLiteral( "bufferSize" ), QStringLiteral( "0" ) ).toDouble();
310 
311  // fix for buffer being keyed off of just its size in the past (<2.0)
312  QVariant drawBuffer = textBufferElem.attribute( QStringLiteral( "bufferDraw" ) );
313  if ( drawBuffer.isValid() )
314  {
315  d->enabled = drawBuffer.toBool();
316  d->size = bufSize;
317  }
318  else if ( bufSize != 0.0 )
319  {
320  d->enabled = true;
321  d->size = bufSize;
322  }
323  else
324  {
325  // keep bufferSize at new 1.0 default
326  d->enabled = false;
327  }
328 
329  if ( !textBufferElem.hasAttribute( QStringLiteral( "bufferSizeUnits" ) ) )
330  {
331  bool bufferSizeInMapUnits = textBufferElem.attribute( QStringLiteral( "bufferSizeInMapUnits" ) ).toInt();
332  d->sizeUnit = bufferSizeInMapUnits ? QgsUnitTypes::RenderMapUnits : QgsUnitTypes::RenderMillimeters;
333  }
334  else
335  {
336  d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textBufferElem.attribute( QStringLiteral( "bufferSizeUnits" ) ) );
337  }
338 
339  if ( !textBufferElem.hasAttribute( QStringLiteral( "bufferSizeMapUnitScale" ) ) )
340  {
341  //fallback to older property
342  double oldMin = textBufferElem.attribute( QStringLiteral( "bufferSizeMapUnitMinScale" ), QStringLiteral( "0" ) ).toDouble();
343  d->sizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
344  double oldMax = textBufferElem.attribute( QStringLiteral( "bufferSizeMapUnitMaxScale" ), QStringLiteral( "0" ) ).toDouble();
345  d->sizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
346  }
347  else
348  {
349  d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textBufferElem.attribute( QStringLiteral( "bufferSizeMapUnitScale" ) ) );
350  }
351  d->color = QgsSymbolLayerUtils::decodeColor( textBufferElem.attribute( QStringLiteral( "bufferColor" ), QgsSymbolLayerUtils::encodeColor( Qt::white ) ) );
352 
353  if ( !textBufferElem.hasAttribute( QStringLiteral( "bufferOpacity" ) ) )
354  {
355  d->opacity = ( 1 - textBufferElem.attribute( QStringLiteral( "bufferTransp" ) ).toInt() / 100.0 ); //0 -100
356  }
357  else
358  {
359  d->opacity = ( textBufferElem.attribute( QStringLiteral( "bufferOpacity" ) ).toDouble() );
360  }
361 
362  d->blendMode = QgsPainting::getCompositionMode(
363  static_cast< QgsPainting::BlendMode >( textBufferElem.attribute( QStringLiteral( "bufferBlendMode" ), QString::number( QgsPainting::BlendNormal ) ).toUInt() ) );
364  d->joinStyle = static_cast< Qt::PenJoinStyle >( textBufferElem.attribute( QStringLiteral( "bufferJoinStyle" ), QString::number( Qt::RoundJoin ) ).toUInt() );
365  d->fillBufferInterior = !textBufferElem.attribute( QStringLiteral( "bufferNoFill" ), QStringLiteral( "0" ) ).toInt();
366  QDomElement effectElem = textBufferElem.firstChildElement( QStringLiteral( "effect" ) );
367  if ( !effectElem.isNull() )
369  else
370  setPaintEffect( nullptr );
371 }
372 
373 QDomElement QgsTextBufferSettings::writeXml( QDomDocument &doc ) const
374 {
375  // text buffer
376  QDomElement textBufferElem = doc.createElement( QStringLiteral( "text-buffer" ) );
377  textBufferElem.setAttribute( QStringLiteral( "bufferDraw" ), d->enabled );
378  textBufferElem.setAttribute( QStringLiteral( "bufferSize" ), d->size );
379  textBufferElem.setAttribute( QStringLiteral( "bufferSizeUnits" ), QgsUnitTypes::encodeUnit( d->sizeUnit ) );
380  textBufferElem.setAttribute( QStringLiteral( "bufferSizeMapUnitScale" ), QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
381  textBufferElem.setAttribute( QStringLiteral( "bufferColor" ), QgsSymbolLayerUtils::encodeColor( d->color ) );
382  textBufferElem.setAttribute( QStringLiteral( "bufferNoFill" ), !d->fillBufferInterior );
383  textBufferElem.setAttribute( QStringLiteral( "bufferOpacity" ), d->opacity );
384  textBufferElem.setAttribute( QStringLiteral( "bufferJoinStyle" ), static_cast< unsigned int >( d->joinStyle ) );
385  textBufferElem.setAttribute( QStringLiteral( "bufferBlendMode" ), QgsPainting::getBlendModeEnum( d->blendMode ) );
386  if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
387  d->paintEffect->saveProperties( doc, textBufferElem );
388  return textBufferElem;
389 }
QColor valueAsColor(int key, const QgsExpressionContext &context, const QColor &defaultColor=QColor(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a color.
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.
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
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.
static QgsPainting::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a BlendMode corresponding to a QPainter::CompositionMode.
Definition: qgspainting.cpp:80
static QPainter::CompositionMode getCompositionMode(QgsPainting::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a BlendMode.
Definition: qgspainting.cpp:20
BlendMode
Blending modes enum defining the available composition modes that can be used when rendering a layer.
Definition: qgspainting.h:37
@ BufferOpacity
Buffer opacity.
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 override
Returns the calculated value of the property with the specified key from within the collection.
bool isActive(int key) const override
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 QColor decodeColor(const QString &str)
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
static QPainter::CompositionMode decodeBlendMode(const QString &s)
static QString encodeColor(const QColor &color)
Container for settings relating to a text buffer.
QgsTextBufferSettings & operator=(const QgsTextBufferSettings &other)
Copy constructor.
void setFillBufferInterior(bool fill)
Sets whether the interior of the buffer will be filled in.
void setBlendMode(QPainter::CompositionMode mode)
Sets the blending mode used for drawing the buffer.
void readFromLayer(QgsVectorLayer *layer)
Reads settings from a layer's custom properties (for QGIS 2.x projects).
QSet< QString > referencedFields(const QgsRenderContext &context) const
Returns all field names referenced by the configuration (e.g.
Qt::PenJoinStyle joinStyle() const
Returns the buffer join style.
double size() const
Returns the size of the buffer.
void setColor(const QColor &color)
Sets the color for the buffer.
void setSizeMapUnitScale(const QgsMapUnitScale &scale)
Sets the map unit scale object for the buffer size.
void setOpacity(double opacity)
Sets the buffer opacity.
QgsMapUnitScale sizeMapUnitScale() const
Returns the map unit scale object for the buffer size.
bool enabled() const
Returns whether the buffer is enabled.
double opacity() const
Returns the buffer opacity.
bool operator!=(const QgsTextBufferSettings &other) const
QDomElement writeXml(QDomDocument &doc) const
Write settings into a DOM element.
bool fillBufferInterior() const
Returns whether the interior of the buffer will be filled in.
void setEnabled(bool enabled)
Sets whether the text buffer will be drawn.
QgsUnitTypes::RenderUnit sizeUnit() const
Returns the units for the buffer size.
void setPaintEffect(QgsPaintEffect *effect)
Sets the current paint effect for the buffer.
const QgsPaintEffect * paintEffect() const
Returns the current paint effect for the buffer.
QColor color() const
Returns the color of the buffer.
bool operator==(const QgsTextBufferSettings &other) const
QPainter::CompositionMode blendMode() const
Returns the blending mode used for drawing the buffer.
void setSizeUnit(QgsUnitTypes::RenderUnit unit)
Sets the units used for the buffer size.
void updateDataDefinedProperties(QgsRenderContext &context, const QgsPropertyCollection &properties)
Updates the format by evaluating current values of data defined properties.
void setJoinStyle(Qt::PenJoinStyle style)
Sets the join style used for drawing the buffer.
void setSize(double size)
Sets the size of the buffer.
void readXml(const QDomElement &elem)
Read settings from a DOM element.
static QColor readColor(QgsVectorLayer *layer, const QString &property, const QColor &defaultColor=Qt::black, bool withAlpha=true)
Converts an encoded color value from a layer property.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
Represents a vector layer which manages a vector based data sets.