File tree Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Expand file tree Collapse file tree 4 files changed +62
-1
lines changed Original file line number Diff line number Diff line change @@ -390,6 +390,16 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
390
390
.. seealso:: applyDefaultSize()
391
391
%End
392
392
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
+
393
403
void applyDefaultSize( QgsUnitTypes::DistanceUnit units = QgsUnitTypes::DistanceMeters );
394
404
%Docstring
395
405
Applies the default size to the scale bar (scale bar 1/5 of map item width)
Original file line number Diff line number Diff line change @@ -192,7 +192,7 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
192
192
if ( targetMap )
193
193
{
194
194
scalebar->setMap ( targetMap );
195
- scalebar->applyDefaultSize ();
195
+ scalebar->applyDefaultSize ( scalebar-> guessUnits () );
196
196
}
197
197
} );
198
198
Original file line number Diff line number Diff line change @@ -390,6 +390,48 @@ void QgsLayoutItemScaleBar::applyDefaultSettings()
390
390
emit changed ();
391
391
}
392
392
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
+
393
435
void QgsLayoutItemScaleBar::applyDefaultSize ( QgsUnitTypes::DistanceUnit units )
394
436
{
395
437
mSettings .setUnits ( units );
Original file line number Diff line number Diff line change @@ -379,6 +379,15 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
379
379
*/
380
380
void applyDefaultSettings ();
381
381
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
+
382
391
/* *
383
392
* Applies the default size to the scale bar (scale bar 1/5 of map item width)
384
393
* \see applyDefaultSettings()
You can’t perform that action at this time.
0 commit comments