QGIS API Documentation  3.0.2-Girona (307d082)
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  QFont scaledFont = settings.font();
37  scaledFont.setPointSizeF( scaledFont.pointSizeF() * context.scaleFactor() );
38 
39  double margin = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
40  //map scalebar alignment to Qt::AlignmentFlag type
41  Qt::AlignmentFlag hAlign = Qt::AlignLeft;
42  switch ( settings.alignment() )
43  {
45  hAlign = Qt::AlignLeft;
46  break;
48  hAlign = Qt::AlignHCenter;
49  break;
51  hAlign = Qt::AlignRight;
52  break;
53  }
54 
55  //text destination is item's rect, excluding the margin
56  QRectF painterRect( margin, margin, context.convertToPainterUnits( scaleContext.size.width(), QgsUnitTypes::RenderMillimeters ) - 2 * margin,
57  context.convertToPainterUnits( scaleContext.size.height(), QgsUnitTypes::RenderMillimeters ) - 2 * margin );
58  QgsLayoutUtils::drawText( painter, painterRect, scaleText( scaleContext.scale ), scaledFont, settings.fontColor(), hAlign, Qt::AlignTop );
59 
60  painter->restore();
61 }
62 
64  const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
65 {
66  double textWidth = QgsLayoutUtils::textWidthMM( settings.font(), scaleText( scaleContext.scale ) );
67  double textHeight = QgsLayoutUtils::fontAscentMM( settings.font() );
68 
69  return QSizeF( 2 * settings.boxContentSpace() + 2 * settings.pen().width() + textWidth,
70  textHeight + 2 * settings.boxContentSpace() );
71 }
72 
73 QString QgsNumericScaleBarRenderer::scaleText( double scale ) const
74 {
75  return "1:" + QStringLiteral( "%L1" ).arg( scale, 0, 'f', 0 );
76 }
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.
QColor fontColor() const
Returns the color used for drawing text in the scalebar.
Alignment alignment() const
Returns the scalebar alignment.
Flags flags() const
Return combination of flags used for rendering.
QFont font() const
Returns the font used for drawing text in the scalebar.
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. ...
static void drawText(QPainter *painter, QPointF position, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of layout specific issues (calculation to...
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...
double scaleFactor() const
Returns the scaling factor for the render to convert painter units to physical sizes.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.