Skip to content
Permalink
Browse files

Merge branch 'scalebar_units'

  • Loading branch information
mhugent committed Aug 13, 2012
2 parents 129dc0b + 9a26650 commit 9b6bcd9d36baf6dae2f7ba7da6ddeb1d19861556
@@ -17,6 +17,14 @@ class QgsComposerScaleBar: QgsComposerItem
Right
};

/**Added in version 1.9*/
enum ScaleBarUnits
{
MapUnits = 0,
Meters,
Feet
};

QgsComposerScaleBar( QgsComposition* composition /TransferThis/);
~QgsComposerScaleBar();

@@ -73,6 +81,12 @@ class QgsComposerScaleBar: QgsComposerItem
/**@note: this method was added in version 1.8*/
void setAlignment( Alignment a );

/**@note: this method was added in version 1.9*/
ScaleBarUnits units() const;

/**@note: this method was added in version 1.9*/
void setUnits( ScaleBarUnits u );

/**Apply default settings*/
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
@@ -158,7 +158,7 @@ class QgsMapRenderer : QObject
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );

//! returns CRS of destination coordinate reference system
const QgsCoordinateReferenceSystem& destinationCrs();
const QgsCoordinateReferenceSystem& destinationCrs() const;

void setOutputUnits( OutputUnits u );

@@ -44,11 +44,17 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
mStyleComboBox->insertItem( 4, tr( "Line Ticks Up" ) );
mStyleComboBox->insertItem( 5, tr( "Numeric" ) );

//alignment combo box
mAlignmentComboBox->insertItem( 0, tr( "Left" ) );
mAlignmentComboBox->insertItem( 1, tr( "Middle" ) );
mAlignmentComboBox->insertItem( 2, tr( "Right" ) );
blockMemberSignals( false );

//units combo box
mUnitsComboBox->insertItem( 0, tr( "Map units" ), 0 );
mUnitsComboBox->insertItem( 1, tr( "Meters" ), 1 );
mUnitsComboBox->insertItem( 2, tr( "Feet" ), 2 );

blockMemberSignals( false );
setGuiElements(); //set the GUI elements to the state of scaleBar
}

@@ -176,6 +182,10 @@ void QgsComposerScaleBarWidget::setGuiElements()

//alignment
mAlignmentComboBox->setCurrentIndex(( int )( mComposerScaleBar->alignment() ) );

//units
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData(( int )mComposerScaleBar->units() ) );

blockMemberSignals( false );
}

@@ -399,6 +409,25 @@ void QgsComposerScaleBarWidget::on_mAlignmentComboBox_currentIndexChanged( int i
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::on_mUnitsComboBox_currentIndexChanged( int index )
{
if ( !mComposerScaleBar )
{
return;
}

QVariant unitData = mUnitsComboBox->itemData( index );
if ( unitData.type() == QVariant::Invalid )
{
return;
}

mComposerScaleBar->beginCommand( tr( "Scalebar unit changed" ) );
mComposerScaleBar->setUnits(( QgsComposerScaleBar::ScaleBarUnits )unitData.toInt() );
mComposerScaleBar->update();
mComposerScaleBar->endCommand();
}

void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
{
mSegmentSizeSpinBox->blockSignals( block );
@@ -413,4 +442,5 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mLabelBarSpaceSpinBox->blockSignals( block );
mBoxSizeSpinBox->blockSignals( block );
mAlignmentComboBox->blockSignals( block );
mUnitsComboBox->blockSignals( block );
}
@@ -47,6 +47,7 @@ class QgsComposerScaleBarWidget: public QWidget, private Ui::QgsComposerScaleBar
void on_mLabelBarSpaceSpinBox_valueChanged( double d );
void on_mBoxSizeSpinBox_valueChanged( double d );
void on_mAlignmentComboBox_currentIndexChanged( int index );
void on_mUnitsComboBox_currentIndexChanged( int index );

private slots:
void setGuiElements();
@@ -16,8 +16,10 @@

#include "qgscomposerscalebar.h"
#include "qgscomposermap.h"
#include "qgsdistancearea.h"
#include "qgsscalebarstyle.h"
#include "qgsdoubleboxscalebarstyle.h"
#include "qgsmaprenderer.h"
#include "qgsnumericscalebarstyle.h"
#include "qgssingleboxscalebarstyle.h"
#include "qgsticksscalebarstyle.h"
@@ -26,6 +28,7 @@
#include <QDomElement>
#include <QFontMetricsF>
#include <QPainter>
#include <QSettings>
#include <cmath>

QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition )
@@ -35,6 +38,7 @@ QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition )
, mStyle( 0 )
, mSegmentMillimeters( 0.0 )
, mAlignment( Left )
, mUnits( MapUnits )
{
applyDefaultSettings();
applyDefaultSize();
@@ -164,10 +168,46 @@ void QgsComposerScaleBar::refreshSegmentMillimeters()
QRectF composerItemRect = mComposerMap->rect();

//calculate size depending on mNumUnitsPerSegment
mSegmentMillimeters = composerItemRect.width() / composerMapRect.width() * mNumUnitsPerSegment;
double itemDiagonal = sqrt( composerItemRect.width() * composerItemRect.width() + composerItemRect.height() * composerItemRect.height() );
mSegmentMillimeters = itemDiagonal / mapDiagonal() * mNumUnitsPerSegment;
}
}

