QGIS API Documentation 3.99.0-Master (2fe06baccd8)
Loading...
Searching...
No Matches
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"
17
18#include "qgsexpression.h"
20#include "qgsrendercontext.h"
21
22#include <QPainter>
23
25 : mSymbolSize( 7, 4 )
26 , mWmsLegendSize( 50, 25 )
27 , mRasterStrokeColor( Qt::black )
28{
29 mStyleMap.resize( static_cast< int >( Qgis::LegendComponent::SymbolLabel ) + 1 );
30
39
41 f.setSize( 16.0 );
43 // these default line heights are not ideal, but needed to maintain api
44 f.setLineHeight( 1.1 );
47
49 f.setSize( 14.0 );
51 f.setLineHeight( 1.1 );
54
56 f.setSize( 12.0 );
58 f.setLineHeight( 1.1 );
61
63 f.setSize( 12.0 );
65 f.setLineHeight( 1.1 );
68}
69
77
82
90
95
101
103{
104 // line spacing *was* a fixed amount (in mm) added between each line of text.
105 mLineSpacing = s;
106
108 // assume font sizes in points, since that was what we always had from before this method was deprecated
109 f.setLineHeight( f.size() * 0.352778 + s );
111 rstyle( Qgis::LegendComponent::Title ).setTextFormat( f );
112
113 f = rstyle( Qgis::LegendComponent::Group ).textFormat();
114 f.setLineHeight( f.size() * 0.352778 + s );
116 rstyle( Qgis::LegendComponent::Group ).setTextFormat( f );
117
118 f = rstyle( Qgis::LegendComponent::Subgroup ).textFormat();
119 f.setLineHeight( f.size() * 0.352778 + s );
121 rstyle( Qgis::LegendComponent::Subgroup ).setTextFormat( f );
122
123 f = rstyle( Qgis::LegendComponent::SymbolLabel ).textFormat();
124 f.setLineHeight( f.size() * 0.352778 + s );
126 rstyle( Qgis::LegendComponent::SymbolLabel ).setTextFormat( f );
127}
128
130{
131 return mMmPerMapUnit;
132}
133
135{
136 mMmPerMapUnit = mmPerMapUnit;
137}
138
140{
141 return mUseAdvancedEffects;
142}
143
145{
146 mUseAdvancedEffects = use;
147}
148
150{
151 return mMapScale;
152}
153
155{
156 mMapScale = scale;
157}
158
160{
161 return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
162}
163
165{
166 mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
167}
168
170{
171 return mDpi;
172}
173
175{
176 mDpi = dpi;
177}
178
179QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
180{
181 const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
182 return splitStringForWrapping( textToRender );
183}
184
185QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplit ) const
186{
187 const QStringList lines = stringToSplit.split( '\n' );
188
189 // If the string contains nothing then just return the string without splitting.
190 if ( wrapChar().isEmpty() )
191 return lines;
192
193 QStringList res;
194 for ( const QString &line : lines )
195 {
196 res.append( line.split( wrapChar() ) );
197 }
198 return res;
199}
200
201#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
202
203
204void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
205{
206 const QFont textFont = scaledFontPixelSize( font );
207
208 const QgsScopedQPainterState painterState( p );
209 p->setFont( textFont );
210 const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
211 p->scale( scaleFactor, scaleFactor );
212 p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
213}
214
215
216void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
217{
218 const QFont textFont = scaledFontPixelSize( font );
219
220 const QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
221 rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
222
223 const QgsScopedQPainterState painterState( p );
224 p->setFont( textFont );
225 const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
226 p->scale( scaleFactor, scaleFactor );
227 p->drawText( scaledRect, halignment | valignment | flags, text );
228}
229
230
231QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
232{
233 QFont scaledFont = font;
234 const double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
235 scaledFont.setPixelSize( pixelSize );
236 return scaledFont;
237}
238
239double QgsLegendSettings::pixelFontSize( double pointSize ) const
240{
241 return ( pointSize * 0.3527 );
242}
243
244double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
245{
246 const QFont metricsFont = scaledFontPixelSize( font );
247 const QFontMetricsF fontMetrics( metricsFont );
248 return ( fontMetrics.horizontalAdvance( text ) / FONT_WORKAROUND_SCALE );
249}
250
251double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
252{
253 const QFont metricsFont = scaledFontPixelSize( font );
254 const QFontMetricsF fontMetrics( metricsFont );
255 return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
256}
257
258double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
259{
260 const QFont metricsFont = scaledFontPixelSize( font );
261 const QFontMetricsF fontMetrics( metricsFont );
262 return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
263}
264
265double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
266{
267 const QFont metricsFont = scaledFontPixelSize( font );
268 const QFontMetricsF fontMetrics( metricsFont );
269 return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
270}
271
273{
274 return mJsonRenderFlags;
275}
276
@ Symbol
Symbol icon (excluding label).
Definition qgis.h:4581
@ Group
Legend group title.
Definition qgis.h:4579
@ Subgroup
Legend subgroup title.
Definition qgis.h:4580
@ Title
Legend title.
Definition qgis.h:4578
@ SymbolLabel
Symbol label (excluding icon).
Definition qgis.h:4582
@ Percentage
Percentage of another measurement (e.g., canvas size, feature size).
Definition qgis.h:5187
@ Millimeters
Millimeters.
Definition qgis.h:5184
@ Points
Points (e.g., for font sizes).
Definition qgis.h:5188
QFlags< LegendJsonRenderFlag > LegendJsonRenderFlags
Definition qgis.h:4599
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.
QgsLegendStyle & rstyle(Qgis::LegendComponent s)
Returns modifiable reference to the style for a legend component.
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
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)
void setJsonRenderFlags(const Qgis::LegendJsonRenderFlags &jsonRenderFlags)
Sets the JSON export flags to jsonRenderFlags.
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)
void updateDataDefinedProperties(QgsRenderContext &context)
Updates any data-defined properties in the settings, using the specified render context.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
QgsLegendStyle style(Qgis::LegendComponent s) const
Returns the style for a legend component.
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...
Qgis::LegendJsonRenderFlags jsonRenderFlags() const
Returns the JSON export flags.
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.
void updateDataDefinedProperties(QgsRenderContext &context)
Updates any data-defined properties in the style, using the specified render context.
@ Left
Left side.
@ Bottom
Bottom side.
void setTextFormat(const QgsTextFormat &format)
Sets the text format used for rendering this legend component.
Contains information about the context of a rendering operation.
Scoped object for saving and restoring a QPainter object's state.
Container for all settings relating to text rendering.
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:114
#define FONT_WORKAROUND_SCALE