QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 
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.
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.
Q_DECL_DEPRECATED void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
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...
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns modifiable reference to the style for a legend component.
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)
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.
QFont & rfont()
Returns a modifiable reference to the component's font.
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)
Scoped object for saving and restoring a QPainter object's state.
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 FONT_WORKAROUND_SCALE