QGIS API Documentation  2.8.2-Wien
 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 <QFont>
21 #include <QMap>
22 #include <QSettings>
23 #include <QString>
24 #include <QDomElement>
25 #include <QDomDocument>
26 #include <QDomNode>
27 
29 {
30  //get default composer font from settings
31  QSettings settings;
32  QString defaultFontString = settings.value( "/Composer/defaultFont" ).toString();
33  if ( !defaultFontString.isEmpty() )
34  {
35  mFont.setFamily( defaultFontString );
36  }
37 }
38 
40 {
41 }
42 
43 void QgsComposerLegendStyle::setMargin( double margin )
44 {
45  mMarginMap[Top] = margin;
46  mMarginMap[Bottom] = margin;
47  mMarginMap[Left] = margin;
48  mMarginMap[Right] = margin;
49 }
50 
51 void QgsComposerLegendStyle::writeXML( QString name, QDomElement& elem, QDomDocument & doc ) const
52 {
53  if ( elem.isNull() ) return;
54 
55  QDomElement styleElem = doc.createElement( "style" );
56 
57  styleElem.setAttribute( "name", name );
58 
59  if ( mMarginMap[Top] != 0 ) styleElem.setAttribute( "marginTop", QString::number( mMarginMap[Top] ) );
60  if ( mMarginMap[Bottom] != 0 ) styleElem.setAttribute( "marginBottom", QString::number( mMarginMap[Bottom] ) );
61  if ( mMarginMap[Left] != 0 ) styleElem.setAttribute( "marginLeft", QString::number( mMarginMap[Left] ) );
62  if ( mMarginMap[Right] != 0 ) styleElem.setAttribute( "marginRight", QString::number( mMarginMap[Right] ) );
63 
64  styleElem.setAttribute( "font", mFont.toString() );
65 
66  elem.appendChild( styleElem );
67 }
68 
69 void QgsComposerLegendStyle::readXML( const QDomElement& elem, const QDomDocument& doc )
70 {
71  Q_UNUSED( doc );
72  if ( elem.isNull() ) return;
73 
74  mFont.fromString( elem.attribute( "font" ) );
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 }