QGIS API Documentation  3.6.0-Noosa (5873452)
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  //get default composer font from settings
33  QgsSettings settings;
34  QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
35  if ( !defaultFontString.isEmpty() )
36  {
37  mFont.setFamily( defaultFontString );
38  }
39 }
40 
42 {
43  mMarginMap[Top] = margin;
44  mMarginMap[Bottom] = margin;
45  mMarginMap[Left] = margin;
46  mMarginMap[Right] = margin;
47 }
48 
49 void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc ) const
50 {
51  if ( elem.isNull() ) return;
52 
53  QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
54 
55  styleElem.setAttribute( QStringLiteral( "name" ), name );
56 
57  if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
58  if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
59  if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
60  if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) ) 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 )
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 
84 {
85  switch ( s )
86  {
87  case Undefined:
88  return QString();
89  case Hidden:
90  return QStringLiteral( "hidden" );
91  case Title:
92  return QStringLiteral( "title" );
93  case Group:
94  return QStringLiteral( "group" );
95  case Subgroup:
96  return QStringLiteral( "subgroup" );
97  case Symbol:
98  return QStringLiteral( "symbol" );
99  case SymbolLabel:
100  return QStringLiteral( "symbolLabel" );
101  }
102  return QString();
103 }
104 
106 {
107  if ( styleName == QLatin1String( "hidden" ) ) return Hidden;
108  else if ( styleName == QLatin1String( "title" ) ) return Title;
109  else if ( styleName == QLatin1String( "group" ) ) return Group;
110  else if ( styleName == QLatin1String( "subgroup" ) ) return Subgroup;
111  else if ( styleName == QLatin1String( "symbol" ) ) return Symbol;
112  else if ( styleName == QLatin1String( "symbolLabel" ) ) return SymbolLabel;
113  return Undefined;
114 }
115 
117 {
118  switch ( s )
119  {
120  case Undefined:
121  return QObject::tr( "Undefined" );
122  case Hidden:
123  return QObject::tr( "Hidden" );
124  case Title:
125  return QObject::tr( "Title" );
126  case Group:
127  return QObject::tr( "Group" );
128  case Subgroup:
129  return QObject::tr( "Subgroup" );
130  case Symbol:
131  return QObject::tr( "Symbol" );
132  case SymbolLabel:
133  return QObject::tr( "Symbol label" );
134  }
135  return QString();
136 }
This class is a composition of two QSettings instances:
Definition: qgssettings.h:58
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:265
double margin(Side side)
static Style styleFromName(const QString &styleName)
Gets style from name, used in project file.
Should not happen, only if corrupted project file.
Symbol without 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.
void setMargin(Side side, double margin)
static QString styleLabel(Style s)
Gets style label, translated, used in UI.
static QString styleName(Style s)
Gets name for style, used in project file.
void readXml(const QDomElement &elem, const QDomDocument &doc)
Special style, item is hidden including margins around.
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc) const
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.