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