Quantum GIS API Documentation  1.8
src/core/composer/qgsscalebarstyle.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                             qgsscalebarstyle.cpp
00003                             --------------------
00004     begin                : June 2008
00005     copyright            : (C) 2008 by Marco Hugentobler
00006     email                : [email protected]
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 "qgsscalebarstyle.h"
00018 #include "qgscomposerscalebar.h"
00019 #include <QFontMetricsF>
00020 #include <QPainter>
00021 
00022 QgsScaleBarStyle::QgsScaleBarStyle( const QgsComposerScaleBar* bar ):  mScaleBar( bar )
00023 {
00024 
00025 }
00026 
00027 QgsScaleBarStyle::QgsScaleBarStyle(): mScaleBar( 0 )
00028 {
00029 
00030 }
00031 
00032 QgsScaleBarStyle::~QgsScaleBarStyle()
00033 {
00034 
00035 }
00036 
00037 void QgsScaleBarStyle::drawLabels( QPainter* p ) const
00038 {
00039   if ( !p || !mScaleBar )
00040   {
00041     return;
00042   }
00043 
00044   p->save();
00045 
00046   p->setFont( mScaleBar->font() );
00047 
00048   QString firstLabel = mScaleBar->firstLabelString();
00049   double xOffset =  mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2;
00050 
00051   //double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
00052   QList<QPair<double, double> > segmentInfo;
00053   mScaleBar->segmentPositions( segmentInfo );
00054 
00055   double currentLabelNumber = 0.0;
00056 
00057   int nSegmentsLeft = mScaleBar->numSegmentsLeft();
00058   int segmentCounter = 0;
00059   QString currentNumericLabel;
00060 
00061   QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin();
00062   for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt )
00063   {
00064     if ( segmentCounter == 0 && nSegmentsLeft > 0 )
00065     {
00066       //label first left segment
00067       currentNumericLabel = firstLabel;
00068     }
00069     else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments
00070     {
00071       currentLabelNumber = 0;
00072     }
00073 
00074     if ( segmentCounter >= nSegmentsLeft )
00075     {
00076       currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
00077     }
00078 
00079     if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
00080     {
00081       p->setPen( QColor( 0, 0, 0 ) );
00082       mScaleBar->drawText( p, segmentIt->first - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font() );
00083     }
00084 
00085     if ( segmentCounter >= nSegmentsLeft )
00086     {
00087       currentLabelNumber += mScaleBar->numUnitsPerSegment();
00088     }
00089     ++segmentCounter;
00090   }
00091 
00092   //also draw the last label
00093   if ( !segmentInfo.isEmpty() )
00094   {
00095     currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
00096     p->setPen( QColor( 0, 0, 0 ) );
00097     mScaleBar->drawText( p, segmentInfo.last().first + mScaleBar->segmentMillimeters() - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font() );
00098   }
00099 
00100   p->restore();
00101 }
00102 
00103 QRectF QgsScaleBarStyle::calculateBoxSize() const
00104 {
00105   if ( !mScaleBar )
00106   {
00107     return QRectF();
00108   }
00109 
00110   //consider centered first label
00111   double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
00112 
00113   //consider last number and label
00114 
00115   double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
00116   QString largestNumberLabel = QString::number( largestLabelNumber );
00117   QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
00118   double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2;
00119 
00120   double totalBarLength = 0.0;
00121 
00122   QList< QPair<double, double> > segmentList;
00123   mScaleBar->segmentPositions( segmentList );
00124 
00125   QList< QPair<double, double> >::const_iterator segmentIt = segmentList.constBegin();
00126   for ( ; segmentIt != segmentList.constEnd(); ++segmentIt )
00127   {
00128     totalBarLength += segmentIt->second;
00129   }
00130 
00131   double width =  firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
00132   double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() );
00133 
00134   return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height );
00135 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines