QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
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"
22 #include <QFontMetricsF>
23 #include <QPainter>
24 
25 void QgsScaleBarRenderer::drawDefaultLabels( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
26 {
27  if ( !context.painter() )
28  {
29  return;
30  }
31 
32  QPainter *painter = context.painter();
33 
34  painter->save();
35 
36  QgsTextFormat format = settings.textFormat();
37 
38  QgsExpressionContextScope *scaleScope = new QgsExpressionContextScope( QStringLiteral( "scalebar_text" ) );
39  QgsExpressionContextScopePopper scopePopper( context.expressionContext(), scaleScope );
40 
41  QString firstLabel = firstLabelString( settings );
42  QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, format );
43  double xOffset = fontMetrics.width( firstLabel ) / 2.0;
44 
45  double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
46  double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), QgsUnitTypes::RenderMillimeters );
47  double scaledHeight = context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters );
48 
49  double currentLabelNumber = 0.0;
50 
51  int nSegmentsLeft = settings.numberOfSegmentsLeft();
52  int segmentCounter = 0;
53 
54  QString currentNumericLabel;
55  QList<double> positions = segmentPositions( scaleContext, settings );
56 
57  bool drawZero = true;
58  switch ( settings.labelHorizontalPlacement() )
59  {
61  drawZero = false;
62  break;
64  drawZero = true;
65  break;
66  }
67 
68  for ( int i = 0; i < positions.size(); ++i )
69  {
70  if ( segmentCounter == 0 && nSegmentsLeft > 0 )
71  {
72  //label first left segment
73  currentNumericLabel = firstLabel;
74  }
75  else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
76  {
77  currentLabelNumber = 0.0;
78  }
79 
80  if ( segmentCounter >= nSegmentsLeft )
81  {
82  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
83  }
84 
85  //don't draw label for intermediate left segments or the zero label when it needs to be skipped
86  if ( ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) && ( currentNumericLabel != QStringLiteral( "0" ) || drawZero ) )
87  {
88  scaleScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "scale_value" ), currentNumericLabel, true, false ) );
89  QPointF pos;
91  {
92  if ( segmentCounter == 0 )
93  {
94  // if the segment counter is zero with a non zero label, this is the left-of-zero label
95  pos.setX( context.convertToPainterUnits( positions.at( i ) + ( scaleContext.segmentWidth / 2 ), QgsUnitTypes::RenderMillimeters ) );
96  }
97  else
98  {
99  pos.setX( context.convertToPainterUnits( positions.at( i ) - ( scaleContext.segmentWidth / 2 ), QgsUnitTypes::RenderMillimeters ) );
100  }
101  }
102  else
103  {
104  pos.setX( context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset );
105  }
106  pos.setY( fontMetrics.ascent() + scaledBoxContentSpace + ( settings.labelVerticalPlacement() == QgsScaleBarSettings::LabelBelowSegment ? scaledHeight + scaledLabelBarSpace : 0 ) );
107  QgsTextRenderer::drawText( pos, 0, QgsTextRenderer::AlignCenter, QStringList() << currentNumericLabel, context, format );
108  }
109 
110  if ( segmentCounter >= nSegmentsLeft )
111  {
112  currentLabelNumber += settings.unitsPerSegment();
113  }
114  ++segmentCounter;
115  }
116 
117  //also draw the last label
118  if ( !positions.isEmpty() )
119  {
120  // note: this label is NOT centered over the end of the bar - rather the numeric portion
121  // of it is, without considering the unit label suffix. That's drawn at the end after
122  // horizontally centering just the numeric portion.
123  currentNumericLabel = QString::number( currentLabelNumber / settings.mapUnitsPerScaleBarUnit() );
124  scaleScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "scale_value" ), currentNumericLabel, true, false ) );
125  QPointF pos;
126  pos.setY( fontMetrics.ascent() + scaledBoxContentSpace + ( settings.labelVerticalPlacement() == QgsScaleBarSettings::LabelBelowSegment ? scaledHeight + scaledLabelBarSpace : 0 ) );
128  {
129  pos.setX( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + ( scaleContext.segmentWidth / 2 ), QgsUnitTypes::RenderMillimeters ) + xOffset );
130  QgsTextRenderer::drawText( pos, 0, QgsTextRenderer::AlignCenter, QStringList() << ( currentNumericLabel + ' ' + settings.unitLabel() ), context, format );
131  }
132  else
133  {
134  pos.setX( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, QgsUnitTypes::RenderMillimeters ) + xOffset
135  - fontMetrics.width( currentNumericLabel ) / 2.0 );
136  QgsTextRenderer::drawText( pos, 0, QgsTextRenderer::AlignLeft, QStringList() << ( currentNumericLabel + ' ' + settings.unitLabel() ), context, format );
137  }
138  }
139 
140  painter->restore();
141 }
142 
144  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
145 {
146  QFont font = settings.textFormat().toQFont();
147 
148  //consider centered first label
149  double firstLabelWidth = QgsLayoutUtils::textWidthMM( font, firstLabelString( settings ) );
151  {
152  if ( firstLabelWidth > scaleContext.segmentWidth )
153  {
154  firstLabelWidth = ( firstLabelWidth - scaleContext.segmentWidth ) / 2;
155  }
156  else
157  {
158  firstLabelWidth = 0.0;
159  }
160  }
161  else
162  {
163  firstLabelWidth = firstLabelWidth / 2;
164  }
165 
166  //consider last number and label
167  double largestLabelNumber = settings.numberOfSegments() * settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit();
168  QString largestNumberLabel = QString::number( largestLabelNumber );
169  QString largestLabel = largestNumberLabel + ' ' + settings.unitLabel();
170  double largestLabelWidth;
172  {
173  largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel );
174  if ( largestLabelWidth > scaleContext.segmentWidth )
175  {
176  largestLabelWidth = ( largestLabelWidth - scaleContext.segmentWidth ) / 2;
177  }
178  else
179  {
180  largestLabelWidth = 0.0;
181  }
182  }
183  else
184  {
185  largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel ) - QgsLayoutUtils::textWidthMM( font, largestNumberLabel ) / 2;
186  }
187 
188  double totalBarLength = scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
189 
190  double width = firstLabelWidth + totalBarLength + 2 * settings.pen().widthF() + largestLabelWidth + 2 * settings.boxContentSpace();
191  double height = settings.height() + settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsLayoutUtils::fontAscentMM( font );
192 
193  return QSizeF( width, height );
194 }
195 
197 {
198  if ( settings.numberOfSegmentsLeft() > 0 )
199  {
200  return QString::number( settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit() );
201  }
202  else
203  {
204  return QStringLiteral( "0" );
205  }
206 }
207 
209 {
210  QString firstLabel = firstLabelString( settings );
212  return QgsLayoutUtils::textWidthMM( settings.font(), firstLabel ) / 2.0;
214 }
215 
216 double QgsScaleBarRenderer::firstLabelXOffset( const QgsScaleBarSettings &settings, const QgsRenderContext &context, const ScaleBarContext &scaleContext ) const
217 {
218  QString firstLabel = firstLabelString( settings );
219  double firstLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << firstLabel );
221  {
222  if ( firstLabelWidth > scaleContext.segmentWidth )
223  {
224  firstLabelWidth = ( firstLabelWidth - scaleContext.segmentWidth ) / 2;
225  }
226  else
227  {
228  firstLabelWidth = 0.0;
229  }
230  }
231  else
232  {
233  firstLabelWidth = firstLabelWidth / 2;
234  }
235  return firstLabelWidth;
236 }
237 
238 QList<double> QgsScaleBarRenderer::segmentPositions( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
239 {
240  QList<double> positions;
241 
242  double currentXCoord = settings.pen().widthF() + settings.boxContentSpace();
243 
244  //left segments
245  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
246  positions.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
247  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
248  {
249  positions << currentXCoord;
250  currentXCoord += leftSegmentSize;
251  }
252 
253  //right segments
254  for ( int i = 0; i < settings.numberOfSegments(); ++i )
255  {
256  positions << currentXCoord;
257  currentXCoord += scaleContext.segmentWidth;
258  }
259  return positions;
260 }
261 
262 QList<double> QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
263 {
264  QList<double> widths;
265  widths.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
266 
267  //left segments
268  if ( settings.numberOfSegmentsLeft() > 0 )
269  {
270  double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
271  for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
272  {
273  widths << leftSegmentSize;
274  }
275  }
276 
277  //right segments
278  for ( int i = 0; i < settings.numberOfSegments(); ++i )
279  {
280  widths << scaleContext.segmentWidth;
281  }
282 
283  return widths;
284 }
Single variable definition for use within a QgsExpressionContextScope.
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:649
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
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).
LabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
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.
Single scope for storing variables and functions for use within a QgsExpressionContext.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context...
LabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
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.
Labels are drawn centered relative to segment&#39;s edge.
QgsExpressionContext & expressionContext()
Gets the expression context.
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:650
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...
Labels are drawn centered relative to segment.
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.
Labels are drawn below the scalebar.
RAII class to pop scope from an expression context on destruction.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.