Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgsnumericscalebarstyle.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 "qgsnumericscalebarstyle.h" 00018 #include "qgscomposermap.h" 00019 #include "qgscomposerscalebar.h" 00020 #include <QList> 00021 #include <QPainter> 00022 00023 QgsNumericScaleBarStyle::QgsNumericScaleBarStyle( QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar ) 00024 { 00025 00026 } 00027 00028 QgsNumericScaleBarStyle::QgsNumericScaleBarStyle(): QgsScaleBarStyle( 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 if ( !p || !mScaleBar ) 00046 { 00047 return; 00048 } 00049 00050 p->save(); 00051 p->setFont( mScaleBar->font() ); 00052 p->setPen( QColor( 0, 0, 0 ) ); 00053 mScaleBar->drawText( p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ), scaleText(), mScaleBar->font() ); 00054 00055 p->restore(); 00056 } 00057 00058 QRectF QgsNumericScaleBarStyle::calculateBoxSize() const 00059 { 00060 QRectF rect; 00061 if ( !mScaleBar ) 00062 { 00063 return rect; 00064 } 00065 00066 double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() ); 00067 double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() ); 00068 00069 return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace() 00070 + 2 * mScaleBar->pen().width() + textWidth, 00071 textHeight + 2 * mScaleBar->boxContentSpace() ); 00072 } 00073 00074 QString QgsNumericScaleBarStyle::scaleText() const 00075 { 00076 QString scaleBarText; 00077 if ( mScaleBar ) 00078 { 00079 //find out scale 00080 const QgsComposerMap* composerMap = mScaleBar->composerMap(); 00081 if ( composerMap ) 00082 { 00083 double scaleDenominator = composerMap->scale(); 00084 scaleBarText = "1:" + QString::number( scaleDenominator, 'f', 0 ); 00085 } 00086 } 00087 return scaleBarText; 00088 }