QGIS API Documentation  2.99.0-Master (314842d)
qgsticksscalebarstyle.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsticksscalebarstyle.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 "qgsticksscalebarstyle.h"
18 #include "qgscomposerscalebar.h"
19 #include "qgscomposerutils.h"
20 #include <QPainter>
21 
23 {
24  mTickPosition = TicksMiddle;
25 }
26 
28 {
29  mTickPosition = TicksMiddle;
30 }
31 
33 {
34  switch ( mTickPosition )
35  {
36  case TicksUp:
37  return QStringLiteral( "Line Ticks Up" );
38  case TicksDown:
39  return QStringLiteral( "Line Ticks Down" );
40  case TicksMiddle:
41  return QStringLiteral( "Line Ticks Middle" );
42  }
43  return QLatin1String( "" ); // to make gcc happy
44 }
45 
46 void QgsTicksScaleBarStyle::draw( QPainter *p, double xOffset ) const
47 {
48  if ( !mScaleBar )
49  {
50  return;
51  }
53  double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
54  double bottomPosition = barTopPosition + mScaleBar->height();
55 
56  p->save();
57  //antialiasing on
58  p->setRenderHint( QPainter::Antialiasing, true );
59  p->setPen( mScaleBar->pen() );
60 
61  QList<QPair<double, double> > segmentInfo;
62  mScaleBar->segmentPositions( segmentInfo );
63 
64  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
65  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
66  {
67  p->drawLine( QLineF( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() ) );
68  }
69 
70  //draw last tick and horizontal line
71  if ( !segmentInfo.isEmpty() )
72  {
73  double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters() + xOffset;
74  double verticalPos = 0.0;
75  switch ( mTickPosition )
76  {
77  case TicksDown:
78  verticalPos = barTopPosition;
79  break;
80  case TicksMiddle:
81  verticalPos = middlePosition;
82  break;
83  case TicksUp:
84  verticalPos = bottomPosition;
85  break;
86  }
87  //horizontal line
88  p->drawLine( QLineF( xOffset + segmentInfo.at( 0 ).first, verticalPos, lastTickPositionX, verticalPos ) );
89  //last vertical line
90  p->drawLine( QLineF( lastTickPositionX, barTopPosition, lastTickPositionX, barTopPosition + mScaleBar->height() ) );
91  }
92 
93  p->restore();
94 
95  //draw labels using the default method
96  drawLabels( p );
97 }
98 
99 
A scale bar item that can be added to a map composition.
QPen pen() const
Returns the pen used for drawing the scalebar.
void draw(QPainter *p, double xOffset=0) const override
Draw method.
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
QString name() const override
Get a name for this style.
double labelBarSpace() const
Abstraction of composer scale bar style.
QgsTicksScaleBarStyle(const QgsComposerScaleBar *bar)
const QgsComposerScaleBar * mScaleBar
double segmentMillimeters() const
double boxContentSpace() const
void segmentPositions(QList< QPair< double, double > > &posWidthList) const
Returns the x - positions of the segment borders (in item coordinates) and the width of the segment...
virtual void drawLabels(QPainter *p) const