Skip to content

Commit b7ef3d3

Browse files
committed
Simplify some code, cleanup UI
1 parent af59c53 commit b7ef3d3

File tree

4 files changed

+50
-92
lines changed

4 files changed

+50
-92
lines changed

python/core/layout/qgslayoutitemscalebar.sip

+5
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
396396
.. seealso:: applyDefaultSettings()
397397
%End
398398

399+
void resizeToMinimumWidth();
400+
%Docstring
401+
Resizes the scale bar to its minimum width, without changing the height.
402+
%End
403+
399404
void setStyle( const QString &name );
400405
%Docstring
401406
Sets the scale bar style by ``name``.

src/app/layout/qgslayoutscalebarwidget.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ QgsLayoutScaleBarWidget::QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBa
8585
mUnitsComboBox->addItem( tr( "Yards" ), QgsUnitTypes::DistanceYards );
8686
mUnitsComboBox->addItem( tr( "Miles" ), QgsUnitTypes::DistanceMiles );
8787
mUnitsComboBox->addItem( tr( "Nautical Miles" ), QgsUnitTypes::DistanceNauticalMiles );
88+
mUnitsComboBox->addItem( tr( "Centimeters" ), QgsUnitTypes::DistanceCentimeters );
89+
mUnitsComboBox->addItem( tr( "Millimeters" ), QgsUnitTypes::DistanceMillimeters );
8890

8991
mFillColorButton->setColorDialogTitle( tr( "Select Fill Color" ) );
9092
mFillColorButton->setAllowOpacity( true );
@@ -466,6 +468,7 @@ void QgsLayoutScaleBarWidget::toggleStyleSpecificControls( const QString &style
466468
mStrokeColorButton->setEnabled( false );
467469
mLineJoinStyleCombo->setEnabled( false );
468470
mLineCapStyleCombo->setEnabled( false );
471+
mAlignmentComboBox->setEnabled( true );
469472
}
470473
else
471474
{
@@ -477,6 +480,7 @@ void QgsLayoutScaleBarWidget::toggleStyleSpecificControls( const QString &style
477480
mFillColorButton->setEnabled( true );
478481
mFillColor2Button->setEnabled( true );
479482
mStrokeColorButton->setEnabled( true );
483+
mAlignmentComboBox->setEnabled( false );
480484
if ( style == QLatin1String( "Single Box" ) || style == QLatin1String( "Double Box" ) )
481485
{
482486
mLineJoinStyleCombo->setEnabled( true );
@@ -531,6 +535,7 @@ void QgsLayoutScaleBarWidget::mAlignmentComboBox_currentIndexChanged( int index
531535
mScalebar->beginCommand( tr( "Set Scalebar Alignment" ) );
532536
disconnectUpdateSignal();
533537
mScalebar->setAlignment( ( QgsScaleBarSettings::Alignment ) index );
538+
mScalebar->update();
534539
connectUpdateSignal();
535540
mScalebar->endCommand();
536541
}

src/core/layout/qgslayoutitemscalebar.cpp

+33-86
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,8 @@ void QgsLayoutItemScaleBar::setNumberOfSegments( int nSegments )
8282
mSettings.setNumberOfSegments( nSegments );
8383
return;
8484
}
85-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
8685
mSettings.setNumberOfSegments( nSegments );
87-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
88-
correctXPositionAlignment( width, widthAfter );
89-
refreshItemSize();
90-
emit changed();
86+
resizeToMinimumWidth();
9187
}
9288

9389
void QgsLayoutItemScaleBar::setUnitsPerSegment( double units )
@@ -97,13 +93,9 @@ void QgsLayoutItemScaleBar::setUnitsPerSegment( double units )
9793
mSettings.setUnitsPerSegment( units );
9894
return;
9995
}
100-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
10196
mSettings.setUnitsPerSegment( units );
10297
refreshSegmentMillimeters();
103-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
104-
correctXPositionAlignment( width, widthAfter );
105-
refreshItemSize();
106-
emit changed();
98+
resizeToMinimumWidth();
10799
}
108100

109101
void QgsLayoutItemScaleBar::setSegmentSizeMode( QgsScaleBarSettings::SegmentSizeMode mode )
@@ -113,13 +105,9 @@ void QgsLayoutItemScaleBar::setSegmentSizeMode( QgsScaleBarSettings::SegmentSize
113105
mSettings.setSegmentSizeMode( mode );
114106
return;
115107
}
116-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
117108
mSettings.setSegmentSizeMode( mode );
118109
refreshSegmentMillimeters();
119-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
120-
correctXPositionAlignment( width, widthAfter );
121-
refreshItemSize();
122-
emit changed();
110+
resizeToMinimumWidth();
123111
}
124112

125113
void QgsLayoutItemScaleBar::setMinimumBarWidth( double minWidth )
@@ -129,13 +117,9 @@ void QgsLayoutItemScaleBar::setMinimumBarWidth( double minWidth )
129117
mSettings.setMinimumBarWidth( minWidth );
130118
return;
131119
}
132-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
133120
mSettings.setMinimumBarWidth( minWidth );
134121
refreshSegmentMillimeters();
135-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
136-
correctXPositionAlignment( width, widthAfter );
137-
refreshItemSize();
138-
emit changed();
122+
resizeToMinimumWidth();
139123
}
140124

141125
void QgsLayoutItemScaleBar::setMaximumBarWidth( double maxWidth )
@@ -145,13 +129,9 @@ void QgsLayoutItemScaleBar::setMaximumBarWidth( double maxWidth )
145129
mSettings.setMaximumBarWidth( maxWidth );
146130
return;
147131
}
148-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
149132
mSettings.setMaximumBarWidth( maxWidth );
150133
refreshSegmentMillimeters();
151-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
152-
correctXPositionAlignment( width, widthAfter );
153-
refreshItemSize();
154-
emit changed();
134+
resizeToMinimumWidth();
155135
}
156136

157137
void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft( int nSegmentsLeft )
@@ -161,12 +141,8 @@ void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft( int nSegmentsLeft )
161141
mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
162142
return;
163143
}
164-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
165144
mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
166-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
167-
correctXPositionAlignment( width, widthAfter );
168-
refreshItemSize();
169-
emit changed();
145+
resizeToMinimumWidth();
170146
}
171147

172148
void QgsLayoutItemScaleBar::setBoxContentSpace( double space )
@@ -176,44 +152,37 @@ void QgsLayoutItemScaleBar::setBoxContentSpace( double space )
176152
mSettings.setBoxContentSpace( space );
177153
return;
178154
}
179-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
180155
mSettings.setBoxContentSpace( space );
181-
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
182-
correctXPositionAlignment( width, widthAfter );
183156
refreshItemSize();
184-
emit changed();
185157
}
186158

187159
void QgsLayoutItemScaleBar::setMap( QgsLayoutItemMap *map )
188160
{
189-
if ( mMap )
190-
{
191-
disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateSegmentSize );
192-
disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::invalidateCurrentMap );
193-
}
161+
disconnectCurrentMap();
162+
194163
mMap = map;
195164

196165
if ( !map )
197166
{
198167
return;
199168
}
200169

201-
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateSegmentSize );
202-
connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::invalidateCurrentMap );
170+
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
171+
connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
203172

204173
refreshSegmentMillimeters();
205174
emit changed();
206175
}
207176

208-
void QgsLayoutItemScaleBar::invalidateCurrentMap()
177+
void QgsLayoutItemScaleBar::disconnectCurrentMap()
209178
{
210179
if ( !mMap )
211180
{
212181
return;
213182
}
214183

215-
disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateSegmentSize );
216-
disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::invalidateCurrentMap );
184+
disconnect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
185+
disconnect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
217186
mMap = nullptr;
218187
}
219188

@@ -491,7 +460,20 @@ void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
491460
}
492461

493462
refreshSegmentMillimeters();
494-
refreshItemSize();
463+
resizeToMinimumWidth();
464+
emit changed();
465+
}
466+
467+
void QgsLayoutItemScaleBar::resizeToMinimumWidth()
468+
{
469+
if ( !mStyle )
470+
return;
471+
472+
double widthMM = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
473+
QgsLayoutSize currentSize = sizeWithUnits();
474+
currentSize.setWidth( mLayout->context().measurementConverter().convert( QgsLayoutMeasurement( widthMM, QgsUnitTypes::LayoutMillimeters ), currentSize.units() ).length() );
475+
attemptResize( currentSize );
476+
update();
495477
emit changed();
496478
}
497479

