QGIS API Documentation  2.5.0-Master
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules 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 : 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 
18 #include "qgscomposermap.h"
19 #include "qgscomposerscalebar.h"
20 #include "qgscomposerutils.h"
21 #include <QList>
22 #include <QPainter>
23 
25 {
26 
27 }
28 
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 
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 }
A scale bar item that can be added to a map composition.
double boxContentSpace() const
double mLastScaleBarWidth
Store last width (in mm) to keep alignment to left/middle/right side.
Alignment alignment() const
Left / Middle/ Right.
static void drawText(QPainter *painter, const QPointF &pos, const QString &text, const QFont &font, const QColor &color=QColor())
Draws text on a painter at a specific position, taking care of composer specific issues (calculation ...
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
QString scaleText() const
Returns the text for the scale bar or an empty string in case of error.
double scale() const
Scale.
Abstraction of composer scale bar style.
const QgsComposerMap * composerMap() const
Object representing map window.
QPen pen() const
Returns the pen used for drawing the scalebar.
QColor fontColor() const
Returns the color used for drawing text in the scalebar.
static double textWidthMM(const QFont &font, const QString &text)
Calculate font width in millimeters for a string, including workarounds for QT font rendering issues...
const QgsComposerScaleBar * mScaleBar
void draw(QPainter *p, double xOffset=0) const
Draws the style.