From 3237577ed116ca345d497fc1210a1b386c17a0ba Mon Sep 17 00:00:00 2001 From: Magnus Homann Date: Wed, 29 Aug 2012 22:56:51 +0200 Subject: [PATCH] Scalebar uses preferred units now --- src/app/qgsdecorationscalebar.cpp | 21 +++++++++++++++++++-- src/app/qgsdecorationscalebar.h | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/app/qgsdecorationscalebar.cpp b/src/app/qgsdecorationscalebar.cpp index 596d6195702..18792178a80 100644 --- a/src/app/qgsdecorationscalebar.cpp +++ b/src/app/qgsdecorationscalebar.cpp @@ -23,6 +23,7 @@ email : sbr00pwb@users.sourceforge.net #include "qgsdecorationscalebardialog.h" +#include "qgis.h" #include "qgisapp.h" #include "qgslogger.h" #include "qgsmapcanvas.h" @@ -119,6 +120,7 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter ) //projections) and that just confuses the rest of the code in this //function, so force to a positive number. double myMapUnitsPerPixelDouble = qAbs( canvas->mapUnitsPerPixel() ); + double myActualSize = mPreferredSize; // Exit if the canvas width is 0 or layercount is 0 or QGIS will freeze int myLayerCount = canvas->layerCount(); @@ -131,9 +133,25 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter ) // Hard coded sizes int myMajorTickSize = 8; int myTextOffsetX = 3; - double myActualSize = mPreferredSize; int myMargin = 20; + QSettings settings; + QGis::UnitType myPreferredUnits = QGis::fromLiteral( settings.value( "/qgis/measure/displayunits", QGis::toLiteral( QGis::Meters ) ).toString() ); + QGis::UnitType myMapUnits = canvas->mapUnits(); + + // Adjust units meter/feet or vice versa + if ( myMapUnits == QGis::Meters && myPreferredUnits == QGis::Feet ) + { + // From meter to feet + myMapUnits = QGis::Feet; + myMapUnitsPerPixelDouble /= 0.3084; + } + else if ( myMapUnits == QGis::Feet && myPreferredUnits == QGis::Meters ) + { + // From feet to meter + myMapUnits = QGis::Meters; + myMapUnitsPerPixelDouble *= 0.3084; + } //Calculate size of scale bar for preferred number of map units double myScaleBarWidth = mPreferredSize / myMapUnitsPerPixelDouble; @@ -164,7 +182,6 @@ void QgsDecorationScaleBar::render( QPainter * theQPainter ) } //Get type of map units and set scale bar unit label text - QGis::UnitType myMapUnits = canvas->mapUnits(); QString myScaleBarUnitLabel; switch ( myMapUnits ) { diff --git a/src/app/qgsdecorationscalebar.h b/src/app/qgsdecorationscalebar.h index d43768aeace..f9961641c89 100644 --- a/src/app/qgsdecorationscalebar.h +++ b/src/app/qgsdecorationscalebar.h @@ -21,6 +21,7 @@ email : sbr00pwb@users.sourceforge.net #ifndef QGSCALEBARPLUGIN #define QGSCALEBARPLUGIN +#include "qgis.h" #include "qgsdecorationitem.h" class QPainter;