Skip to content

Commit a615a48

Browse files
committedNov 24, 2017
Make scalebars auto selected between m/km and ft/miles when newly added
Based on current linked map scale and linked map CRS
1 parent 3decab9 commit a615a48

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed
 

‎python/core/layout/qgslayoutitemscalebar.sip

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,16 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
390390
.. seealso:: applyDefaultSize()
391391
%End
392392

393+
QgsUnitTypes::DistanceUnit guessUnits() const;
394+
%Docstring
395+
Attempts to guess the most reasonable unit choice for the scalebar, given
396+
the current linked map's scale.
397+
398+
This method also considers the linked map's CRS, in order to determine if
399+
metric or imperial units are more appropriate.
400+
:rtype: QgsUnitTypes.DistanceUnit
401+
%End
402+
393403
void applyDefaultSize( QgsUnitTypes::DistanceUnit units = QgsUnitTypes::DistanceMeters );
394404
%Docstring
395405
Applies the default size to the scale bar (scale bar 1/5 of map item width)

‎src/app/layout/qgslayoutapputils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
192192
if ( targetMap )
193193
{
194194
scalebar->setMap( targetMap );
195-
scalebar->applyDefaultSize();
195+
scalebar->applyDefaultSize( scalebar->guessUnits() );
196196
}
197197
} );
198198

‎src/core/layout/qgslayoutitemscalebar.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,48 @@ void QgsLayoutItemScaleBar::applyDefaultSettings()
390390
emit changed();
391391
}
392392

393+
QgsUnitTypes::DistanceUnit QgsLayoutItemScaleBar::guessUnits() const
394+
{
395+
if ( !mMap )
396+
return QgsUnitTypes::DistanceMeters;
397+
398+
QgsCoordinateReferenceSystem crs = mMap->crs();
399+
// start with crs units
400+
QgsUnitTypes::DistanceUnit unit = crs.mapUnits();
401+
if ( unit == QgsUnitTypes::DistanceDegrees || unit == QgsUnitTypes::DistanceUnknownUnit )
402+
{
403+
// geographic CRS, use metric units
404+
unit = QgsUnitTypes::DistanceMeters;
405+
}
406+
407+
// try to pick reasonable choice between metric / imperial units
408+
double widthInSelectedUnits = mapWidth();
409+
double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
410+
switch ( unit )
411+
{
412+
case QgsUnitTypes::DistanceMeters:
413+
{
414+
if ( initialUnitsPerSegment > 1000.0 )
415+
{
416+
unit = QgsUnitTypes::DistanceKilometers;
417+
}
418+
break;
419+
}
420+
case QgsUnitTypes::DistanceFeet:
421+
{
422+
if ( initialUnitsPerSegment > 5419.95 )
423+
{
424+
unit = QgsUnitTypes::DistanceMiles;
425+
}
426+
break;
427+
}
428+
default:
429+
break;
430+
}
431+
432+
return unit;
433+
}
434+
393435
void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
394436
{
395437
mSettings.setUnits( units );

‎src/core/layout/qgslayoutitemscalebar.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
379379
*/
380380
void applyDefaultSettings();
381381

382+
/**
383+
* Attempts to guess the most reasonable unit choice for the scalebar, given
384+
* the current linked map's scale.
385+
*
386+
* This method also considers the linked map's CRS, in order to determine if
387+
* metric or imperial units are more appropriate.
388+
*/
389+
QgsUnitTypes::DistanceUnit guessUnits() const;
390+
382391
/**
383392
* Applies the default size to the scale bar (scale bar 1/5 of map item width)
384393
* \see applyDefaultSettings()

0 commit comments

Comments
 (0)
Please sign in to comment.