QGIS API Documentation  3.2.0-Bonn (bc43194)
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 
22 #include <QFont>
23 #include <QMap>
24 #include <QString>
25 #include <QDomElement>
26 #include <QDomDocument>
27 #include <QDomNode>
28 
30 {
31  //get default composer font from settings
32  QgsSettings settings;
33  QString defaultFontString = settings.value( QStringLiteral( "LayoutDesigner/defaultFont" ), QVariant(), QgsSettings::Gui ).toString();
34  if ( !defaultFontString.isEmpty() )
35  {
36  mFont.setFamily( defaultFontString );
37  }
38 }
39 
41 {
42  mMarginMap[Top] = margin;
43  mMarginMap[Bottom] = margin;
44  mMarginMap[Left] = margin;
45  mMarginMap[Right] = margin;
46 }
47 
48 void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc ) const
49 {
50  if ( elem.isNull() ) return;
51 
52  QDomElement styleElem = doc.createElement( QStringLiteral( "style" ) );
53 
54  styleElem.setAttribute( QStringLiteral( "name" ), name );
55 
56  if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginTop" ), QString::number( mMarginMap[Top] ) );
57  if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginBottom" ), QString::number( mMarginMap[Bottom] ) );
58  if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) ) styleElem.setAttribute( QStringLiteral( "marginLeft" ), QString::number( mMarginMap[Left] ) );
59  if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) ) 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 
83 {
84  switch ( s )
85  {
86  case Undefined:
87  return QLatin1String( "" );
88  case Hidden:
89  return QStringLiteral( "hidden" );
90  case Title:
91  return QStringLiteral( "title" );
92  case Group:
93  return QStringLiteral( "group" );
94  case Subgroup:
95  return QStringLiteral( "subgroup" );
96  case Symbol:
97  return QStringLiteral( "symbol" );
98  case SymbolLabel:
99  return QStringLiteral( "symbolLabel" );
100  }
101  return QLatin1String( "" );
102 }
103 
105 {
106  if ( styleName == QLatin1String( "hidden" ) ) return Hidden;
107  else if ( styleName == QLatin1String( "title" ) ) return Title;
108  else if ( styleName == QLatin1String( "group" ) ) return Group;
109  else if ( styleName == QLatin1String( "subgroup" ) ) return Subgroup;
110  else if ( styleName == QLatin1String( "symbol" ) ) return Symbol;
111  else if ( styleName == QLatin1String( "symbolLabel" ) ) return SymbolLabel;
112  return Undefined;
113 }
114 
116 {
117  switch ( s )
118  {
119  case Undefined:
120  return QObject::tr( "Undefined" );
121  case Hidden:
122  return QObject::tr( "Hidden" );
123  case Title:
124  return QObject::tr( "Title" );
125  case Group:
126  return QObject::tr( "Group" );
127  case Subgroup:
128  return QObject::tr( "Subgroup" );
129  case Symbol:
130  return QObject::tr( "Symbol" );
131  case SymbolLabel:
132  return QObject::tr( "Symbol label" );
133  }
134  return QLatin1String( "" );
135 }
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:251
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.