QGIS API Documentation 3.99.0-Master (26c88405ac0)
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
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
20#include "qgis.h"
21#include "qgsfontutils.h"
23#include "qgsreadwritecontext.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27#include <QDomNode>
28#include <QFont>
29#include <QMap>
30#include <QString>
31
35
36void QgsLegendStyle::setFont( const QFont &font )
37{
38 mTextFormat.setFont( font );
39 if ( font.pointSizeF() > 0 )
40 {
41 mTextFormat.setSize( font.pointSizeF() );
42 mTextFormat.setSizeUnit( Qgis::RenderUnit::Points );
43 }
44 else if ( font.pixelSize() > 0 )
45 {
46 mTextFormat.setSize( font.pixelSize() );
47 mTextFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
48 }
49}
50
52{
53 mMarginMap[Top] = margin;
54 mMarginMap[Bottom] = margin;
55 mMarginMap[Left] = margin;
56 mMarginMap[Right] = margin;
57}
58
59void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
60{
61 if ( elem.isNull() )
62 return;
63
64 QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
65
66 styleElem.setAttribute( QStringLiteral( "name" ), name );
67 styleElem.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
68 styleElem.setAttribute( QStringLiteral( "indent" ), QString::number( mIndent ) );
69
70 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
71 styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
72 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
73 styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
74 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
75 styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
76 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
77 styleElem.setAttribute( QStringLiteral( "marginRight" ), QString::number( mMarginMap[Right] ) );
78
79 QDomElement textElem = mTextFormat.writeXml( doc, context );
80 styleElem.appendChild( textElem );
81
82 elem.appendChild( styleElem );
83}
84
85void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context )
86{
87 Q_UNUSED( doc )
88 if ( elem.isNull() ) return;
89
90 QDomNodeList textFormatNodeList = elem.elementsByTagName( QStringLiteral( "text-style" ) );
91 if ( !textFormatNodeList.isEmpty() )
92 {
93 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
94 mTextFormat.readXml( textFormatElem, context );
95 }
96 else
97 {
98 QFont f;
99 if ( !QgsFontUtils::setFromXmlChildNode( f, elem, QStringLiteral( "styleFont" ) ) )
100 {
101 f.fromString( elem.attribute( QStringLiteral( "font" ) ) );
102 }
103 mTextFormat = QgsTextFormat::fromQFont( f );
104 }
105
106 mMarginMap[Top] = elem.attribute( QStringLiteral( "marginTop" ), QStringLiteral( "0" ) ).toDouble();
107 mMarginMap[Bottom] = elem.attribute( QStringLiteral( "marginBottom" ), QStringLiteral( "0" ) ).toDouble();
108 mMarginMap[Left] = elem.attribute( QStringLiteral( "marginLeft" ), QStringLiteral( "0" ) ).toDouble();
109 mMarginMap[Right] = elem.attribute( QStringLiteral( "marginRight" ), QStringLiteral( "0" ) ).toDouble();
110
111 mAlignment = static_cast< Qt::Alignment >( elem.attribute( QStringLiteral( "alignment" ), QString::number( Qt::AlignLeft ) ).toInt() );
112 mIndent = elem.attribute( QStringLiteral( "indent" ), QStringLiteral( "0" ) ).toDouble();
113}
114
116{
117 if ( mTextFormat.dataDefinedProperties().hasActiveProperties() ) // note, we use format instead of tmpFormat here, it's const and potentially avoids a detach
118 mTextFormat.updateDataDefinedProperties( context );
119
120}
121
123{
124 switch ( s )
125 {
127 return QString();
129 return QStringLiteral( "hidden" );
131 return QStringLiteral( "title" );
133 return QStringLiteral( "group" );
135 return QStringLiteral( "subgroup" );
137 return QStringLiteral( "symbol" );
139 return QStringLiteral( "symbolLabel" );
140 }
141 return QString();
142}
143
145{
146 if ( styleName == QLatin1String( "hidden" ) )
148 else if ( styleName == QLatin1String( "title" ) )
150 else if ( styleName == QLatin1String( "group" ) )
152 else if ( styleName == QLatin1String( "subgroup" ) )
154 else if ( styleName == QLatin1String( "symbol" ) )
156 else if ( styleName == QLatin1String( "symbolLabel" ) )
159}
160
162{
163 switch ( s )
164 {
166 return QObject::tr( "Undefined" );
168 return QObject::tr( "Hidden" );
170 return QObject::tr( "Title" );
172 return QObject::tr( "Group" );
174 return QObject::tr( "Subgroup" );
176 return QObject::tr( "Symbol" );
178 return QObject::tr( "Symbol label" );
179 }
180 return QString();
181}
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4575
@ Symbol
Symbol icon (excluding label).
Definition qgis.h:4581
@ Group
Legend group title.
Definition qgis.h:4579
@ Hidden
Special style, item is hidden including margins around.
Definition qgis.h:4577
@ Subgroup
Legend subgroup title.
Definition qgis.h:4580
@ Title
Legend title.
Definition qgis.h:4578
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4582
@ Undefined
Should not happen, only if corrupted project file.
Definition qgis.h:4576
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5188
@ Pixels
Pixels.
Definition qgis.h:5186
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.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6607