42
42
43
43
QgsLayoutItemScaleBar::QgsLayoutItemScaleBar ( QgsLayout *layout )
44
44
: QgsLayoutItem( layout )
45
- , mSegmentMillimeters( 0.0 )
46
45
{
47
46
applyDefaultSettings ();
48
47
applyDefaultSize ();
49
48
}
50
49
51
- QgsLayoutItemScaleBar::~QgsLayoutItemScaleBar ()
52
- {
53
- delete mStyle ;
54
- }
55
-
56
50
int QgsLayoutItemScaleBar::type () const
57
51
{
58
52
return QgsLayoutItemRegistry::LayoutScaleBar;
@@ -68,6 +62,11 @@ QgsLayoutItemScaleBar *QgsLayoutItemScaleBar::create( QgsLayout *layout )
68
62
return new QgsLayoutItemScaleBar ( layout );
69
63
}
70
64
65
+ QgsLayoutSize QgsLayoutItemScaleBar::minimumSize () const
66
+ {
67
+ return QgsLayoutSize ( mStyle ->calculateBoxSize ( mSettings , createScaleContext () ), QgsUnitTypes::LayoutMillimeters );
68
+ }
69
+
71
70
void QgsLayoutItemScaleBar::draw ( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
72
71
{
73
72
if ( !mStyle )
@@ -87,6 +86,7 @@ void QgsLayoutItemScaleBar::setNumberOfSegments( int nSegments )
87
86
mSettings .setNumberOfSegments ( nSegments );
88
87
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
89
88
correctXPositionAlignment ( width, widthAfter );
89
+ refreshItemSize ();
90
90
emit changed ();
91
91
}
92
92
@@ -102,6 +102,7 @@ void QgsLayoutItemScaleBar::setUnitsPerSegment( double units )
102
102
refreshSegmentMillimeters ();
103
103
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
104
104
correctXPositionAlignment ( width, widthAfter );
105
+ refreshItemSize ();
105
106
emit changed ();
106
107
}
107
108
@@ -117,6 +118,7 @@ void QgsLayoutItemScaleBar::setSegmentSizeMode( QgsScaleBarSettings::SegmentSize
117
118
refreshSegmentMillimeters ();
118
119
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
119
120
correctXPositionAlignment ( width, widthAfter );
121
+ refreshItemSize ();
120
122
emit changed ();
121
123
}
122
124
@@ -132,6 +134,7 @@ void QgsLayoutItemScaleBar::setMinimumBarWidth( double minWidth )
132
134
refreshSegmentMillimeters ();
133
135
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
134
136
correctXPositionAlignment ( width, widthAfter );
137
+ refreshItemSize ();
135
138
emit changed ();
136
139
}
137
140
@@ -147,6 +150,7 @@ void QgsLayoutItemScaleBar::setMaximumBarWidth( double maxWidth )
147
150
refreshSegmentMillimeters ();
148
151
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
149
152
correctXPositionAlignment ( width, widthAfter );
153
+ refreshItemSize ();
150
154
emit changed ();
151
155
}
152
156
@@ -161,6 +165,7 @@ void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft( int nSegmentsLeft )
161
165
mSettings .setNumberOfSegmentsLeft ( nSegmentsLeft );
162
166
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
163
167
correctXPositionAlignment ( width, widthAfter );
168
+ refreshItemSize ();
164
169
emit changed ();
165
170
}
166
171
@@ -175,6 +180,7 @@ void QgsLayoutItemScaleBar::setBoxContentSpace( double space )
175
180
mSettings .setBoxContentSpace ( space );
176
181
double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
177
182
correctXPositionAlignment ( width, widthAfter );
183
+ refreshItemSize ();
178
184
emit changed ();
179
185
}
180
186
@@ -247,6 +253,7 @@ void QgsLayoutItemScaleBar::refreshDataDefinedProperty( const QgsLayoutObject::D
247
253
}
248
254
if ( forceUpdate )
249
255
{
256
+ refreshItemSize ();
250
257
update ();
251
258
}
252
259
@@ -353,14 +360,15 @@ QgsScaleBarRenderer::ScaleBarContext QgsLayoutItemScaleBar::createScaleContext()
353
360
void QgsLayoutItemScaleBar::setAlignment ( QgsScaleBarSettings::Alignment a )
354
361
{
355
362
mSettings .setAlignment ( a );
356
- update ();
363
+ refreshItemSize ();
357
364
emit changed ();
358
365
}
359
366
360
367
void QgsLayoutItemScaleBar::setUnits ( QgsUnitTypes::DistanceUnit u )
361
368
{
362
369
mSettings .setUnits ( u );
363
370
refreshSegmentMillimeters ();
371
+ refreshItemSize ();
364
372
emit changed ();
365
373
}
366
374
@@ -391,8 +399,7 @@ void QgsLayoutItemScaleBar::setLineCapStyle( Qt::PenCapStyle style )
391
399
void QgsLayoutItemScaleBar::applyDefaultSettings ()
392
400
{
393
401
// style
394
- delete mStyle ;
395
- mStyle = new QgsSingleBoxScaleBarRenderer ();
402
+ mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
396
403
397
404
// default to no background
398
405
setBackgroundEnabled ( false );
@@ -409,6 +416,7 @@ void QgsLayoutItemScaleBar::applyDefaultSettings()
409
416
mSettings .setFont ( f );
410
417
411
418
mSettings .setUnits ( QgsUnitTypes::DistanceUnknownUnit );
419
+ refreshItemSize ();
412
420
413
421
emit changed ();
414
422
}
@@ -483,76 +491,16 @@ void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
483
491
}
484
492
485
493
refreshSegmentMillimeters ();
486
- adjustBoxSize ();
494
+ refreshItemSize ();
487
495
emit changed ();
488
496
}
489
497
490
- void QgsLayoutItemScaleBar::adjustBoxSize ()
491
- {
492
- if ( !mStyle )
493
- {
494
- return ;
495
- }
496
-
497
- QRectF box = QRectF ( pos (), mStyle ->calculateBoxSize ( mSettings , createScaleContext () ) );
498
- if ( rect ().height () > box.height () )
499
- {
500
- // keep user specified item height if higher than minimum scale bar height
501
- box.setHeight ( rect ().height () );
502
- }
503
-
504
- #if 0 //TODO
505
- //update rect for data defined size and position
506
- QRectF newRect = evalItemRect( box, true );
507
-
508
- //scale bars have a minimum size, respect that regardless of data defined settings
509
- if ( newRect.width() < box.width() )
510
- {
511
- newRect.setWidth( box.width() );
512
- }
513
- if ( newRect.height() < box.height() )
514
- {
515
- newRect.setHeight( box.height() );
516
- }
517
-
518
- QgsLayoutItem::setSceneRect( newRect );
519
- #endif
520
- }
521
-
522
- #if 0 //TODO
523
- void QgsLayoutItemScaleBar::setSceneRect( const QRectF &rectangle )
524
- {
525
- QRectF box = QRectF( pos(), mStyle->calculateBoxSize( mSettings, createScaleContext() ) );
526
- if ( rectangle.height() > box.height() )
527
- {
528
- //keep user specified item height if higher than minimum scale bar height
529
- box.setHeight( rectangle.height() );
530
- }
531
- box.moveTopLeft( rectangle.topLeft() );
532
-
533
- //update rect for data defined size and position
534
- QRectF newRect = evalItemRect( rectangle );
535
-
536
- //scale bars have a minimum size, respect that regardless of data defined settings
537
- if ( newRect.width() < box.width() )
538
- {
539
- newRect.setWidth( box.width() );
540
- }
541
- if ( newRect.height() < box.height() )
542
- {
543
- newRect.setHeight( box.height() );
544
- }
545
-
546
- QgsComposerItem::setSceneRect( newRect );
547
- }
548
- #endif
549
-
550
498
void QgsLayoutItemScaleBar::update ()
551
499
{
552
500
// Don't adjust box size for numeric scale bars:
553
501
if ( mStyle && mStyle ->name () != QLatin1String ( " Numeric" ) )
554
502
{
555
- adjustBoxSize ();
503
+ refreshItemSize ();
556
504
}
557
505
QgsLayoutItem::update ();
558
506
}
@@ -563,31 +511,31 @@ void QgsLayoutItemScaleBar::updateSegmentSize()
563
511
{
564
512
return ;
565
513
}
566
- double width = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
514
+ double widthMM = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
567
515
refreshSegmentMillimeters ();
568
- double widthAfter = mStyle ->calculateBoxSize ( mSettings , createScaleContext () ).width ();
569
- correctXPositionAlignment ( width, widthAfter );
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 );
570
521
update ();
571
522
emit changed ();
572
523
}
573
524
574
525
void QgsLayoutItemScaleBar::setStyle ( const QString &styleName )
575
526
{
576
- delete mStyle ;
577
- mStyle = nullptr ;
578
-
579
527
// switch depending on style name
580
528
if ( styleName == QLatin1String ( " Single Box" ) )
581
529
{
582
- mStyle = new QgsSingleBoxScaleBarRenderer ();
530
+ mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer > ();
583
531
}
584
532
else if ( styleName == QLatin1String ( " Double Box" ) )
585
533
{
586
- mStyle = new QgsDoubleBoxScaleBarRenderer ();
534
+ mStyle = qgis::make_unique< QgsDoubleBoxScaleBarRenderer > ();
587
535
}
588
536
else if ( styleName == QLatin1String ( " Line Ticks Middle" ) || styleName == QLatin1String ( " Line Ticks Down" ) || styleName == QLatin1String ( " Line Ticks Up" ) )
589
537
{
590
- QgsTicksScaleBarRenderer * tickStyle = new QgsTicksScaleBarRenderer ();
538
+ std::unique_ptr< QgsTicksScaleBarRenderer > tickStyle = qgis::make_unique< QgsTicksScaleBarRenderer > ();
591
539
if ( styleName == QLatin1String ( " Line Ticks Middle" ) )
592
540
{
593
541
tickStyle->setTickPosition ( QgsTicksScaleBarRenderer::TicksMiddle );
@@ -600,12 +548,13 @@ void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
600
548
{
601
549
tickStyle->setTickPosition ( QgsTicksScaleBarRenderer::TicksUp );
602
550
}
603
- mStyle = tickStyle;
551
+ mStyle = std::move ( tickStyle ) ;
604
552
}
605
553
else if ( styleName == QLatin1String ( " Numeric" ) )
606
554
{
607
- mStyle = new QgsNumericScaleBarRenderer ();
555
+ mStyle = qgis::make_unique< QgsNumericScaleBarRenderer > ();
608
556
}
557
+ refreshItemSize ();
609
558
emit changed ();
610
559
}
611
560
@@ -629,7 +578,7 @@ QFont QgsLayoutItemScaleBar::font() const
629
578
void QgsLayoutItemScaleBar::setFont ( const QFont &font )
630
579
{
631
580
mSettings .setFont ( font );
632
- update ();
581
+ refreshItemSize ();
633
582
emit changed ();
634
583
}
635
584
@@ -705,7 +654,7 @@ bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScale
705
654
return true ;
706
655
}
707
656
708
- bool QgsLayoutItemScaleBar::readPropertiesFromElement ( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
657
+ bool QgsLayoutItemScaleBar::readPropertiesFromElement ( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
709
658
{
710
659
mSettings .setHeight ( itemElem.attribute ( QStringLiteral ( " height" ), QStringLiteral ( " 5.0" ) ).toDouble () );
711
660
mSettings .setLabelBarSpace ( itemElem.attribute ( QStringLiteral ( " labelBarSpace" ), QStringLiteral ( " 3.0" ) ).toDouble () );
@@ -831,10 +780,8 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
831
780
}
832
781
833
782
// style
834
- delete mStyle ;
835
- mStyle = nullptr ;
836
783
QString styleString = itemElem.attribute ( QStringLiteral ( " style" ), QLatin1String ( " " ) );
837
- setStyle ( tr ( styleString.toLocal8Bit ().data () ) );
784
+ setStyle ( styleString.toLocal8Bit ().data () );
838
785
839
786
if ( itemElem.attribute ( QStringLiteral ( " unitType" ) ).isEmpty () )
840
787
{
@@ -897,23 +844,28 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
897
844
return true ;
898
845
}
899
846
900
- void QgsLayoutItemScaleBar::correctXPositionAlignment ( double width , double widthAfter )
847
+ void QgsLayoutItemScaleBar::correctXPositionAlignment ( double widthMM , double widthAfterMM )
901
848
{
902
849
// Don't adjust position for numeric scale bars:
903
850
if ( mStyle ->name () == QLatin1String ( " Numeric" ) )
904
851
{
905
852
return ;
906
853
}
907
- #if 0 //TODO
908
854
855
+ QgsLayoutPoint currentPos = positionWithUnits ();
856
+
857
+ double deltaMM = 0.0 ;
909
858
if ( mSettings .alignment () == QgsScaleBarSettings::AlignMiddle )
910
859
{
911
- move( -( widthAfter - width ) / 2.0, 0 ) ;
860
+ deltaMM = -( widthAfterMM - widthMM ) / 2.0 ;
912
861
}
913
862
else if ( mSettings .alignment () == QgsScaleBarSettings::AlignRight )
914
863
{
915
- move( -( widthAfter - width ), 0 );
864
+ deltaMM = -( widthAfterMM - widthMM );
916
865
}
917
- #endif
866
+
867
+ double delta = mLayout ->context ().measurementConverter ().convert ( QgsLayoutMeasurement ( deltaMM, QgsUnitTypes::LayoutMillimeters ), currentPos.units () ).length ();
868
+ currentPos.setX ( currentPos.x () + delta );
869
+ attemptMove ( currentPos );
918
870
}
919
871
0 commit comments