QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
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
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
19#include "qgslayoututils.h"
20#include "qgsnumericformat.h"
21#include "qgsscalebarsettings.h"
22#include "qgstextrenderer.h"
23
24#include <QList>
25#include <QPainter>
26#include <QString>
27
28using namespace Qt::StringLiterals;
29
31{
32 return u"Numeric"_s;
33}
34
36{
37 return QObject::tr( "Numeric" );
38}
39
41{
42 return 100;
43}
44
49
54
55void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
56{
57 if ( !context.painter() )
58 {
59 return;
60 }
61
62 QPainter *painter = context.painter();
63
64 const QgsScopedQPainterState painterState( painter );
65 context.setPainterFlagsUsingContext( painter );
66
67 const double margin = context.convertToPainterUnits( settings.boxContentSpace(), Qgis::RenderUnit::Millimeters );
68 //map scalebar alignment to Qt::AlignmentFlag type
70 switch ( settings.alignment() )
71 {
74 break;
77 break;
80 break;
81 }
82
83 //text destination is item's rect, excluding the margin
84 const QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), Qgis::RenderUnit::Millimeters ) - 2 * margin,
85 context.convertToPainterUnits( scaleContext.size.height(), Qgis::RenderUnit::Millimeters ) - 2 * margin );
86 QgsTextRenderer::drawText( painterRect, 0, hAlign, QStringList() << scaleText( scaleContext.scale, settings ), context, settings.textFormat() );
87}
88
90 const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
91{
92 const double painterToMm = 1.0 / context.convertToPainterUnits( 1, Qgis::RenderUnit::Millimeters );
93
94 const double textWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;
95 const double textHeight = QgsTextRenderer::textHeight( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;
96
97 return QSizeF( 2 * settings.boxContentSpace() + textWidth,
98 textHeight + 2 * settings.boxContentSpace() );
99}
100
102{
103 const QFont font = settings.textFormat().toQFont();
104
105 const double textWidth = QgsLayoutUtils::textWidthMM( font, scaleText( scaleContext.scale, settings ) );
106 const double textHeight = QgsLayoutUtils::fontAscentMM( font );
107
108 return QSizeF( 2 * settings.boxContentSpace() + textWidth,
109 textHeight + 2 * settings.boxContentSpace() );
110}
111
112QString QgsNumericScaleBarRenderer::scaleText( double scale, const QgsScaleBarSettings &settings ) const
113{
114 return "1:" + settings.numericFormat()->formatDouble( scale, QgsNumericFormatContext() );
115}
@ Right
Right aligned.
Definition qgis.h:5377
@ Left
Left aligned.
Definition qgis.h:5375
@ Middle
Center aligned.
Definition qgis.h:5376
@ Millimeters
Millimeters.
Definition qgis.h:5256
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:3000
@ Center
Center align.
Definition qgis.h:3002
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.
QgsNumericScaleBarRenderer()=default
Flags flags() const override
Returns the scalebar rendering flags, which dictates the renderer's behavior.
int sortKey() const override
Returns a sorting key value, where renderers with a lower sort key will be shown earlier in lists.
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.
QString visibleName() const override
Returns the user friendly, translated name for the renderer.
QgsNumericScaleBarRenderer * clone() const override
Returns a clone of the renderer.
QString id() const override
Returns the unique ID for this renderer.
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...
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.
void setPainterFlagsUsingContext(QPainter *painter=nullptr) const
Sets relevant flags on a destination painter, using the flags and settings currently defined for the ...
@ FlagUsesAlignment
Renderer uses the QgsScaleBarSettings::alignment() setting.
Stores the appearance and layout settings for scalebar drawing with QgsScaleBarRenderer.
Qgis::ScaleBarAlignment alignment() const
Returns the scalebar alignment.
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
QgsTextFormat & textFormat()
Returns the text format used for drawing text in the scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters.
Scoped object for saving and restoring a QPainter object's state.
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 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.
Contains parameters regarding scalebar calculations.
QSizeF size
Destination size for scalebar.