QGIS API Documentation  3.6.0-Noosa (5873452)
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 <QList>
21 #include <QPainter>
22 
23 void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
24 {
25  if ( !context.painter() )
26  {
27  return;
28  }
29 
30  QPainter *painter = context.painter();
31 
32  painter->save();
33  if ( context.flags() & QgsRenderContext::Antialiasing )
34  painter->setRenderHint( QPainter::Antialiasing, true );
35 
36  double margin = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
37  //map scalebar alignment to Qt::AlignmentFlag type
39  switch ( settings.alignment() )
40  {
43  break;
46  break;
49  break;
50  }
51 
52  //text destination is item's rect, excluding the margin
53  QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), QgsUnitTypes::RenderMillimeters ) - 2 * margin,
54  context.convertToPainterUnits( scaleContext.size.height(), QgsUnitTypes::RenderMillimeters ) - 2 * margin );
55  QgsTextRenderer::drawText( painterRect, 0, hAlign, QStringList() << scaleText( scaleContext.scale ), context, settings.textFormat() );
56 
57  painter->restore();
58 }
59 
61  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
62 {
63  QFont font = settings.textFormat().toQFont();
64 
65  double textWidth = QgsLayoutUtils::textWidthMM( font, scaleText( scaleContext.scale ) );
66  double textHeight = QgsLayoutUtils::fontAscentMM( font );
67 
68  return QSizeF( 2 * settings.boxContentSpace() + 2 * settings.pen().width() + textWidth,
69  textHeight + 2 * settings.boxContentSpace() );
70 }
71 
72 QString QgsNumericScaleBarRenderer::scaleText( double scale ) const
73 {
74  return "1:" + QStringLiteral( "%L1" ).arg( scale, 0, 'f', 0 );
75 }
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.
Use antialiasing while drawing.
Alignment alignment() const
Returns the scalebar alignment.
HAlignment
Horizontal alignment.
Flags flags() const
Returns combination of flags used for rendering.
QSizeF calculateBoxSize(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...
QSizeF size
Destination size for scalebar.
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
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.
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...
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).
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...
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.