QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
qgstextrendererutils.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgstextrendererutils.h
3  -----------------
4  begin : May 2020
5  copyright : (C) 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 "qgstextrendererutils.h"
17 #include "qgsvectorlayer.h"
18 
20 {
22  const QString skind = string.trimmed();
23 
24  if ( skind.compare( QLatin1String( "Square" ), Qt::CaseInsensitive ) == 0 )
25  {
27  }
28  else if ( skind.compare( QLatin1String( "Ellipse" ), Qt::CaseInsensitive ) == 0 )
29  {
31  }
32  else if ( skind.compare( QLatin1String( "Circle" ), Qt::CaseInsensitive ) == 0 )
33  {
35  }
36  else if ( skind.compare( QLatin1String( "SVG" ), Qt::CaseInsensitive ) == 0 )
37  {
39  }
40  else if ( skind.compare( QLatin1String( "marker" ), Qt::CaseInsensitive ) == 0 )
41  {
43  }
44  return shpkind;
45 }
46 
48 {
49  const QString stype = string.trimmed();
50  // "Buffer"
52 
53  if ( stype.compare( QLatin1String( "Fixed" ), Qt::CaseInsensitive ) == 0 )
54  {
56  }
57  return sizType;
58 }
59 
61 {
62  const QString rotstr = string.trimmed();
63  // "Sync"
65 
66  if ( rotstr.compare( QLatin1String( "Offset" ), Qt::CaseInsensitive ) == 0 )
67  {
69  }
70  else if ( rotstr.compare( QLatin1String( "Fixed" ), Qt::CaseInsensitive ) == 0 )
71  {
73  }
74  return rottype;
75 }
76 
78 {
79  const QString str = string.trimmed();
80  // "Lowest"
82 
83  if ( str.compare( QLatin1String( "Text" ), Qt::CaseInsensitive ) == 0 )
84  {
86  }
87  else if ( str.compare( QLatin1String( "Buffer" ), Qt::CaseInsensitive ) == 0 )
88  {
90  }
91  else if ( str.compare( QLatin1String( "Background" ), Qt::CaseInsensitive ) == 0 )
92  {
94  }
95  return shdwtype;
96 }
97 
99 {
100  switch ( orientation )
101  {
103  return QStringLiteral( "horizontal" );
105  return QStringLiteral( "vertical" );
107  return QStringLiteral( "rotation-based" );
108  }
109  return QString();
110 }
111 
113 {
114  if ( ok )
115  *ok = true;
116 
117  QString cleaned = name.toLower().trimmed();
118 
119  if ( cleaned == QLatin1String( "horizontal" ) )
121  else if ( cleaned == QLatin1String( "vertical" ) )
123  else if ( cleaned == QLatin1String( "rotation-based" ) )
125 
126  if ( ok )
127  *ok = false;
129 }
130 
132 {
133  if ( val == 0 )
135  else if ( val == 1 )
137  else if ( val == 2 )
139  else if ( val == 3 )
141  else
143 }
144 
145 QColor QgsTextRendererUtils::readColor( QgsVectorLayer *layer, const QString &property, const QColor &defaultColor, bool withAlpha )
146 {
147  int r = layer->customProperty( property + 'R', QVariant( defaultColor.red() ) ).toInt();
148  int g = layer->customProperty( property + 'G', QVariant( defaultColor.green() ) ).toInt();
149  int b = layer->customProperty( property + 'B', QVariant( defaultColor.blue() ) ).toInt();
150  int a = withAlpha ? layer->customProperty( property + 'A', QVariant( defaultColor.alpha() ) ).toInt() : 255;
151  return QColor( r, g, b, a );
152 }
Q_INVOKABLE QVariant customProperty(const QString &value, const QVariant &defaultValue=QVariant()) const
Read a custom property from layer.
SizeType
Methods for determining the background shape size.
@ SizeBuffer
Shape size is determined by adding a buffer margin around text.
ShapeType
Background shape types.
@ ShapeSquare
Square - buffered sizes only.
RotationType
Methods for determining the rotation of the background shape.
@ RotationOffset
Shape rotation is offset from text rotation.
@ RotationSync
Shape rotation is synced with text rotation.
@ RotationFixed
Shape rotation is a fixed angle.
TextOrientation
Text orientation.
Definition: qgstextformat.h:46
@ HorizontalOrientation
Vertically oriented text.
Definition: qgstextformat.h:47
@ RotationBasedOrientation
Horizontally or vertically oriented text based on rotation (only available for map labeling)
Definition: qgstextformat.h:49
@ VerticalOrientation
Horizontally oriented text.
Definition: qgstextformat.h:48
static QgsTextBackgroundSettings::ShapeType decodeShapeType(const QString &string)
Decodes a string representation of a background shape type to a type.
static QString encodeTextOrientation(QgsTextFormat::TextOrientation orientation)
Encodes a text orientation.
static QColor readColor(QgsVectorLayer *layer, const QString &property, const QColor &defaultColor=Qt::black, bool withAlpha=true)
Converts an encoded color value from a layer property.
static QgsTextFormat::TextOrientation decodeTextOrientation(const QString &name, bool *ok=nullptr)
Attempts to decode a string representation of a text orientation.
static QgsTextShadowSettings::ShadowPlacement decodeShadowPlacementType(const QString &string)
Decodes a string representation of a shadow placement type to a type.
static QgsTextBackgroundSettings::RotationType decodeBackgroundRotationType(const QString &string)
Decodes a string representation of a background rotation type to a type.
static QgsTextBackgroundSettings::SizeType decodeBackgroundSizeType(const QString &string)
Decodes a string representation of a background size type to a type.
static QgsUnitTypes::RenderUnit convertFromOldLabelUnit(int val)
Converts a unit from an old (pre 3.0) label unit.
ShadowPlacement
Placement positions for text shadow.
@ ShadowBuffer
Draw shadow under buffer.
@ ShadowShape
Draw shadow under background shape.
@ ShadowLowest
Draw shadow below all text components.
@ ShadowText
Draw shadow under text.
RenderUnit
Rendering size units.
Definition: qgsunittypes.h:167
@ RenderPercentage
Percentage of another measurement (e.g., canvas size, feature size)
Definition: qgsunittypes.h:171
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:172
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:168
@ RenderMapUnits
Map units.
Definition: qgsunittypes.h:169
Represents a vector layer which manages a vector based data sets.