|
QGIS API Documentation
master-59fd5e0
|
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 ), 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( mScaleBar->fontColor() ); 00054 00055 //call QgsComposerItem's pen() function, since that refers to the frame pen 00056 //and QgsComposerScalebar's pen() function refers to the scale bar line width, 00057 //which is not used for numeric scale bars. Divide the pen width by 2 since 00058 //half the width of the frame is drawn outside the item. 00059 double penWidth = mScaleBar->QgsComposerItem::pen().widthF() / 2.0; 00060 double margin = mScaleBar->boxContentSpace(); 00061 //map scalebar alignment to Qt::AlignmentFlag type 00062 Qt::AlignmentFlag hAlign; 00063 switch ( mScaleBar->alignment() ) 00064 { 00065 case QgsComposerScaleBar::Left: 00066 hAlign = Qt::AlignLeft; 00067 break; 00068 case QgsComposerScaleBar::Middle: 00069 hAlign = Qt::AlignHCenter; 00070 break; 00071 case QgsComposerScaleBar::Right: 00072 hAlign = Qt::AlignRight; 00073 break; 00074 default: 00075 hAlign = Qt::AlignLeft; 00076 break; 00077 } 00078 00079 //text destination is item's rect, excluding the margin and frame 00080 QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin ); 00081 mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop ); 00082 00083 p->restore(); 00084 } 00085 00086 QRectF QgsNumericScaleBarStyle::calculateBoxSize() const 00087 { 00088 QRectF rect; 00089 if ( !mScaleBar ) 00090 { 00091 return rect; 00092 } 00093 00094 double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() ); 00095 double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() ); 00096 00097 rect = QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace() 00098 + 2 * mScaleBar->pen().width() + textWidth, 00099 textHeight + 2 * mScaleBar->boxContentSpace() ); 00100 00101 if ( mLastScaleBarWidth != rect.width() && mLastScaleBarWidth > 0 && rect.width() > 0 ) 00102 { 00103 //hack to move scale bar the left / right in order to keep the bar alignment 00104 const_cast<QgsComposerScaleBar*>( mScaleBar )->correctXPositionAlignment( mLastScaleBarWidth, rect.width() ); 00105 } 00106 mLastScaleBarWidth = rect.width(); 00107 return rect; 00108 } 00109 00110 QString QgsNumericScaleBarStyle::scaleText() const 00111 { 00112 QString scaleBarText; 00113 if ( mScaleBar ) 00114 { 00115 //find out scale 00116 double scaleDenominator = 1; 00117 const QgsComposerMap* composerMap = mScaleBar->composerMap(); 00118 if ( composerMap ) 00119 { 00120 scaleDenominator = composerMap->scale(); 00121 scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 ); 00122 } 00123 scaleBarText = "1:" + QString( "%L1" ).arg( scaleDenominator, 0, 'f', 0 ); 00124 } 00125 return scaleBarText; 00126 }