[composer] Add nautical miles support to scalebar, more rational default scale

This commit is contained in:
jtornero 2013-11-15 00:01:31 +01:00 committed by Nyall Dawson
parent 926a676555
commit 4fd3b1fb07
4 changed files with 100 additions and 19 deletions

View File

@ -23,7 +23,8 @@ class QgsComposerScaleBar: QgsComposerItem
{
MapUnits = 0,
Meters,
Feet
Feet,
NauticalMiles
};
QgsComposerScaleBar( QgsComposition* composition /TransferThis/ );
@ -95,7 +96,7 @@ class QgsComposerScaleBar: QgsComposerItem
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
@note this method was added in version 1.7*/
void applyDefaultSize();
void applyDefaultSize( ScaleBarUnits u = Meters );
/**Sets style by name
@param styleName (untranslated) style name. Possibilities are: 'Single Box', 'Double Box', 'Line Ticks Middle', 'Line Ticks Down', 'Line Ticks Up', 'Numeric'*/

View File

@ -51,7 +51,7 @@ QgsComposerScaleBarWidget::QgsComposerScaleBarWidget( QgsComposerScaleBar* scale
mUnitsComboBox->insertItem( 0, tr( "Map units" ), 0 );
mUnitsComboBox->insertItem( 1, tr( "Meters" ), 1 );
mUnitsComboBox->insertItem( 2, tr( "Feet" ), 2 );
mUnitsComboBox->insertItem( 3, tr( "Nautical Miles" ), 3 );
blockMemberSignals( false );
setGuiElements(); //set the GUI elements to the state of scaleBar
}
@ -532,9 +532,37 @@ void QgsComposerScaleBarWidget::on_mUnitsComboBox_currentIndexChanged( int index
return;
}
mComposerScaleBar->beginCommand( tr( "Scalebar unit changed" ) );
disconnectUpdateSignal();
mComposerScaleBar->setUnits(( QgsComposerScaleBar::ScaleBarUnits )unitData.toInt() );
switch ( mUnitsComboBox->currentIndex() )
{
case 0:
{
mComposerScaleBar->beginCommand( tr( "Scalebar changed to map units" ) );
mComposerScaleBar->applyDefaultSize( QgsComposerScaleBar::MapUnits );
break;
}
case 2:
{
mComposerScaleBar->beginCommand( tr( "Scalebar changed to feet" ) );
mComposerScaleBar->applyDefaultSize( QgsComposerScaleBar::Feet );
break;
}
case 3:
{
mComposerScaleBar->beginCommand( tr( "Scalebar changed to nautical miles" ) );
mComposerScaleBar->applyDefaultSize( QgsComposerScaleBar::NauticalMiles );
break;
}
case 1:
default:
{
mComposerScaleBar->beginCommand( tr( "Scalebar changed to meters" ) );
mComposerScaleBar->applyDefaultSize( QgsComposerScaleBar::Meters );
break;
}
}
mComposerScaleBar->update();
connectUpdateSignal();
mComposerScaleBar->endCommand();

View File

@ -194,10 +194,14 @@ double QgsComposerScaleBar::mapWidth() const
da.setEllipsoid( QgsProject::instance()->readEntry( "Measure", "/Ellipsoid", "WGS84" ) );
double measure = da.measureLine( QgsPoint( composerMapRect.xMinimum(), composerMapRect.yMinimum() ), QgsPoint( composerMapRect.xMaximum(), composerMapRect.yMinimum() ) );
if ( mUnits == Feet )
if ( mUnits == QgsComposerScaleBar::Feet )
{
measure /= 0.3048;
}
else if ( mUnits == QgsComposerScaleBar::NauticalMiles )
{
measure /= 1852.0;
}
return measure;
}
}
@ -250,25 +254,71 @@ void QgsComposerScaleBar::applyDefaultSettings()
emit itemChanged();
}
void QgsComposerScaleBar::applyDefaultSize()
void QgsComposerScaleBar::applyDefaultSize( QgsComposerScaleBar::ScaleBarUnits u )
{
if ( mComposerMap )
{
setUnits( Meters );
double widthMeter = mapWidth();
int nUnitsPerSegment = widthMeter / 10.0; //default scalebar width equals half the map width
setNumUnitsPerSegment( nUnitsPerSegment );
setUnits( u );
double upperMagnitudeMultiplier;
double widthInSelectedUnits = mapWidth();
double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
setNumUnitsPerSegment( initialUnitsPerSegment );
if ( nUnitsPerSegment > 1000 )
switch ( mUnits )
{
setNumUnitsPerSegment(( int )( numUnitsPerSegment() / 1000.0 + 0.5 ) * 1000 );
setUnitLabeling( tr( "km" ) );
setNumMapUnitsPerScaleBarUnit( 1000 );
case MapUnits:
{
upperMagnitudeMultiplier = 1.0;
setUnitLabeling( tr( "units" ) );
break;
}
case Meters:
{
if ( initialUnitsPerSegment > 1000.0 )
{
upperMagnitudeMultiplier = 1000.0;
setUnitLabeling( tr( "km" ) );
}
else
{
upperMagnitudeMultiplier = 1.0;
setUnitLabeling( tr( "m" ) );
}
break;
}
case Feet:
{
if ( initialUnitsPerSegment > 5419.95 )
{
upperMagnitudeMultiplier = 5419.95;
setUnitLabeling( tr( "miles" ) );
}
else
{
upperMagnitudeMultiplier = 1.0;
setUnitLabeling( tr( "ft" ) );
}
break;
}
case NauticalMiles:
{
upperMagnitudeMultiplier = 1;
setUnitLabeling( tr( "Nm" ) );
break;
}
}
else
double segmentWidth = initialUnitsPerSegment / upperMagnitudeMultiplier;
int segmentMagnitude = floor( log10( segmentWidth ) );
double unitsPerSegment = upperMagnitudeMultiplier * ( pow( 10, segmentMagnitude ) );
double multiplier = floor(( widthInSelectedUnits / ( unitsPerSegment * 10 ) ) / 2.5 ) * 2.5;
if ( multiplier > 0 )
{
setUnitLabeling( tr( "m" ) );
unitsPerSegment = unitsPerSegment * multiplier;
}
setNumUnitsPerSegment( unitsPerSegment );
setNumMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
setNumSegments( 4 );
setNumSegmentsLeft( 2 );

View File

@ -46,7 +46,8 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
{
MapUnits = 0,
Meters,
Feet
Feet,
NauticalMiles
};
QgsComposerScaleBar( QgsComposition* composition );
@ -118,7 +119,7 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
void applyDefaultSettings();
/**Apply default size (scale bar 1/5 of map item width)
@note this method was added in version 1.7*/
void applyDefaultSize();
void applyDefaultSize( ScaleBarUnits u = Meters );
/**Sets style by name
@param styleName (untranslated) style name. Possibilities are: 'Single Box', 'Double Box', 'Line Ticks Middle', 'Line Ticks Down', 'Line Ticks Up', 'Numeric'*/
@ -203,8 +204,9 @@ class CORE_EXPORT QgsComposerScaleBar: public QgsComposerItem
/**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)*/
/**Returns diagonal of composer map in selected units (map units / meters / feet / nautical miles)*/
double mapWidth() const;
};
#endif //QGSCOMPOSERSCALEBAR_H