QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgssymbollayerutils.h"
20 
22  : mFlags( UsePartialCandidates )
23 {
24 }
25 
27 {
28  *this = QgsLabelingEngineSettings();
29 }
30 
32 {
33  bool saved = false;
34  mSearchMethod = static_cast< Search >( prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( Chain ), &saved ) );
35  mMaxLineCandidatesPerCm = prj->readDoubleEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLinePerCM" ), 5, &saved );
36  mMaxPolygonCandidatesPerCmSquared = prj->readDoubleEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygonPerCM" ), 2.5, &saved );
37 
38  mFlags = nullptr;
39  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), false, &saved ) ) mFlags |= DrawCandidates;
40  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), false, &saved ) ) mFlags |= DrawLabelRectOnly;
41  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), false, &saved ) ) mFlags |= UseAllLabels;
42  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), true, &saved ) ) mFlags |= UsePartialCandidates;
43  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), false, &saved ) ) mFlags |= DrawUnplacedLabels;
44 
45  mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;
46  // if users have disabled the older PAL "DrawOutlineLabels" setting, respect that
47  if ( !prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true ) )
48  mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysText;
49  // otherwise, prefer the new setting
50  const int projectTextFormat = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), -1 );
51  if ( projectTextFormat >= 0 )
52  mDefaultTextRenderFormat = static_cast< QgsRenderContext::TextRenderFormat >( projectTextFormat );
53 
54  mUnplacedLabelColor = QgsSymbolLayerUtils::decodeColor( prj->readEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QStringLiteral( "#ff0000" ) ) );
55 
56  mPlacementVersion = static_cast< PlacementEngineVersion >( prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/PlacementEngineVersion" ), static_cast< int >( PlacementEngineVersion1 ) ) );
57 }
58 
60 {
61  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( mSearchMethod ) );
62  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLinePerCM" ), mMaxLineCandidatesPerCm );
63  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygonPerCM" ), mMaxPolygonCandidatesPerCmSquared );
64 
65  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), mFlags.testFlag( DrawCandidates ) );
66  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), mFlags.testFlag( DrawLabelRectOnly ) );
67  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), mFlags.testFlag( DrawUnplacedLabels ) );
68  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), mFlags.testFlag( UseAllLabels ) );
69  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), mFlags.testFlag( UsePartialCandidates ) );
70 
71  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), static_cast< int >( mDefaultTextRenderFormat ) );
72 
73  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QgsSymbolLayerUtils::encodeColor( mUnplacedLabelColor ) );
74 
75  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/PlacementEngineVersion" ), mPlacementVersion );
76 }
77 
79 {
80  return mUnplacedLabelColor;
81 }
82 
83 void QgsLabelingEngineSettings::setUnplacedLabelColor( const QColor &unplacedLabelColor )
84 {
85  mUnplacedLabelColor = unplacedLabelColor;
86 }
87 
89 {
90  return mPlacementVersion;
91 }
92 
94 {
95  mPlacementVersion = placementVersion;
96 }
97 
98 
QgsProject::writeEntry
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean entry to the project file.
Definition: qgsproject.cpp:2366
QgsLabelingEngineSettings::readSettingsFromProject
void readSettingsFromProject(QgsProject *project)
Read configuration of the labeling engine from a project.
Definition: qgslabelingenginesettings.cpp:31
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:52
QgsLabelingEngineSettings::clear
void clear()
Returns the configuration to the defaults.
Definition: qgslabelingenginesettings.cpp:26
QgsLabelingEngineSettings::DrawCandidates
@ DrawCandidates
Whether to draw rectangles of generated candidates (good for debugging)
Definition: qgslabelingenginesettings.h:41
qgslabelingenginesettings.h
qgssymbollayerutils.h
QgsRenderContext::TextRenderFormat
TextRenderFormat
Options for rendering text.
Definition: qgsrendercontext.h:90
QgsLabelingEngineSettings::placementVersion
PlacementEngineVersion placementVersion() const
Returns the placement engine version, which dictates how the label placement problem is solved.
Definition: qgslabelingenginesettings.cpp:88
QgsProject::readEntry
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
Definition: qgsproject.cpp:2448
QgsProject::readBoolEntry
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Definition: qgsproject.cpp:2519
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:57
QgsProject
Definition: qgsproject.h:92
QgsLabelingEngineSettings::QgsLabelingEngineSettings
QgsLabelingEngineSettings()
Definition: qgslabelingenginesettings.cpp:21
QgsLabelingEngineSettings::PlacementEngineVersion
PlacementEngineVersion
Placement engine version.
Definition: qgslabelingenginesettings.h:66
QgsLabelingEngineSettings::writeSettingsToProject
void writeSettingsToProject(QgsProject *project)
Write configuration of the labeling engine to a project.
Definition: qgslabelingenginesettings.cpp:59
QgsLabelingEngineSettings::setUnplacedLabelColor
void setUnplacedLabelColor(const QColor &color)
Sets the color to use when rendering unplaced labels.
Definition: qgslabelingenginesettings.cpp:83
QgsRenderContext::TextFormatAlwaysText
@ TextFormatAlwaysText
Always render text as text objects.
Definition: qgsrendercontext.h:127
QgsLabelingEngineSettings::unplacedLabelColor
QColor unplacedLabelColor() const
Returns the color to use when rendering unplaced labels.
Definition: qgslabelingenginesettings.cpp:78
QgsProject::readDoubleEntry
double readDoubleEntry(const QString &scope, const QString &key, double def=0, bool *ok=nullptr) const
Definition: qgsproject.cpp:2499
QgsLabelingEngineSettings::PlacementEngineVersion1
@ PlacementEngineVersion1
Version 1, matches placement from QGIS <= 3.10.1.
Definition: qgslabelingenginesettings.h:68
QgsLabelingEngineSettings::Search
Search
Search methods in the PAL library to remove colliding labels (methods have different processing speed...
Definition: qgslabelingenginesettings.h:52
QgsLabelingEngineSettings::setPlacementVersion
void setPlacementVersion(PlacementEngineVersion version)
Sets the placement engine version, which dictates how the label placement problem is solved.
Definition: qgslabelingenginesettings.cpp:93
QgsLabelingEngineSettings::UsePartialCandidates
@ UsePartialCandidates
Whether to use also label candidates that are partially outside of the map view.
Definition: qgslabelingenginesettings.h:37
QgsLabelingEngineSettings::Chain
@ Chain
Definition: qgslabelingenginesettings.h:54
QgsLabelingEngineSettings::DrawLabelRectOnly
@ DrawLabelRectOnly
Whether to only draw the label rect and not the actual label text (used for unit tests)
Definition: qgslabelingenginesettings.h:40
QgsRenderContext::TextFormatAlwaysOutlines
@ TextFormatAlwaysOutlines
Always render text using path objects (AKA outlines/curves).
Definition: qgsrendercontext.h:111
QgsLabelingEngineSettings::UseAllLabels
@ UseAllLabels
Whether to draw all labels even if there would be collisions.
Definition: qgslabelingenginesettings.h:36
qgsproject.h
QgsLabelingEngineSettings::DrawUnplacedLabels
@ DrawUnplacedLabels
Whether to render unplaced labels as an indicator/warning for users.
Definition: qgslabelingenginesettings.h:42
QgsProject::readNumEntry
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
Definition: qgsproject.cpp:2472