QGIS API Documentation 3.41.0-Master (1deb1daf037)
Loading...
Searching...
No Matches
qgslegendstyle.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslegendstyle.cpp
3 ---------------------
4 begin : March 2013
5 copyright : (C) 2013 by Radim Blazek
6 email : radim.blazek@gmail.com
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
18#include "qgslegendstyle.h"
19#include "qgsfontutils.h"
20#include "qgis.h"
21#include "qgsreadwritecontext.h"
23
24#include <QFont>
25#include <QMap>
26#include <QString>
27#include <QDomElement>
28#include <QDomDocument>
29#include <QDomNode>
30
34
35void QgsLegendStyle::setFont( const QFont &font )
36{
37 mTextFormat.setFont( font );
38 if ( font.pointSizeF() > 0 )
39 {
40 mTextFormat.setSize( font.pointSizeF() );
42 }
43 else if ( font.pixelSize() > 0 )
44 {
45 mTextFormat.setSize( font.pixelSize() );
47 }
48}
49
50void QgsLegendStyle::setMargin( double margin )
51{
52 mMarginMap[Top] = margin;
53 mMarginMap[Bottom] = margin;
54 mMarginMap[Left] = margin;
55 mMarginMap[Right] = margin;
56}
57
58void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
59{
60 if ( elem.isNull() )
61 return;
62
63 QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
64
65 styleElem.setAttribute( QStringLiteral( "name" ), name );
66 styleElem.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
67 styleElem.setAttribute( QStringLiteral( "indent" ), QString::number( mIndent ) );
68
69 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
70 styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
71 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
72 styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
73 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
74 styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
75 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
76 styleElem.setAttribute( QStringLiteral( "marginRight" ), QString::number( mMarginMap[Right] ) );
77
78 QDomElement textElem = mTextFormat.writeXml( doc, context );
79 styleElem.appendChild( textElem );
80
81 elem.appendChild( styleElem );
82}
83
84void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context )
85{
86 Q_UNUSED( doc )
87 if ( elem.isNull() ) return;
88
89 QDomNodeList textFormatNodeList = elem.elementsByTagName( QStringLiteral( "text-style" ) );
90 if ( !textFormatNodeList.isEmpty() )
91 {
92 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
93 mTextFormat.readXml( textFormatElem, context );
94 }
95 else
96 {
97 QFont f;
98 if ( !QgsFontUtils::setFromXmlChildNode( f, elem, QStringLiteral( "styleFont" ) ) )
99 {
100 f.fromString( elem.attribute( QStringLiteral( "font" ) ) );
101 }
102 mTextFormat = QgsTextFormat::fromQFont( f );
103 }
104
105 mMarginMap[Top] = elem.attribute( QStringLiteral( "marginTop" ), QStringLiteral( "0" ) ).toDouble();
106 mMarginMap[Bottom] = elem.attribute( QStringLiteral( "marginBottom" ), QStringLiteral( "0" ) ).toDouble();
107 mMarginMap[Left] = elem.attribute( QStringLiteral( "marginLeft" ), QStringLiteral( "0" ) ).toDouble();
108 mMarginMap[Right] = elem.attribute( QStringLiteral( "marginRight" ), QStringLiteral( "0" ) ).toDouble();
109
110 mAlignment = static_cast< Qt::Alignment >( elem.attribute( QStringLiteral( "alignment" ), QString::number( Qt::AlignLeft ) ).toInt() );
111 mIndent = elem.attribute( QStringLiteral( "indent" ), QStringLiteral( "0" ) ).toDouble();
112}
113
115{
116 if ( mTextFormat.dataDefinedProperties().hasActiveProperties() ) // note, we use format instead of tmpFormat here, it's const and potentially avoids a detach
117 mTextFormat.updateDataDefinedProperties( context );
118
119}
120
122{
123 switch ( s )
124 {
126 return QString();
128 return QStringLiteral( "hidden" );
130 return QStringLiteral( "title" );
132 return QStringLiteral( "group" );
134 return QStringLiteral( "subgroup" );
136 return QStringLiteral( "symbol" );
138 return QStringLiteral( "symbolLabel" );
139 }
140 return QString();
141}
142
144{
145 if ( styleName == QLatin1String( "hidden" ) )
147 else if ( styleName == QLatin1String( "title" ) )
149 else if ( styleName == QLatin1String( "group" ) )
151 else if ( styleName == QLatin1String( "subgroup" ) )
153 else if ( styleName == QLatin1String( "symbol" ) )
155 else if ( styleName == QLatin1String( "symbolLabel" ) )
158}
159
161{
162 switch ( s )
163 {
165 return QObject::tr( "Undefined" );
167 return QObject::tr( "Hidden" );
169 return QObject::tr( "Title" );
171 return QObject::tr( "Group" );
173 return QObject::tr( "Subgroup" );
175 return QObject::tr( "Symbol" );
177 return QObject::tr( "Symbol label" );
178 }
179 return QString();
180}
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4342
@ Symbol
Symbol icon (excluding label)
@ Group
Legend group title.
@ Hidden
Special style, item is hidden including margins around.
@ Subgroup
Legend subgroup title.
@ Title
Legend title.
@ SymbolLabel
Symbol label (excluding icon)
@ Undefined
Should not happen, only if corrupted project file.
@ Points
Points (e.g., for font sizes)
static bool setFromXmlChildNode(QFont &font, const QDomElement &element, const QString &childNode)
Sets the properties of a font to match the properties stored in an XML child node.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for rendering this legend component.
static QString styleName(Qgis::LegendComponent s)
Returns the name for a style component as a string.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
void updateDataDefinedProperties(QgsRenderContext &context)
Updates any data-defined properties in the style, using the specified render context.
@ Right
Right side.
@ Left
Left side.
@ Bottom
Bottom side.
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
double margin(Side side) const
Returns the margin (in mm) for the specified side of the component.
static Qgis::LegendComponent styleFromName(const QString &styleName)
Returns the style from name string.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for rendering this legend component.
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
static QString styleLabel(Qgis::LegendComponent s)
Returns a translated string representing a style component, for use in UI.
bool hasActiveProperties() const final
Returns true if the collection has any active properties, or false if all properties within the colle...
The class is used as a container of context for various read/write operations on other objects.
Contains information about the context of a rendering operation.
void setSize(double size)
Sets the size for rendered text.
void setFont(const QFont &font)
Sets the font used for rendering text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void updateDataDefinedProperties(QgsRenderContext &context)
Updates the format by evaluating current values of data defined properties.
QgsPropertyCollection & dataDefinedProperties()
Returns a reference to the format's property collection, used for data defined overrides.
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition qgis.h:6144