Quantum GIS API Documentation  1.8
src/core/composer/qgsticksscalebarstyle.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                             qgsticksscalebarstyle.cpp
00003                             -------------------------
00004     begin                : June 2008
00005     copyright            : (C) 2008 by Marco Hugentobler
00006     email                : marco.hugentobler@karto.baug.ethz.ch
00007  ***************************************************************************/
00008 /***************************************************************************
00009  *                                                                         *
00010  *   This program is free software; you can redistribute it and/or modify  *
00011  *   it under the terms of the GNU General Public License as published by  *
00012  *   the Free Software Foundation; either version 2 of the License, or     *
00013  *   (at your option) any later version.                                   *
00014  *                                                                         *
00015  ***************************************************************************/
00016 
00017 #include "qgsticksscalebarstyle.h"
00018 #include "qgscomposerscalebar.h"
00019 #include <QPainter>
00020 
00021 QgsTicksScaleBarStyle::QgsTicksScaleBarStyle( const QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar )
00022 {
00023   mTickPosition = TicksMiddle;
00024 }
00025 
00026 QgsTicksScaleBarStyle::QgsTicksScaleBarStyle(): QgsScaleBarStyle( 0 )
00027 {
00028   mTickPosition = TicksMiddle;
00029 }
00030 
00031 QgsTicksScaleBarStyle::~QgsTicksScaleBarStyle()
00032 {
00033 
00034 }
00035 
00036 QString QgsTicksScaleBarStyle::name() const
00037 {
00038   switch ( mTickPosition )
00039   {
00040     case TicksUp:
00041       return "Line Ticks Up";
00042     case TicksDown:
00043       return "Line Ticks Down";
00044     case TicksMiddle:
00045       return "Line Ticks Middle";
00046   }
00047   return "";  // to make gcc happy
00048 }
00049 
00050 void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
00051 {
00052   if ( !mScaleBar )
00053   {
00054     return;
00055   }
00056   double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
00057   double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
00058   double bottomPosition = barTopPosition + mScaleBar->height();
00059 
00060   p->save();
00061   p->setPen( mScaleBar->pen() );
00062 
00063   QList<QPair<double, double> > segmentInfo;
00064   mScaleBar->segmentPositions( segmentInfo );
00065 
00066   QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
00067   for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
00068   {
00069     p->drawLine( segmentIt->first + xOffset, barTopPosition, segmentIt->first + xOffset, barTopPosition + mScaleBar->height() );
00070     switch ( mTickPosition )
00071     {
00072       case TicksDown:
00073         p->drawLine( xOffset + segmentIt->first, barTopPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), barTopPosition );
00074         break;
00075       case TicksMiddle:
00076         p->drawLine( xOffset + segmentIt->first, middlePosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), middlePosition );
00077         break;
00078       case TicksUp:
00079         p->drawLine( xOffset + segmentIt->first, bottomPosition, xOffset + segmentIt->first + mScaleBar->segmentMillimeters(), bottomPosition );
00080         break;
00081     }
00082   }
00083 
00084   //draw last tick
00085   if ( !segmentInfo.isEmpty() )
00086   {
00087     double lastTickPositionX = segmentInfo.last().first + mScaleBar->segmentMillimeters();
00088     p->drawLine( lastTickPositionX + xOffset, barTopPosition, lastTickPositionX + xOffset, barTopPosition + mScaleBar->height() );
00089   }
00090 
00091   p->restore();
00092 
00093   //draw labels using the default method
00094   drawLabels( p );
00095 }
00096 
00097 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines