QGIS API Documentation  2.7.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 : 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 "qgscomposerscalebar.h"
19 #include "qgscomposerutils.h"
20 #include <QList>
21 #include <QPainter>
22 
24 {
25 
26 }
27 
28 QgsSingleBoxScaleBarStyle::QgsSingleBoxScaleBarStyle(): QgsScaleBarStyle( 0 )
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.
virtual void drawLabels(QPainter *p) const
double boxContentSpace() const
void draw(QPainter *p, double xOffset=0) 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...
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
QgsSingleBoxScaleBarStyle(const QgsComposerScaleBar *bar)
double labelBarSpace() const
Abstraction of composer scale bar style.
QPen pen() const
Returns the pen used for drawing the scalebar.
const QgsComposerScaleBar * mScaleBar
QBrush brush() const
Returns the primary brush for the scalebar.
QBrush brush2() const
Returns the secondary brush for the scalebar.