QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgslabelplacementsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelplacementsettings.cpp
3 --------------------------
4 Date : May 2022
5 Copyright : (C) 2022 by 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
17
19#include "qgspallabeling.h"
21
22#include <QString>
23
24using namespace Qt::StringLiterals;
25
27{
29 {
30 context.setOriginalValueVariable( mAllowDegradedPlacement );
31 mAllowDegradedPlacement = properties.valueAsBool( QgsPalLayerSettings::Property::AllowDegradedPlacement, context, mAllowDegradedPlacement );
32 }
33
35 {
36 const QString handlingString = properties.valueAsString( QgsPalLayerSettings::Property::OverlapHandling, context );
37 const QString cleanedString = handlingString.trimmed();
38 if ( cleanedString.compare( "prevent"_L1, Qt::CaseInsensitive ) == 0 )
40 else if ( cleanedString.compare( "allowifneeded"_L1, Qt::CaseInsensitive ) == 0 )
42 else if ( cleanedString.compare( "alwaysallow"_L1, Qt::CaseInsensitive ) == 0 )
44 }
45
46 // this property is messy - to avoid breaking old projects we need to also allow it to be treated as a boolean
48 {
49 const QString stringValue = properties.valueAsString( QgsPalLayerSettings::Property::LabelAllParts, context );
50 bool handledAsString = false;
51 if ( !stringValue.isEmpty() )
52 {
53 const QString cleanedString = stringValue.trimmed();
54 if ( cleanedString.compare( "LargestPartOnly"_L1, Qt::CaseInsensitive ) == 0 )
55 {
56 handledAsString = true;
58 }
59 else if ( cleanedString.compare( "LabelEveryPart"_L1, Qt::CaseInsensitive ) == 0 )
60 {
61 handledAsString = true;
63 }
64 else if ( cleanedString.compare( "SplitLabelTextLinesOverParts"_L1, Qt::CaseInsensitive ) == 0 )
65 {
66 handledAsString = true;
68 }
69 }
70
71 // fallback to old boolean compatibility
72 if ( !handledAsString )
73 {
76 {
78 }
79 else
80 {
82 }
83 }
84 }
85
87 {
88 bool ok = false;
89 const QString value = properties.valueAsString( QgsPalLayerSettings::Property::WhitespaceCollisionHandling, context, QString(), &ok ).trimmed();
90 if ( ok )
91 {
92 if ( value.compare( "TreatWhitespaceAsCollision"_L1, Qt::CaseInsensitive ) == 0 )
94 else if ( value.compare( "IgnoreWhitespaceCollisions"_L1, Qt::CaseInsensitive ) == 0 )
96 }
97 }
98}
@ LabelLargestPartOnly
Place a label only on the largest part from the geometry.
Definition qgis.h:1284
@ SplitLabelTextLinesOverParts
Splits the label text over the parts of the geometry, such that each consecutive part is labeled with...
Definition qgis.h:1286
@ LabelEveryPartWithEntireLabel
Place the (same) entire label over every part from the geometry.
Definition qgis.h:1285
@ TreatWhitespaceAsCollision
Treat overlapping whitespace text in labels and whitespace overlapping obstacles as collisions.
Definition qgis.h:1200
@ IgnoreWhitespaceCollisions
Ignore overlapping whitespace text in labels and whitespace overlapping obstacles.
Definition qgis.h:1201
@ AllowOverlapAtNoCost
Labels may freely overlap other labels, at no cost.
Definition qgis.h:1189
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
Definition qgis.h:1188
@ PreventOverlap
Do not allow labels to overlap other labels.
Definition qgis.h:1187
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.
QString valueAsString(int key, const QgsExpressionContext &context, const QString &defaultString=QString(), bool *ok=nullptr) const
Calculates the current value of the property with the specified key and interprets it as a string.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
void updateDataDefinedProperties(const QgsPropertyCollection &properties, QgsExpressionContext &context)
Updates the placement settings to respect any data defined properties set within the specified proper...
@ WhitespaceCollisionHandling
Whitespace collision handling.
@ AllowDegradedPlacement
Allow degraded label placements.
@ OverlapHandling
Overlap handling technique.
@ LabelAllParts
Multipart geometry behavior.
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.