QGIS API Documentation 4.0.0-Norrköping (1ddcee3d0e4)
Loading...
Searching...
No Matches
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
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
20#include "qgis.h"
21#include "qgsfontutils.h"
23#include "qgsreadwritecontext.h"
24
25#include <QDomDocument>
26#include <QDomElement>
27#include <QDomNode>
28#include <QFont>
29#include <QMap>
30#include <QString>
31
32using namespace Qt::StringLiterals;
33
36
37void QgsLegendStyle::setFont( const QFont &font )
38{
39 mTextFormat.setFont( font );
40 if ( font.pointSizeF() > 0 )
41 {
42 mTextFormat.setSize( font.pointSizeF() );
43 mTextFormat.setSizeUnit( Qgis::RenderUnit::Points );
44 }
45 else if ( font.pixelSize() > 0 )
46 {
47 mTextFormat.setSize( font.pixelSize() );
48 mTextFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
49 }
50}
51
53{
54 mMarginMap[Top] = margin;
55 mMarginMap[Bottom] = margin;
56 mMarginMap[Left] = margin;
57 mMarginMap[Right] = margin;
58}
59
60void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
61{
62 if ( elem.isNull() )
63 return;
64
65 QDomElement styleElem = doc.createElement( u"style"_s );
66
67 styleElem.setAttribute( u"name"_s, name );
68 styleElem.setAttribute( u"alignment"_s, QString::number( mAlignment ) );
69 styleElem.setAttribute( u"indent"_s, QString::number( mIndent ) );
70
71 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
72 styleElem.setAttribute( u"marginTop"_s, QString::number( mMarginMap[Top] ) );
73 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
74 styleElem.setAttribute( u"marginBottom"_s, QString::number( mMarginMap[Bottom] ) );
75 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
76 styleElem.setAttribute( u"marginLeft"_s, QString::number( mMarginMap[Left] ) );
77 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
78 styleElem.setAttribute( u"marginRight"_s, QString::number( mMarginMap[Right] ) );
79
80 QDomElement textElem = mTextFormat.writeXml( doc, context );
81 styleElem.appendChild( textElem );
82
83 elem.appendChild( styleElem );
84}
85
86void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context )
87{
88 Q_UNUSED( doc )
89 if ( elem.isNull() )
90 return;
91
92 QDomNodeList textFormatNodeList = elem.elementsByTagName( u"text-style"_s );
93 if ( !textFormatNodeList.isEmpty() )
94 {
95 QDomElement textFormatElem = textFormatNodeList.at( 0 ).toElement();
96 mTextFormat.readXml( textFormatElem, context );
97 }
98 else
99 {
100 QFont f;
101 if ( !QgsFontUtils::setFromXmlChildNode( f, elem, u"styleFont"_s ) )
102 {
103 f.fromString( elem.attribute( u"font"_s ) );
104 }
105 mTextFormat = QgsTextFormat::fromQFont( f );
106 }
107
108 mMarginMap[Top] = elem.attribute( u"marginTop"_s, u"0"_s ).toDouble();
109 mMarginMap[Bottom] = elem.attribute( u"marginBottom"_s, u"0"_s ).toDouble();
110 mMarginMap[Left] = elem.attribute( u"marginLeft"_s, u"0"_s ).toDouble();
111 mMarginMap[Right] = elem.attribute( u"marginRight"_s, u"0"_s ).toDouble();
112
113 mAlignment = static_cast< Qt::Alignment >( elem.attribute( u"alignment"_s, QString::number( Qt::AlignLeft ) ).toInt() );
114 mIndent = elem.attribute( u"indent"_s, u"0"_s ).toDouble();
115}
116
118{
119 if ( mTextFormat.dataDefinedProperties().hasActiveProperties() ) // note, we use format instead of tmpFormat here, it's const and potentially avoids a detach
120 mTextFormat.updateDataDefinedProperties( context );
121}
122
124{
125 switch ( s )
126 {
128 return QString();
130 return u"hidden"_s;
132 return u"title"_s;
134 return u"group"_s;
136 return u"subgroup"_s;
138 return u"symbol"_s;
140 return u"symbolLabel"_s;
141 }
142 return QString();
143}
144
146{
147 if ( styleName == "hidden"_L1 )
149 else if ( styleName == "title"_L1 )
151 else if ( styleName == "group"_L1 )
153 else if ( styleName == "subgroup"_L1 )
155 else if ( styleName == "symbol"_L1 )
157 else if ( styleName == "symbolLabel"_L1 )
160}
161
163{
164 switch ( s )
165 {
167 return QObject::tr( "Undefined" );
169 return QObject::tr( "Hidden" );
171 return QObject::tr( "Title" );
173 return QObject::tr( "Group" );
175 return QObject::tr( "Subgroup" );
177 return QObject::tr( "Symbol" );
179 return QObject::tr( "Symbol label" );
180 }
181 return QString();
182}
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4719
@ Symbol
Symbol icon (excluding label).
Definition qgis.h:4725
@ Group
Legend group title.
Definition qgis.h:4723
@ Hidden
Special style, item is hidden including margins around.
Definition qgis.h:4721
@ Subgroup
Legend subgroup title.
Definition qgis.h:4724
@ Title
Legend title.
Definition qgis.h:4722
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4726
@ Undefined
Should not happen, only if corrupted project file.
Definition qgis.h:4720
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5345
@ Pixels
Pixels.
Definition qgis.h:5343
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.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for rendering this legend component.
static QString styleName(Qgis::LegendComponent s)
Returns the name for a style component as a string.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
void updateDataDefinedProperties(QgsRenderContext &context)
Updates any data-defined properties in the style, using the specified render context.
@ Right
Right side.
@ Left
Left side.
@ Bottom
Bottom side.
void readXml(const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext())
Reads the component's style definition from an XML element.
double margin(Side side) const
Returns the margin (in mm) for the specified side of the component.
static Qgis::LegendComponent styleFromName(const QString &styleName)
Returns the style from name string.
Q_DECL_DEPRECATED void setFont(const QFont &font)
Sets the font used for rendering this legend component.
void writeXml(const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context=QgsReadWriteContext()) const
Writes the component's style definition to an XML element.
static QString styleLabel(Qgis::LegendComponent s)
Returns a translated string representing a style component, for use in UI.
A container for the context for various read/write operations on objects.
Contains information about the context of a rendering operation.
static QgsTextFormat fromQFont(const QFont &font)
Returns a text format matching the settings from an input font.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference).
Definition qgis.h:6975