QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
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#include "qgsfontutils.h"
20#include "qgssettings.h"
21#include "qgis.h"
22#include "qgsreadwritecontext.h"
23
24#include <QFont>
25#include <QMap>
26#include <QString>
27#include <QDomElement>
28#include <QDomDocument>
29#include <QDomNode>
30
32{
33}
34
35void QgsLegendStyle::setMargin( double margin )
36{
37 mMarginMap[Top] = margin;
38 mMarginMap[Bottom] = margin;
39 mMarginMap[Left] = margin;
40 mMarginMap[Right] = margin;
41}
42
43void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext & ) const
44{
45 if ( elem.isNull() )
46 return;
47
48 QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
49
50 styleElem.setAttribute( QStringLiteral( "name" ), name );
51 styleElem.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
52 styleElem.setAttribute( QStringLiteral( "indent" ), QString::number( mIndent ) );
53
54 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
55 styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
56 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
57 styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
58 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
59 styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
60 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
61 styleElem.setAttribute( QStringLiteral( "marginRight" ), QString::number( mMarginMap[Right] ) );
62
63 styleElem.appendChild( QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "styleFont" ) ) );
64
65 elem.appendChild( styleElem );
66}
67
68void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext & )
69{
70 Q_UNUSED( doc )
71 if ( elem.isNull() ) return;
72
73 if ( !QgsFontUtils::setFromXmlChildNode( mFont, elem, QStringLiteral( "styleFont" ) ) )
74 {
75 mFont.fromString( elem.attribute( QStringLiteral( "font" ) ) );
76 }
77
78 mMarginMap[Top] = elem.attribute( QStringLiteral( "marginTop" ), QStringLiteral( "0" ) ).toDouble();
79 mMarginMap[Bottom] = elem.attribute( QStringLiteral( "marginBottom" ), QStringLiteral( "0" ) ).toDouble();
80 mMarginMap[Left] = elem.attribute( QStringLiteral( "marginLeft" ), QStringLiteral( "0" ) ).toDouble();
81 mMarginMap[Right] = elem.attribute( QStringLiteral( "marginRight" ), QStringLiteral( "0" ) ).toDouble();
82
83 mAlignment = static_cast< Qt::Alignment >( elem.attribute( QStringLiteral( "alignment" ), QString::number( Qt::AlignLeft ) ).toInt() );
84 mIndent = elem.attribute( QStringLiteral( "indent" ), QStringLiteral( "0" ) ).toDouble();
85}
86
88{
89 switch ( s )
90 {
91 case Undefined:
92 return QString();
93 case Hidden:
94 return QStringLiteral( "hidden" );
95 case Title:
96 return QStringLiteral( "title" );
97 case Group:
98 return QStringLiteral( "group" );
99 case Subgroup:
100 return QStringLiteral( "subgroup" );
101 case Symbol:
102 return QStringLiteral( "symbol" );
103 case SymbolLabel:
104 return QStringLiteral( "symbolLabel" );
105 }
106 return QString();
107}
108
110{
111 if ( styleName == QLatin1String( "hidden" ) )
112 return Hidden;
113 else if ( styleName == QLatin1String( "title" ) )
114 return Title;
115 else if ( styleName == QLatin1String( "group" ) )
116 return Group;
117 else if ( styleName == QLatin1String( "subgroup" ) )
118 return Subgroup;
119 else if ( styleName == QLatin1String( "symbol" ) )
120 return Symbol;
121 else if ( styleName == QLatin1String( "symbolLabel" ) )
122 return SymbolLabel;
123 return Undefined;
124}
125
127{
128 switch ( s )
129 {
130 case Undefined:
131 return QObject::tr( "Undefined" );
132 case Hidden:
133 return QObject::tr( "Hidden" );
134 case Title:
135 return QObject::tr( "Title" );
136 case Group:
137 return QObject::tr( "Group" );
138 case Subgroup:
139 return QObject::tr( "Subgroup" );
140 case Symbol:
141 return QObject::tr( "Symbol" );
142 case SymbolLabel:
143 return QObject::tr( "Symbol label" );
144 }
145 return QString();
146}
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.
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
double margin(Side side)
Returns the margin (in mm) for the specified side of the component.
static QString styleLabel(Style s)
Returns a translated string representing a style component, for use in UI.
static Style styleFromName(const QString &styleName)
Returns the style from name string.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
@ Right
Right side.
@ Left
Left side.
@ Bottom
Bottom side.
@ Top
Top side.
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
static QString styleName(Style s)
Returns the name for a style component as a string.
Style
Component of legends which can be styled.
@ Group
Legend group title.
@ Symbol
Symbol icon (excluding label)
@ Undefined
Should not happen, only if corrupted project file.
@ Subgroup
Legend subgroup title.
@ Title
Legend title.
@ Hidden
Special style, item is hidden including margins around.
@ SymbolLabel
Symbol label (excluding icon)
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
The class is used as a container of context for various read/write operations on other objects.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:2527