QGIS API Documentation  2.3.0-Master
 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 : 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 <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 }
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.
double height() const
QString scaleText() const
Returns the text for the scale bar or an empty string in case of error.
double scale() const
Scale.
double textWidthMillimeters(const QFont &font, const QString &text) const
Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE...
void drawText(QPainter *p, double x, double y, const QString &text, const QFont &font) const
Draws Text.
Abstraction of composer scale bar style.
const QgsComposerMap * composerMap() const
Object representing map window.
QColor fontColor() const
const QgsComposerScaleBar * mScaleBar
void draw(QPainter *p, double xOffset=0) const
Draws the style.
double fontAscentMillimeters(const QFont &font) const
Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCAL...