QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
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 "qgsapplication.h"
19#include "qgscolorutils.h"
22#include "qgsproject.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
30
32
34 //****** IMPORTANT! editing this? make sure you update the move constructor too! *****
35 : mFlags( other.mFlags )
36 , mSearchMethod( other.mSearchMethod )
37 , mMaxLineCandidatesPerCm( other.mMaxLineCandidatesPerCm )
38 , mMaxPolygonCandidatesPerCmSquared( other.mMaxPolygonCandidatesPerCmSquared )
39 , mUnplacedLabelColor( other.mUnplacedLabelColor )
40 , mPlacementVersion( other.mPlacementVersion )
41 , mDefaultTextRenderFormat( other.mDefaultTextRenderFormat )
42//****** IMPORTANT! editing this? make sure you update the move constructor too! *****
43{
44 mEngineRules.reserve( other.mEngineRules.size() );
45 for ( const auto &rule : other.mEngineRules )
46 {
47 mEngineRules.emplace_back( rule->clone() );
48 }
49}
50
52 : mFlags( other.mFlags )
53 , mSearchMethod( other.mSearchMethod )
54 , mMaxLineCandidatesPerCm( other.mMaxLineCandidatesPerCm )
55 , mMaxPolygonCandidatesPerCmSquared( other.mMaxPolygonCandidatesPerCmSquared )
56 , mUnplacedLabelColor( std::move( other.mUnplacedLabelColor ) )
57 , mPlacementVersion( other.mPlacementVersion )
58 , mDefaultTextRenderFormat( other.mDefaultTextRenderFormat )
59 , mEngineRules( std::move( other.mEngineRules ) )
60{}
61
63{
64 if ( &other == this )
65 return *this;
66
67 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
68 mFlags = other.mFlags;
69 mSearchMethod = other.mSearchMethod;
70 mMaxLineCandidatesPerCm = other.mMaxLineCandidatesPerCm;
71 mMaxPolygonCandidatesPerCmSquared = other.mMaxPolygonCandidatesPerCmSquared;
72 mUnplacedLabelColor = other.mUnplacedLabelColor;
73 mPlacementVersion = other.mPlacementVersion;
74 mDefaultTextRenderFormat = other.mDefaultTextRenderFormat;
75 mEngineRules.clear();
76 mEngineRules.reserve( other.mEngineRules.size() );
77 for ( const auto &rule : other.mEngineRules )
78 {
79 mEngineRules.emplace_back( rule->clone() );
80 }
81 //****** IMPORTANT! editing this? make sure you update the move assignment operator too! *****
82 return *this;
83}
84
86{
87 if ( &other == this )
88 return *this;
89
90 mFlags = other.mFlags;
91 mSearchMethod = other.mSearchMethod;
92 mMaxLineCandidatesPerCm = other.mMaxLineCandidatesPerCm;
93 mMaxPolygonCandidatesPerCmSquared = other.mMaxPolygonCandidatesPerCmSquared;
94 mUnplacedLabelColor = std::move( other.mUnplacedLabelColor );
95 mPlacementVersion = other.mPlacementVersion;
96 mDefaultTextRenderFormat = other.mDefaultTextRenderFormat;
97 mEngineRules = std::move( other.mEngineRules );
98 return *this;
99}
100
105
107{
108 bool saved = false;
109 mSearchMethod = static_cast< Search >( prj->readNumEntry( u"PAL"_s, u"/SearchMethod"_s, static_cast< int >( Chain ), &saved ) );
110 mMaxLineCandidatesPerCm = prj->readDoubleEntry( u"PAL"_s, u"/CandidatesLinePerCM"_s, 5, &saved );
111 mMaxPolygonCandidatesPerCmSquared = prj->readDoubleEntry( u"PAL"_s, u"/CandidatesPolygonPerCM"_s, 2.5, &saved );
112
113 mFlags = Qgis::LabelingFlags();
114 if ( prj->readBoolEntry( u"PAL"_s, u"/ShowingCandidates"_s, false, &saved ) )
116 if ( prj->readBoolEntry( u"PAL"_s, u"/DrawRectOnly"_s, false, &saved ) )
118 if ( prj->readBoolEntry( u"PAL"_s, u"/ShowingAllLabels"_s, false, &saved ) )
120 if ( prj->readBoolEntry( u"PAL"_s, u"/ShowingPartialsLabels"_s, true, &saved ) )
122 if ( prj->readBoolEntry( u"PAL"_s, u"/DrawUnplaced"_s, false, &saved ) )
124 if ( prj->readBoolEntry( u"PAL"_s, u"/DrawLabelMetrics"_s, false, &saved ) )
126
127 mDefaultTextRenderFormat = Qgis::TextRenderFormat::AlwaysOutlines;
128 // if users have disabled the older PAL "DrawOutlineLabels" setting, respect that
129 if ( !prj->readBoolEntry( u"PAL"_s, u"/DrawOutlineLabels"_s, true ) )
130 mDefaultTextRenderFormat = Qgis::TextRenderFormat::AlwaysText;
131 // otherwise, prefer the new setting
132 const int projectTextFormat = prj->readNumEntry( u"PAL"_s, u"/TextFormat"_s, -1 );
133 if ( projectTextFormat >= 0 )
134 mDefaultTextRenderFormat = static_cast< Qgis::TextRenderFormat >( projectTextFormat );
135
136 mUnplacedLabelColor = QgsColorUtils::colorFromString( prj->readEntry( u"PAL"_s, u"/UnplacedColor"_s, u"#ff0000"_s ) );
137
138 mPlacementVersion = static_cast< Qgis::LabelPlacementEngineVersion >( prj->readNumEntry( u"PAL"_s, u"/PlacementEngineVersion"_s, static_cast< int >( Qgis::LabelPlacementEngineVersion::Version1 ) ) );
139}
140
142{
143 project->writeEntry( u"PAL"_s, u"/SearchMethod"_s, static_cast< int >( mSearchMethod ) );
144 project->writeEntry( u"PAL"_s, u"/CandidatesLinePerCM"_s, mMaxLineCandidatesPerCm );
145 project->writeEntry( u"PAL"_s, u"/CandidatesPolygonPerCM"_s, mMaxPolygonCandidatesPerCmSquared );
146
147 project->writeEntry( u"PAL"_s, u"/ShowingCandidates"_s, mFlags.testFlag( Qgis::LabelingFlag::DrawCandidates ) );
148 project->writeEntry( u"PAL"_s, u"/DrawRectOnly"_s, mFlags.testFlag( Qgis::LabelingFlag::DrawLabelRectOnly ) );
149 project->writeEntry( u"PAL"_s, u"/DrawUnplaced"_s, mFlags.testFlag( Qgis::LabelingFlag::DrawUnplacedLabels ) );
150 project->writeEntry( u"PAL"_s, u"/ShowingAllLabels"_s, mFlags.testFlag( Qgis::LabelingFlag::UseAllLabels ) );
151 project->writeEntry( u"PAL"_s, u"/ShowingPartialsLabels"_s, mFlags.testFlag( Qgis::LabelingFlag::UsePartialCandidates ) );
152 project->writeEntry( u"PAL"_s, u"/DrawLabelMetrics"_s, mFlags.testFlag( Qgis::LabelingFlag::DrawLabelMetrics ) );
153
154 project->writeEntry( u"PAL"_s, u"/TextFormat"_s, static_cast< int >( mDefaultTextRenderFormat ) );
155
156 project->writeEntry( u"PAL"_s, u"/UnplacedColor"_s, QgsColorUtils::colorToString( mUnplacedLabelColor ) );
157
158 project->writeEntry( u"PAL"_s, u"/PlacementEngineVersion"_s, static_cast< int >( mPlacementVersion ) );
159}
160
161void QgsLabelingEngineSettings::writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const
162{
163 if ( !mEngineRules.empty() )
164 {
165 QDomElement rulesElement = doc.createElement( u"rules"_s );
166 for ( const auto &rule : mEngineRules )
167 {
168 QDomElement ruleElement = doc.createElement( u"rule"_s );
169 ruleElement.setAttribute( u"id"_s, rule->id() );
170 if ( !rule->name().isEmpty() )
171 ruleElement.setAttribute( u"name"_s, rule->name() );
172 if ( !rule->active() )
173 ruleElement.setAttribute( u"active"_s, u"0"_s );
174 rule->writeXml( doc, ruleElement, context );
175 rulesElement.appendChild( ruleElement );
176 }
177 element.appendChild( rulesElement );
178 }
179}
180
181void QgsLabelingEngineSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
182{
183 mEngineRules.clear();
184 {
185 const QDomElement rulesElement = element.firstChildElement( u"rules"_s );
186 const QDomNodeList rules = rulesElement.childNodes();
187 for ( int i = 0; i < rules.length(); i++ )
188 {
189 const QDomElement ruleElement = rules.at( i ).toElement();
190 const QString id = ruleElement.attribute( u"id"_s );
191 const QString name = ruleElement.attribute( u"name"_s );
192 const bool active = ruleElement.attribute( u"active"_s, u"1"_s ).toInt();
193 std::unique_ptr< QgsAbstractLabelingEngineRule > rule( QgsApplication::labelingEngineRuleRegistry()->create( id ) );
194 if ( rule )
195 {
196 rule->setName( name );
197 rule->setActive( active );
198 rule->readXml( ruleElement, context );
199 mEngineRules.emplace_back( std::move( rule ) );
200 }
201 }
202 }
203}
204
206{
207 for ( const auto &rule : mEngineRules )
208 {
209 rule->resolveReferences( project );
210 }
211}
212
214{
215 return mUnplacedLabelColor;
216}
217
219{
220 mUnplacedLabelColor = unplacedLabelColor;
221}
222
227
232
233QList<QgsAbstractLabelingEngineRule *> QgsLabelingEngineSettings::rules()
234{
235 QList<QgsAbstractLabelingEngineRule *> res;
236 for ( const auto &it : mEngineRules )
237 {
238 res << it.get();
239 }
240 return res;
241}
242
243QList<const QgsAbstractLabelingEngineRule *> QgsLabelingEngineSettings::rules() const
244{
245 QList<const QgsAbstractLabelingEngineRule *> res;
246 for ( const auto &it : mEngineRules )
247 {
248 res << it.get();
249 }
250 return res;
251}
252
254{
255 mEngineRules.emplace_back( rule );
256}
257
258void QgsLabelingEngineSettings::setRules( const QList<QgsAbstractLabelingEngineRule *> &rules )
259{
260 mEngineRules.clear();
262 {
263 mEngineRules.emplace_back( rule );
264 }
265}
@ DrawCandidates
Whether to draw rectangles of generated candidates (good for debugging).
Definition qgis.h:2948
@ DrawLabelMetrics
Whether to render label metric guides (for debugging).
Definition qgis.h:2951
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
Definition qgis.h:2949
@ UseAllLabels
Whether to draw all labels even if there would be collisions.
Definition qgis.h:2943
@ DrawLabelRectOnly
Whether to only draw the label rect and not the actual label text (used for unit tests).
Definition qgis.h:2947
@ UsePartialCandidates
Whether to use also label candidates that are partially outside of the map view.
Definition qgis.h:2944
TextRenderFormat
Options for rendering text.
Definition qgis.h:2923
@ AlwaysOutlines
Always render text using path objects (AKA outlines/curves). This setting guarantees the best quality...
Definition qgis.h:2924
@ AlwaysText
Always render text as text objects. While this mode preserves text objects as text for post-processin...
Definition qgis.h:2927
QFlags< LabelingFlag > LabelingFlags
Flags that affect drawing and placement of labels.
Definition qgis.h:2962
LabelPlacementEngineVersion
Labeling placement engine version.
Definition qgis.h:2973
@ Version1
Version 1, matches placement from QGIS <= 3.10.1.
Definition qgis.h:2974
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.
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 satisfied 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 satisfied while placing labels.
QList< QgsAbstractLabelingEngineRule * > rules()
Returns a list of labeling engine rules which must be satisfied 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:113
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.
A container for the context for various read/write operations on objects.