QGIS API Documentation  3.0.2-Girona (307d082)
qgsdoubleboxscalebarrenderer.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdoubleboxscalebarrenderer.cpp
3  --------------------------------
4  begin : June 2008
5  copyright : (C) 2008 by Marco Hugentobler
6  email : marco.hugentobler@karto.baug.ethz.ch
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 QgsDoubleBoxScaleBarRenderer::draw( QgsRenderContext &context, const QgsScaleBarSettings &settings, const ScaleBarContext &scaleContext ) const
24 {
25  if ( !context.painter() )
26  {
27  return;
28  }
29  QPainter *painter = context.painter();
30 
31  double barTopPosition = context.convertToPainterUnits( QgsLayoutUtils::fontAscentMM( settings.font() ) + settings.labelBarSpace() + settings.boxContentSpace(), QgsUnitTypes::RenderMillimeters );
32  double segmentHeight = context.convertToPainterUnits( settings.height() / 2, QgsUnitTypes::RenderMillimeters );
33 
34  painter->save();
35  if ( context.flags() & QgsRenderContext::Antialiasing )
36  painter->setRenderHint( QPainter::Antialiasing, true );
37 
38  QPen pen = settings.pen();
39  pen.setWidthF( context.convertToPainterUnits( pen.widthF(), QgsUnitTypes::RenderMillimeters ) );
40  painter->setPen( pen );
41 
42  bool useColor = true; //alternate brush color/white
43 
44  double xOffset = context.convertToPainterUnits( firstLabelXOffset( settings ), QgsUnitTypes::RenderMillimeters );
45 
46  QList<double> positions = segmentPositions( scaleContext, settings );
47  QList<double> widths = segmentWidths( scaleContext, settings );
48 
49  for ( int i = 0; i < positions.size(); ++i )
50  {
51  //draw top half
52  if ( useColor )
53  {
54  painter->setBrush( settings.brush() );
55  }
56  else //secondary color
57  {
58  painter->setBrush( settings.brush2() );
59  }
60 
61  QRectF segmentRectTop( context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset,
62  barTopPosition,
63  context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters ), segmentHeight );
64  painter->drawRect( segmentRectTop );
65 
66  //draw bottom half
67  if ( useColor )
68  {
69  //secondary color
70  painter->setBrush( settings.brush2() );
71  }
72  else //primary color
73  {
74  painter->setBrush( settings.brush() );
75  }
76 
77  QRectF segmentRectBottom( context.convertToPainterUnits( positions.at( i ), QgsUnitTypes::RenderMillimeters ) + xOffset,
78  barTopPosition + segmentHeight,
79  context.convertToPainterUnits( widths.at( i ), QgsUnitTypes::RenderMillimeters ), segmentHeight );
80  painter->drawRect( segmentRectBottom );
81  useColor = !useColor;
82  }
83 
84  painter->restore();
85 
86  //draw labels using the default method
87  drawDefaultLabels( context, settings, scaleContext );
88 }
QBrush brush() const
Returns the primary brush used for filling the scalebar.
QPen pen() const
Returns the pen used for drawing outlines in the scalebar.
Use antialiasing while drawing.
Flags flags() const
Return combination of flags used for rendering.
QFont font() const
Returns the font used for drawing text in the scalebar.
double firstLabelXOffset(const QgsScaleBarSettings &settings) const
Returns the x-offset (in millimeters) used for the first label in the scalebar.
double height() const
Returns the scalebar height (in millimeters).
double boxContentSpace() const
Returns the spacing (margin) between the scalebar box and content in millimeters. ...
double labelBarSpace() const
Returns the spacing (in millimeters) between labels and the scalebar.
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...
QList< double > segmentWidths(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of widths of each segment of 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...
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).
QList< double > segmentPositions(const QgsScaleBarRenderer::ScaleBarContext &scaleContext, const QgsScaleBarSettings &settings) const
Returns a list of positions for each segment within the scalebar.
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...
QBrush brush2() const
Returns the secondary brush for the scalebar.
The QgsScaleBarSettings class stores the appearance and layout settings for scalebar drawing with Qgs...
Contains parameters regarding scalebar calculations.