QGIS API Documentation
4.3.0-Master (16eddb2670e)
Toggle main menu visibility
Loading...
Searching...
No Matches
src
core
labeling
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
16
#include "
qgslabelplacementsettings.h
"
17
18
#include "
qgsexpressioncontext.h
"
19
#include "
qgspallabeling.h
"
20
#include "
qgspropertycollection.h
"
21
22
#include <QString>
23
24
using namespace
Qt::StringLiterals;
25
26
void
QgsLabelPlacementSettings::updateDataDefinedProperties
(
const
QgsPropertyCollection
&properties,
QgsExpressionContext
&context )
27
{
28
if
( properties.
isActive
(
QgsPalLayerSettings::Property::AllowDegradedPlacement
) )
29
{
30
context.
setOriginalValueVariable
( mAllowDegradedPlacement );
31
mAllowDegradedPlacement = properties.
valueAsBool
(
QgsPalLayerSettings::Property::AllowDegradedPlacement
, context, mAllowDegradedPlacement );
32
}
33
34
if
( properties.
isActive
(
QgsPalLayerSettings::Property::OverlapHandling
) )
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 )
39
mOverlapHandling =
Qgis::LabelOverlapHandling::PreventOverlap
;
40
else
if
( cleanedString.compare(
"allowifneeded"
_L1, Qt::CaseInsensitive ) == 0 )
41
mOverlapHandling =
Qgis::LabelOverlapHandling::AllowOverlapIfRequired
;
42
else
if
( cleanedString.compare(
"alwaysallow"
_L1, Qt::CaseInsensitive ) == 0 )
43
mOverlapHandling =
Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
;
44
}
45
46
// this property is messy - to avoid breaking old projects we need to also allow it to be treated as a boolean
47
if
( properties.
isActive
(
QgsPalLayerSettings::Property::LabelAllParts
) )
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
;
57
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelLargestPartOnly
;
58
}
59
else
if
( cleanedString.compare(
"LabelEveryPart"
_L1, Qt::CaseInsensitive ) == 0 )
60
{
61
handledAsString =
true
;
62
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
;
63
}
64
else
if
( cleanedString.compare(
"SplitLabelTextLinesOverParts"
_L1, Qt::CaseInsensitive ) == 0 )
65
{
66
handledAsString =
true
;
67
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::SplitLabelTextLinesOverParts
;
68
}
69
}
70
71
// fallback to old boolean compatibility
72
if
( !handledAsString )
73
{
74
context.
setOriginalValueVariable
( mMultiPartBehavior ==
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
);
75
if
( properties.
valueAsBool
(
QgsPalLayerSettings::Property::LabelAllParts
, context, mMultiPartBehavior ==
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
) )
76
{
77
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
;
78
}
79
else
80
{
81
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelLargestPartOnly
;
82
}
83
}
84
}
85
86
if
( properties.
isActive
(
QgsPalLayerSettings::Property::WhitespaceCollisionHandling
) )
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 )
93
mWhitespaceCollisionHandling =
Qgis::LabelWhitespaceCollisionHandling::TreatWhitespaceAsCollision
;
94
else
if
( value.compare(
"IgnoreWhitespaceCollisions"
_L1, Qt::CaseInsensitive ) == 0 )
95
mWhitespaceCollisionHandling =
Qgis::LabelWhitespaceCollisionHandling::IgnoreWhitespaceCollisions
;
96
}
97
}
98
}
Qgis::MultiPartLabelingBehavior::LabelLargestPartOnly
@ LabelLargestPartOnly
Place a label only on the largest part from the geometry.
Definition
qgis.h:1366
Qgis::MultiPartLabelingBehavior::SplitLabelTextLinesOverParts
@ SplitLabelTextLinesOverParts
Splits the label text over the parts of the geometry, such that each consecutive part is labeled with...
Definition
qgis.h:1368
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
@ LabelEveryPartWithEntireLabel
Place the (same) entire label over every part from the geometry.
Definition
qgis.h:1367
Qgis::LabelWhitespaceCollisionHandling::TreatWhitespaceAsCollision
@ TreatWhitespaceAsCollision
Treat overlapping whitespace text in labels and whitespace overlapping obstacles as collisions.
Definition
qgis.h:1282
Qgis::LabelWhitespaceCollisionHandling::IgnoreWhitespaceCollisions
@ IgnoreWhitespaceCollisions
Ignore overlapping whitespace text in labels and whitespace overlapping obstacles.
Definition
qgis.h:1283
Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
@ AllowOverlapAtNoCost
Labels may freely overlap other labels, at no cost.
Definition
qgis.h:1271
Qgis::LabelOverlapHandling::AllowOverlapIfRequired
@ AllowOverlapIfRequired
Avoids overlapping labels when possible, but permit overlaps if labels for features cannot otherwise ...
Definition
qgis.h:1270
Qgis::LabelOverlapHandling::PreventOverlap
@ PreventOverlap
Do not allow labels to overlap other labels.
Definition
qgis.h:1269
QgsAbstractPropertyCollection::valueAsBool
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.
Definition
qgspropertycollection.cpp:91
QgsAbstractPropertyCollection::valueAsString
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.
Definition
qgspropertycollection.cpp:45
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition
qgsexpressioncontext.h:491
QgsExpressionContext::setOriginalValueVariable
void setOriginalValueVariable(const QVariant &value)
Sets the original value variable value for the context.
Definition
qgsexpressioncontext.cpp:754
QgsLabelPlacementSettings::updateDataDefinedProperties
void updateDataDefinedProperties(const QgsPropertyCollection &properties, QgsExpressionContext &context)
Updates the placement settings to respect any data defined properties set within the specified proper...
Definition
qgslabelplacementsettings.cpp:26
QgsPalLayerSettings::Property::WhitespaceCollisionHandling
@ WhitespaceCollisionHandling
Whitespace collision handling.
Definition
qgspallabeling.h:243
QgsPalLayerSettings::Property::AllowDegradedPlacement
@ AllowDegradedPlacement
Allow degraded label placements.
Definition
qgspallabeling.h:241
QgsPalLayerSettings::Property::OverlapHandling
@ OverlapHandling
Overlap handling technique.
Definition
qgspallabeling.h:242
QgsPalLayerSettings::Property::LabelAllParts
@ LabelAllParts
Multipart geometry behavior.
Definition
qgspallabeling.h:218
QgsPropertyCollection
A grouped map of multiple QgsProperty objects, each referenced by an integer key value.
Definition
qgspropertycollection.h:348
QgsPropertyCollection::isActive
bool isActive(int key) const final
Returns true if the collection contains an active property with the specified key.
Definition
qgspropertycollection.cpp:286
qgsexpressioncontext.h
qgslabelplacementsettings.h
qgspallabeling.h
qgspropertycollection.h
Generated on
for QGIS API Documentation by
1.17.0