QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
18
19#include "qgsapplication.h"
20#include "qgscolorutils.h"
22#include "qgspainting.h"
23#include "qgspallabeling.h"
24#include "qgssymbollayerutils.h"
25#include "qgstextrenderer_p.h"
27#include "qgsunittypes.h"
28#include "qgsvectorlayer.h"
29
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
35{
36 d = new QgsTextBufferSettingsPrivate();
37}
38
40 : d( other.d )
41{
42}
44 : d( std::move( other.d ) )
45{
46}
47
49{
50 if ( &other == this )
51 return *this;
52
53 d = other.d;
54 return *this;
55}
56
58{
59 if ( &other == this )
60 return *this;
61
62 d = std::move( other.d );
63 return *this;
64}
65
70
72{
73 if ( d->enabled != other.enabled()
74 || d->size != other.size()
75 || d->sizeUnit != other.sizeUnit()
76 || d->sizeMapUnitScale != other.sizeMapUnitScale()
77 || d->color != other.color()
78 || d->opacity != other.opacity()
79 || d->fillBufferInterior != other.fillBufferInterior()
80 || d->joinStyle != other.joinStyle()
81 || d->blendMode != other.blendMode() )
82 return false;
83
84 if ( static_cast< bool >( d->paintEffect ) != static_cast< bool >( other.paintEffect() )
85 || ( d->paintEffect && d->paintEffect->properties() != other.paintEffect()->properties() ) )
86 return false;
87
88 return true;
89}
90
92{
93 return !( *this == other );
94}
95
97{
98 return d->enabled;
99}
100
102{
103 d->enabled = enabled;
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
137{
138 return d->color;
139}
140
142{
143 d->color = color;
144}
145
147{
148 return d->fillBufferInterior;
149}
150
152{
153 d->fillBufferInterior = fill;
154}
155
157{
158 return d->opacity;
159}
160
162{
163 d->opacity = opacity;
164}
165
166Qt::PenJoinStyle QgsTextBufferSettings::joinStyle() const
167{
168 return d->joinStyle;
169}
170
171void QgsTextBufferSettings::setJoinStyle( Qt::PenJoinStyle style )
172{
173 d->joinStyle = style;
174}
175
176QPainter::CompositionMode QgsTextBufferSettings::blendMode() const
177{
178 return d->blendMode;
179}
180
181void QgsTextBufferSettings::setBlendMode( QPainter::CompositionMode mode )
182{
183 d->blendMode = mode;
184}
185
187{
188 return d->paintEffect.get();
189}
190
192{
193 d->paintEffect.reset( effect );
194}
195
197{
199 {
200 context.expressionContext().setOriginalValueVariable( d->enabled );
201 d->enabled = properties.valueAsBool( QgsPalLayerSettings::Property::BufferDraw, context.expressionContext(), d->enabled );
202 }
203
205 {
206 context.expressionContext().setOriginalValueVariable( d->size );
207 d->size = properties.valueAsDouble( QgsPalLayerSettings::Property::BufferSize, context.expressionContext(), d->size );
208 }
209
210 QVariant exprVal = properties.value( QgsPalLayerSettings::Property::BufferUnit, context.expressionContext() );
211 if ( !QgsVariantUtils::isNull( exprVal ) )
212 {
213 const QString units = exprVal.toString();
214 if ( !units.isEmpty() )
215 {
216 bool ok;
217 const Qgis::RenderUnit res = QgsUnitTypes::decodeRenderUnit( units, &ok );
218 if ( ok )
219 d->sizeUnit = res;
220 }
221 }
222
224 {
225 context.expressionContext().setOriginalValueVariable( d->opacity * 100 );
226 const QVariant val = properties.value( QgsPalLayerSettings::Property::BufferOpacity, context.expressionContext(), d->opacity * 100 );
227 if ( !QgsVariantUtils::isNull( val ) )
228 {
229 d->opacity = val.toDouble() / 100.0;
230 }
231 }
232
234 {
236 d->color = properties.valueAsColor( QgsPalLayerSettings::Property::BufferColor, context.expressionContext(), d->color );
237 }
238
240 {
242 const QString blendstr = exprVal.toString().trimmed();
243 if ( !blendstr.isEmpty() )
244 d->blendMode = QgsSymbolLayerUtils::decodeBlendMode( blendstr );
245 }
246
248 {
250 const QString joinstr = exprVal.toString().trimmed();
251 if ( !joinstr.isEmpty() )
252 {
253 d->joinStyle = QgsSymbolLayerUtils::decodePenJoinStyle( joinstr );
254 }
255 }
256}
257
259{
260 return QSet< QString >(); // nothing for now
261}
262
264{
265 // text buffer
266 const double bufSize = layer->customProperty( u"labeling/bufferSize"_s, QVariant( 0.0 ) ).toDouble();
267
268 // fix for buffer being keyed off of just its size in the past (<2.0)
269 const QVariant drawBuffer = layer->customProperty( u"labeling/bufferDraw"_s, QVariant() );
270 if ( drawBuffer.isValid() )
271 {
272 d->enabled = drawBuffer.toBool();
273 d->size = bufSize;
274 }
275 else if ( bufSize != 0.0 )
276 {
277 d->enabled = true;
278 d->size = bufSize;
279 }
280 else
281 {
282 // keep bufferSize at new 1.0 default
283 d->enabled = false;
284 }
285
286 if ( layer->customProperty( u"labeling/bufferSizeUnits"_s ).toString().isEmpty() )
287 {
288 const bool bufferSizeInMapUnits = layer->customProperty( u"labeling/bufferSizeInMapUnits"_s ).toBool();
289 d->sizeUnit = bufferSizeInMapUnits ? Qgis::RenderUnit::MapUnits : Qgis::RenderUnit::Millimeters;
290 }
291 else
292 {
293 d->sizeUnit = QgsUnitTypes::decodeRenderUnit( layer->customProperty( u"labeling/bufferSizeUnits"_s ).toString() );
294 }
295
296 if ( layer->customProperty( u"labeling/bufferSizeMapUnitScale"_s ).toString().isEmpty() )
297 {
298 //fallback to older property
299 const double oldMin = layer->customProperty( u"labeling/bufferSizeMapUnitMinScale"_s, 0.0 ).toDouble();
300 d->sizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
301 const double oldMax = layer->customProperty( u"labeling/bufferSizeMapUnitMaxScale"_s, 0.0 ).toDouble();
302 d->sizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
303 }
304 else
305 {
306 d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( layer->customProperty( u"labeling/bufferSizeMapUnitScale"_s ).toString() );
307 }
308 d->color = QgsTextRendererUtils::readColor( layer, u"labeling/bufferColor"_s, Qt::white, false );
309 if ( layer->customProperty( u"labeling/bufferOpacity"_s ).toString().isEmpty() )
310 {
311 d->opacity = ( 1 - layer->customProperty( u"labeling/bufferTransp"_s ).toInt() / 100.0 ); //0 -100
312 }
313 else
314 {
315 d->opacity = ( layer->customProperty( u"labeling/bufferOpacity"_s ).toDouble() );
316 }
317 d->blendMode = QgsPainting::getCompositionMode(
318 static_cast< Qgis::BlendMode >( layer->customProperty( u"labeling/bufferBlendMode"_s, QVariant( static_cast< int >( Qgis::BlendMode::Normal ) ) ).toUInt() ) );
319 d->joinStyle = static_cast< Qt::PenJoinStyle >( layer->customProperty( u"labeling/bufferJoinStyle"_s, QVariant( Qt::RoundJoin ) ).toUInt() );
320
321 d->fillBufferInterior = !layer->customProperty( u"labeling/bufferNoFill"_s, QVariant( false ) ).toBool();
322
323 if ( layer->customProperty( u"labeling/bufferEffect"_s ).isValid() )
324 {
325 QDomDocument doc( u"effect"_s );
326 doc.setContent( layer->customProperty( u"labeling/bufferEffect"_s ).toString() );
327 const QDomElement effectElem = doc.firstChildElement( u"effect"_s ).firstChildElement( u"effect"_s );
328 setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
329 }
330 else
331 setPaintEffect( nullptr );
332}
333
334void QgsTextBufferSettings::readXml( const QDomElement &elem )
335{
336 const QDomElement textBufferElem = elem.firstChildElement( u"text-buffer"_s );
337 const double bufSize = textBufferElem.attribute( u"bufferSize"_s, u"0"_s ).toDouble();
338
339 // fix for buffer being keyed off of just its size in the past (<2.0)
340 const QVariant drawBuffer = textBufferElem.attribute( u"bufferDraw"_s );
341 if ( drawBuffer.isValid() )
342 {
343 d->enabled = drawBuffer.toBool();
344 d->size = bufSize;
345 }
346 else if ( bufSize != 0.0 )
347 {
348 d->enabled = true;
349 d->size = bufSize;
350 }
351 else
352 {
353 // keep bufferSize at new 1.0 default
354 d->enabled = false;
355 }
356
357 if ( !textBufferElem.hasAttribute( u"bufferSizeUnits"_s ) )
358 {
359 const bool bufferSizeInMapUnits = textBufferElem.attribute( u"bufferSizeInMapUnits"_s ).toInt();
360 d->sizeUnit = bufferSizeInMapUnits ? Qgis::RenderUnit::MapUnits : Qgis::RenderUnit::Millimeters;
361 }
362 else
363 {
364 d->sizeUnit = QgsUnitTypes::decodeRenderUnit( textBufferElem.attribute( u"bufferSizeUnits"_s ) );
365 }
366
367 if ( !textBufferElem.hasAttribute( u"bufferSizeMapUnitScale"_s ) )
368 {
369 //fallback to older property
370 const double oldMin = textBufferElem.attribute( u"bufferSizeMapUnitMinScale"_s, u"0"_s ).toDouble();
371 d->sizeMapUnitScale.minScale = oldMin != 0 ? 1.0 / oldMin : 0;
372 const double oldMax = textBufferElem.attribute( u"bufferSizeMapUnitMaxScale"_s, u"0"_s ).toDouble();
373 d->sizeMapUnitScale.maxScale = oldMax != 0 ? 1.0 / oldMax : 0;
374 }
375 else
376 {
377 d->sizeMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( textBufferElem.attribute( u"bufferSizeMapUnitScale"_s ) );
378 }
379 d->color = QgsColorUtils::colorFromString( textBufferElem.attribute( u"bufferColor"_s, QgsColorUtils::colorToString( Qt::white ) ) );
380
381 if ( !textBufferElem.hasAttribute( u"bufferOpacity"_s ) )
382 {
383 d->opacity = ( 1 - textBufferElem.attribute( u"bufferTransp"_s ).toInt() / 100.0 ); //0 -100
384 }
385 else
386 {
387 d->opacity = ( textBufferElem.attribute( u"bufferOpacity"_s ).toDouble() );
388 }
389
390 d->blendMode = QgsPainting::getCompositionMode(
391 static_cast< Qgis::BlendMode >( textBufferElem.attribute( u"bufferBlendMode"_s, QString::number( static_cast< int >( Qgis::BlendMode::Normal ) ) ).toUInt() ) );
392 d->joinStyle = static_cast< Qt::PenJoinStyle >( textBufferElem.attribute( u"bufferJoinStyle"_s, QString::number( Qt::RoundJoin ) ).toUInt() );
393 d->fillBufferInterior = !textBufferElem.attribute( u"bufferNoFill"_s, u"0"_s ).toInt();
394 const QDomElement effectElem = textBufferElem.firstChildElement( u"effect"_s );
395 if ( !effectElem.isNull() )
396 setPaintEffect( QgsApplication::paintEffectRegistry()->createEffect( effectElem ) );
397 else
398 setPaintEffect( nullptr );
399}
400
401QDomElement QgsTextBufferSettings::writeXml( QDomDocument &doc ) const
402{
403 // text buffer
404 QDomElement textBufferElem = doc.createElement( u"text-buffer"_s );
405 textBufferElem.setAttribute( u"bufferDraw"_s, d->enabled );
406 textBufferElem.setAttribute( u"bufferSize"_s, d->size );
407 textBufferElem.setAttribute( u"bufferSizeUnits"_s, QgsUnitTypes::encodeUnit( d->sizeUnit ) );
408 textBufferElem.setAttribute( u"bufferSizeMapUnitScale"_s, QgsSymbolLayerUtils::encodeMapUnitScale( d->sizeMapUnitScale ) );
409 textBufferElem.setAttribute( u"bufferColor"_s, QgsColorUtils::colorToString( d->color ) );
410 textBufferElem.setAttribute( u"bufferNoFill"_s, !d->fillBufferInterior );
411 textBufferElem.setAttribute( u"bufferOpacity"_s, d->opacity );
412 textBufferElem.setAttribute( u"bufferJoinStyle"_s, static_cast< unsigned int >( d->joinStyle ) );
413 textBufferElem.setAttribute( u"bufferBlendMode"_s, static_cast< int >( QgsPainting::getBlendModeEnum( d->blendMode ) ) );
414 if ( d->paintEffect && !QgsPaintEffectRegistry::isDefaultStack( d->paintEffect.get() ) )
415 d->paintEffect->saveProperties( doc, textBufferElem );
416 return textBufferElem;
417}
BlendMode
Blending modes defining the available composition modes that can be used when painting.
Definition qgis.h:5002
@ Normal
Normal.
Definition qgis.h:5003
RenderUnit
Rendering size units.
Definition qgis.h:5255
@ Millimeters
Millimeters.
Definition qgis.h:5256
@ MapUnits
Map units.
Definition qgis.h:5257
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.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
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.
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 Qgis::BlendMode getBlendModeEnum(QPainter::CompositionMode blendMode)
Returns a Qgis::BlendMode corresponding to a QPainter::CompositionMode.
static QPainter::CompositionMode getCompositionMode(Qgis::BlendMode blendMode)
Returns a QPainter::CompositionMode corresponding to a Qgis::BlendMode.
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)
static QPainter::CompositionMode decodeBlendMode(const QString &s)
static QString encodeColor(const QColor &color)
QgsTextBufferSettings & operator=(const QgsTextBufferSettings &other)
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.
Qgis::RenderUnit sizeUnit() const
Returns the units for the buffer size.
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.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units used for the buffer size.
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.
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 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 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.
Represents a vector layer which manages a vector based dataset.