QGIS API Documentation  3.2.0-Bonn (bc43194)
qgsscalebarrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalebarrenderer.cpp
3  -----------------------
4  begin : June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : [email protected]
7  ***************************************************************************/
8 /***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include "qgsscalebarrenderer.h"
18 #include "qgsscalebarsettings.h"
19 #include "qgslayoututils.h"
20 #include "qgstextrenderer.h"
21 #include <QFontMetricsF>
22 #include <QPainter>
23 
24 void QgsScaleBarRenderer::drawDefaultLabels( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
25 {
26  if ( !context.painter() )
27  {
28  return;
29  }
30 
31  QPainter *painter = context.painter();
32 
33  painter->save();
34 
35  QgsTextFormat format = settings.textFormat();
36 
37  QString firstLabel = firstLabelString( settings );
38  QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, format );
39  double xOffset = fontMetrics.width( firstLabel ) / 2.0;
40 
41  double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
42 
43  double currentLabelNumber = 0.0;
44 
45  int nSegmentsLeft = settings.numberOfSegmentsLeft();
46  int segmentCounter = 0;
47  QString currentNumericLabel;
48 
49  QList<double> positions = segmentPositions( scaleContext, settings );
50 
51  for ( int i = 0; i < positions.size(); ++i )
52  {
53  if ( segmentCounter == 0 && nSegmentsLeft > 0 )
54  {
55  //label first left segment
56  currentNumericLabel = firstLabel;
57  }
58  else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
59  {
60  currentLabelNumber = 0;
61  }
62 
63  if ( segmentCounter >= nSegmentsLeft )
64  {
65  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
66  }
67 
68  if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
69  {
70  QgsTextRenderer::drawText( QPointF( context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset,
71  fontMetrics.ascent() + scaledBoxContentSpace ), 0, QgsTextRenderer::AlignCenter,
72  QStringList() << currentNumericLabel, context, format );
73  }
74 
75  if ( segmentCounter >= nSegmentsLeft )
76  {
77  currentLabelNumber += settings.unitsPerSegment();
78  }
79  ++segmentCounter;
80  }
81 
82  //also draw the last label
83  if ( !positions.isEmpty() )
84  {
85  // note: this label is NOT centered over the end of the bar - rather the numeric portion
86  // of it is, without considering the unit label suffix. That's drawn at the end after
87  // horizontally centering just the numeric portion.
88  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
89  QgsTextRenderer::drawText( QPointF( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, QgsUnitTypes::RenderMillimeters ) + xOffset
90  - fontMetrics.width( currentNumericLabel ) / 2.0,
91  fontMetrics.ascent() + scaledBoxContentSpace ), 0, QgsTextRenderer::AlignLeft,
92  QStringList() << ( currentNumericLabel + ' ' + settings.unitLabel() ), context, format );
93  }
94 
95  painter->restore();
96 }
97 
99  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
100 {
101  QFont font = settings.textFormat().toQFont();
102 
103  //consider centered first label
104  double firstLabelLeft = QgsLayoutUtils::textWidthMM( font, firstLabelString( settings ) ) / 2;
105 
106  //consider last number and label
107 
108  double largestLabelNumber = settings.numberOfSegments() * settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit();
109  QString largestNumberLabel = QString::number( largestLabelNumber );
110  QString largestLabel = QString::number( largestLabelNumber ) + ' ' + settings.unitLabel();
111  double largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel ) - QgsLayoutUtils::textWidthMM( font, largestNumberLabel ) / 2;
112 
113  double totalBarLength = scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
114 
115  double width = firstLabelLeft + totalBarLength + 2 * settings.pen().widthF() + largestLabelWidth + 2 * settings.boxContentSpace();
116  double height = settings.height() + settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsLayoutUtils::fontAscentMM( font );
117 
118  return QSizeF( width, height );
119 }
120 
122 {
123  if ( settings.numberOfSegmentsLeft() > 0 )
124  {
125  return QString::number( settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit() );
126  }
127  else
128  {
129  return QStringLiteral( "0" );
130  }
131 }
132 
134 {
135  QString firstLabel = firstLabelString( settings );
137  return QgsLayoutUtils::textWidthMM( settings.font(), firstLabel ) / 2.0;
139 }
140 
141 double QgsScaleBarRenderer::firstLabelXOffset( const QgsScaleBarSettings &settings, const QgsRenderContext &context ) const
142 {
143  QString firstLabel = firstLabelString( settings );
144  return QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << firstLabel ) / 2.0;
145 }
146 
147 QList<double> QgsScaleBarRenderer::segmentPositions( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
148 {
149  QList<double> positions;
150 
151  double currentXCoord = settings.pen().widthF() + settings.boxContentSpace();
152 
153  //left segments
154  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
155  positions.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
156  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
157  {
158  positions << currentXCoord;
159  currentXCoord += leftSegmentSize;
160  }
161 
162  //right segments
163  for ( int i = 0; i < settings.numberOfSegments(); ++i )
164  {
165  positions << currentXCoord;
166  currentXCoord += scaleContext.segmentWidth;
167  }
168  return positions;
169 }
170 
171 QList<double> QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
172 {
173  QList<double> widths;
174  widths.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
175 
176  //left segments
177  if ( settings.numberOfSegmentsLeft() > 0 )
178  {
179  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
180  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
181  {
182  widths << leftSegmentSize;
183  }
184  }
185 
186  //right segments
187  for ( int i = 0; i < settings.numberOfSegments(); ++i )
188  {
189  widths << scaleContext.segmentWidth;
190  }
191 
192  return widths;
193 }
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
QString unitLabel() const
Returns the label for units.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
static double textWidthMM(const QFont &font, const QString &text)
Calculate a font width in millimeters for a text string, including workarounds for QT font rendering ...
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
#define Q_NOWARN_DEPRECATED_PUSH
Definition: qgis.h:538
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
double height() const
Returns the scalebar height (in millimeters).
int numberOfSegments() const
Returns the number of segments included in the scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context...
void drawDefaultLabels(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Draws default scalebar labels using the specified settings and scaleContext to a destination render c...
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true)
Draws text within a rectangle using the specified settings.
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
#define Q_NOWARN_DEPRECATED_POP
Definition: qgis.h:539
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
QPainter * painter()
Returns the destination QPainter for the render operation.
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues...
double unitsPerSegment() const
Returns the number of scalebar units per segment.
virtual QSizeF calculateBoxSize(const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const
Calculates the required box size (in millimeters) for a scalebar using the specified settings and sca...
Container for all settings relating to text rendering.
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
static double textWidth(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, QFontMetricsF *fontMetrics=nullptr)
Returns the width of a text based on a given format.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.