QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
qgscomposerlegendstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscomposerlegendstyle.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 "qgscomposerlegendstyle.h"
19 #include "qgscomposition.h"
20 #include "qgsfontutils.h"
21 #include <QFont>
22 #include <QMap>
23 #include <QSettings>
24 #include <QString>
25 #include <QDomElement>
26 #include <QDomDocument>
27 #include <QDomNode>
28 
30 {
31  //get default composer font from settings
32  QSettings settings;
33  QString defaultFontString = settings.value( "/Composer/defaultFont" ).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 QgsComposerLegendStyle::writeXML( const QString& name, QDomElement& elem, QDomDocument & doc ) const
49 {
50  if ( elem.isNull() ) return;
51 
52  QDomElement styleElem = doc.createElement( "style" );
53 
54  styleElem.setAttribute( "name", name );
55 
56  if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) ) styleElem.setAttribute( "marginTop", QString::number( mMarginMap[Top] ) );
57  if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) ) styleElem.setAttribute( "marginBottom", QString::number( mMarginMap[Bottom] ) );
58  if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) ) styleElem.setAttribute( "marginLeft", QString::number( mMarginMap[Left] ) );
59  if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) ) styleElem.setAttribute( "marginRight", QString::number( mMarginMap[Right] ) );
60 
61  styleElem.appendChild( QgsFontUtils::toXmlElement( mFont, doc, "styleFont" ) );
62 
63  elem.appendChild( styleElem );
64 }
65 
67 {
68  Q_UNUSED( doc );
69  if ( elem.isNull() ) return;
70 
71  if ( !QgsFontUtils::setFromXmlChildNode( mFont, elem, "styleFont" ) )
72  {
73  mFont.fromString( elem.attribute( "font" ) );
74  }
75 
76  mMarginMap[Top] = elem.attribute( "marginTop", "0" ).toDouble();
77  mMarginMap[Bottom] = elem.attribute( "marginBottom", "0" ).toDouble();
78  mMarginMap[Left] = elem.attribute( "marginLeft", "0" ).toDouble();
79  mMarginMap[Right] = elem.attribute( "marginRight", "0" ).toDouble();
80 }
81 
83 {
84  switch ( s )
85  {
86  case Undefined:
87  return "";
88  case Hidden:
89  return "hidden";
90  case Title:
91  return "title";
92  case Group:
93  return "group";
94  case Subgroup:
95  return "subgroup";
96  case Symbol:
97  return "symbol";
98  case SymbolLabel:
99  return "symbolLabel";
100  }
101  return "";
102 }
103 
105 {
106  if ( styleName == "hidden" ) return Hidden;
107  else if ( styleName == "title" ) return Title;
108  else if ( styleName == "group" ) return Group;
109  else if ( styleName == "subgroup" ) return Subgroup;
110  else if ( styleName == "symbol" ) return Symbol;
111  else if ( styleName == "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 "";
135 }
static Style styleFromName(const QString &styleName)
Get style from name, used in project file.
void readXML(const QDomElement &elem, const QDomDocument &doc)
QDomNode appendChild(const QDomNode &newChild)
void setMargin(Side side, double margin)
QString attribute(const QString &name, const QString &defValue) const
double toDouble(bool *ok) const
void writeXML(const QString &name, QDomElement &elem, QDomDocument &doc) const
QString tr(const char *sourceText, const char *disambiguation, int n)
bool qgsDoubleNear(double a, double b, double epsilon=4 *DBL_EPSILON)
Compare two doubles (but allow some difference)
Definition: qgis.h:353
static QString styleName(Style s)
Get name for style, used in project file.
QString number(int n, int base)
bool fromString(const QString &descrip)
void setAttribute(const QString &name, const QString &value)
bool isEmpty() const
static QString styleLabel(Style s)
Get style label, translated, used in UI.
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.
bool isNull() const
QVariant value(const QString &key, const QVariant &defaultValue) const
void setFamily(const QString &family)
static QDomElement toXmlElement(const QFont &font, QDomDocument &document, const QString &elementName)
Returns a DOM element containing the properties of the font.
QDomElement createElement(const QString &tagName)
QString toString() const