QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 : [email protected]
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 
35 void QgsLegendStyle::setMargin( double margin )
36 {
37  mMarginMap[Top] = margin;
38  mMarginMap[Bottom] = margin;
39  mMarginMap[Left] = margin;
40  mMarginMap[Right] = margin;
41 }
42 
43 void 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 
53  if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
54  styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
55  if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
56  styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
57  if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
58  styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
59  if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
60  styleElem.setAttribute( QStringLiteral( "marginRight" ), QString::number( mMarginMap[Right] ) );
61 
62  styleElem.appendChild( QgsFontUtils::toXmlElement( mFont, doc, QStringLiteral( "styleFont" ) ) );
63 
64  elem.appendChild( styleElem );
65 }
66 
67 void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext & )
68 {
69  Q_UNUSED( doc )
70  if ( elem.isNull() ) return;
71 
72  if ( !QgsFontUtils::setFromXmlChildNode( mFont, elem, QStringLiteral( "styleFont" ) ) )
73  {
74  mFont.fromString( elem.attribute( QStringLiteral( "font" ) ) );
75  }
76 
77  mMarginMap[Top] = elem.attribute( QStringLiteral( "marginTop" ), QStringLiteral( "0" ) ).toDouble();
78  mMarginMap[Bottom] = elem.attribute( QStringLiteral( "marginBottom" ), QStringLiteral( "0" ) ).toDouble();
79  mMarginMap[Left] = elem.attribute( QStringLiteral( "marginLeft" ), QStringLiteral( "0" ) ).toDouble();
80  mMarginMap[Right] = elem.attribute( QStringLiteral( "marginRight" ), QStringLiteral( "0" ) ).toDouble();
81 
82  mAlignment = static_cast< Qt::Alignment >( elem.attribute( QStringLiteral( "alignment" ), QString::number( Qt::AlignLeft ) ).toInt() );
83 }
84 
86 {
87  switch ( s )
88  {
89  case Undefined:
90  return QString();
91  case Hidden:
92  return QStringLiteral( "hidden" );
93  case Title:
94  return QStringLiteral( "title" );
95  case Group:
96  return QStringLiteral( "group" );
97  case Subgroup:
98  return QStringLiteral( "subgroup" );
99  case Symbol:
100  return QStringLiteral( "symbol" );
101  case SymbolLabel:
102  return QStringLiteral( "symbolLabel" );
103  }
104  return QString();
105 }
106 
108 {
109  if ( styleName == QLatin1String( "hidden" ) )
110  return Hidden;
111  else if ( styleName == QLatin1String( "title" ) )
112  return Title;
113  else if ( styleName == QLatin1String( "group" ) )
114  return Group;
115  else if ( styleName == QLatin1String( "subgroup" ) )
116  return Subgroup;
117  else if ( styleName == QLatin1String( "symbol" ) )
118  return Symbol;
119  else if ( styleName == QLatin1String( "symbolLabel" ) )
120  return SymbolLabel;
121  return Undefined;
122 }
123 
125 {
126  switch ( s )
127  {
128  case Undefined:
129  return QObject::tr( "Undefined" );
130  case Hidden:
131  return QObject::tr( "Hidden" );
132  case Title:
133  return QObject::tr( "Title" );
134  case Group:
135  return QObject::tr( "Group" );
136  case Subgroup:
137  return QObject::tr( "Subgroup" );
138  case Symbol:
139  return QObject::tr( "Symbol" );
140  case SymbolLabel:
141  return QObject::tr( "Symbol label" );
142  }
143  return QString();
144 }
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:598