QGIS API Documentation 3.41.0-Master (cea29feecf2)
Loading...
Searching...
No Matches
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
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 "qgssymbol.h"
24#include "qgssymbollayerutils.h"
25#include "qgslinesymbol.h"
26
27#include <QFontMetricsF>
28#include <QPainter>
29
30void QgsScaleBarRenderer::drawDefaultLabels( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
31{
32 if ( !context.painter() )
33 {
34 return;
35 }
36
37 QPainter *painter = context.painter();
38
39 painter->save();
40
41 const QgsTextFormat format = settings.textFormat();
42
43 QgsExpressionContextScope *scaleScope = new QgsExpressionContextScope( QStringLiteral( "scalebar_text" ) );
44 const QgsExpressionContextScopePopper scopePopper( context.expressionContext(), scaleScope );
45
46 const QString firstLabel = firstLabelString( settings );
47 const QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, format );
48 const double xOffset = fontMetrics.horizontalAdvance( firstLabel ) / 2.0;
49
50 const double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), Qgis::RenderUnit::Millimeters );
51 const double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), Qgis::RenderUnit::Millimeters );
52 double scaledHeight;
53 if ( ( scaleContext.flags & Flag::FlagUsesSubdivisionsHeight ) && ( settings.numberOfSubdivisions() > 1 ) && ( settings.subdivisionsHeight() > settings.height() ) )
54 {
56 }
57 else
58 {
59 scaledHeight = context.convertToPainterUnits( settings.height(), Qgis::RenderUnit::Millimeters );
60 }
61
62 double currentLabelNumber = 0.0;
63
64 const int nSegmentsLeft = settings.numberOfSegmentsLeft();
65 int segmentCounter = 0;
66
67 QString currentNumericLabel;
68 const QList<double> positions = segmentPositions( context, scaleContext, settings );
69
70 bool drawZero = true;
71 switch ( settings.labelHorizontalPlacement() )
72 {
74 drawZero = false;
75 break;
77 drawZero = true;
78 break;
79 }
80
81 const QgsNumericFormatContext numericContext;
82
83 for ( int i = 0; i < positions.size(); ++i )
84 {
85 if ( segmentCounter == 0 && nSegmentsLeft > 0 )
86 {
87 //label first left segment
88 currentNumericLabel = firstLabel;
89 }
90 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
91 {
92 currentLabelNumber = 0.0;
93 }
94
95 if ( segmentCounter >= nSegmentsLeft )
96 {
97 currentNumericLabel = settings.numericFormat()->formatDouble( currentLabelNumber / settings.mapUnitsPerScaleBarUnit(), numericContext );
98 }
99
100 //don't draw label for intermediate left segments or the zero label when it needs to be skipped
101 if ( ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) && ( currentNumericLabel != QLatin1String( "0" ) || drawZero ) )
102 {
103 scaleScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "scale_value" ), currentNumericLabel, true, false ) );
104 QPointF pos;
106 {
107 if ( segmentCounter == 0 )
108 {
109 // if the segment counter is zero with a non zero label, this is the left-of-zero label
110 pos.setX( context.convertToPainterUnits( positions.at( i ) + ( scaleContext.segmentWidth / 2 ), Qgis::RenderUnit::Millimeters ) );
111 }
112 else
113 {
114 pos.setX( context.convertToPainterUnits( positions.at( i ) - ( scaleContext.segmentWidth / 2 ), Qgis::RenderUnit::Millimeters ) );
115 }
116 }
117 else
118 {
119 pos.setX( context.convertToPainterUnits( positions.at( i ), Qgis::RenderUnit::Millimeters ) + xOffset );
120 }
121 pos.setY( fontMetrics.ascent() + scaledBoxContentSpace + ( settings.labelVerticalPlacement() == Qgis::ScaleBarDistanceLabelVerticalPlacement::BelowSegment ? scaledHeight + scaledLabelBarSpace : 0 ) );
122 QgsTextRenderer::drawText( pos, 0, Qgis::TextHorizontalAlignment::Center, QStringList() << currentNumericLabel, context, format );
123 }
124
125 if ( segmentCounter >= nSegmentsLeft )
126 {
127 currentLabelNumber += settings.unitsPerSegment();
128 }
129 ++segmentCounter;
130 }
131
132 //also draw the last label
133 if ( !positions.isEmpty() )
134 {
135 // note: this label is NOT centered over the end of the bar - rather the numeric portion
136 // of it is, without considering the unit label suffix. That's drawn at the end after
137 // horizontally centering just the numeric portion.
138 currentNumericLabel = settings.numericFormat()->formatDouble( currentLabelNumber / settings.mapUnitsPerScaleBarUnit(), numericContext );
139 scaleScope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "scale_value" ), currentNumericLabel, true, false ) );
140 QPointF pos;
141 pos.setY( fontMetrics.ascent() + scaledBoxContentSpace + ( settings.labelVerticalPlacement() == Qgis::ScaleBarDistanceLabelVerticalPlacement::BelowSegment ? scaledHeight + scaledLabelBarSpace : 0 ) );
143 {
144 pos.setX( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + ( scaleContext.segmentWidth / 2 ), Qgis::RenderUnit::Millimeters ) + xOffset );
145 QgsTextRenderer::drawText( pos, 0, Qgis::TextHorizontalAlignment::Center, QStringList() << ( currentNumericLabel + ' ' + settings.unitLabel() ), context, format );
146 }
147 else
148 {
149 pos.setX( context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, Qgis::RenderUnit::Millimeters ) + xOffset
150 - fontMetrics.horizontalAdvance( currentNumericLabel ) / 2.0 );
151 QgsTextRenderer::drawText( pos, 0, Qgis::TextHorizontalAlignment::Left, QStringList() << ( currentNumericLabel + ' ' + settings.unitLabel() ), context, format );
152 }
153 }
154
155 painter->restore();
156}
157
162
164{
165 return 100;
166}
167
169 const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
170{
171 const QFont font = settings.textFormat().toQFont();
172
173 //consider centered first label
174 double firstLabelWidth = QgsLayoutUtils::textWidthMM( font, firstLabelString( settings ) );
176 {
177 if ( firstLabelWidth > scaleContext.segmentWidth )
178 {
179 firstLabelWidth = ( firstLabelWidth - scaleContext.segmentWidth ) / 2;
180 }
181 else
182 {
183 firstLabelWidth = 0.0;
184 }
185 }
186 else
187 {
188 firstLabelWidth = firstLabelWidth / 2;
189 }
190
191 //consider last number and label
192 const double largestLabelNumber = settings.numberOfSegments() * settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit();
193 const QString largestNumberLabel = settings.numericFormat()->formatDouble( largestLabelNumber, QgsNumericFormatContext() );
194 const QString largestLabel = largestNumberLabel + ' ' + settings.unitLabel();
195 double largestLabelWidth;
197 {
198 largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel );
199 if ( largestLabelWidth > scaleContext.segmentWidth )
200 {
201 largestLabelWidth = ( largestLabelWidth - scaleContext.segmentWidth ) / 2;
202 }
203 else
204 {
205 largestLabelWidth = 0.0;
206 }
207 }
208 else
209 {
210 largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel ) - QgsLayoutUtils::textWidthMM( font, largestNumberLabel ) / 2;
211 }
212
213 const double totalBarLength = scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
214
215 // this whole method is deprecated, so we can still call the deprecated settings.pen() getter
217 const double width = firstLabelWidth + totalBarLength + 2 * settings.pen().widthF() + largestLabelWidth + 2 * settings.boxContentSpace();
219
220 const double height = settings.height() + settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsLayoutUtils::fontAscentMM( font );
221
222 return QSizeF( width, height );
223}
224
226{
227 const double painterToMm = 1.0 / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
228 //consider centered first label
229 double firstLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << firstLabelString( settings ) ) * painterToMm;
230
232 {
233 if ( firstLabelWidth > scaleContext.segmentWidth )
234 {
235 firstLabelWidth = ( firstLabelWidth - scaleContext.segmentWidth ) / 2;
236 }
237 else
238 {
239 firstLabelWidth = 0.0;
240 }
241 }
242 else
243 {
244 firstLabelWidth = firstLabelWidth / 2;
245 }
246
247 //consider last number and label
248 const double largestLabelNumber = settings.numberOfSegments() * settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit();
249 const QString largestNumberLabel = std::isnan( largestLabelNumber ) ? QString() : settings.numericFormat()->formatDouble( largestLabelNumber, QgsNumericFormatContext() );
250 const QString largestLabel = largestNumberLabel + ' ' + settings.unitLabel();
251 double largestLabelWidth;
253 {
254 largestLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm;
255
256 if ( largestLabelWidth > scaleContext.segmentWidth )
257 {
258 largestLabelWidth = ( largestLabelWidth - scaleContext.segmentWidth ) / 2;
259 }
260 else
261 {
262 largestLabelWidth = 0.0;
263 }
264 }
265 else
266 {
267 largestLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm
268 - QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestNumberLabel ) * painterToMm / 2;
269 }
270
271 // segmentWidth can be NaN in extreme cases, eg trying to make a scalebar for a global map with a very small segment size (eg meters)
272 const double totalBarLength = std::isnan( scaleContext.segmentWidth ) ? 0
273 : scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
274
275 double lineWidth = QgsSymbolLayerUtils::estimateMaxSymbolBleed( settings.lineSymbol(), context ) * 2;
276 // need to convert to mm
277 lineWidth /= context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
278
279 const double width = firstLabelWidth + totalBarLength + 2 * lineWidth + largestLabelWidth + 2 * settings.boxContentSpace();
280 double height;
281 if ( ( scaleContext.flags & Flag::FlagUsesSubdivisionsHeight ) && ( settings.numberOfSubdivisions() > 1 ) && ( settings.subdivisionsHeight() > settings.height() ) )
282 {
283 height = settings.subdivisionsHeight();
284 }
285 else
286 {
287 height = settings.height();
288 }
289
290 // TODO -- we technically should check the height of ALL labels here and take the maximum
291 height += settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsTextRenderer::textHeight( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm;
292
293 return QSizeF( width, height );
294}
295
297{
298 return false;
299}
300
302{
303 if ( settings.numberOfSegmentsLeft() > 0 )
304 {
305 return settings.numericFormat()->formatDouble( settings.unitsPerSegment() / settings.mapUnitsPerScaleBarUnit(), QgsNumericFormatContext() );
306 }
307 else
308 {
309 return settings.numericFormat()->formatDouble( 0, QgsNumericFormatContext() );
310 }
311}
312
314{
315 const QString firstLabel = firstLabelString( settings );
317 return QgsLayoutUtils::textWidthMM( settings.font(), firstLabel ) / 2.0;
319}
320
321double QgsScaleBarRenderer::firstLabelXOffset( const QgsScaleBarSettings &settings, const QgsRenderContext &context, const ScaleBarContext &scaleContext ) const
322{
323 const QString firstLabel = firstLabelString( settings );
324 double firstLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << firstLabel );
326 {
327 if ( firstLabelWidth > scaleContext.segmentWidth )
328 {
329 firstLabelWidth = ( firstLabelWidth - scaleContext.segmentWidth ) / 2;
330 }
331 else
332 {
333 firstLabelWidth = 0.0;
334 }
335 }
336 else
337 {
338 firstLabelWidth = firstLabelWidth / 2;
339 }
340 return firstLabelWidth;
341}
342
343QList<double> QgsScaleBarRenderer::segmentPositions( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
344{
345 QList<double> positions;
346
347 // this whole method is deprecated, so calling a deprecated function is fine
349 double currentXCoord = settings.pen().widthF() + settings.boxContentSpace();
351
352 //left segments
353 const double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
354 positions.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
355 for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
356 {
357 positions << currentXCoord;
358 currentXCoord += leftSegmentSize;
359 }
360
361 //right segments
362 for ( int i = 0; i < settings.numberOfSegments(); ++i )
363 {
364 positions << currentXCoord;
365 currentXCoord += scaleContext.segmentWidth;
366 }
367 return positions;
368}
369
371{
372 QList<double> positions;
373
374 double lineWidth = QgsSymbolLayerUtils::estimateMaxSymbolBleed( settings.lineSymbol(), context ) * 2.0;
375 // need to convert to mm
376 lineWidth /= context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
377
378 double currentXCoord = lineWidth + settings.boxContentSpace();
379
380 //left segments
381 const double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
382 positions.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
383 for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
384 {
385 positions << currentXCoord;
386 currentXCoord += leftSegmentSize;
387 }
388
389 //right segments
390 for ( int i = 0; i < settings.numberOfSegments(); ++i )
391 {
392 positions << currentXCoord;
393 currentXCoord += scaleContext.segmentWidth;
394 }
395 return positions;
396}
397
398QList<double> QgsScaleBarRenderer::segmentWidths( const ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings ) const
399{
400 QList<double> widths;
401 widths.reserve( settings.numberOfSegmentsLeft() + settings.numberOfSegments() );
402
403 //left segments
404 if ( settings.numberOfSegmentsLeft() > 0 )
405 {
406 const double leftSegmentSize = scaleContext.segmentWidth / settings.numberOfSegmentsLeft();
407 for ( int i = 0; i < settings.numberOfSegmentsLeft(); ++i )
408 {
409 widths << leftSegmentSize;
410 }
411 }
412
413 //right segments
414 for ( int i = 0; i < settings.numberOfSegments(); ++i )
415 {
416 widths << scaleContext.segmentWidth;
417 }
418
419 return widths;
420}
421
423{
424 return !std::isnan( segmentWidth );
425}
@ CenteredSegment
Labels are drawn centered relative to segment.
@ CenteredEdge
Labels are drawn centered relative to segment's edge.
@ BelowSegment
Labels are drawn below the scalebar.
@ Millimeters
Millimeters.
RAII class to pop scope from an expression context on destruction.
Single scope for storing variables and functions for use within a QgsExpressionContext.
void addVariable(const QgsExpressionContextScope::StaticVariable &variable)
Adds a variable into the context scope.
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
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 ...
A context for numeric formats.
virtual QString formatDouble(double value, const QgsNumericFormatContext &context) const =0
Returns a formatted string representation of a numeric double value.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, Qgis::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsExpressionContext & expressionContext()
Gets the expression context.
virtual Flags flags() const
Returns the scalebar rendering flags, which dictates the renderer's behavior.
virtual bool applyDefaultSettings(QgsScaleBarSettings &settings) const
Applies any default settings relating to the scalebar to the passed settings object.
virtual int sortKey() const
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
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...
virtual Q_DECL_DEPRECATED 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...
QString firstLabelString(const QgsScaleBarSettings &settings) const
Returns the text used for the first label in the scalebar.
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of the scalebar.
Q_DECL_DEPRECATED QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within 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.
@ FlagUsesSubdivisionsHeight
Renderer uses the scalebar subdivisions height (see QgsScaleBarSettings::subdivisionsHeight() )
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
double subdivisionsHeight() const
Returns the scalebar subdivisions height (in millimeters) for segments included in the right part of ...
QgsLineSymbol * lineSymbol() const
Returns the line symbol used to render the scalebar (only used for some scalebar types).
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
int numberOfSegments() const
Returns the number of segments included in the scalebar.
Qgis::ScaleBarDistanceLabelVerticalPlacement labelVerticalPlacement() const
Returns the vertical placement of text labels.
double unitsPerSegment() const
Returns the number of scalebar units per segment.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
Q_DECL_DEPRECATED QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
QString unitLabel() const
Returns the label for units.
int numberOfSubdivisions() const
Returns the number of subdivisions for segments included in the right part of the scalebar (only used...
Qgis::ScaleBarDistanceLabelHorizontalPlacement labelHorizontalPlacement() const
Returns the horizontal placement of text labels.
Q_DECL_DEPRECATED QFont font() const
Returns the font used for drawing text in the scalebar.
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
double height() const
Returns the scalebar height (in millimeters).
int numberOfSegmentsLeft() const
Returns the number of segments included in the left part of the scalebar.
double mapUnitsPerScaleBarUnit() const
Returns the number of map units per scale bar unit used by the scalebar.
static double estimateMaxSymbolBleed(QgsSymbol *symbol, const QgsRenderContext &context)
Returns the maximum estimated bleed for the symbol.
Container for all settings relating to text rendering.
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
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.
static void drawText(const QRectF &rect, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, Qgis::TextVerticalAlignment vAlignment=Qgis::TextVerticalAlignment::Top, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Rectangle)
Draws text within a rectangle using the specified settings.
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format, double scaleFactor=1.0)
Returns the font metrics for the given text format, when rendered in the specified render context.
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, Qgis::TextLayoutMode mode=Qgis::TextLayoutMode::Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
#define Q_NOWARN_DEPRECATED_POP
Definition qgis.h:6601
#define Q_NOWARN_DEPRECATED_PUSH
Definition qgis.h:6600
Single variable definition for use within a QgsExpressionContextScope.
Contains parameters regarding scalebar calculations.
Flags flags
Scalebar renderer flags.
bool isValid() const
Returns true if the context has valid settings.
double segmentWidth
The width, in millimeters, of each individual segment drawn.