QGIS API Documentation
4.3.0-Master (bd2f5e21fd1)
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 "
qgsdatadefinedutils.h
"
19
#include "
qgsexpressioncontext.h
"
20
#include "
qgspallabeling.h
"
21
#include "
qgspropertycollection.h
"
22
23
#include <QString>
24
25
using namespace
Qt::StringLiterals;
26
27
void
QgsLabelPlacementSettings::updateDataDefinedProperties
(
const
QgsPropertyCollection
&properties,
QgsExpressionContext
&context )
28
{
29
if
( properties.
isActive
(
QgsPalLayerSettings::Property::AllowDegradedPlacement
) )
30
{
31
context.
setOriginalValueVariable
( mAllowDegradedPlacement );
32
mAllowDegradedPlacement = properties.
valueAsBool
(
QgsPalLayerSettings::Property::AllowDegradedPlacement
, context, mAllowDegradedPlacement );
33
}
34
35
if
( properties.
isActive
(
QgsPalLayerSettings::Property::OverlapHandling
) )
36
{
37
const
QString handlingString = properties.
valueAsString
(
QgsPalLayerSettings::Property::OverlapHandling
, context );
38
39
static
constexpr
auto
OVERLAP_HANDLING_MAP =
makeDataDefinedStringDecoder<Qgis::LabelOverlapHandling>
( {
40
{ u
"prevent"
,
Qgis::LabelOverlapHandling::PreventOverlap
},
41
{ u
"allowifneeded"
,
Qgis::LabelOverlapHandling::AllowOverlapIfRequired
},
42
{ u
"alwaysallow"
,
Qgis::LabelOverlapHandling::AllowOverlapAtNoCost
},
43
} );
44
45
auto
evaluatedHandling = OVERLAP_HANDLING_MAP.lookup( handlingString );
46
if
( evaluatedHandling )
47
{
48
mOverlapHandling = *evaluatedHandling;
49
}
50
}
51
52
// this property is messy - to avoid breaking old projects we need to also allow it to be treated as a boolean
53
if
( properties.
isActive
(
QgsPalLayerSettings::Property::LabelAllParts
) )
54
{
55
const
QString stringValue = properties.
valueAsString
(
QgsPalLayerSettings::Property::LabelAllParts
, context );
56
bool
handledAsString =
false
;
57
if
( !stringValue.isEmpty() )
58
{
59
static
constexpr
auto
MULTIPART_HANDLING_MAP =
makeDataDefinedStringDecoder<Qgis::MultiPartLabelingBehavior>
( {
60
{ u
"largestpartonly"
,
Qgis::MultiPartLabelingBehavior::LabelLargestPartOnly
},
61
{ u
"labeleverypart"
,
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
},
62
{ u
"splitlabeltextlinesoverparts"
,
Qgis::MultiPartLabelingBehavior::SplitLabelTextLinesOverParts
},
63
} );
64
65
auto
evaluatedHandling = MULTIPART_HANDLING_MAP.lookup( stringValue );
66
if
( evaluatedHandling )
67
{
68
handledAsString =
true
;
69
mMultiPartBehavior = *evaluatedHandling;
70
}
71
}
72
73
// fallback to old boolean compatibility
74
if
( !handledAsString )
75
{
76
context.
setOriginalValueVariable
( mMultiPartBehavior ==
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
);
77
if
( properties.
valueAsBool
(
QgsPalLayerSettings::Property::LabelAllParts
, context, mMultiPartBehavior ==
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
) )
78
{
79
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelEveryPartWithEntireLabel
;
80
}
81
else
82
{
83
mMultiPartBehavior =
Qgis::MultiPartLabelingBehavior::LabelLargestPartOnly
;
84
}
85
}
86
}
87
88
if
( properties.
isActive
(
QgsPalLayerSettings::Property::WhitespaceCollisionHandling
) )
89
{
90
bool
ok =
false
;
91
const
QString value = properties.
valueAsString
(
QgsPalLayerSettings::Property::WhitespaceCollisionHandling
, context, QString(), &ok );
92
if
( ok )
93
{
94
static
constexpr
auto
WHITESPACE_HANDLING_MAP =
makeDataDefinedStringDecoder<Qgis::LabelWhitespaceCollisionHandling>
(
95
{ { u
"treatwhitespaceascollision"
,
Qgis::LabelWhitespaceCollisionHandling::TreatWhitespaceAsCollision
},
96
{ u
"ignorewhitespacecollisions"
,
Qgis::LabelWhitespaceCollisionHandling::IgnoreWhitespaceCollisions
} }
97
);
98
99
auto
evaluatedHandling = WHITESPACE_HANDLING_MAP.lookup( value );
100
if
( evaluatedHandling )
101
{
102
mWhitespaceCollisionHandling = *evaluatedHandling;
103
}
104
}
105
}
106
}
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:784
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:27
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
qgsdatadefinedutils.h
makeDataDefinedStringDecoder
constexpr QgsDataDefinedStringDecoder< V, N > makeDataDefinedStringDecoder(const DataDefinedDecoderEntry< V >(&values)[N])
Creates a QgsDataDefinedStringDecoder from a hardcoded lookup table.
Definition
qgsdatadefinedutils.h:256
qgsexpressioncontext.h
qgslabelplacementsettings.h
qgspallabeling.h
qgspropertycollection.h
Generated on
for QGIS API Documentation by
1.17.0