QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 #include "qgsexpressioncontext.h"
18 #include "qgsexpression.h"
19 #include "qgsrendercontext.h"
20 
21 #include <QPainter>
22 
24  : mFontColor( QColor( 0, 0, 0 ) )
25  , mSymbolSize( 7, 4 )
26  , mWmsLegendSize( 50, 25 )
27  , mRasterStrokeColor( Qt::black )
28 {
35  rstyle( QgsLegendStyle::Title ).rfont().setPointSizeF( 16.0 );
36  rstyle( QgsLegendStyle::Group ).rfont().setPointSizeF( 14.0 );
37  rstyle( QgsLegendStyle::Subgroup ).rfont().setPointSizeF( 12.0 );
38  rstyle( QgsLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
41 }
42 
44 {
45  return mMmPerMapUnit;
46 }
47 
48 void QgsLegendSettings::setMmPerMapUnit( double mmPerMapUnit )
49 {
50  mMmPerMapUnit = mmPerMapUnit;
51 }
52 
54 {
55  return mUseAdvancedEffects;
56 }
57 
59 {
60  mUseAdvancedEffects = use;
61 }
62 
64 {
65  return mMapScale;
66 }
67 
68 void QgsLegendSettings::setMapScale( double scale )
69 {
70  mMapScale = scale;
71 }
72 
74 {
75  return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
76 }
77 
78 void QgsLegendSettings::setMapUnitsPerPixel( double mapUnitsPerPixel )
79 {
80  mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
81 }
82 
84 {
85  return mDpi;
86 }
87 
89 {
90  mDpi = dpi;
91 }
92 
93 QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
94 {
95  const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
96  return splitStringForWrapping( textToRender );
97 }
98 
99 QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplit ) const
100 {
101  const QStringList lines = stringToSplit.split( '\n' );
102 
103  // If the string contains nothing then just return the string without splitting.
104  if ( wrapChar().isEmpty() )
105  return lines;
106 
107  QStringList res;
108  for ( const QString &line : lines )
109  {
110  res.append( line.split( wrapChar() ) );
111  }
112  return res;
113 }
114 
115 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
116 
117 
118 void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
119 {
120  const QFont textFont = scaledFontPixelSize( font );
121 
122  const QgsScopedQPainterState painterState( p );
123  p->setFont( textFont );
124  const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
125  p->scale( scaleFactor, scaleFactor );
126  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
127 }
128 
129 
130 void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
131 {
132  const QFont textFont = scaledFontPixelSize( font );
133 
134  const QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
135  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
136 
137  const QgsScopedQPainterState painterState( p );
138  p->setFont( textFont );
139  const double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
140  p->scale( scaleFactor, scaleFactor );
141  p->drawText( scaledRect, halignment | valignment | flags, text );
142 }
143 
144 
145 QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
146 {
147  QFont scaledFont = font;
148  const double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
149  scaledFont.setPixelSize( pixelSize );
150  return scaledFont;
151 }
152 
153 double QgsLegendSettings::pixelFontSize( double pointSize ) const
154 {
155  return ( pointSize * 0.3527 );
156 }
157 
158 double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
159 {
160  const QFont metricsFont = scaledFontPixelSize( font );
161  const QFontMetricsF fontMetrics( metricsFont );
162  return ( fontMetrics.horizontalAdvance( text ) / FONT_WORKAROUND_SCALE );
163 }
164 
165 double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
166 {
167  const QFont metricsFont = scaledFontPixelSize( font );
168  const QFontMetricsF fontMetrics( metricsFont );
169  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
170 }
171 
172 double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
173 {
174  const QFont metricsFont = scaledFontPixelSize( font );
175  const QFontMetricsF fontMetrics( metricsFont );
176  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
177 }
178 
179 double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
180 {
181  const QFont metricsFont = scaledFontPixelSize( font );
182  const QFontMetricsF fontMetrics( metricsFont );
183  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
184 }
185 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:406
QgsLegendSettings::splitStringForWrapping
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
Definition: qgslegendsettings.cpp:99
QgsLegendStyle::Symbol
@ Symbol
Symbol icon (excluding label)
Definition: qgslegendstyle.h:73
QgsLegendSettings::pixelFontSize
double pixelFontSize(double pointSize) const
Calculates font to from point size to pixel size.
Definition: qgslegendsettings.cpp:153
QgsLegendSettings::useAdvancedEffects
Q_DECL_DEPRECATED bool useAdvancedEffects() const
Definition: qgslegendsettings.cpp:53
QgsLegendSettings::fontAscentMillimeters
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
Definition: qgslegendsettings.cpp:172
QgsLegendSettings::mmPerMapUnit
Q_DECL_DEPRECATED double mmPerMapUnit() const
Definition: qgslegendsettings.cpp:43
QgsLegendSettings::textWidthMillimeters
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
Definition: qgslegendsettings.cpp:158
qgsexpression.h
QgsLegendSettings::drawText
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Definition: qgslegendsettings.cpp:118
QgsLegendSettings::setMapUnitsPerPixel
Q_DECL_DEPRECATED void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
Definition: qgslegendsettings.cpp:78
QgsLegendStyle::setMargin
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
Definition: qgslegendstyle.h:113
QgsLegendSettings::mapScale
Q_DECL_DEPRECATED double mapScale() const
Returns the legend map scale.
Definition: qgslegendsettings.cpp:63
QgsLegendSettings::setMmPerMapUnit
Q_DECL_DEPRECATED void setMmPerMapUnit(double mmPerMapUnit)
Definition: qgslegendsettings.cpp:48
QgsLegendStyle::Bottom
@ Bottom
Bottom side.
Definition: qgslegendstyle.h:70
QgsLegendStyle::SymbolLabel
@ SymbolLabel
Symbol label (excluding icon)
Definition: qgslegendstyle.h:74
QgsLegendStyle::rfont
QFont & rfont()
Returns a modifiable reference to the component's font.
Definition: qgslegendstyle.h:95
QgsLegendSettings::dpi
Q_DECL_DEPRECATED int dpi() const
Definition: qgslegendsettings.cpp:83
QgsLegendStyle::setIndent
void setIndent(double indent)
Sets the indent (in mm) of a group or subgroup.
Definition: qgslegendstyle.h:151
QgsLegendStyle::Title
@ Title
Legend title.
Definition: qgslegendstyle.h:70
QgsLegendSettings::scaledFontPixelSize
QFont scaledFontPixelSize(const QFont &font) const
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
Definition: qgslegendsettings.cpp:145
qgsexpressioncontext.h
qgslegendsettings.h
qgsrendercontext.h
FONT_WORKAROUND_SCALE
#define FONT_WORKAROUND_SCALE
Definition: qgslegendsettings.cpp:115
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
QgsLegendSettings::setDpi
Q_DECL_DEPRECATED void setDpi(int dpi)
Definition: qgslegendsettings.cpp:88
QgsLegendSettings::setMapScale
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
Definition: qgslegendsettings.cpp:68
QgsLegendStyle::Left
@ Left
Left side.
Definition: qgslegendstyle.h:71
c
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
Definition: porting_processing.dox:1
QgsLegendSettings::QgsLegendSettings
QgsLegendSettings()
Definition: qgslegendsettings.cpp:23
QgsLegendSettings::fontHeightCharacterMM
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
Definition: qgslegendsettings.cpp:165
QgsLegendSettings::setUseAdvancedEffects
Q_DECL_DEPRECATED void setUseAdvancedEffects(bool use)
Definition: qgslegendsettings.cpp:58
QgsLegendSettings::rstyle
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns modifiable reference to the style for a legend component.
Definition: qgslegendsettings.h:74
QgsLegendSettings::fontDescentMillimeters
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
Definition: qgslegendsettings.cpp:179
QgsLegendStyle::Top
@ Top
Top side.
Definition: qgslegendstyle.h:69
QgsExpression::replaceExpressionText
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...
Definition: qgsexpression.cpp:434
QgsLegendStyle::Group
@ Group
Legend group title.
Definition: qgslegendstyle.h:71
QgsLegendStyle::Subgroup
@ Subgroup
Legend subgroup title.
Definition: qgslegendstyle.h:72
QgsLegendSettings::mapUnitsPerPixel
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 ...
Definition: qgslegendsettings.cpp:73
QgsLegendSettings::wrapChar
QString wrapChar() const
Returns the string used as a wrapping character.
Definition: qgslegendsettings.h:124
QgsLegendSettings::evaluateItemText
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...
Definition: qgslegendsettings.cpp:93