QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgsnumericscalebarrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnumericscalebarrenderer.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 
18 #include "qgsscalebarsettings.h"
19 #include "qgslayoututils.h"
20 #include "qgsnumericformat.h"
21 #include "qgstextrenderer.h"
22 #include <QList>
23 #include <QPainter>
24 
26 {
27  return QStringLiteral( "Numeric" );
28 }
29 
31 {
32  return QObject::tr( "Numeric" );
33 }
34 
36 {
37  return 100;
38 }
39 
40 QgsScaleBarRenderer::Flags QgsNumericScaleBarRenderer::flags() const
41 {
43 }
44 
46 {
47  return new QgsNumericScaleBarRenderer( *this );
48 }
49 
50 void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
51 {
52  if ( !context.painter() )
53  {
54  return;
55  }
56 
57  QPainter *painter = context.painter();
58 
59  const QgsScopedQPainterState painterState( painter );
60  context.setPainterFlagsUsingContext( painter );
61 
62  const double margin = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
63  //map scalebar alignment to Qt::AlignmentFlag type
65  switch ( settings.alignment() )
66  {
69  break;
72  break;
75  break;
76  }
77 
78  //text destination is item's rect, excluding the margin
79  const QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), QgsUnitTypes::RenderMillimeters ) - 2 * margin,
80  context.convertToPainterUnits( scaleContext.size.height(), QgsUnitTypes::RenderMillimeters ) - 2 * margin );
81  QgsTextRenderer::drawText( painterRect, 0, hAlign, QStringList() << scaleText( scaleContext.scale, settings ), context, settings.textFormat() );
82 }
83 
85  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
86 {
87  const double painterToMm = 1.0 / context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
88 
89  const double textWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;
90  const double textHeight = QgsTextRenderer::textHeight( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;
91 
92  return QSizeF( 2 * settings.boxContentSpace() + textWidth,
93  textHeight + 2 * settings.boxContentSpace() );
94 }
95 
97 {
98  const QFont font = settings.textFormat().toQFont();
99 
100  const double textWidth = QgsLayoutUtils::textWidthMM( font, scaleText( scaleContext.scale, settings ) );
101  const double textHeight = QgsLayoutUtils::fontAscentMM( font );
102 
103  return QSizeF( 2 * settings.boxContentSpace() + textWidth,
104  textHeight + 2 * settings.boxContentSpace() );
105 }
106 
107 QString QgsNumericScaleBarRenderer::scaleText( double scale, const QgsScaleBarSettings &settings ) const
108 {
109  return "1:" + settings.numericFormat()->formatDouble( scale, QgsNumericFormatContext() );
110 }
QgsNumericScaleBarRenderer::draw
void draw(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const override
Draws the scalebar using the specified settings and scaleContext to a destination render context.
Definition: qgsnumericscalebarrenderer.cpp:50
QgsNumericScaleBarRenderer::clone
QgsNumericScaleBarRenderer * clone() const override
Returns a clone of the renderer.
Definition: qgsnumericscalebarrenderer.cpp:45
QgsRenderContext::setPainterFlagsUsingContext
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
Definition: qgsrendercontext.cpp:169
QgsScaleBarRenderer::ScaleBarContext
Contains parameters regarding scalebar calculations.
Definition: qgsscalebarrenderer.h:70
qgstextrenderer.h
QgsTextRenderer::AlignCenter
@ AlignCenter
Center align.
Definition: qgstextrenderer.h:61
QgsScaleBarSettings::AlignLeft
@ AlignLeft
Left aligned.
Definition: qgsscalebarsettings.h:49
QgsTextRenderer::AlignRight
@ AlignRight
Right align.
Definition: qgstextrenderer.h:62
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsNumericScaleBarRenderer::id
QString id() const override
Returns the unique ID for this renderer.
Definition: qgsnumericscalebarrenderer.cpp:25
QgsUnitTypes::RenderMillimeters
@ RenderMillimeters
Millimeters.
Definition: qgsunittypes.h:169
QgsNumericScaleBarRenderer::calculateBoxSize
QSizeF calculateBoxSize(QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext) const override
Calculates the required box size (in millimeters) for a scalebar using the specified settings and sca...
Definition: qgsnumericscalebarrenderer.cpp:84
QgsNumericScaleBarRenderer::sortKey
int sortKey() const override
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
Definition: qgsnumericscalebarrenderer.cpp:35
QgsScaleBarSettings::AlignMiddle
@ AlignMiddle
Center aligned.
Definition: qgsscalebarsettings.h:50
QgsTextRenderer::textHeight
static double textHeight(const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode=Point, QFontMetricsF *fontMetrics=nullptr, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags(), double maxLineWidth=0)
Returns the height of a text based on a given format.
Definition: qgstextrenderer.cpp:620
QgsTextRenderer::textWidth
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.
Definition: qgstextrenderer.cpp:545
qgsscalebarsettings.h
qgslayoututils.h
QgsScaleBarRenderer::ScaleBarContext::size
QSizeF size
Destination size for scalebar.
Definition: qgsscalebarrenderer.h:84
QgsScaleBarSettings::alignment
Alignment alignment() const
Returns the scalebar alignment.
Definition: qgsscalebarsettings.h:614
QgsScaleBarSettings::boxContentSpace
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
Definition: qgsscalebarsettings.h:602
QgsScaleBarSettings::textFormat
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
Definition: qgsscalebarsettings.h:268
QgsNumericScaleBarRenderer::visibleName
QString visibleName() const override
Returns the user friendly, translated name for the renderer.
Definition: qgsnumericscalebarrenderer.cpp:30
QgsScaleBarSettings
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Definition: qgsscalebarsettings.h:40
QgsNumericScaleBarRenderer::QgsNumericScaleBarRenderer
QgsNumericScaleBarRenderer()=default
Constructor for QgsNumericScaleBarRenderer.
QgsScaleBarRenderer::Flag::FlagUsesAlignment
@ FlagUsesAlignment
Renderer uses the QgsScaleBarSettings::alignment() setting.
QgsScaleBarRenderer::ScaleBarContext::scale
double scale
Scale denominator.
Definition: qgsscalebarrenderer.h:87
QgsTextRenderer::drawText
static void drawText(const QRectF &rect, double rotation, HAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool drawAsOutlines=true, VAlignment vAlignment=AlignTop, Qgis::TextRendererFlags flags=Qgis::TextRendererFlags())
Draws text within a rectangle using the specified settings.
Definition: qgstextrenderer.cpp:81
QgsTextRenderer::AlignLeft
@ AlignLeft
Left align.
Definition: qgstextrenderer.h:60
QgsNumericScaleBarRenderer::flags
Flags flags() const override
Returns the scalebar rendering flags, which dictates the renderer's behavior.
Definition: qgsnumericscalebarrenderer.cpp:40
QgsScaleBarSettings::AlignRight
@ AlignRight
Right aligned.
Definition: qgsscalebarsettings.h:51
QgsNumericFormat::formatDouble
virtual QString formatDouble(double value, const QgsNumericFormatContext &context) const =0
Returns a formatted string representation of a numeric double value.
QgsTextFormat::toQFont
QFont toQFont() const
Returns a QFont matching the relevant settings from this text format.
Definition: qgstextformat.cpp:758
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
qgsnumericformat.h
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:367
QgsLayoutUtils::fontAscentMM
static double fontAscentMM(const QFont &font)
Calculates a font ascent in millimeters, including workarounds for QT font rendering issues.
Definition: qgslayoututils.cpp:184
qgsnumericscalebarrenderer.h
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsScaleBarSettings::numericFormat
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Definition: qgsscalebarsettings.cpp:325
QgsNumericScaleBarRenderer
A scale bar style that draws text in the form of '1:XXXXX'.
Definition: qgsnumericscalebarrenderer.h:30
QgsNumericFormatContext
A context for numeric formats.
Definition: qgsnumericformat.h:34
QgsTextRenderer::HAlignment
HAlignment
Horizontal alignment.
Definition: qgstextrenderer.h:58
QgsLayoutUtils::textWidthMM
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 ...
Definition: qgslayoututils.cpp:222