QGIS API Documentation  3.4.15-Madeira (e83d02e274)
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 
18 #include <QPainter>
19 
21  : mFontColor( QColor( 0, 0, 0 ) )
22  , mSymbolSize( 7, 4 )
23  , mWmsLegendSize( 50, 25 )
24  , mRasterStrokeColor( Qt::black )
25 {
32  rstyle( QgsLegendStyle::Title ).rfont().setPointSizeF( 16.0 );
33  rstyle( QgsLegendStyle::Group ).rfont().setPointSizeF( 14.0 );
34  rstyle( QgsLegendStyle::Subgroup ).rfont().setPointSizeF( 12.0 );
35  rstyle( QgsLegendStyle::SymbolLabel ).rfont().setPointSizeF( 12.0 );
36 }
37 
39 {
40  return 1 / ( mMmPerMapUnit * ( mDpi / 25.4 ) );
41 }
42 
44 {
45  mMmPerMapUnit = 1 / mapUnitsPerPixel / ( mDpi / 25.4 );
46 }
47 
48 QStringList QgsLegendSettings::splitStringForWrapping( const QString &stringToSplt ) const
49 {
50  QStringList list;
51  // If the string contains nothing then just return the string without splitting.
52  if ( wrapChar().count() == 0 )
53  list << stringToSplt;
54  else
55  list = stringToSplt.split( wrapChar() );
56  return list;
57 }
58 
59 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
60 
61 
62 void QgsLegendSettings::drawText( QPainter *p, double x, double y, const QString &text, const QFont &font ) const
63 {
64  QFont textFont = scaledFontPixelSize( font );
65 
66  p->save();
67  p->setFont( textFont );
68  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
69  p->scale( scaleFactor, scaleFactor );
70  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
71  p->restore();
72 }
73 
74 
75 void QgsLegendSettings::drawText( QPainter *p, const QRectF &rect, const QString &text, const QFont &font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
76 {
77  QFont textFont = scaledFontPixelSize( font );
78 
79  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
80  rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
81 
82  p->save();
83  p->setFont( textFont );
84  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
85  p->scale( scaleFactor, scaleFactor );
86  p->drawText( scaledRect, halignment | valignment | flags, text );
87  p->restore();
88 }
89 
90 
91 QFont QgsLegendSettings::scaledFontPixelSize( const QFont &font ) const
92 {
93  QFont scaledFont = font;
94  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
95  scaledFont.setPixelSize( pixelSize );
96  return scaledFont;
97 }
98 
99 double QgsLegendSettings::pixelFontSize( double pointSize ) const
100 {
101  return ( pointSize * 0.3527 );
102 }
103 
104 double QgsLegendSettings::textWidthMillimeters( const QFont &font, const QString &text ) const
105 {
106  QFont metricsFont = scaledFontPixelSize( font );
107  QFontMetricsF fontMetrics( metricsFont );
108  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
109 }
110 
111 double QgsLegendSettings::fontHeightCharacterMM( const QFont &font, QChar c ) const
112 {
113  QFont metricsFont = scaledFontPixelSize( font );
114  QFontMetricsF fontMetrics( metricsFont );
115  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
116 }
117 
118 double QgsLegendSettings::fontAscentMillimeters( const QFont &font ) const
119 {
120  QFont metricsFont = scaledFontPixelSize( font );
121  QFontMetricsF fontMetrics( metricsFont );
122  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
123 }
124 
125 double QgsLegendSettings::fontDescentMillimeters( const QFont &font ) const
126 {
127  QFont metricsFont = scaledFontPixelSize( font );
128  QFontMetricsF fontMetrics( metricsFont );
129  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
130 }
131 
QString wrapChar() const
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
double mapUnitsPerPixel() const
Returns the factor of map units per pixel for symbols with size given in map units calculated by mDpi...
void setMapUnitsPerPixel(double mapUnitsPerPixel)
Sets the mMmPerMapUnit calculated by mapUnitsPerPixel mostly taken from the map settings.
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
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
QStringList splitStringForWrapping(const QString &stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
Symbol without label.
void setMargin(Side side, double margin)
QgsLegendStyle & rstyle(QgsLegendStyle::Style s)
Returns reference to modifiable style.
#define FONT_WORKAROUND_SCALE
QFont & rfont()
Modifiable reference to font.
double pixelFontSize(double pointSize) const
Calculates font to from point size to pixel size.
QFont scaledFontPixelSize(const QFont &font) const
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...