Skip to content

Commit af59c53

Browse files
committed
[layouts] Add more unit choices to scalebars
Explicitly add options for kilometers, miles, etc as the current UX for setting these is too confusing! Fixes #16921
1 parent 3c4fae0 commit af59c53

File tree

2 files changed

+17
-42
lines changed

2 files changed

+17
-42
lines changed

src/app/layout/qgslayoutscalebarwidget.cpp

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,13 @@ QgsLayoutScaleBarWidget::QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBa
7878
mAlignmentComboBox->insertItem( 2, tr( "Right" ) );
7979

8080
//units combo box
81-
mUnitsComboBox->insertItem( 0, tr( "Map units" ), QgsUnitTypes::DistanceUnknownUnit );
82-
mUnitsComboBox->insertItem( 1, tr( "Meters" ), QgsUnitTypes::DistanceMeters );
83-
mUnitsComboBox->insertItem( 2, tr( "Feet" ), QgsUnitTypes::DistanceFeet );
84-
mUnitsComboBox->insertItem( 3, tr( "Nautical Miles" ), QgsUnitTypes::DistanceNauticalMiles );
81+
mUnitsComboBox->addItem( tr( "Map units" ), QgsUnitTypes::DistanceUnknownUnit );
82+
mUnitsComboBox->addItem( tr( "Meters" ), QgsUnitTypes::DistanceMeters );
83+
mUnitsComboBox->addItem( tr( "Kilometers" ), QgsUnitTypes::DistanceKilometers );
84+
mUnitsComboBox->addItem( tr( "Feet" ), QgsUnitTypes::DistanceFeet );
85+
mUnitsComboBox->addItem( tr( "Yards" ), QgsUnitTypes::DistanceYards );
86+
mUnitsComboBox->addItem( tr( "Miles" ), QgsUnitTypes::DistanceMiles );
87+
mUnitsComboBox->addItem( tr( "Nautical Miles" ), QgsUnitTypes::DistanceNauticalMiles );
8588

8689
mFillColorButton->setColorDialogTitle( tr( "Select Fill Color" ) );
8790
mFillColorButton->setAllowOpacity( true );
@@ -193,7 +196,7 @@ void QgsLayoutScaleBarWidget::setGuiElements()
193196
mAlignmentComboBox->setCurrentIndex( ( int )( mScalebar->alignment() ) );
194197

195198
//units
196-
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( ( int )mScalebar->units() ) );
199+
mUnitsComboBox->setCurrentIndex( mUnitsComboBox->findData( static_cast< int >( mScalebar->units() ) ) );
197200

198201
if ( mScalebar->segmentSizeMode() == QgsScaleBarSettings::SegmentSizeFixed )
199202
{
@@ -546,36 +549,8 @@ void QgsLayoutScaleBarWidget::mUnitsComboBox_currentIndexChanged( int index )
546549
}
547550

548551
disconnectUpdateSignal();
549-
mScalebar->setUnits( ( QgsUnitTypes::DistanceUnit )unitData.toInt() );
550-
switch ( mUnitsComboBox->currentIndex() )
551-
{
552-
case 0:
553-
{
554-
mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
555-
mScalebar->applyDefaultSize( QgsUnitTypes::DistanceUnknownUnit );
556-
break;
557-
}
558-
case 2:
559-
{
560-
mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
561-
mScalebar->applyDefaultSize( QgsUnitTypes::DistanceFeet );
562-
break;
563-
}
564-
case 3:
565-
{
566-
mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
567-
mScalebar->applyDefaultSize( QgsUnitTypes::DistanceNauticalMiles );
568-
break;
569-
}
570-
case 1:
571-
default:
572-
{
573-
mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
574-
mScalebar->applyDefaultSize( QgsUnitTypes::DistanceMeters );
575-
break;
576-
}
577-
}
578-
552+
mScalebar->beginCommand( tr( "Set Scalebar Units" ) );
553+
mScalebar->applyDefaultSize( static_cast< QgsUnitTypes::DistanceUnit >( unitData.toInt() ) );
579554
mScalebar->update();
580555

581556
mUnitLabelLineEdit->setText( mScalebar->unitLabel() );

src/core/layout/qgslayoutitemscalebar.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -423,13 +423,13 @@ void QgsLayoutItemScaleBar::applyDefaultSettings()
423423

424424
void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
425425
{
426+
mSettings.setUnits( units );
426427
if ( mMap )
427428
{
428-
setUnits( units );
429429
double upperMagnitudeMultiplier = 1.0;
430430
double widthInSelectedUnits = mapWidth();
431431
double initialUnitsPerSegment = widthInSelectedUnits / 10.0; //default scalebar width equals half the map width
432-
setUnitsPerSegment( initialUnitsPerSegment );
432+
mSettings.setUnitsPerSegment( initialUnitsPerSegment );
433433

434434
switch ( units )
435435
{
@@ -483,11 +483,11 @@ void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
483483
{
484484
unitsPerSegment = unitsPerSegment * multiplier;
485485
}
486-
setUnitsPerSegment( unitsPerSegment );
487-
setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
486+
mSettings.setUnitsPerSegment( unitsPerSegment );
487+
mSettings.setMapUnitsPerScaleBarUnit( upperMagnitudeMultiplier );
488488

489-
setNumberOfSegments( 4 );
490-
setNumberOfSegmentsLeft( 2 );
489+
mSettings.setNumberOfSegments( 4 );
490+
mSettings.setNumberOfSegmentsLeft( 2 );
491491
}
492492

493493
refreshSegmentMillimeters();
@@ -611,7 +611,7 @@ bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScale
611611
//map id
612612
if ( mMap )
613613
{
614-
composerScaleBarElem.setAttribute( QStringLiteral( "mapId" ), mMap->id() );
614+
composerScaleBarElem.setAttribute( QStringLiteral( "mapUuid" ), mMap->uuid() );
615615
}
616616

617617
//colors

0 commit comments

Comments
 (0)