QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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  mCandPoint = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ), 16, &saved );
36  mCandLine = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ), 50, &saved );
37  mCandPolygon = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ), 30, &saved );
38 
39  mFlags = nullptr;
40  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingCandidates" ), false, &saved ) ) mFlags |= DrawCandidates;
41  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawRectOnly" ), false, &saved ) ) mFlags |= DrawLabelRectOnly;
42  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingAllLabels" ), false, &saved ) ) mFlags |= UseAllLabels;
43  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/ShowingPartialsLabels" ), true, &saved ) ) mFlags |= UsePartialCandidates;
44  if ( prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawUnplaced" ), false, &saved ) ) mFlags |= DrawUnplacedLabels;
45 
46  mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysOutlines;
47  // if users have disabled the older PAL "DrawOutlineLabels" setting, respect that
48  if ( !prj->readBoolEntry( QStringLiteral( "PAL" ), QStringLiteral( "/DrawOutlineLabels" ), true ) )
49  mDefaultTextRenderFormat = QgsRenderContext::TextFormatAlwaysText;
50  // otherwise, prefer the new setting
51  const int projectTextFormat = prj->readNumEntry( QStringLiteral( "PAL" ), QStringLiteral( "/TextFormat" ), -1 );
52  if ( projectTextFormat >= 0 )
53  mDefaultTextRenderFormat = static_cast< QgsRenderContext::TextRenderFormat >( projectTextFormat );
54 
55  mUnplacedLabelColor = QgsSymbolLayerUtils::decodeColor( prj->readEntry( QStringLiteral( "PAL" ), QStringLiteral( "/UnplacedColor" ), QStringLiteral( "#ff0000" ) ) );
56 }
57 
59 {
60  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/SearchMethod" ), static_cast< int >( mSearchMethod ) );
61  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPoint" ), mCandPoint );
62  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesLine" ), mCandLine );
63  project->writeEntry( QStringLiteral( "PAL" ), QStringLiteral( "/CandidatesPolygon" ), mCandPolygon );
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 
77 {
78  return mUnplacedLabelColor;
79 }
80 
82 {
83  mUnplacedLabelColor = unplacedLabelColor;
84 }
85 
86 
QColor unplacedLabelColor() const
Returns the color to use when rendering unplaced labels.
QString readEntry(const QString &scope, const QString &key, const QString &def=QString(), bool *ok=nullptr) const
bool readBoolEntry(const QString &scope, const QString &key, bool def=false, bool *ok=nullptr) const
Always render text as text objects.
void setUnplacedLabelColor(const QColor &color)
Sets the color to use when rendering unplaced labels.
Whether to use also label candidates that are partially outside of the map view.
Whether to only draw the label rect and not the actual label text (used for unit tests) ...
int readNumEntry(const QString &scope, const QString &key, int def=0, bool *ok=nullptr) const
static QString encodeColor(const QColor &color)
void readSettingsFromProject(QgsProject *project)
Read configuration of the labeling engine from a project.
void clear()
Returns the configuration to the defaults.
Whether to draw all labels even if there would be collisions.
bool writeEntry(const QString &scope, const QString &key, bool value)
Write a boolean entry to the project file.
Whether to render unplaced labels as an indicator/warning for users.
Always render text using path objects (AKA outlines/curves).
Encapsulates a QGIS project, including sets of map layers and their styles, layouts, annotations, canvases, etc.
Definition: qgsproject.h:89
TextRenderFormat
Options for rendering text.
Whether to draw rectangles of generated candidates (good for debugging)
void writeSettingsToProject(QgsProject *project)
Write configuration of the labeling engine to a project.
Search
Search methods in the PAL library to remove colliding labels (methods have different processing speed...
static QColor decodeColor(const QString &str)