QGIS API Documentation  2.8.2-Wien
 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 "qgscomposerutils.h"
21 #include <QList>
22 #include <QPainter>
23 
25 {
26 
27 }
28 
29 QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 ), mLastScaleBarWidth( 0 )
30 {
31 
32 }
33 
35 {
36 
37 }
38 
40 {
41  return "Numeric";
42 }
43 
44 void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
45 {
46  Q_UNUSED( xOffset );
47  if ( !p || !mScaleBar )
48  {
49  return;
50  }
51 
52  p->save();
53  //antialiasing on
54  p->setRenderHint( QPainter::Antialiasing, true );
55  p->setFont( mScaleBar->font() );
56 
57  //call QgsComposerItem's pen() function, since that refers to the frame pen
58  //and QgsComposerScalebar's pen() function refers to the scale bar line width,
59  //which is not used for numeric scale bars. Divide the pen width by 2 since
60  //half the width of the frame is drawn outside the item.
61  double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0;
62  double margin = mScaleBar->boxContentSpace();
63  //map scalebar alignment to Qt::AlignmentFlag type
64  Qt::AlignmentFlag hAlign;
65  switch ( mScaleBar->alignment() )
66  {
68  hAlign = Qt::AlignLeft;
69  break;
71  hAlign = Qt::AlignHCenter;
72  break;
74  hAlign = Qt::AlignRight;
75  break;
76  default:
77  hAlign = Qt::AlignLeft;
78  break;
79  }
80 
81  //text destination is item's rect, excluding the margin and frame
82  QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
83  QgsComposerUtils::drawText( p, painterRect, scaleText(), mScaleBar->font(), mScaleBar->fontColor(), hAlign, Qt::AlignTop );
84 
85  p->restore();
86 }
87 
89 {
90  QRectF rect;
91  if ( !mScaleBar )
92  {
93  return rect;
94  }
95 
96  double textWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), scaleText() );
97  double textHeight = QgsComposerUtils::fontAscentMM( mScaleBar->font() );
98 
99  rect = QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), 2 * mScaleBar->boxContentSpace()
100  + 2 * mScaleBar->pen().width() + textWidth,
101  textHeight + 2 * mScaleBar->boxContentSpace() );
102 
103  if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 )
104  {
105  //hack to move scale bar the left / right in order to keep the bar alignment
106  const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
107  }
108  mLastScaleBarWidth = rect.width();
109  return rect;
110 }
111 
112 QString QgsNumericScaleBarStyle::scaleText() const
113 {
114  QString scaleBarText;
115  if ( mScaleBar )
116  {
117  //find out scale
118  double scaleDenominator = 1;
119  const QgsComposerMap* composerMap = mScaleBar->composerMap();
120  if ( composerMap )
121  {
122  scaleDenominator = composerMap->scale();
123  scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 );
124  }
125  scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 );
126  }
127  return scaleBarText;
128 }