QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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"
22
23#include <QFont>
24#include <QMap>
25#include <QString>
26#include <QDomElement>
27#include <QDomDocument>
28#include <QDomNode>
29
33
34void QgsLegendStyle::setFont( const QFont &font )
35{
36 mTextFormat.setFont( font );
37 if ( font.pointSizeF() > 0 )
38 {
39 mTextFormat.setSize( font.pointSizeF() );
41 }
42 else if ( font.pixelSize() > 0 )
43 {
44 mTextFormat.setSize( font.pixelSize() );
46 }
47}
48
49void QgsLegendStyle::setMargin( double margin )
50{
51 mMarginMap[Top] = margin;
52 mMarginMap[Bottom] = margin;
53 mMarginMap[Left] = margin;
54 mMarginMap[Right] = margin;
55}
56
57void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
58{
59 if ( elem.isNull() )
60 return;
61
62 QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
63
64 styleElem.setAttribute( QStringLiteral( "name" ), name );
65 styleElem.setAttribute( QStringLiteral( "alignment" ), QString::number( mAlignment ) );
66 styleElem.setAttribute( QStringLiteral( "indent" ), QString::number( mIndent ) );
67
68 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
69 styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
70 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
71 styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
72 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
73 styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
74 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
75 styleElem.setAttribute( QStringLiteral( "marginRight" ), QString::number( mMarginMap[Right] ) );
76
77 QDomElement textElem = mTextFormat.writeXml( doc, context );
78 styleElem.appendChild( textElem );
79
80 elem.appendChild( styleElem );
81}
82
83void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context )
84{
85 Q_UNUSED( doc )
86 if ( elem.isNull() ) return;
87
88 QDomNodeList textFormatNodeList = elem.elementsByTagName( QStringLiteral( "text-style" ) );
89 if ( !textFormatNodeList.isEmpty() )
90 {
91 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
92 mTextFormat.readXml( textFormatElem, context );
93 }
94 else
95 {
96 QFont f;
97 if ( !QgsFontUtils::setFromXmlChildNode( f, elem, QStringLiteral( "styleFont" ) ) )
98 {
99 f.fromString( elem.attribute( QStringLiteral( "font" ) ) );
100 }
101 mTextFormat = QgsTextFormat::fromQFont( f );
102 }
103
104 mMarginMap[Top] = elem.attribute( QStringLiteral( "marginTop" ), QStringLiteral( "0" ) ).toDouble();
105 mMarginMap[Bottom] = elem.attribute( QStringLiteral( "marginBottom" ), QStringLiteral( "0" ) ).toDouble();
106 mMarginMap[Left] = elem.attribute( QStringLiteral( "marginLeft" ), QStringLiteral( "0" ) ).toDouble();
107 mMarginMap[Right] = elem.attribute( QStringLiteral( "marginRight" ), QStringLiteral( "0" ) ).toDouble();
108
109 mAlignment = static_cast< Qt::Alignment >( elem.attribute( QStringLiteral( "alignment" ), QString::number( Qt::AlignLeft ) ).toInt() );
110 mIndent = elem.attribute( QStringLiteral( "indent" ), QStringLiteral( "0" ) ).toDouble();
111}
112
114{
115 switch ( s )
116 {
117 case Undefined:
118 return QString();
119 case Hidden:
120 return QStringLiteral( "hidden" );
121 case Title:
122 return QStringLiteral( "title" );
123 case Group:
124 return QStringLiteral( "group" );
125 case Subgroup:
126 return QStringLiteral( "subgroup" );
127 case Symbol:
128 return QStringLiteral( "symbol" );
129 case SymbolLabel:
130 return QStringLiteral( "symbolLabel" );
131 }
132 return QString();
133}
134
136{
137 if ( styleName == QLatin1String( "hidden" ) )
138 return Hidden;
139 else if ( styleName == QLatin1String( "title" ) )
140 return Title;
141 else if ( styleName == QLatin1String( "group" ) )
142 return Group;
143 else if ( styleName == QLatin1String( "subgroup" ) )
144 return Subgroup;
145 else if ( styleName == QLatin1String( "symbol" ) )
146 return Symbol;
147 else if ( styleName == QLatin1String( "symbolLabel" ) )
148 return SymbolLabel;
149 return Undefined;
150}
151
153{
154 switch ( s )
155 {
156 case Undefined:
157 return QObject::tr( "Undefined" );
158 case Hidden:
159 return QObject::tr( "Hidden" );
160 case Title:
161 return QObject::tr( "Title" );
162 case Group:
163 return QObject::tr( "Group" );
164 case Subgroup:
165 return QObject::tr( "Subgroup" );
166 case Symbol:
167 return QObject::tr( "Symbol" );
168 case SymbolLabel:
169 return QObject::tr( "Symbol label" );
170 }
171 return QString();
172}
@ 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 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.
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.
double margin(Side side) const
Returns the margin (in mm) for the specified side of the component.
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)
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.
The class is used as a container of context for various read/write operations on other objects.
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 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:5958