QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 }
43 
44 void QgsComposerLegendStyle::setMargin( double margin )
45 {
46  mMarginMap[Top] = margin;
47  mMarginMap[Bottom] = margin;
48  mMarginMap[Left] = margin;
49  mMarginMap[Right] = margin;
50 }
51 
53 {
54  if ( elem.isNull() ) return;
55 
56  QDomElement styleElem = doc.createElement( "style" );
57 
58  styleElem.setAttribute( "name", name );
59 
60  if ( mMarginMap[Top] != 0 ) styleElem.setAttribute( "marginTop", QString::number( mMarginMap[Top] ) );
61  if ( mMarginMap[Bottom] != 0 ) styleElem.setAttribute( "marginBottom", QString::number( mMarginMap[Bottom] ) );
62  if ( mMarginMap[Left] != 0 ) styleElem.setAttribute( "marginLeft", QString::number( mMarginMap[Left] ) );
63  if ( mMarginMap[Right] != 0 ) styleElem.setAttribute( "marginRight", QString::number( mMarginMap[Right] ) );
64 
65  styleElem.appendChild( QgsFontUtils::toXmlElement( mFont, doc, "styleFont" ) );
66 
67  elem.appendChild( styleElem );
68 }
69 
71 {
72  Q_UNUSED( doc );
73  if ( elem.isNull() ) return;
74 
75  if ( !QgsFontUtils::setFromXmlChildNode( mFont, elem, "styleFont" ) )
76  {
77  mFont.fromString( elem.attribute( "font" ) );
78  }
79 
80  mMarginMap[Top] = elem.attribute( "marginTop", "0" ).toDouble();
81  mMarginMap[Bottom] = elem.attribute( "marginBottom", "0" ).toDouble();
82  mMarginMap[Left] = elem.attribute( "marginLeft", "0" ).toDouble();
83  mMarginMap[Right] = elem.attribute( "marginRight", "0" ).toDouble();
84 }
85 
87 {
88  switch ( s )
89  {
90  case Undefined:
91  return "";
92  case Hidden:
93  return "hidden";
94  case Title:
95  return "title";
96  case Group:
97  return "group";
98  case Subgroup:
99  return "subgroup";
100  case Symbol:
101  return "symbol";
102  case SymbolLabel:
103  return "symbolLabel";
104  }
105  return "";
106 }
107 
109 {
110  if ( styleName == "hidden" ) return Hidden;
111  else if ( styleName == "title" ) return Title;
112  else if ( styleName == "group" ) return Group;
113  else if ( styleName == "subgroup" ) return Subgroup;
114  else if ( styleName == "symbol" ) return Symbol;
115  else if ( styleName == "symbolLabel" ) return SymbolLabel;
116  return Undefined;
117 }
118 
120 {
121  switch ( s )
122  {
123  case Undefined:
124  return QObject::tr( "Undefined" );
125  case Hidden:
126  return QObject::tr( "Hidden" );
127  case Title:
128  return QObject::tr( "Title" );
129  case Group:
130  return QObject::tr( "Group" );
131  case Subgroup:
132  return QObject::tr( "Subgroup" );
133  case Symbol:
134  return QObject::tr( "Symbol" );
135  case SymbolLabel:
136  return QObject::tr( "Symbol label" );
137  }
138  return "";
139 }
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
QString tr(const char *sourceText, const char *disambiguation, int n)
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.
static Style styleFromName(QString styleName)
Get style from name, used in project file.
void writeXML(QString name, QDomElement &elem, QDomDocument &doc) const
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