QGIS API Documentation  3.4.15-Madeira (e83d02e274)
qgsticksscalebarrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsticksscalebarrenderer.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 <QPainter>
21 
23 {
24  switch ( mTickPosition )
25  {
26  case TicksUp:
27  return QStringLiteral( "Line Ticks Up" );
28  case TicksDown:
29  return QStringLiteral( "Line Ticks Down" );
30  case TicksMiddle:
31  return QStringLiteral( "Line Ticks Middle" );
32  }
33  return QString(); // to make gcc happy
34 }
35 
36 void QgsTicksScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
37 {
38  if ( !context.painter() )
39  return;
40 
41  QPainter *painter = context.painter();
42 
43  double scaledLabelBarSpace = context.convertToPainterUnits( settings.labelBarSpace(), QgsUnitTypes::RenderMillimeters );
44  double scaledBoxContentSpace = context.convertToPainterUnits( settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
45  QFontMetricsF fontMetrics = QgsTextRenderer::fontMetrics( context, settings.textFormat() );
46  double barTopPosition = fontMetrics.ascent() + scaledLabelBarSpace + scaledBoxContentSpace;
47  double middlePosition = barTopPosition + context.convertToPainterUnits( settings.height() / 2.0, QgsUnitTypes::RenderMillimeters );
48  double bottomPosition = barTopPosition + context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters );
49 
50  double xOffset = firstLabelXOffset( settings, context );
51 
52  painter->save();
53  if ( context.flags() & QgsRenderContext::Antialiasing )
54  painter->setRenderHint( QPainter::Antialiasing, true );
55 
56  QPen pen = settings.pen();
57  pen.setWidthF( context.convertToPainterUnits( pen.widthF(), QgsUnitTypes::RenderMillimeters ) );
58  painter->setPen( pen );
59 
60  QList<double> positions = segmentPositions( scaleContext, settings );
61 
62  for ( int i = 0; i < positions.size(); ++i )
63  {
64  painter->drawLine( QLineF( context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset, barTopPosition,
65  context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset,
66  barTopPosition + context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters ) ) );
67  }
68 
69  //draw last tick and horizontal line
70  if ( !positions.isEmpty() )
71  {
72  double lastTickPositionX = context.convertToPainterUnits( positions.at( positions.size() - 1 ) + scaleContext.segmentWidth, QgsUnitTypes::RenderMillimeters ) + xOffset;
73  double verticalPos = 0.0;
74  switch ( mTickPosition )
75  {
76  case TicksDown:
77  verticalPos = barTopPosition;
78  break;
79  case TicksMiddle:
80  verticalPos = middlePosition;
81  break;
82  case TicksUp:
83  verticalPos = bottomPosition;
84  break;
85  }
86  //horizontal line
87  painter->drawLine( QLineF( xOffset + context.convertToPainterUnits( positions.at( 0 ), QgsUnitTypes::RenderMillimeters ),
88  verticalPos, lastTickPositionX, verticalPos ) );
89  //last vertical line
90  painter->drawLine( QLineF( lastTickPositionX, barTopPosition, lastTickPositionX, barTopPosition + context.convertToPainterUnits( settings.height(), QgsUnitTypes::RenderMillimeters ) ) );
91  }
92 
93  painter->restore();
94 
95  //draw labels using the default method
96  drawDefaultLabels( context, settings, scaleContext );
97 }
98 
99 
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
Use antialiasing while drawing.
double segmentWidth
The width, in millimeters, of each individual segment drawn.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
QString name() const override
Returns the unique name for this style.
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...
static QFontMetricsF fontMetrics(QgsRenderContext &context, const QgsTextFormat &format)
Returns the font metrics for the given text format, when rendered in the specified render context...
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. ...
Contains information about the context of a rendering operation.
double height() const
Returns the scalebar height (in millimeters).
QPainter * painter()
Returns the destination QPainter for the render operation.
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
Flags flags() const
Returns combination of flags used for rendering.
Q_DECL_DEPRECATED double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
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...
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.