QGIS API Documentation  2.2.0-Valmiera
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
qgsnumericscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsnumericscalebarstyle.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 
18 #include "qgscomposermap.h"
19 #include "qgscomposerscalebar.h"
20 #include <QList>
21 #include <QPainter>
22 
24 {
25 
26 }
27 
29 {
30 
31 }
32 
34 {
35 
36 }
37 
39 {
40  return "Numeric";
41 }
42 
43 void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
44 {
45  Q_UNUSED( xOffset );
46  if ( !p || !mScaleBar )
47  {
48  return;
49  }
50 
51  p->save();
52  p->setFont( mScaleBar->font() );
53  p->setPen( mScaleBar->fontColor() );
54 
55  //call QgsComposerItem's pen() function, since that refers to the frame pen
56  //and QgsComposerScalebar's pen() function refers to the scale bar line width,
57  //which is not used for numeric scale bars. Divide the pen width by 2 since
58  //half the width of the frame is drawn outside the item.
59  double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0;
60  double margin = mScaleBar->boxContentSpace();
61  //map scalebar alignment to Qt::AlignmentFlag type
62  Qt::AlignmentFlag hAlign;
63  switch ( mScaleBar->alignment() )
64  {
66  hAlign = Qt::AlignLeft;
67  break;
69  hAlign = Qt::AlignHCenter;
70  break;
72  hAlign = Qt::AlignRight;
73  break;
74  default:
75  hAlign = Qt::AlignLeft;
76  break;
77  }
78 
79  //text destination is item's rect, excluding the margin and frame
80  QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
81  mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop );
82 
83  p->restore();
84 }
85 
87 {
88  QRectF rect;
89  if ( !mScaleBar )
90  {
91  return rect;
92  }
93 
94  double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
95  double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );
96 
97  rect = QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), 2 * mScaleBar->boxContentSpace()
98  + 2 * mScaleBar->pen().width() + textWidth,
99  textHeight + 2 * mScaleBar->boxContentSpace() );
100 
101  if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 )
102  {
103  //hack to move scale bar the left / right in order to keep the bar alignment
104  const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
105  }
106  mLastScaleBarWidth = rect.width();
107  return rect;
108 }
109 
111 {
112  QString scaleBarText;
113  if ( mScaleBar )
114  {
115  //find out scale
116  double scaleDenominator = 1;
117  const QgsComposerMap* composerMap = mScaleBar->composerMap();
118  if ( composerMap )
119  {
120  scaleDenominator = composerMap->scale();
121  scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 );
122  }
123  scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 );
124  }
125  return scaleBarText;
126 }