QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 }
QgsLegendStyle::Symbol
@ Symbol
Symbol icon (excluding label)
Definition: qgslegendstyle.h:73
QgsLegendStyle::Style
Style
Component of legends which can be styled.
Definition: qgslegendstyle.h:53
QgsReadWriteContext
Definition: qgsreadwritecontext.h:34
qgsreadwritecontext.h
QgsLegendStyle::setMargin
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
Definition: qgslegendstyle.h:113
qgis.h
QgsLegendStyle::Bottom
@ Bottom
Bottom side.
Definition: qgslegendstyle.h:70
QgsLegendStyle::writeXml
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
Definition: qgslegendstyle.cpp:43
qgsfontutils.h
QgsLegendStyle::SymbolLabel
@ SymbolLabel
Symbol label (excluding icon)
Definition: qgslegendstyle.h:74
QgsLegendStyle::Title
@ Title
Legend title.
Definition: qgslegendstyle.h:70
QgsLegendStyle::styleLabel
static QString styleLabel(Style s)
Returns a translated string representing a style component, for use in UI.
Definition: qgslegendstyle.cpp:124
QgsLegendStyle::Undefined
@ Undefined
Should not happen, only if corrupted project file.
Definition: qgslegendstyle.h:68
qgsDoubleNear
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:315
QgsLegendStyle::styleName
static QString styleName(Style s)
Returns the name for a style component as a string.
Definition: qgslegendstyle.cpp:85
QgsLegendStyle::Right
@ Right
Right side.
Definition: qgslegendstyle.h:72
QgsLegendStyle::Left
@ Left
Left side.
Definition: qgslegendstyle.h:71
QgsFontUtils::setFromXmlChildNode
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.
Definition: qgsfontutils.cpp:348
qgslegendstyle.h
QgsFontUtils::toXmlElement
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
Definition: qgsfontutils.cpp:324
qgssettings.h
QgsLegendStyle::QgsLegendStyle
QgsLegendStyle()
Definition: qgslegendstyle.cpp:31
QgsLegendStyle::Hidden
@ Hidden
Special style, item is hidden including margins around.
Definition: qgslegendstyle.h:69
QgsLegendStyle::styleFromName
static Style styleFromName(const QString &styleName)
Returns the style from name string.
Definition: qgslegendstyle.cpp:107
QgsLegendStyle::Top
@ Top
Top side.
Definition: qgslegendstyle.h:69
QgsLegendStyle::readXml
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
Definition: qgslegendstyle.cpp:67
QgsLegendStyle::Group
@ Group
Legend group title.
Definition: qgslegendstyle.h:71
QgsLegendStyle::Subgroup
@ Subgroup
Legend subgroup title.
Definition: qgslegendstyle.h:72
QgsLegendStyle::margin
double margin(Side side)
Returns the margin (in mm) for the specified side of the component.
Definition: qgslegendstyle.h:104