Skip to content
Permalink
Browse files

[composer] Add nautical miles support to scalebar, more rational defa…

…ult scale
  • Loading branch information
jtornero authored and nyalldawson committed Jan 8, 2014
1 parent 926a676 commit 4fd3b1fb076aae2ad2e60460e33f39833b8cad83
@@ -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'*/
@@ -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();
@@ -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 )

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Jan 8, 2014

Contributor

Hi @nyalldawson, I would denote that my proposed pull request #1056 (ahuarte47@b0582a7) rewrite the conversion to one generic function.

Best Regards

This comment has been minimized.

Copy link
@nyalldawson

nyalldawson Jan 12, 2014

Collaborator

I realised that -- I'll update this code to use the new functions when that request gets committed. Thanks!

This comment has been minimized.

Copy link
@ahuarte47

ahuarte47 Jan 12, 2014

Contributor

@nyalldawson, thanks to you!

{
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 );
@@ -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

0 comments on commit 4fd3b1f

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