QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
36 }
37 
39 {
40  return "Double Box";
41 }
42 
43 void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
44 {
45  if ( !mScaleBar )
46  {
47  return;
48  }
50  double segmentHeight = mScaleBar->height() / 2;
51 
52  p->save();
53  //antialiasing on
54  p->setRenderHint( QPainter::Antialiasing, true );
55  p->setPen( mScaleBar->pen() );
56 
57  QList<QPair<double, double> > segmentInfo;
58  mScaleBar->segmentPositions( segmentInfo );
59 
60  bool useColor = true; //alternate brush color/white
61 
62  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
63  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
64  {
65  //draw top half
66  if ( useColor )
67  {
68  p->setBrush( mScaleBar->brush() );
69  }
70  else //secondary color
71  {
72  p->setBrush( mScaleBar->brush2() );
73  }
74 
75  QRectF segmentRectTop( segmentIt->first + xOffset, barTopPosition, segmentIt->second, segmentHeight );
76  p->drawRect( segmentRectTop );
77 
78  //draw bottom half
79  if ( useColor )
80  {
81  //secondary color
82  p->setBrush( mScaleBar->brush2() );
83  }
84  else //primary color
85  {
86  p->setBrush( mScaleBar->brush() );
87  }
88 
89  QRectF segmentRectBottom( segmentIt->first + xOffset, barTopPosition + segmentHeight, segmentIt->second, segmentHeight );
90  p->drawRect( segmentRectBottom );
91  useColor = !useColor;
92  }
93 
94  p->restore();
95 
96  //draw labels using the default method
97  drawLabels( p );
98 }
A scale bar item that can be added to a map composition.
void setRenderHint(RenderHint hint, bool on)
QPen pen() const
Returns the pen used for drawing the scalebar.
QBrush brush2() const
Returns the secondary brush for the scalebar.
void save()
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
void drawRect(const QRectF &rectangle)
void draw(QPainter *p, double xOffset=0) const override
Draws the style.
QBrush brush() const
Returns the primary brush for the scalebar.
void setPen(const QColor &color)
void setBrush(const QBrush &brush)
double labelBarSpace() const
T & first()
Abstraction of composer scale bar style.
void restore()
const QgsComposerScaleBar * mScaleBar
double boxContentSpace() const
QgsDoubleBoxScaleBarStyle(const QgsComposerScaleBar *bar)
const_iterator constEnd() const
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...
const_iterator constBegin() const
virtual void drawLabels(QPainter *p) const