QGIS API Documentation  2.10.1-Pisa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  , mMmPerMapUnit( 1 )
34  , mUseAdvancedEffects( true )
35  , mMapScale( 1 )
36  , mDpi( 96 ) // based on QImage's default DPI
37 {
48 }
49 
51 {
52  QStringList list;
53  // If the string contains nothing then just return the string without spliting.
54  if ( wrapChar().count() == 0 )
55  list << stringToSplt;
56  else
57  list = stringToSplt.split( wrapChar() );
58  return list;
59 }
60 
61 #define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
62 
63 
64 void QgsLegendSettings::drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const
65 {
66  QFont textFont = scaledFontPixelSize( font );
67 
68  p->save();
69  p->setFont( textFont );
70  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
71  p->scale( scaleFactor, scaleFactor );
72  p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
73  p->restore();
74 }
75 
76 
77 void QgsLegendSettings::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
78 {
79  QFont textFont = scaledFontPixelSize( font );
80 
81  QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
83 
84  p->save();
85  p->setFont( textFont );
86  double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
87  p->scale( scaleFactor, scaleFactor );
88  p->drawText( scaledRect, halignment | valignment | flags, text );
89  p->restore();
90 }
91 
92 
94 {
95  QFont scaledFont = font;
96  double pixelSize = pixelFontSize( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
97  scaledFont.setPixelSize( pixelSize );
98  return scaledFont;
99 }
100 
101 double QgsLegendSettings::pixelFontSize( double pointSize ) const
102 {
103  return ( pointSize * 0.3527 );
104 }
105 
106 double QgsLegendSettings::textWidthMillimeters( const QFont& font, const QString& text ) const
107 {
108  QFont metricsFont = scaledFontPixelSize( font );
109  QFontMetricsF fontMetrics( metricsFont );
110  return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
111 }
112 
113 double QgsLegendSettings::fontHeightCharacterMM( const QFont& font, const QChar& c ) const
114 {
115  QFont metricsFont = scaledFontPixelSize( font );
116  QFontMetricsF fontMetrics( metricsFont );
117  return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
118 }
119 
121 {
122  QFont metricsFont = scaledFontPixelSize( font );
123  QFontMetricsF fontMetrics( metricsFont );
124  return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
125 }
126 
128 {
129  QFont metricsFont = scaledFontPixelSize( font );
130  QFontMetricsF fontMetrics( metricsFont );
131  return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
132 }
133 
QString wrapChar() const
QStringList splitStringForWrapping(QString stringToSplt) const
Splits a string using the wrap char taking into account handling empty wrap char which means no wrapp...
QgsComposerLegendStyle & rstyle(QgsComposerLegendStyle::Style s)
Returns reference to modifiable style.
void setMargin(Side side, double margin)
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...
qreal x() const
qreal y() const
qreal pointSizeF() const
void scale(qreal sx, qreal sy)
void save()
qreal width(const QString &text) const
void setPixelSize(int pixelSize)
void setFont(const QFont &font)
QRectF boundingRect(const QString &text) const
double fontDescentMillimeters(const QFont &font) const
Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCA...
void drawText(const QPointF &position, const QString &text)
double fontHeightCharacterMM(const QFont &font, const 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 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.
#define tr(sourceText)
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...