Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsscalebarstyle.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 "qgsscalebarstyle.h"
18 #include "qgscomposerscalebar.h"
19 #include <QFontMetricsF>
20 #include <QPainter>
21 
23 {
24 
25 }
26 
28 {
29 
30 }
31 
33 {
34 
35 }
36 
37 void QgsScaleBarStyle::drawLabels( QPainter* p ) const
38 {
39  if ( !p || !mScaleBar )
40  {
41  return;
42  }
43 
44  p->save();
45 
46  p->setFont( mScaleBar->font() );
47 
48  QString firstLabel = mScaleBar->firstLabelString();
49  double xOffset = mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2;
50 
51  //double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
52  QList<QPair<double, double> > segmentInfo;
53  mScaleBar->segmentPositions( segmentInfo );
54 
55  double currentLabelNumber = 0.0;
56 
57  int nSegmentsLeft = mScaleBar->numSegmentsLeft();
58  int segmentCounter = 0;
59  QString currentNumericLabel;
60 
61  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
62  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
63  {
64  if ( segmentCounter == 0 && nSegmentsLeft > 0 )
65  {
66  //label first left segment
67  currentNumericLabel = firstLabel;
68  }
69  else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
70  {
71  currentLabelNumber = 0;
72  }
73 
74  if ( segmentCounter >= nSegmentsLeft )
75  {
76  currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
77  }
78 
79  if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
80  {
81  p->setPen( QColor( 0, 0, 0 ) );
82  mScaleBar->drawText( p, segmentIt->first - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font() );
83  }
84 
85  if ( segmentCounter >= nSegmentsLeft )
86  {
87  currentLabelNumber += mScaleBar->numUnitsPerSegment();
88  }
89  ++segmentCounter;
90  }
91 
92  //also draw the last label
93  if ( !segmentInfo.isEmpty() )
94  {
95  currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
96  p->setPen( QColor( 0, 0, 0 ) );
97  mScaleBar->drawText( p, segmentInfo.last().first + mScaleBar->segmentMillimeters() - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font() );
98  }
99 
100  p->restore();
101 }
102 
104 {
105  if ( !mScaleBar )
106  {
107  return QRectF();
108  }
109 
110  //consider centered first label
111  double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
112 
113  //consider last number and label
114 
116  QString largestNumberLabel = QString::number( largestLabelNumber );
117  QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
118  double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2;
119 
120  double totalBarLength = 0.0;
121 
122  QList< QPair<double, double> > segmentList;
123  mScaleBar->segmentPositions( segmentList );
124 
125  QList< QPair<double, double> >::const_iterator segmentIt = segmentList.constBegin();
126  for ( ; segmentIt != segmentList.constEnd(); ++segmentIt )
127  {
128  totalBarLength += segmentIt->second;
129  }
130 
131  double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
133 
134  return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height );
135 }