QGIS API Documentation 3.30.0-'s-Hertogenbosch (f186b8efe0)
qgslegendsettings.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslegendsettings.cpp
3 --------------------------------------
4 Date : July 2014
5 Copyright : (C) 2014 by Martin Dobias
6 Email : wonder dot sk at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#include "qgslegendsettings.h"
18#include "qgsexpression.h"
19#include "qgsrendercontext.h"
20
21#include <QPainter>
22
24 : mSymbolSize( 7, 4 )
25 , mWmsLegendSize( 50, 25 )
26 , mRasterStrokeColor( Qt::black )
27{
36
38 f.setSize( 16.0 );
39 f.setSizeUnit( Qgis::RenderUnit::Points );
40 // these default line heights are not ideal, but needed to maintain api
41 f.setLineHeight( 1.1 );
42 f.setLineHeightUnit( Qgis::RenderUnit::Percentage );
44
46 f.setSize( 14.0 );
47 f.setSizeUnit( Qgis::RenderUnit::Points );
48 f.setLineHeight( 1.1 );
49 f.setLineHeightUnit( Qgis::RenderUnit::Percentage );
51
53 f.setSize( 12.0 );
54 f.setSizeUnit( Qgis::RenderUnit::Points );
55 f.setLineHeight( 1.1 );
56 f.setLineHeightUnit( Qgis::RenderUnit::Percentage );
58
60 f.setSize( 12.0 );
61 f.setSizeUnit( Qgis::RenderUnit::Points );
62 f.setLineHeight( 1.1 );
63 f.setLineHeightUnit( Qgis::RenderUnit::Percentage );
65}
66
68{
70}
71
73{
78}
79
81{
83}
84
85void QgsLegendSettings::setLayerFontColor( const QColor &fontColor )
86{
89}
90
92{
93 // line spacing *was* a fixed amount (in mm) added between each line of text.
94 mLineSpacing = s;
95
96 QgsTextFormat f = rstyle( QgsLegendStyle::Title ).textFormat();
97 // assume font sizes in points, since that was what we always had from before this method was deprecated
98 f.setLineHeight( f.size() * 0.352778 + s );
99 f.setLineHeightUnit( Qgis::RenderUnit::Millimeters );
100 rstyle( QgsLegendStyle::Title ).setTextFormat( f );
101
102 f = rstyle( QgsLegendStyle::Group ).textFormat();
103 f.setLineHeight( f.size() * 0.352778 + s );
104 f.setLineHeightUnit( Qgis::RenderUnit::Millimeters );
105 rstyle( QgsLegendStyle::Group ).setTextFormat( f );
106
107 f = rstyle( QgsLegendStyle::Subgroup ).textFormat();
108 f.setLineHeight( f.size() * 0.352778 + s );
109 f.setLineHeightUnit( Qgis::RenderUnit::Millimeters );
110 rstyle( QgsLegendStyle::Subgroup ).setTextFormat( f );
111
112 f = rstyle( QgsLegendStyle::SymbolLabel ).textFormat();
113 f.setLineHeight( f.size() * 0.352778 + s );
114 f.setLineHeightUnit( Qgis::RenderUnit::Millimeters );
115 rstyle( QgsLegendStyle::SymbolLabel ).setTextFormat( f );
116}
117
119{
120 return mMmPerMapUnit;
121}
122
123void QgsLegendSettings::setMmPerMapUnit( double mmPerMapUnit )
124{
125 mMmPerMapUnit = mmPerMapUnit;
126}
127
129{
130 return mUseAdvancedEffects;
131}
132
134{
135 mUseAdvancedEffects = use;
136}
137
139{
140 return mMapScale;
141}
142
144{
145 mMapScale = scale;
146}
147
149{
150 return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
151}
152
153void QgsLegendSettings::setMapUnitsPerPixel( double mapUnitsPerPixel )
154{
155 mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
156}
157
159{
160 return mDpi;
161}
162
164{
165 mDpi = dpi;
166}
167
168QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
169{
170 const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
171 return splitStringForWrapping( textToRender );
172}
173
174QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplit ) const
175{
176 const QStringList lines = stringToSplit.split( '\n' );
177
178 // If the string contains nothing then just return the string without splitting.
179 if ( wrapChar().isEmpty() )
180 return lines;
181
182 QStringList res;
183 for ( const QString &line : lines )
184 {
185 res.append( line.split( wrapChar() ) );
186 }
187 return res;
188}
189
190#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
191
192
193void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
194{
195 const QFont textFont = scaledFontPixelSize( font );
196
197 const QgsScopedQPainterState painterState( p );
198 p->setFont( textFont );
199 const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
200 p->scale( scaleFactor, scaleFactor );
201 p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
202}
203
204
205void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
206{
207 const QFont textFont = scaledFontPixelSize( font );
208
209 const QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
210 rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
211
212 const QgsScopedQPainterState painterState( p );
213 p->setFont( textFont );
214 const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
215 p->scale( scaleFactor, scaleFactor );
216 p->drawText( scaledRect, halignment | valignment | flags, text );
217}
218
219
220QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
221{
222 QFont scaledFont = font;
223 const double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
224 scaledFont.setPixelSize( pixelSize );
225 return scaledFont;
226}
227
228double QgsLegendSettings::pixelFontSize( double pointSize ) const
229{
230 return ( pointSize * 0.3527 );
231}
232
233double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
234{
235 const QFont metricsFont = scaledFontPixelSize( font );
236 const QFontMetricsF fontMetrics( metricsFont );
237 return ( fontMetrics.horizontalAdvance( text ) / FONT_WORKAROUND_SCALE );
238}
239
240double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
241{
242 const QFont metricsFont = scaledFontPixelSize( font );
243 const QFontMetricsF fontMetrics( metricsFont );
244 return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
245}
246
247double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
248{
249 const QFont metricsFont = scaledFontPixelSize( font );
250 const QFontMetricsF fontMetrics( metricsFont );
251 return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
252}
253
254double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
255{
256 const QFont metricsFont = scaledFontPixelSize( font );
257 const QFontMetricsF fontMetrics( metricsFont );
258 return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
259}
260
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
static QString replaceExpressionText(const QString &action, const QgsExpressionContext *context, const QgsDistanceArea *distanceArea=nullptr)
This function replaces each expression between [% and %] in the string with the result of its evaluat...
QString wrapChar() const
Returns the string used as a wrapping character.
Q_DECL_DEPRECATED void setFontColor(const QColor &c)
Sets the font color used for legend items.
Q_DECL_DEPRECATED QColor layerFontColor() const
Returns layer font color, defaults to fontColor()
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
QgsLegendStyle style(QgsLegendStyle::Style s) const
Returns the style for a legend component.
Q_DECL_DEPRECATED void setLayerFontColor(const QColor &fontColor)
Sets layer font color to fontColor Overrides fontColor()
Q_DECL_DEPRECATED void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
Q_DECL_DEPRECATED void setLineSpacing(double s)
Sets the line spacing to use between lines of legend text.
Q_DECL_DEPRECATED void setMmPerMapUnit(double mmPerMapUnit)
Q_DECL_DEPRECATED void setDpi(int dpi)
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
Q_DECL_DEPRECATED QColor fontColor() const
Returns the font color used for legend items.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
Q_DECL_DEPRECATED bool useAdvancedEffects() const
double pixelFontSize(double pointSize) const
Calculates font to from point size to pixel size.
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
Q_DECL_DEPRECATED void setUseAdvancedEffects(bool use)
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns modifiable reference to the style for a legend component.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
Q_DECL_DEPRECATED int dpi() const
QFont scaledFontPixelSize(const QFont &font) const
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
Q_DECL_DEPRECATED double mapUnitsPerPixel() const
Returns the factor of map units per pixel for symbols with size given in map units calculated by dpi ...
Q_DECL_DEPRECATED double mmPerMapUnit() const
QStringList evaluateItemText(const QString &text, const QgsExpressionContext &context) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
Q_DECL_DEPRECATED double mapScale() const
Returns the legend map scale.
void setIndent(double indent)
Sets the indent (in mm) of a group or subgroup.
QgsTextFormat & textFormat()
Returns the text format used for rendering this legend component.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
@ Left
Left side.
@ Bottom
Bottom side.
@ Top
Top side.
@ Group
Legend group title.
@ Symbol
Symbol icon (excluding label)
@ Subgroup
Legend subgroup title.
@ Title
Legend title.
@ SymbolLabel
Symbol label (excluding icon)
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for rendering this legend component.
Scoped object for saving and restoring a QPainter object's state.
Container for all settings relating to text rendering.
Definition: qgstextformat.h:41
void setColor(const QColor &color)
Sets the color that text will be rendered in.
void setSize(double size)
Sets the size for rendered text.
void setSizeUnit(Qgis::RenderUnit unit)
Sets the units for the size of rendered text.
void setLineHeightUnit(Qgis::RenderUnit unit)
Sets the unit for the line height for text.
double size() const
Returns the size for rendered text.
QColor color() const
Returns the color that text will be rendered in.
void setLineHeight(double height)
Sets the line height for text.
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define SIP_DEPRECATED
Definition: qgis_sip.h:106
#define FONT_WORKAROUND_SCALE