QGIS API Documentation  3.0.2-Girona (307d082)
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 : marco.hugentobler@karto.baug.ethz.ch
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 <QFontMetricsF>
21 #include <QPainter>
22 
23 void QgsScaleBarRenderer::drawDefaultLabels( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
24 {
25  if ( !context.painter() )
26  {
27  return;
28  }
29 
30  QPainter *painter = context.painter();
31 
32  painter->save();
33 
34  QFont scaledFont = settings.font();
35  scaledFont.setPointSizeF( scaledFont.pointSizeF() * context.scaleFactor() );
36  painter->setPen( QPen( settings.fontColor() ) );
37 
38  QString firstLabel = firstLabelString( settings );
39  double xOffset = context.convertToPainterUnits( QgsLayoutUtils::textWidthMM( settings.font(), firstLabel ) / 2, QgsUnitTypes::RenderMillimeters );
40 
41  double currentLabelNumber = 0.0;
42 
43  int nSegmentsLeft = settings.numberOfSegmentsLeft();
44  int segmentCounter = 0;
45  QString currentNumericLabel;
46 
47  QList<double> positions = segmentPositions( scaleContext, settings );
48 
49  for ( int i = 0; i < positions.size(); ++i )
50  {
51  if ( segmentCounter == 0 && nSegmentsLeft > 0 )
52  {
53  //label first left segment
54  currentNumericLabel = firstLabel;
55  }
56  else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
57  {
58  currentLabelNumber = 0;
59  }
60 
61  if ( segmentCounter >= nSegmentsLeft )
62  {
63  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
64  }
65 
66  if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
67  {
68  QgsLayoutUtils::drawText( painter, QPointF( context.convertToPainterUnits( positions.at( i ) - QgsLayoutUtils::textWidthMM( settings.font(), currentNumericLabel ) / 2, QgsUnitTypes::RenderMillimeters ) + xOffset,
70  currentNumericLabel, scaledFont, settings.fontColor() );
71  }
72 
73  if ( segmentCounter >= nSegmentsLeft )
74  {
75  currentLabelNumber += settings.unitsPerSegment();
76  }
77  ++segmentCounter;
78  }
79 
80  //also draw the last label
81  if ( !positions.isEmpty() )
82  {
83  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
84  QgsLayoutUtils::drawText( painter, QPointF( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth - QgsLayoutUtils::textWidthMM( settings.font(), currentNumericLabel ) / 2, QgsUnitTypes::RenderMillimeters ) + xOffset,
86  currentNumericLabel + ' ' + settings.unitLabel(), scaledFont, settings.fontColor() );
87  }
88 
89  painter->restore();
90 }
91 
93  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
94 {
95  //consider centered first label
96  double firstLabelLeft = QgsLayoutUtils::textWidthMM( settings.font(), firstLabelString( settings ) ) / 2;
97 
98  //consider last number and label
99 
100  double largestLabelNumber = settings.numberOfSegments() * settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit();
101  QString largestNumberLabel = QString::number( largestLabelNumber );
102  QString largestLabel = QString::number( largestLabelNumber ) + ' ' + settings.unitLabel();
103  double largestLabelWidth = QgsLayoutUtils::textWidthMM( settings.font(), largestLabel ) - QgsLayoutUtils::textWidthMM( settings.font(), largestNumberLabel ) / 2;
104 
105  double totalBarLength = scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
106 
107  double width = firstLabelLeft + totalBarLength + 2 * settings.pen().widthF() + largestLabelWidth + 2 * settings.boxContentSpace();
108  double height = settings.height() + settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsLayoutUtils::fontAscentMM( settings.font() );
109 
110  return QSizeF( width, height );
111 }
112 
114 {
115  if ( settings.numberOfSegmentsLeft() > 0 )
116  {
117  return QString::number( settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit() );
118  }
119  else
120  {
121  return QStringLiteral( "0" );
122  }
123 }
124 
126 {
127  QString firstLabel = firstLabelString( settings );
128  return QgsLayoutUtils::textWidthMM( settings.font(), firstLabel ) / 2.0;
129 }
130 
131 QList<double> QgsScaleBarRenderer::segmentPositions( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
132 {
133  QList<double> positions;
134 
135  double currentXCoord = settings.pen().widthF() + settings.boxContentSpace();
136 
137  //left segments
138  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
139  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
140  {
141  positions << currentXCoord;
142  currentXCoord += leftSegmentSize;
143  }
144 
145  //right segments
146  for ( int i = 0; i < settings.numberOfSegments(); ++i )
147  {
148  positions << currentXCoord;
149  currentXCoord += scaleContext.segmentWidth;
150  }
151  return positions;
152 }
153 
154 QList<double> QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
155 {
156  QList<double> widths;
157 
158  //left segments
159  if ( settings.numberOfSegmentsLeft() > 0 )
160  {
161  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
162  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
163  {
164  widths << leftSegmentSize;
165  }
166  }
167 
168  //right segments
169  for ( int i = 0; i < settings.numberOfSegments(); ++i )
170  {
171  widths << scaleContext.segmentWidth;
172  }
173 
174  return widths;
175 }
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.
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
Width of each individual segment (in millimeters)
QColor fontColor() const
Returns the color used for drawing text in the scalebar.
QFont font() const
Returns the font used for drawing text in the scalebar.
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 void drawText(QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of layout specific issues (calculation to...
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...
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
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...
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.