QGIS API Documentation  2.17.0-Master (0497e4a)
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  : mTitle( QObject::tr( "Legend" ) )
22  , mTitleAlignment( Qt::AlignLeft )
23  , mWrapChar( "" )
24  , mFontColor( QColor( 0, 0, 0 ) )
25  , mBoxSpace( 2 )
26  , mSymbolSize( 7, 4 )
27  , mWmsLegendSize( 50, 25 )
28  , mLineSpacing( 1.5 )
29  , mColumnSpace( 2 )
30  , mColumnCount( 1 )
31  , mSplitLayer( false )
32  , mEqualColumnWidth( false )
33  , mRasterSymbolBorder( true )
34  , mRasterBorderColor( Qt::black )
35  , mRasterBorderWidth( 0.0 )
36  , mMmPerMapUnit( 1 )
37  , mUseAdvancedEffects( true )
38  , mMapScale( 1 )
39  , mDpi( 96 ) // based on QImage's default DPI
40 {
51 }
52 
54 {
55  QStringList list;
56  // If the string contains nothing then just return the string without spliting.
57  if ( wrapChar().count() == 0 )
58  list << stringToSplt;
59  else
60  list = stringToSplt.split( wrapChar() );
61  return list;
62 }
63 
64 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
65 
66 
67 void QgsLegendSettings::drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const
68 {
69  QFont textFont = scaledFontPixelSize( font );
70 
71  p->save();
72  p->setFont( textFont );
73  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
74  p->scale( scaleFactor, scaleFactor );
75  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
76  p->restore();
77 }
78 
79 
80 void QgsLegendSettings::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
81 {
82  QFont textFont = scaledFontPixelSize( font );
83 
84  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
86 
87  p->save();
88  p->setFont( textFont );
89  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
90  p->scale( scaleFactor, scaleFactor );
91  p->drawText( scaledRect, halignment | valignment | flags, text );
92  p->restore();
93 }
94 
95 
97 {
98  QFont scaledFont = font;
99  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
100  scaledFont.setPixelSize( pixelSize );
101  return scaledFont;
102 }
103 
104 double QgsLegendSettings::pixelFontSize( double pointSize ) const
105 {
106  return ( pointSize * 0.3527 );
107 }
108 
109 double QgsLegendSettings::textWidthMillimeters( const QFont& font, const QString& text ) const
110 {
111  QFont metricsFont = scaledFontPixelSize( font );
112  QFontMetricsF fontMetrics( metricsFont );
113  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
114 }
115 
117 {
118  QFont metricsFont = scaledFontPixelSize( font );
119  QFontMetricsF fontMetrics( metricsFont );
120  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
121 }
122 
124 {
125  QFont metricsFont = scaledFontPixelSize( font );
126  QFontMetricsF fontMetrics( metricsFont );
127  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
128 }
129 
131 {
132  QFont metricsFont = scaledFontPixelSize( font );
133  QFontMetricsF fontMetrics( metricsFont );
134  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
135 }
136 
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...
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
void setMargin(Side side, double margin)
QString wrapChar() const
qreal x() const
qreal y() const
qreal pointSizeF() const
void scale(qreal sx, qreal sy)
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 save()
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
qreal width(const QString &text) const
void setPixelSize(int pixelSize)
void setFont(const QFont &font)
QFont scaledFontPixelSize(const QFont &font) const
Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE.
QRectF boundingRect(const QString &text) const
void drawText(const QPointF &position, const QString &text)
double fontHeightCharacterMM(const QFont &font, QChar c) const
Returns the font height of a character in millimeters.
void setPointSizeF(qreal pointSize)
void restore()
qreal width() const
qreal ascent() const
#define FONT_WORKAROUND_SCALE
qreal descent() const
QStringList split(const QString &sep, const QString &str, bool allowEmptyEntries)
qreal height() const
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...