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