QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
20 #include <QPainter>
21 
23  : mFontColor( QColor( 0, 0, 0 ) )
24  , mSymbolSize( 7, 4 )
25  , mWmsLegendSize( 50, 25 )
26  , mRasterStrokeColor( Qt::black )
27 {
34  rstyle( QgsLegendStyle::Title ).rfont().setPointSizeF( 16.0 );
35  rstyle( QgsLegendStyle::Group ).rfont().setPointSizeF( 14.0 );
36  rstyle( QgsLegendStyle::Subgroup ).rfont().setPointSizeF( 12.0 );
37  rstyle( QgsLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
38 }
39 
41 {
42  return mMmPerMapUnit;
43 }
44 
45 void QgsLegendSettings::setMmPerMapUnit( double mmPerMapUnit )
46 {
47  mMmPerMapUnit = mmPerMapUnit;
48 }
49 
51 {
52  return mUseAdvancedEffects;
53 }
54 
56 {
57  mUseAdvancedEffects = use;
58 }
59 
61 {
62  return mMapScale;
63 }
64 
65 void QgsLegendSettings::setMapScale( double scale )
66 {
67  mMapScale = scale;
68 }
69 
71 {
72  return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
73 }
74 
75 void QgsLegendSettings::setMapUnitsPerPixel( double mapUnitsPerPixel )
76 {
77  mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
78 }
79 
81 {
82  return mDpi;
83 }
84 
86 {
87  mDpi = dpi;
88 }
89 
90 QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
91 {
92  const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
93  return splitStringForWrapping( textToRender );
94 }
95 
96 QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplit ) const
97 {
98  const QStringList lines = stringToSplit.split( '\n' );
99 
100  // If the string contains nothing then just return the string without splitting.
101  if ( wrapChar().isEmpty() )
102  return lines;
103 
104  QStringList res;
105  for ( const QString &line : lines )
106  {
107  res.append( line.split( wrapChar() ) );
108  }
109  return res;
110 }
111 
112 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
113 
114 
115 void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
116 {
117  QFont textFont = scaledFontPixelSize( font );
118 
119  p->save();
120  p->setFont( textFont );
121  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
122  p->scale( scaleFactor, scaleFactor );
123  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
124  p->restore();
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  p->save();
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  p->restore();
141 }
142 
143 
144 QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
145 {
146  QFont scaledFont = font;
147  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
148  scaledFont.setPixelSize( pixelSize );
149  return scaledFont;
150 }
151 
152 double QgsLegendSettings::pixelFontSize( double pointSize ) const
153 {
154  return ( pointSize * 0.3527 );
155 }
156 
157 double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
158 {
159  QFont metricsFont = scaledFontPixelSize( font );
160  QFontMetricsF fontMetrics( metricsFont );
161  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
162 }
163 
164 double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
165 {
166  QFont metricsFont = scaledFontPixelSize( font );
167  QFontMetricsF fontMetrics( metricsFont );
168  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
169 }
170 
171 double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
172 {
173  QFont metricsFont = scaledFontPixelSize( font );
174  QFontMetricsF fontMetrics( metricsFont );
175  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
176 }
177 
178 double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
179 {
180  QFont metricsFont = scaledFontPixelSize( font );
181  QFontMetricsF fontMetrics( metricsFont );
182  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
183 }
184 
QgsExpressionContext
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Definition: qgsexpressioncontext.h:369
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:96
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:152
QgsLegendSettings::useAdvancedEffects
Q_DECL_DEPRECATED bool useAdvancedEffects() const
Definition: qgslegendsettings.cpp:50
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:171
QgsLegendSettings::mmPerMapUnit
Q_DECL_DEPRECATED double mmPerMapUnit() const
Definition: qgslegendsettings.cpp:40
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:157
qgsexpression.h
QgsLegendSettings::drawText
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Definition: qgslegendsettings.cpp:115
QgsLegendSettings::setMapUnitsPerPixel
Q_DECL_DEPRECATED void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
Definition: qgslegendsettings.cpp:75
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:60
QgsLegendSettings::setMmPerMapUnit
Q_DECL_DEPRECATED void setMmPerMapUnit(double mmPerMapUnit)
Definition: qgslegendsettings.cpp:45
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:80
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:144
qgsexpressioncontext.h
qgslegendsettings.h
FONT_WORKAROUND_SCALE
#define FONT_WORKAROUND_SCALE
Definition: qgslegendsettings.cpp:112
QgsLegendSettings::setDpi
Q_DECL_DEPRECATED void setDpi(int dpi)
Definition: qgslegendsettings.cpp:85
QgsLegendSettings::setMapScale
Q_DECL_DEPRECATED void setMapScale(double scale)
Sets the legend map scale.
Definition: qgslegendsettings.cpp:65
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:22
QgsLegendSettings::fontHeightCharacterMM
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
Definition: qgslegendsettings.cpp:164
QgsLegendSettings::setUseAdvancedEffects
Q_DECL_DEPRECATED void setUseAdvancedEffects(bool use)
Definition: qgslegendsettings.cpp:55
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:178
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:430
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:70
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:90