QGIS API Documentation  3.6.0-Noosa (5873452)
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 
40 QStringList QgsLegendSettings::evaluateItemText( const QString &text, const QgsExpressionContext &context ) const
41 {
42  const QString textToRender = QgsExpression::replaceExpressionText( text, &context );
43  return splitStringForWrapping( textToRender );
44 }
45 
46 QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplt ) const
47 {
48  QStringList list;
49  // If the string contains nothing then just return the string without splitting.
50  if ( wrapChar().count() == 0 )
51  list << stringToSplt;
52  else
53  list = stringToSplt.split( wrapChar() );
54  return list;
55 }
56 
57 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
58 
59 
60 void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
61 {
62  QFont textFont = scaledFontPixelSize( font );
63 
64  p->save();
65  p->setFont( textFont );
66  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
67  p->scale( scaleFactor, scaleFactor );
68  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
69  p->restore();
70 }
71 
72 
73 void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
74 {
75  QFont textFont = scaledFontPixelSize( font );
76 
77  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
78  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
79 
80  p->save();
81  p->setFont( textFont );
82  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
83  p->scale( scaleFactor, scaleFactor );
84  p->drawText( scaledRect, halignment | valignment | flags, text );
85  p->restore();
86 }
87 
88 
89 QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
90 {
91  QFont scaledFont = font;
92  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
93  scaledFont.setPixelSize( pixelSize );
94  return scaledFont;
95 }
96 
97 double QgsLegendSettings::pixelFontSize( double pointSize ) const
98 {
99  return ( pointSize * 0.3527 );
100 }
101 
102 double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
103 {
104  QFont metricsFont = scaledFontPixelSize( font );
105  QFontMetricsF fontMetrics( metricsFont );
106  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
107 }
108 
109 double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
110 {
111  QFont metricsFont = scaledFontPixelSize( font );
112  QFontMetricsF fontMetrics( metricsFont );
113  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
114 }
115 
116 double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
117 {
118  QFont metricsFont = scaledFontPixelSize( font );
119  QFontMetricsF fontMetrics( metricsFont );
120  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
121 }
122 
123 double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
124 {
125  QFont metricsFont = scaledFontPixelSize( font );
126  QFontMetricsF fontMetrics( metricsFont );
127  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
128 }
129 
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...
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 without label.
void setMargin(Side side, double margin)
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()
Modifiable reference to font.
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
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...