QGIS API Documentation 3.40.0-Bratislava (b56115d8743)
Loading...
Searching...
No Matches
qgslabelingenginesettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslabelingenginesettings.cpp
3 --------------------------------------
4 Date : April 2017
5 Copyright : (C) 2017 by Martin Dobias
6 Email : wonder dot sk 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
18#include "qgsproject.h"
19#include "qgscolorutils.h"
21#include "qgsapplication.h"
23
27
29
31 : mFlags( other.mFlags )
32 , mSearchMethod( other.mSearchMethod )
33 , mMaxLineCandidatesPerCm( other.mMaxLineCandidatesPerCm )
34 , mMaxPolygonCandidatesPerCmSquared( other.mMaxPolygonCandidatesPerCmSquared )
35 , mUnplacedLabelColor( other.mUnplacedLabelColor )
36 , mPlacementVersion( other.mPlacementVersion )
37 , mDefaultTextRenderFormat( other.mDefaultTextRenderFormat )
38{
39 mEngineRules.reserve( other.mEngineRules.size() );
40 for ( const auto &rule : other.mEngineRules )
41 {
42 mEngineRules.emplace_back( rule->clone() );
43 }
44}
45
47{
48 mFlags = other.mFlags;
49 mSearchMethod = other.mSearchMethod;
50 mMaxLineCandidatesPerCm = other.mMaxLineCandidatesPerCm;
51 mMaxPolygonCandidatesPerCmSquared = other.mMaxPolygonCandidatesPerCmSquared;
52 mUnplacedLabelColor = other.mUnplacedLabelColor;
53 mPlacementVersion = other.mPlacementVersion;
54 mDefaultTextRenderFormat = other.mDefaultTextRenderFormat;
55 mEngineRules.clear();
56 mEngineRules.reserve( other.mEngineRules.size() );
57 for ( const auto &rule : other.mEngineRules )
58 {
59 mEngineRules.emplace_back( rule->clone() );
60 }
61 return *this;
62}
63
68
70{
71 bool saved = false;
72 mSearchMethod = static_cast< Search >( prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( Chain ), &saved ) );
73 mMaxLineCandidatesPerCm = prj->readDoubleEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLinePerCM" ), 5, &saved );
74 mMaxPolygonCandidatesPerCmSquared = prj->readDoubleEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygonPerCM" ), 2.5, &saved );
75
76 mFlags = Qgis::LabelingFlags();
77 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), false, &saved ) ) mFlags |= Qgis::LabelingFlag::DrawCandidates;
78 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), false, &saved ) ) mFlags |= Qgis::LabelingFlag::DrawLabelRectOnly;
79 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), false, &saved ) ) mFlags |= Qgis::LabelingFlag::UseAllLabels;
80 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), true, &saved ) ) mFlags |= Qgis::LabelingFlag::UsePartialCandidates;
81 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), false, &saved ) ) mFlags |= Qgis::LabelingFlag::DrawUnplacedLabels;
82 if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawLabelMetrics" ), false, &saved ) ) mFlags |= Qgis::LabelingFlag::DrawLabelMetrics;
83
84 mDefaultTextRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
85 // if users have disabled the older PAL "DrawOutlineLabels" setting, respect that
86 if ( !prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true ) )
87 mDefaultTextRenderFormat = Qgis::TextRenderFormat::AlwaysText;
88 // otherwise, prefer the new setting
89 const int projectTextFormat = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), -1 );
90 if ( projectTextFormat >= 0 )
91 mDefaultTextRenderFormat = static_cast< Qgis::TextRenderFormat >( projectTextFormat );
92
93 mUnplacedLabelColor = QgsColorUtils::colorFromString( prj->readEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QStringLiteral( "#ff0000" ) ) );
94
95 mPlacementVersion = static_cast< Qgis::LabelPlacementEngineVersion >( prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/PlacementEngineVersion" ), static_cast< int >( Qgis::LabelPlacementEngineVersion::Version1 ) ) );
96}
97
99{
100 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( mSearchMethod ) );
101 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLinePerCM" ), mMaxLineCandidatesPerCm );
102 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygonPerCM" ), mMaxPolygonCandidatesPerCmSquared );
103
104 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), mFlags.testFlag( Qgis::LabelingFlag::DrawCandidates ) );
105 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), mFlags.testFlag( Qgis::LabelingFlag::DrawLabelRectOnly ) );
106 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), mFlags.testFlag( Qgis::LabelingFlag::DrawUnplacedLabels ) );
107 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), mFlags.testFlag( Qgis::LabelingFlag::UseAllLabels ) );
108 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), mFlags.testFlag( Qgis::LabelingFlag::UsePartialCandidates ) );
109 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawLabelMetrics" ), mFlags.testFlag( Qgis::LabelingFlag::DrawLabelMetrics ) );
110
111 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), static_cast< int >( mDefaultTextRenderFormat ) );
112
113 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QgsColorUtils::colorToString( mUnplacedLabelColor ) );
114
115 project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/PlacementEngineVersion" ), static_cast< int >( mPlacementVersion ) );
116}
117
118void QgsLabelingEngineSettings::writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const
119{
120 if ( !mEngineRules.empty() )
121 {
122 QDomElement rulesElement = doc.createElement( QStringLiteral( "rules" ) );
123 for ( const auto &rule : mEngineRules )
124 {
125 QDomElement ruleElement = doc.createElement( QStringLiteral( "rule" ) );
126 ruleElement.setAttribute( QStringLiteral( "id" ), rule->id() );
127 if ( !rule->name().isEmpty() )
128 ruleElement.setAttribute( QStringLiteral( "name" ), rule->name() );
129 if ( !rule->active() )
130 ruleElement.setAttribute( QStringLiteral( "active" ), QStringLiteral( "0" ) );
131 rule->writeXml( doc, ruleElement, context );
132 rulesElement.appendChild( ruleElement );
133 }
134 element.appendChild( rulesElement );
135 }
136}
137
138void QgsLabelingEngineSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
139{
140 mEngineRules.clear();
141 {
142 const QDomElement rulesElement = element.firstChildElement( QStringLiteral( "rules" ) );
143 const QDomNodeList rules = rulesElement.childNodes();
144 for ( int i = 0; i < rules.length(); i++ )
145 {
146 const QDomElement ruleElement = rules.at( i ).toElement();
147 const QString id = ruleElement.attribute( QStringLiteral( "id" ) );
148 const QString name = ruleElement.attribute( QStringLiteral( "name" ) );
149 const bool active = ruleElement.attribute( QStringLiteral( "active" ), QStringLiteral( "1" ) ).toInt();
150 std::unique_ptr< QgsAbstractLabelingEngineRule > rule( QgsApplication::labelingEngineRuleRegistry()->create( id ) );
151 if ( rule )
152 {
153 rule->setName( name );
154 rule->setActive( active );
155 rule->readXml( ruleElement, context );
156 mEngineRules.emplace_back( std::move( rule ) );
157 }
158 }
159 }
160}
161
163{
164 for ( const auto &rule : mEngineRules )
165 {
166 rule->resolveReferences( project );
167 }
168}
169
171{
172 return mUnplacedLabelColor;
173}
174
175void QgsLabelingEngineSettings::setUnplacedLabelColor( const QColor &unplacedLabelColor )
176{
177 mUnplacedLabelColor = unplacedLabelColor;
178}
179
184
189
190QList<QgsAbstractLabelingEngineRule *> QgsLabelingEngineSettings::rules()
191{
192 QList<QgsAbstractLabelingEngineRule *> res;
193 for ( const auto &it : mEngineRules )
194 {
195 res << it.get();
196 }
197 return res;
198}
199
200QList<const QgsAbstractLabelingEngineRule *> QgsLabelingEngineSettings::rules() const
201{
202 QList<const QgsAbstractLabelingEngineRule *> res;
203 for ( const auto &it : mEngineRules )
204 {
205 res << it.get();
206 }
207 return res;
208}
209
211{
212 mEngineRules.emplace_back( rule );
213}
214
215void QgsLabelingEngineSettings::setRules( const QList<QgsAbstractLabelingEngineRule *> &rules )
216{
217 mEngineRules.clear();
219 {
220 mEngineRules.emplace_back( rule );
221 }
222}
223
224
@ DrawCandidates
Whether to draw rectangles of generated candidates (good for debugging)
@ DrawLabelMetrics
Whether to render label metric guides (for debugging)
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
@ UseAllLabels
Whether to draw all labels even if there would be collisions.
@ DrawLabelRectOnly
Whether to only draw the label rect and not the actual label text (used for unit tests)
@ UsePartialCandidates
Whether to use also label candidates that are partially outside of the map view.
TextRenderFormat
Options for rendering text.
Definition qgis.h:2624
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
@ AlwaysText
Always render text as text objects. While this mode preserves text objects as text for post-processin...
QFlags< LabelingFlag > LabelingFlags
Flags that affect drawing and placement of labels.
Definition qgis.h:2659
LabelPlacementEngineVersion
Labeling placement engine version.
Definition qgis.h:2670
@ Version1
Version 1, matches placement from QGIS <= 3.10.1.
Abstract base class for labeling engine rules.
static QgsLabelingEngineRuleRegistry * labelingEngineRuleRegistry()
Gets the registry of available labeling engine rules.
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.
Stores global configuration for labeling engine.
void resolveReferences(const QgsProject *project)
Resolves reference to layers from stored layer ID.
void writeSettingsToProject(QgsProject *project)
Write configuration of the labeling engine to a project.
void setPlacementVersion(Qgis::LabelPlacementEngineVersion version)
Sets the placement engine version, which dictates how the label placement problem is solved.
void setRules(const QList< QgsAbstractLabelingEngineRule * > &rules)
Sets the labeling engine rules which must be satifisfied while placing labels.
Search
Search methods in the PAL library to remove colliding labels (methods have different processing speed...
QColor unplacedLabelColor() const
Returns the color to use when rendering unplaced labels.
Qgis::LabelPlacementEngineVersion placementVersion() const
Returns the placement engine version, which dictates how the label placement problem is solved.
void clear()
Returns the configuration to the defaults.
void setUnplacedLabelColor(const QColor &color)
Sets the color to use when rendering unplaced labels.
void addRule(QgsAbstractLabelingEngineRule *rule)
Adds a labeling engine rule which must be satifisfied while placing labels.
QList< QgsAbstractLabelingEngineRule * > rules()
Returns a list of labeling engine rules which must be satifisfied while placing labels.
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes the label engine settings to an XML element.
QgsLabelingEngineSettings & operator=(const QgsLabelingEngineSettings &other)
void readSettingsFromProject(QgsProject *project)
Read configuration of the labeling engine from a project.
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads the label engine settings from an XML element.
Encapsulates a QGIS project, including sets of map layers and their styles, layouts,...
Definition qgsproject.h:107
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Reads an integer from the specified scope and key.
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Reads a string from the specified scope and key.
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Reads a boolean from the specified scope and key.
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
Reads a double from the specified scope and key.
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean value to the project file.
The class is used as a container of context for various read/write operations on other objects.