|
QGIS API Documentation
master-3f58142
|
00001 /*************************************************************************** 00002 qgsscalebarstyle.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 "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 p->setPen( QPen( mScaleBar->fontColor() ) ); 00048 00049 QString firstLabel = mScaleBar->firstLabelString(); 00050 double xOffset = mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2; 00051 00052 //double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(); 00053 QList<QPair<double, double> > segmentInfo; 00054 mScaleBar->segmentPositions( segmentInfo ); 00055 00056 double currentLabelNumber = 0.0; 00057 00058 int nSegmentsLeft = mScaleBar->numSegmentsLeft(); 00059 int segmentCounter = 0; 00060 QString currentNumericLabel; 00061 00062 QList<QPair<double, double> >::const_iterator segmentIt = segmentInfo.constBegin(); 00063 for ( ; segmentIt != segmentInfo.constEnd(); ++segmentIt ) 00064 { 00065 if ( segmentCounter == 0 && nSegmentsLeft > 0 ) 00066 { 00067 //label first left segment 00068 currentNumericLabel = firstLabel; 00069 } 00070 else if ( segmentCounter != 0 && segmentCounter == nSegmentsLeft ) //reset label number to 0 if there are left segments 00071 { 00072 currentLabelNumber = 0; 00073 } 00074 00075 if ( segmentCounter >= nSegmentsLeft ) 00076 { 00077 currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() ); 00078 } 00079 00080 if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments 00081 { 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 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() ); 00097 } 00098 00099 p->restore(); 00100 } 00101 00102 QRectF QgsScaleBarStyle::calculateBoxSize() const 00103 { 00104 if ( !mScaleBar ) 00105 { 00106 return QRectF(); 00107 } 00108 00109 //consider centered first label 00110 double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2; 00111 00112 //consider last number and label 00113 00114 double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit(); 00115 QString largestNumberLabel = QString::number( largestLabelNumber ); 00116 QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling(); 00117 double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2; 00118 00119 double totalBarLength = 0.0; 00120 00121 QList< QPair<double, double> > segmentList; 00122 mScaleBar->segmentPositions( segmentList ); 00123 00124 QList< QPair<double, double> >::const_iterator segmentIt = segmentList.constBegin(); 00125 for ( ; segmentIt != segmentList.constEnd(); ++segmentIt ) 00126 { 00127 totalBarLength += segmentIt->second; 00128 } 00129 00130 double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace(); 00131 double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() ); 00132 00133 return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height ); 00134 }