/
qgscomposerlegendstyle.cpp
139 lines (122 loc) · 4.32 KB
/
qgscomposerlegendstyle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/***************************************************************************
qgscomposerlegendstyle.cpp
---------------------
begin : March 2013
copyright : (C) 2013 by Radim Blazek
email : radim.blazek@gmail.com
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgscomposerlegendstyle.h"
#include "qgscomposition.h"
#include "qgsfontutils.h"
#include <QFont>
#include <QMap>
#include <QSettings>
#include <QString>
#include <QDomElement>
#include <QDomDocument>
#include <QDomNode>
QgsComposerLegendStyle::QgsComposerLegendStyle()
{
//get default composer font from settings
QSettings settings;
QString defaultFontString = settings.value( "/Composer/defaultFont" ).toString();
if ( !defaultFontString.isEmpty() )
{
mFont.setFamily( defaultFontString );
}
}
QgsComposerLegendStyle::~QgsComposerLegendStyle()
{
}
void QgsComposerLegendStyle::setMargin( double margin )
{
mMarginMap[Top] = margin;
mMarginMap[Bottom] = margin;
mMarginMap[Left] = margin;
mMarginMap[Right] = margin;
}
void QgsComposerLegendStyle::writeXML( QString name, QDomElement& elem, QDomDocument & doc ) const
{
if ( elem.isNull() ) return;
QDomElement styleElem = doc.createElement( "style" );
styleElem.setAttribute( "name", name );
if ( mMarginMap[Top] != 0 ) styleElem.setAttribute( "marginTop", QString::number( mMarginMap[Top] ) );
if ( mMarginMap[Bottom] != 0 ) styleElem.setAttribute( "marginBottom", QString::number( mMarginMap[Bottom] ) );
if ( mMarginMap[Left] != 0 ) styleElem.setAttribute( "marginLeft", QString::number( mMarginMap[Left] ) );
if ( mMarginMap[Right] != 0 ) styleElem.setAttribute( "marginRight", QString::number( mMarginMap[Right] ) );
styleElem.appendChild( QgsFontUtils::toXmlElement( mFont, doc, "styleFont" ) );
elem.appendChild( styleElem );
}
void QgsComposerLegendStyle::readXML( const QDomElement& elem, const QDomDocument& doc )
{
Q_UNUSED( doc );
if ( elem.isNull() ) return;
if ( !QgsFontUtils::setFromXmlChildNode( mFont, elem, "styleFont" ) )
{
mFont.fromString( elem.attribute( "font" ) );
}
mMarginMap[Top] = elem.attribute( "marginTop", "0" ).toDouble();
mMarginMap[Bottom] = elem.attribute( "marginBottom", "0" ).toDouble();
mMarginMap[Left] = elem.attribute( "marginLeft", "0" ).toDouble();
mMarginMap[Right] = elem.attribute( "marginRight", "0" ).toDouble();
}
QString QgsComposerLegendStyle::styleName( Style s )
{
switch ( s )
{
case Undefined:
return "";
case Hidden:
return "hidden";
case Title:
return "title";
case Group:
return "group";
case Subgroup:
return "subgroup";
case Symbol:
return "symbol";
case SymbolLabel:
return "symbolLabel";
}
return "";
}
QgsComposerLegendStyle::Style QgsComposerLegendStyle::styleFromName( QString styleName )
{
if ( styleName == "hidden" ) return Hidden;
else if ( styleName == "title" ) return Title;
else if ( styleName == "group" ) return Group;
else if ( styleName == "subgroup" ) return Subgroup;
else if ( styleName == "symbol" ) return Symbol;
else if ( styleName == "symbolLabel" ) return SymbolLabel;
return Undefined;
}
QString QgsComposerLegendStyle::styleLabel( Style s )
{
switch ( s )
{
case Undefined:
return QObject::tr( "Undefined" );
case Hidden:
return QObject::tr( "Hidden" );
case Title:
return QObject::tr( "Title" );
case Group:
return QObject::tr( "Group" );
case Subgroup:
return QObject::tr( "Subgroup" );
case Symbol:
return QObject::tr( "Symbol" );
case SymbolLabel:
return QObject::tr( "Symbol label" );
}
return "";
}