QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 
35 }
36 
38 {
39  switch ( mTickPosition )
40  {
41  case TicksUp:
42  return "Line Ticks Up";
43  case TicksDown:
44  return "Line Ticks Down";
45  case TicksMiddle:
46  return "Line Ticks Middle";
47  }
48  return ""; // to make gcc happy
49 }
50 
51 void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
52 {
53  if ( !mScaleBar )
54  {
55  return;
56  }
58  double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
59  double bottomPosition = barTopPosition + mScaleBar->height();
60 
61  p->save();
62  //antialiasing on
63  p->setRenderHint( QPainter::Antialiasing, true );
64  p->setPen( mScaleBar->pen() );
65 
66  QList<QPair<double, double> > segmentInfo;
67  mScaleBar->segmentPositions( segmentInfo );
68 
69  QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
70  for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
71  {
72  p->drawLine( QLineF( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() ) );
73  }
74 
75  //draw last tick and horizontal line
76  if ( !segmentInfo.isEmpty() )
77  {
78  double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters() + xOffset;
79  double verticalPos = 0.0;
80  switch ( mTickPosition )
81  {
82  case TicksDown:
83  verticalPos = barTopPosition;
84  break;
85  case TicksMiddle:
86  verticalPos = middlePosition;
87  break;
88  case TicksUp:
89  verticalPos = bottomPosition;
90  break;
91  }
92  //horizontal line
93  p->drawLine( QLineF( xOffset + segmentInfo.at( 0 ).first, verticalPos, lastTickPositionX, verticalPos ) );
94  //last vertical line
95  p->drawLine( QLineF( lastTickPositionX, barTopPosition, lastTickPositionX, barTopPosition + mScaleBar->height() ) );
96  }
97 
98  p->restore();
99 
100  //draw labels using the default method
101  drawLabels( p );
102 }
103 
104 
A scale bar item that can be added to a map composition.
void setRenderHint(RenderHint hint, bool on)
QPen pen() const
Returns the pen used for drawing the scalebar.
void draw(QPainter *p, double xOffset=0) const override
Draw method.
const T & at(int i) const
void save()
static double fontAscentMM(const QFont &font)
Calculate font ascent in millimeters, including workarounds for QT font rendering issues...
void drawLine(const QLineF &line)
void setPen(const QColor &color)
bool isEmpty() const
QString name() const override
double labelBarSpace() const
T & first()
Abstraction of composer scale bar style.
QgsTicksScaleBarStyle(const QgsComposerScaleBar *bar)
void restore()
const QgsComposerScaleBar * mScaleBar
double segmentMillimeters() const
double boxContentSpace() const
T & last()
const_iterator constEnd() 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...
const_iterator constBegin() const
virtual void drawLabels(QPainter *p) const