@@ -82,12 +82,8 @@ void QgsLayoutItemScaleBar::setNumberOfSegments( int nSegments )
82
82
mSettings .setNumberOfSegments ( nSegments );
83
83
return ;
84
84
}
85
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
86
85
mSettings .setNumberOfSegments ( nSegments );
87
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
88
- correctXPositionAlignment ( width, widthAfter );
89
- refreshItemSize ();
90
- emit changed ();
86
+ resizeToMinimumWidth ();
91
87
}
92
88
93
89
void QgsLayoutItemScaleBar::setUnitsPerSegment ( double units )
@@ -97,13 +93,9 @@ void QgsLayoutItemScaleBar::setUnitsPerSegment( double units )
97
93
mSettings .setUnitsPerSegment ( units );
98
94
return ;
99
95
}
100
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
101
96
mSettings .setUnitsPerSegment ( units );
102
97
refreshSegmentMillimeters ();
103
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
104
- correctXPositionAlignment ( width, widthAfter );
105
- refreshItemSize ();
106
- emit changed ();
98
+ resizeToMinimumWidth ();
107
99
}
108
100
109
101
void QgsLayoutItemScaleBar::setSegmentSizeMode ( QgsScaleBarSettings::SegmentSizeMode mode )
@@ -113,13 +105,9 @@ void QgsLayoutItemScaleBar::setSegmentSizeMode( QgsScaleBarSettings::SegmentSize
113
105
mSettings .setSegmentSizeMode ( mode );
114
106
return ;
115
107
}
116
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
117
108
mSettings .setSegmentSizeMode ( mode );
118
109
refreshSegmentMillimeters ();
119
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
120
- correctXPositionAlignment ( width, widthAfter );
121
- refreshItemSize ();
122
- emit changed ();
110
+ resizeToMinimumWidth ();
123
111
}
124
112
125
113
void QgsLayoutItemScaleBar::setMinimumBarWidth ( double minWidth )
@@ -129,13 +117,9 @@ void QgsLayoutItemScaleBar::setMinimumBarWidth( double minWidth )
129
117
mSettings .setMinimumBarWidth ( minWidth );
130
118
return ;
131
119
}
132
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
133
120
mSettings .setMinimumBarWidth ( minWidth );
134
121
refreshSegmentMillimeters ();
135
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
136
- correctXPositionAlignment ( width, widthAfter );
137
- refreshItemSize ();
138
- emit changed ();
122
+ resizeToMinimumWidth ();
139
123
}
140
124
141
125
void QgsLayoutItemScaleBar::setMaximumBarWidth ( double maxWidth )
@@ -145,13 +129,9 @@ void QgsLayoutItemScaleBar::setMaximumBarWidth( double maxWidth )
145
129
mSettings .setMaximumBarWidth ( maxWidth );
146
130
return ;
147
131
}
148
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
149
132
mSettings .setMaximumBarWidth ( maxWidth );
150
133
refreshSegmentMillimeters ();
151
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
152
- correctXPositionAlignment ( width, widthAfter );
153
- refreshItemSize ();
154
- emit changed ();
134
+ resizeToMinimumWidth ();
155
135
}
156
136
157
137
void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft ( int nSegmentsLeft )
@@ -161,12 +141,8 @@ void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft( int nSegmentsLeft )
161
141
mSettings .setNumberOfSegmentsLeft ( nSegmentsLeft );
162
142
return ;
163
143
}
164
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
165
144
mSettings .setNumberOfSegmentsLeft ( nSegmentsLeft );
166
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
167
- correctXPositionAlignment ( width, widthAfter );
168
- refreshItemSize ();
169
- emit changed ();
145
+ resizeToMinimumWidth ();
170
146
}
171
147
172
148
void QgsLayoutItemScaleBar::setBoxContentSpace ( double space )
@@ -176,44 +152,37 @@ void QgsLayoutItemScaleBar::setBoxContentSpace( double space )
176
152
mSettings .setBoxContentSpace ( space );
177
153
return ;
178
154
}
179
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
180
155
mSettings .setBoxContentSpace ( space );
181
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
182
- correctXPositionAlignment ( width, widthAfter );
183
156
refreshItemSize ();
184
- emit changed ();
185
157
}
186
158
187
159
void QgsLayoutItemScaleBar::setMap ( QgsLayoutItemMap *map )
188
160
{
189
- if ( mMap )
190
- {
191
- disconnect ( mMap , &QgsLayoutItemMap::extentChanged, this , &QgsLayoutItemScaleBar::updateSegmentSize );
192
- disconnect ( mMap , &QObject::destroyed, this , &QgsLayoutItemScaleBar::invalidateCurrentMap );
193
- }
161
+ disconnectCurrentMap ();
162
+
194
163
mMap = map;
195
164
196
165
if ( !map )
197
166
{
198
167
return ;
199
168
}
200
169
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 );
203
172
204
173
refreshSegmentMillimeters ();
205
174
emit changed ();
206
175
}
207
176
208
- void QgsLayoutItemScaleBar::invalidateCurrentMap ()
177
+ void QgsLayoutItemScaleBar::disconnectCurrentMap ()
209
178
{
210
179
if ( !mMap )
211
180
{
212
181
return ;
213
182
}
214
183
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 );
217
186
mMap = nullptr ;
218
187
}
219
188
@@ -491,7 +460,20 @@ void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
491
460
}
492
461
493
462
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 ();
495
477
emit changed ();
496
478
}
497
479
@@ -505,21 +487,11 @@ void QgsLayoutItemScaleBar::update()
505
487
QgsLayoutItem::update ();
506
488
}
507
489
508
- void QgsLayoutItemScaleBar::updateSegmentSize ()
490
+ void QgsLayoutItemScaleBar::updateScale ()
509
491
{
510
- if ( !mStyle )
511
- {
512
- return ;
513
- }
514
- double widthMM = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
515
492
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 ();
521
494
update ();
522
- emit changed ();
523
495
}
524
496
525
497
void QgsLayoutItemScaleBar::setStyle ( const QString &styleName )
@@ -831,41 +803,16 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
831
803
}
832
804
else
833
805
{
834
- invalidateCurrentMap ();
806
+ disconnectCurrentMap ();
835
807
mMap = qobject_cast< QgsLayoutItemMap * >( mLayout ->itemByUuid ( mapId ) );
836
808
if ( mMap )
837
809
{
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 );
840
812
}
841
813
}
842
814
843
- updateSegmentSize ();
815
+ updateScale ();
844
816
return true ;
845
817
}
846
818
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
-
0 commit comments