Skip to content

Commit

Permalink
[FEATURE]: possibility to have scale bar in meters also for maps in d…
Browse files Browse the repository at this point in the history
…egrees (based on map diagonal)
  • Loading branch information
mhugent committed Aug 13, 2012
1 parent 2263435 commit 18f2c43
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 56 deletions.
14 changes: 14 additions & 0 deletions python/core/qgscomposerscalebar.sip
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ class QgsComposerScaleBar: QgsComposerItem
Right
};

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

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

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion python/core/qgsmaprenderer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down
30 changes: 29 additions & 1 deletion src/app/composer/qgscomposerscalebarwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -177,6 +183,9 @@ void QgsComposerScaleBarWidget::setGuiElements()
//alignment
mAlignmentComboBox->setCurrentIndex(( int )( mComposerScaleBar->alignment() ) );
blockMemberSignals( false );

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

//slots
Expand Down Expand Up @@ -399,6 +408,24 @@ 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 );
Expand All @@ -413,4 +440,5 @@ void QgsComposerScaleBarWidget::blockMemberSignals( bool block )
mLabelBarSpaceSpinBox->blockSignals( block );
mBoxSizeSpinBox->blockSignals( block );
mAlignmentComboBox->blockSignals( block );
mUnitsComboBox->blockSignals( block );
}
1 change: 1 addition & 0 deletions src/app/composer/qgscomposerscalebarwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
45 changes: 44 additions & 1 deletion src/core/composer/qgscomposerscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -26,6 +28,7 @@
#include <QDomElement>
#include <QFontMetricsF>
#include <QPainter>
#include <QSettings>
#include <cmath>

QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition )
Expand All @@ -35,6 +38,7 @@ QgsComposerScaleBar::QgsComposerScaleBar( QgsComposition* composition )
, mStyle( 0 )
, mSegmentMillimeters( 0.0 )
, mAlignment( Left )
, mUnits( MapUnits )
{
applyDefaultSettings();
applyDefaultSize();
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -428,6 +469,8 @@ bool QgsComposerScaleBar::readXML( const QDomElement& itemElem, const QDomDocume
}
}

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

refreshSegmentMillimeters();

//alignment
Expand Down
19 changes: 19 additions & 0 deletions src/core/composer/qgscomposerscalebar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down
Loading

0 comments on commit 18f2c43

Please sign in to comment.