double QgsComposerScaleBar::mapDiagonal() const
{
if ( !mComposerMap )
{
return 0.0;
}

QgsRectangle composerMapRect = mComposerMap->extent();
if ( mUnits == MapUnits )
{
return sqrt( composerMapRect.width() * composerMapRect.width() + composerMapRect.height() * composerMapRect.height() );
}
else
{
QgsDistanceArea da;
da.setProjectionsEnabled( true );
da.setSourceCrs( mComposerMap->mapRenderer()->destinationCrs().srsid() );
QSettings s;
da.setEllipsoid( s.value( "/qgis/measure/ellipsoid", "WGS84" ).toString() );
double measure = da.measureLine( QgsPoint( composerMapRect.xMinimum(), composerMapRect.yMaximum() ), QgsPoint( composerMapRect.xMaximum(), composerMapRect.yMinimum() ) );
if ( mUnits == Feet )
{
measure /= 0.3048;
}
return measure;
}
}

void QgsComposerScaleBar::setUnits( ScaleBarUnits u )
{
mUnits = u;
refreshSegmentMillimeters();
emit itemChanged();
}

void QgsComposerScaleBar::applyDefaultSettings()
{
mNumSegments = 2;
@@ -358,6 +398,7 @@ bool QgsComposerScaleBar::writeXML( QDomElement& elem, QDomDocument & doc ) cons
composerScaleBarElem.setAttribute( "font", mFont.toString() );
composerScaleBarElem.setAttribute( "outlineWidth", QString::number( mPen.widthF() ) );
composerScaleBarElem.setAttribute( "unitLabel", mUnitLabeling );
composerScaleBarElem.setAttribute( "units", mUnits );

//style
if ( mStyle )
@@ -428,6 +469,8 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
}
}

mUnits = ( ScaleBarUnits )itemElem.attribute( "units" ).toInt();

refreshSegmentMillimeters();

//alignment
@@ -40,6 +40,14 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
Right
};

/**Added in version 1.9*/
enum ScaleBarUnits
{
MapUnits = 0,
Meters,
Feet
};

QgsComposerScaleBar( QgsComposition* composition );
~QgsComposerScaleBar();

@@ -96,6 +104,12 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
/**@note: this method was added in version 1.8*/
void setAlignment( Alignment a ) { mAlignment = a; }

/**@note: this method was added in version 1.9*/
ScaleBarUnits units() const { return mUnits; }

/**@note: this method was added in version 1.9*/
void setUnits( ScaleBarUnits u );

/**Apply default settings*/
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
@@ -179,8 +193,13 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem

Alignment mAlignment;

ScaleBarUnits mUnits;

/**Calculates with of a segment in mm and stores it in mSegmentMillimeters*/
void refreshSegmentMillimeters();

/**Returns diagonal of composer map in selected units (map units / meters / feet)*/
double mapDiagonal() const;
};

#endif //QGSCOMPOSERSCALEBAR_H
@@ -704,7 +704,7 @@ void QgsMapRenderer::setDestinationCrs( const QgsCoordinateReferenceSystem& crs
}
}

const QgsCoordinateReferenceSystem& QgsMapRenderer::destinationCrs()
const QgsCoordinateReferenceSystem& QgsMapRenderer::destinationCrs() const
{
QgsDebugMsgLevel( "* Returning destCRS", 3 );
QgsDebugMsgLevel( "* DestCRS.srsid() = " + QString::number( mDestCRS->srsid() ), 3 );
@@ -202,7 +202,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
void setDestinationCrs( const QgsCoordinateReferenceSystem& crs );

//! returns CRS of destination coordinate reference system
const QgsCoordinateReferenceSystem& destinationCrs();
const QgsCoordinateReferenceSystem& destinationCrs() const;

void setOutputUnits( OutputUnits u ) {mOutputUnits = u;}

0 comments on commit 9b6bcd9

Please sign in to comment.
You can’t perform that action at this time.