QGIS API Documentation  2.17.0-Master (eef6f05)
qgssingleboxscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgssingleboxscalebarstyle.h
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  //nothing to do...
36 }
37 
38 void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
39 {
40  if ( !mScaleBar )
41  {
42  return;
43  }
45 
46  p->save();
47  //antialiasing on
48  p->setRenderHint( QPainter::Antialiasing, true );
49  p->setPen( mScaleBar->pen() );
50 
51  QList<QPair<double, double> > segmentInfo;
52  mScaleBar->segmentPositions( segmentInfo );
53 
54  bool useColor = true; //alternate brush color/white
55 
56  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
57  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
58  {
59  if ( useColor ) //alternating colors
60  {
61  p->setBrush( mScaleBar->brush() );
62  }
63  else //secondary color
64  {
65  p->setBrush( mScaleBar->brush2() );
66  }
67 
68  QRectF segmentRect( segmentIt->first + xOffset, barTopPosition, segmentIt->second, mScaleBar->height() );
69  p->drawRect( segmentRect );
70  useColor = !useColor;
71  }
72 
73  p->restore();
74 
75  //draw labels using the default method
76  drawLabels( p );
77 }
78 
80 {
81  return "Single Box";
82 }
83 
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...
QgsSingleBoxScaleBarStyle(const QgsComposerScaleBar *bar)
void drawRect(const QRectF &rectangle)
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 draw(QPainter *p, double xOffset=0) const override
Draw method.
void restore()
const QgsComposerScaleBar * mScaleBar
double boxContentSpace() const
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