@@ -505,21 +487,11 @@ void QgsLayoutItemScaleBar::update()
505487
QgsLayoutItem::update();
506488
}
507489

508-
void QgsLayoutItemScaleBar::updateSegmentSize()
490+
void QgsLayoutItemScaleBar::updateScale()
509491
{
510-
if ( !mStyle )
511-
{
512-
return;
513-
}
514-
double widthMM = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
515492
refreshSegmentMillimeters();
516-
double widthAfterMM = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
517-
correctXPositionAlignment( widthMM, widthAfterMM );
518-
QgsLayoutSize currentSize = sizeWithUnits();
519-
currentSize.setWidth( mLayout->context().measurementConverter().convert( QgsLayoutMeasurement( widthAfterMM, QgsUnitTypes::LayoutMillimeters ), currentSize.units() ).length() );
520-
attemptResize( currentSize );
493+
resizeToMinimumWidth();
521494
update();
522-
emit changed();
523495
}
524496

525497
void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
@@ -831,41 +803,16 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
831803
}
832804
else
833805
{
834-
invalidateCurrentMap();
806+
disconnectCurrentMap();
835807
mMap = qobject_cast< QgsLayoutItemMap * >( mLayout->itemByUuid( mapId ) );
836808
if ( mMap )
837809
{
838-
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateSegmentSize );
839-
connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::invalidateCurrentMap );
810+
connect( mMap, &QgsLayoutItemMap::extentChanged, this, &QgsLayoutItemScaleBar::updateScale );
811+
connect( mMap, &QObject::destroyed, this, &QgsLayoutItemScaleBar::disconnectCurrentMap );
840812
}
841813
}
842814

843-
updateSegmentSize();
815+
updateScale();
844816
return true;
845817
}
846818

847-
void QgsLayoutItemScaleBar::correctXPositionAlignment( double widthMM, double widthAfterMM )
848-
{
849-
//Don't adjust position for numeric scale bars:
850-
if ( mStyle->name() == QLatin1String( "Numeric" ) )
851-
{
852-
return;
853-
}
854-
855-
QgsLayoutPoint currentPos = positionWithUnits();
856-
857-
double deltaMM = 0.0;
858-
if ( mSettings.alignment() == QgsScaleBarSettings::AlignMiddle )
859-
{
860-
deltaMM = -( widthAfterMM - widthMM ) / 2.0;
861-
}
862-
else if ( mSettings.alignment() == QgsScaleBarSettings::AlignRight )
863-
{
864-
deltaMM = -( widthAfterMM - widthMM );
865-
}
866-
867-
double delta = mLayout->context().measurementConverter().convert( QgsLayoutMeasurement( deltaMM, QgsUnitTypes::LayoutMillimeters ), currentPos.units() ).length();
868-
currentPos.setX( currentPos.x() + delta );
869-
attemptMove( currentPos );
870-
}
871-

src/core/layout/qgslayoutitemscalebar.h

+7-6
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,11 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
385385
*/
386386
void applyDefaultSize( QgsUnitTypes::DistanceUnit units = QgsUnitTypes::DistanceMeters );
387387

388+
/**
389+
* Resizes the scale bar to its minimum width, without changing the height.
390+
*/
391+
void resizeToMinimumWidth();
392+
388393
/**
389394
* Sets the scale bar style by \a name.
390395
*
@@ -416,9 +421,8 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
416421
bool readPropertiesFromElement( const QDomElement &element, const QDomDocument &document, const QgsReadWriteContext &context ) override;
417422

418423
private slots:
419-
void updateSegmentSize();
420-
//! Sets mCompositionMap to 0 if the map is deleted
421-
void invalidateCurrentMap();
424+
void updateScale();
425+
void disconnectCurrentMap();
422426

423427
private:
424428

@@ -433,9 +437,6 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
433437
//! Width of a segment (in mm)
434438
double mSegmentMillimeters = 0.0;
435439

436-
//! Moves scalebar position to the left / right depending on alignment and change in item width
437-
void correctXPositionAlignment( double widthMM, double widthAfterMM );
438-
439440
//! Calculates with of a segment in mm and stores it in mSegmentMillimeters
440441
void refreshSegmentMillimeters();
441442

0 commit comments

Comments
 (0)