QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
43 }
44 
46 {
47  mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
48 }
49 
50 QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
51 {
52  const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
53  return splitStringForWrapping( textToRender );
54 }
55 
56 QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplt ) const
57 {
58  QStringList list;
59  // If the string contains nothing then just return the string without splitting.
60  if ( wrapChar().count() == 0 )
61  list << stringToSplt;
62  else
63  list = stringToSplt.split( wrapChar() );
64  return list;
65 }
66 
67 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
68 
69 
70 void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
71 {
72  QFont textFont = scaledFontPixelSize( font );
73 
74  p->save();
75  p->setFont( textFont );
76  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
77  p->scale( scaleFactor, scaleFactor );
78  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
79  p->restore();
80 }
81 
82 
83 void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
84 {
85  QFont textFont = scaledFontPixelSize( font );
86 
87  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
88  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
89 
90  p->save();
91  p->setFont( textFont );
92  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
93  p->scale( scaleFactor, scaleFactor );
94  p->drawText( scaledRect, halignment | valignment | flags, text );
95  p->restore();
96 }
97 
98 
99 QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
100 {
101  QFont scaledFont = font;
102  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
103  scaledFont.setPixelSize( pixelSize );
104  return scaledFont;
105 }
106 
107 double QgsLegendSettings::pixelFontSize( double pointSize ) const
108 {
109  return ( pointSize * 0.3527 );
110 }
111 
112 double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
113 {
114  QFont metricsFont = scaledFontPixelSize( font );
115  QFontMetricsF fontMetrics( metricsFont );
116  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
117 }
118 
119 double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
120 {
121  QFont metricsFont = scaledFontPixelSize( font );
122  QFontMetricsF fontMetrics( metricsFont );
123  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
124 }
125 
126 double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
127 {
128  QFont metricsFont = scaledFontPixelSize( font );
129  QFontMetricsF fontMetrics( metricsFont );
130  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
131 }
132 
133 double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
134 {
135  QFont metricsFont = scaledFontPixelSize( font );
136  QFontMetricsF fontMetrics( metricsFont );
137  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
138 }
139 
double pixelFontSize(double pointSize) const
Calculates font to from point size to pixel size.
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
QString wrapChar() 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...
void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
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
QFont scaledFontPixelSize(const QFont &font) const
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
Symbol icon (excluding label)
Legend subgroup title.
void setMargin(Side side, double margin)
Sets the margin (in mm) for the specified side of the component.
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable style.
#define FONT_WORKAROUND_SCALE
QFont & rfont()
Returns a modifiable reference to the component&#39;s font.
Symbol label (excluding icon)
double mapUnitsPerPixel() const
Returns the factor of map units per pixel for symbols with size given in map units calculated by dpi ...
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
Legend group title.
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...