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