QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties 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 : [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 
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  p->setPen( QPen( mScaleBar->fontColor() ) );
48 
49  QString firstLabel = mScaleBar->firstLabelString();
50  double xOffset = mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2;
51 
52  //double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
53  QList<QPair<double, double> > segmentInfo;
54  mScaleBar->segmentPositions( segmentInfo );
55 
56  double currentLabelNumber = 0.0;
57 
58  int nSegmentsLeft = mScaleBar->numSegmentsLeft();
59  int segmentCounter = 0;
60  QString currentNumericLabel;
61 
62  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
63  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
64  {
65  if ( segmentCounter == 0 && nSegmentsLeft > 0 )
66  {
67  //label first left segment
68  currentNumericLabel = firstLabel;
69  }
70  else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
71  {
72  currentLabelNumber = 0;
73  }
74 
75  if ( segmentCounter >= nSegmentsLeft )
76  {
77  currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
78  }
79 
80  if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
81  {
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  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() );
97  }
98 
99  p->restore();
100 }
101 
103 {
104  if ( !mScaleBar )
105  {
106  return QRectF();
107  }
108 
109  //consider centered first label
110  double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
111 
112  //consider last number and label
113 
115  QString largestNumberLabel = QString::number( largestLabelNumber );
116  QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
117  double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2;
118 
119  double totalBarLength = 0.0;
120 
121  QList< QPair<double, double> > segmentList;
122  mScaleBar->segmentPositions( segmentList );
123 
124  QList< QPair<double, double> >::const_iterator segmentIt = segmentList.constBegin();
125  for ( ; segmentIt != segmentList.constEnd(); ++segmentIt )
126  {
127  totalBarLength += segmentIt->second;
128  }
129 
130  double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
132 
133  return QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), width, height );
134 }