QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsticksscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsticksscalebarstyle.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 
17 #include "qgsticksscalebarstyle.h"
18 #include "qgscomposerscalebar.h"
19 #include "qgscomposerutils.h"
20 #include <QPainter>
21 
23 {
25 }
26 
28 {
30 }
31 
33 {
34 
35 }
36 
38 {
39  switch ( mTickPosition )
40  {
41  case TicksUp:
42  return "Line Ticks Up";
43  case TicksDown:
44  return "Line Ticks Down";
45  case TicksMiddle:
46  return "Line Ticks Middle";
47  }
48  return ""; // to make gcc happy
49 }
50 
51 void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
52 {
53  if ( !mScaleBar )
54  {
55  return;
56  }
58  double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
59  double bottomPosition = barTopPosition + mScaleBar->height();
60 
61  p->save();
62  //antialiasing on
63  p->setRenderHint( QPainter::Antialiasing, true );
64  p->setPen( mScaleBar->pen() );
65 
66  QList<QPair<double, double> > segmentInfo;
67  mScaleBar->segmentPositions( segmentInfo );
68 
69  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
70  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
71  {
72  p->drawLine( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() );
73  switch ( mTickPosition )
74  {
75  case TicksDown:
76  p->drawLine( xOffset + segmentIt->first, barTopPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), barTopPosition );
77  break;
78  case TicksMiddle:
79  p->drawLine( xOffset + segmentIt->first, middlePosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), middlePosition );
80  break;
81  case TicksUp:
82  p->drawLine( xOffset + segmentIt->first, bottomPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), bottomPosition );
83  break;
84  }
85  }
86 
87  //draw last tick
88  if ( !segmentInfo.isEmpty() )
89  {
90  double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters();
91  p->drawLine( lastTickPositionX + xOffset, barTopPosition, lastTickPositionX + xOffset, barTopPosition + mScaleBar->height() );
92  }
93 
94  p->restore();
95 
96  //draw labels using the default method
97  drawLabels( p );
98 }
99 
100 
A scale bar item that can be added to a map composition.
virtual void drawLabels(QPainter *p) const
double boxContentSpace() 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...
void draw(QPainter *p, double xOffset=0) const
double labelBarSpace() const
double segmentMillimeters() const
Abstraction of composer scale bar style.
QPen pen() const
Returns the pen used for drawing the scalebar.
const QgsComposerScaleBar * mScaleBar