QGIS API Documentation 3.99.0-Master (d270888f95f)
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
37
38void QgsLegendStyle::setFont( const QFont &font )
39{
40 mTextFormat.setFont( font );
41 if ( font.pointSizeF() > 0 )
42 {
43 mTextFormat.setSize( font.pointSizeF() );
44 mTextFormat.setSizeUnit( Qgis::RenderUnit::Points );
45 }
46 else if ( font.pixelSize() > 0 )
47 {
48 mTextFormat.setSize( font.pixelSize() );
49 mTextFormat.setSizeUnit( Qgis::RenderUnit::Pixels );
50 }
51}
52
54{
55 mMarginMap[Top] = margin;
56 mMarginMap[Bottom] = margin;
57 mMarginMap[Left] = margin;
58 mMarginMap[Right] = margin;
59}
60
61void QgsLegendStyle::writeXml( const QString &name, QDomElement &elem, QDomDocument &doc, const QgsReadWriteContext &context ) const
62{
63 if ( elem.isNull() )
64 return;
65
66 QDomElement styleElem = doc.createElement( u"style"_s );
67
68 styleElem.setAttribute( u"name"_s, name );
69 styleElem.setAttribute( u"alignment"_s, QString::number( mAlignment ) );
70 styleElem.setAttribute( u"indent"_s, QString::number( mIndent ) );
71
72 if ( !qgsDoubleNear( mMarginMap[Top], 0.0 ) )
73 styleElem.setAttribute( u"marginTop"_s, QString::number( mMarginMap[Top] ) );
74 if ( !qgsDoubleNear( mMarginMap[Bottom], 0.0 ) )
75 styleElem.setAttribute( u"marginBottom"_s, QString::number( mMarginMap[Bottom] ) );
76 if ( !qgsDoubleNear( mMarginMap[Left], 0.0 ) )
77 styleElem.setAttribute( u"marginLeft"_s, QString::number( mMarginMap[Left] ) );
78 if ( !qgsDoubleNear( mMarginMap[Right], 0.0 ) )
79 styleElem.setAttribute( u"marginRight"_s, QString::number( mMarginMap[Right] ) );
80
81 QDomElement textElem = mTextFormat.writeXml( doc, context );
82 styleElem.appendChild( textElem );
83
84 elem.appendChild( styleElem );
85}
86
87void QgsLegendStyle::readXml( const QDomElement &elem, const QDomDocument &doc, const QgsReadWriteContext &context )
88{
89 Q_UNUSED( doc )
90 if ( elem.isNull() ) 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}
123
125{
126 switch ( s )
127 {
129 return QString();
131 return u"hidden"_s;
133 return u"title"_s;
135 return u"group"_s;
137 return u"subgroup"_s;
139 return u"symbol"_s;
141 return u"symbolLabel"_s;
142 }
143 return QString();
144}
145
147{
148 if ( styleName == "hidden"_L1 )
150 else if ( styleName == "title"_L1 )
152 else if ( styleName == "group"_L1 )
154 else if ( styleName == "subgroup"_L1 )
156 else if ( styleName == "symbol"_L1 )
158 else if ( styleName == "symbolLabel"_L1 )
161}
162
164{
165 switch ( s )
166 {
168 return QObject::tr( "Undefined" );
170 return QObject::tr( "Hidden" );
172 return QObject::tr( "Title" );
174 return QObject::tr( "Group" );
176 return QObject::tr( "Subgroup" );
178 return QObject::tr( "Symbol" );
180 return QObject::tr( "Symbol label" );
181 }
182 return QString();
183}
LegendComponent
Component of legends which can be styled.
Definition qgis.h:4634
@ Symbol
Symbol icon (excluding label).
Definition qgis.h:4640
@ Group
Legend group title.
Definition qgis.h:4638
@ Hidden
Special style, item is hidden including margins around.
Definition qgis.h:4636
@ Subgroup
Legend subgroup title.
Definition qgis.h:4639
@ Title
Legend title.
Definition qgis.h:4637
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4641
@ Undefined
Should not happen, only if corrupted project file.
Definition qgis.h:4635
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5260
@ Pixels
Pixels.
Definition qgis.h:5258
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:6900