QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
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.
void setIndent(double indent)
Sets the indent (in mm) of a group or subgroup.
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