QGIS API Documentation  2.99.0-Master (314842d)
qgsdoubleboxscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsdoubleboxscalebarstyle.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 "qgscomposerscalebar.h"
19 #include "qgscomposerutils.h"
20 #include <QList>
21 #include <QPainter>
22 
24 {
25 
26 }
27 
29 {
30 
31 }
32 
34 {
35  return QStringLiteral( "Double Box" );
36 }
37 
38 void QgsDoubleBoxScaleBarStyle::draw( QPainter *p, double xOffset ) const
39 {
40  if ( !mScaleBar )
41  {
42  return;
43  }
45  double segmentHeight = mScaleBar->height() / 2;
46 
47  p->save();
48  //antialiasing on
49  p->setRenderHint( QPainter::Antialiasing, true );
50  p->setPen( mScaleBar->pen() );
51 
52  QList<QPair<double, double> > segmentInfo;
53  mScaleBar->segmentPositions( segmentInfo );
54 
55  bool useColor = true; //alternate brush color/white
56 
57  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
58  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
59  {
60  //draw top half
61  if ( useColor )
62  {
63  p->setBrush( mScaleBar->brush() );
64  }
65  else //secondary color
66  {
67  p->setBrush( mScaleBar->brush2() );
68  }
69 
70  QRectF segmentRectTop( segmentIt->first + xOffset, barTopPosition, segmentIt->second, segmentHeight );
71  p->drawRect( segmentRectTop );
72 
73  //draw bottom half
74  if ( useColor )
75  {
76  //secondary color
77  p->setBrush( mScaleBar->brush2() );
78  }
79  else //primary color
80  {
81  p->setBrush( mScaleBar->brush() );
82  }
83 
84  QRectF segmentRectBottom( segmentIt->first + xOffset, barTopPosition + segmentHeight, segmentIt->second, segmentHeight );
85  p->drawRect( segmentRectBottom );
86  useColor = !useColor;
87  }
88 
89  p->restore();
90 
91  //draw labels using the default method
92  drawLabels( p );
93 }
A scale bar item that can be added to a map composition.
QPen pen() const
Returns the pen used for drawing the scalebar.
QBrush brush2() const
Returns the secondary brush for the scalebar.
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
void draw(QPainter *p, double xOffset=0) const override
Draws the style.
QBrush brush() const
Returns the primary brush for the scalebar.
QString name() const override
Get a name for this style.
double labelBarSpace() const
Abstraction of composer scale bar style.
const QgsComposerScaleBar * mScaleBar
double boxContentSpace() const
QgsDoubleBoxScaleBarStyle(const QgsComposerScaleBar *bar)
void segmentPositions(QList< QPair< double, double > > &posWidthList) const
Returns the x - positions of the segment borders (in item coordinates) and the width of the segment...
virtual void drawLabels(QPainter *p) const