Quantum GIS API Documentation  1.8
src/core/composer/qgsnumericscalebarstyle.cpp
Go to the documentation of this file.
00001 /***************************************************************************
00002                             qgsnumericscalebarstyle.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 "qgsnumericscalebarstyle.h"
00018 #include "qgscomposermap.h"
00019 #include "qgscomposerscalebar.h"
00020 #include <QList>
00021 #include <QPainter>
00022 
00023 QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar ), mLastScaleBarWidth( 0 )
00024 {
00025 
00026 }
00027 
00028 QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 0 ), mLastScaleBarWidth( 0 )
00029 {
00030 
00031 }
00032 
00033 QgsNumericScaleBarStyle::~QgsNumericScaleBarStyle()
00034 {
00035 
00036 }
00037 
00038 QString QgsNumericScaleBarStyle::name() const
00039 {
00040   return "Numeric";
00041 }
00042 
00043 void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
00044 {
00045   Q_UNUSED( xOffset );
00046   if ( !p || !mScaleBar )
00047   {
00048     return;
00049   }
00050 
00051   p->save();
00052   p->setFont( mScaleBar->font() );
00053   p->setPen( QColor( 0, 0, 0 ) );
00054   mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() );
00055 
00056   p->restore();
00057 }
00058 
00059 QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
00060 {
00061   QRectF rect;
00062   if ( !mScaleBar )
00063   {
00064     return rect;
00065   }
00066 
00067   double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
00068   double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );
00069 
00070   rect = QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
00071                  + 2 * mScaleBar->pen().width() + textWidth,
00072                  textHeight + 2 * mScaleBar->boxContentSpace() );
00073 
00074   if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 )
00075   {
00076     //hack to move scale bar the the left / right in order to keep the bar alignment
00077     const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() );
00078   }
00079   mLastScaleBarWidth = rect.width();
00080   return rect;
00081 }
00082 
00083 QString QgsNumericScaleBarStyle::scaleText() const
00084 {
00085   QString scaleBarText;
00086   if ( mScaleBar )
00087   {
00088     //find out scale
00089     double scaleDenominator = 1;
00090     const QgsComposerMap* composerMap = mScaleBar->composerMap();
00091     if ( composerMap )
00092     {
00093       scaleDenominator = composerMap->scale();
00094       scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
00095     }
00096     scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 );
00097   }
00098   return scaleBarText;
00099 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines