Skip to content

Commit 5b1c690

Browse files
committedNov 24, 2017
Port more scalebar functionality
1 parent 7e7770b commit 5b1c690

11 files changed

+1680
-114
lines changed
 

‎python/core/layout/qgslayoutitem.sip

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,20 @@ class QgsLayoutItem : QgsLayoutObject, QGraphicsRectItem, QgsLayoutUndoObjectInt
105105
UndoLegendGroupFont,
106106
UndoLegendLayerFont,
107107
UndoLegendItemFont,
108+
UndoScaleBarLineWidth,
109+
UndoScaleBarSegmentSize,
110+
UndoScaleBarSegmentsLeft,
111+
UndoScaleBarSegments,
112+
UndoScaleBarHeight,
113+
UndoScaleBarFontColor,
114+
UndoScaleBarFillColor,
115+
UndoScaleBarFillColor2,
116+
UndoScaleBarStrokeColor,
117+
UndoScaleBarUnitText,
118+
UndoScaleBarMapUnitsSegment,
119+
UndoScaleBarLabelBarSize,
120+
UndoScaleBarBoxContentSpace,
121+
108122
UndoCustomCommand,
109123
};
110124

‎python/core/layout/qgslayoutitemscalebar.sip

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
2424
%Docstring
2525
Constructor for QgsLayoutItemScaleBar, with the specified parent ``layout``.
2626
%End
27-
~QgsLayoutItemScaleBar();
2827

2928
virtual int type() const;
3029

@@ -38,6 +37,8 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
3837
The caller takes responsibility for deleting the returned object.
3938
:rtype: QgsLayoutItemScaleBar
4039
%End
40+
virtual QgsLayoutSize minimumSize() const;
41+
4142

4243
int numberOfSegments() const;
4344
%Docstring
@@ -413,15 +414,11 @@ class QgsLayoutItemScaleBar: QgsLayoutItem
413414
:rtype: str
414415
%End
415416

416-
void adjustBoxSize();
417-
%Docstring
418-
Sets the scale bar box size to a size suitable for the scalebar content.
419-
%End
420-
421417
void update();
422418
%Docstring
423419
Adjusts the scale bar box size and updates the item.
424420
%End
421+
425422
virtual void refreshDataDefinedProperty( const QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties );
426423

427424

‎src/app/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ SET(QGIS_APP_SRCS
193193
layout/qgslayoutpolygonwidget.cpp
194194
layout/qgslayoutpolylinewidget.cpp
195195
layout/qgslayoutpropertieswidget.cpp
196+
layout/qgslayoutscalebarwidget.cpp
196197
layout/qgslayoutshapewidget.cpp
197198

198199
locator/qgsinbuiltlocatorfilters.cpp
@@ -402,6 +403,7 @@ SET (QGIS_APP_MOC_HDRS
402403
layout/qgslayoutpolygonwidget.h
403404
layout/qgslayoutpolylinewidget.h
404405
layout/qgslayoutpropertieswidget.h
406+
layout/qgslayoutscalebarwidget.h
405407
layout/qgslayoutshapewidget.h
406408

407409
locator/qgsinbuiltlocatorfilters.h

‎src/app/layout/qgslayoutapputils.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
#include "qgslayoutframe.h"
3838
#include "qgslayoutitemhtml.h"
3939
#include "qgslayouthtmlwidget.h"
40+
#include "qgslayoutscalebarwidget.h"
4041
#include "qgisapp.h"
4142
#include "qgsmapcanvas.h"
4243

@@ -143,10 +144,10 @@ void QgsLayoutAppUtils::registerGuiForKnownItemTypes()
143144

144145
// scalebar item
145146

146-
auto scalebarItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutScaleBar, QObject::tr( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddScalebar.svg" ) ),
147+
auto scalebarItemMetadata = qgis::make_unique< QgsLayoutItemGuiMetadata >( QgsLayoutItemRegistry::LayoutScaleBar, QObject::tr( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionScaleBar.svg" ) ),
147148
[ = ]( QgsLayoutItem * item )->QgsLayoutItemBaseWidget *
148149
{
149-
return nullptr;//new QgsLayoutLegendWidget( qobject_cast< QgsLayoutItemLegend * >( item ) );
150+
return new QgsLayoutScaleBarWidget( qobject_cast< QgsLayoutItemScaleBar * >( item ) );
150151
}, createRubberBand );
151152
scalebarItemMetadata->setItemAddedToLayoutFunction( [ = ]( QgsLayoutItem * item )
152153
{

‎src/app/layout/qgslayoutscalebarwidget.cpp

Lines changed: 731 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/***************************************************************************
2+
qgslayoutscalebarwidget.h
3+
---------------------------
4+
begin : 11 June 2008
5+
copyright : (C) 2008 by Marco Hugentobler
6+
email : marco dot hugentobler at karto dot baug dot ethz dot ch
7+
***************************************************************************/
8+
/***************************************************************************
9+
* *
10+
* This program is free software; you can redistribute it and/or modify *
11+
* it under the terms of the GNU General Public License as published by *
12+
* the Free Software Foundation; either version 2 of the License, or *
13+
* (at your option) any later version. *
14+
* *
15+
***************************************************************************/
16+
17+
#ifndef QGSLAYOUTSCALEBARWIDGET_H
18+
#define QGSLAYOUTSCALEBARWIDGET_H
19+
20+
#include "ui_qgslayoutscalebarwidgetbase.h"
21+
#include "qgslayoutitemwidget.h"
22+
23+
class QgsLayoutItemScaleBar;
24+
25+
/**
26+
* \ingroup app
27+
* A widget to define the properties of a QgsLayoutItemScaleBar.
28+
*/
29+
class QgsLayoutScaleBarWidget: public QgsLayoutItemBaseWidget, private Ui::QgsLayoutScaleBarWidgetBase
30+
{
31+
Q_OBJECT
32+
33+
public:
34+
explicit QgsLayoutScaleBarWidget( QgsLayoutItemScaleBar *scaleBar );
35+
36+
protected:
37+
38+
bool setNewItem( QgsLayoutItem *item ) override;
39+
40+
public slots:
41+
42+
void mHeightSpinBox_valueChanged( double d );
43+
void mLineWidthSpinBox_valueChanged( double d );
44+
void mSegmentSizeSpinBox_valueChanged( double d );
45+
void mSegmentsLeftSpinBox_valueChanged( int i );
46+
void mNumberOfSegmentsSpinBox_valueChanged( int i );
47+
void mUnitLabelLineEdit_textChanged( const QString &text );
48+
void mMapUnitsPerBarUnitSpinBox_valueChanged( double d );
49+
void mFontColorButton_colorChanged( const QColor &newColor );
50+
void mFillColorButton_colorChanged( const QColor &newColor );
51+
void mFillColor2Button_colorChanged( const QColor &newColor );
52+
void mStrokeColorButton_colorChanged( const QColor &newColor );
53+
void mStyleComboBox_currentIndexChanged( const QString &text );
54+
void mLabelBarSpaceSpinBox_valueChanged( double d );
55+
void mBoxSizeSpinBox_valueChanged( double d );
56+
void mAlignmentComboBox_currentIndexChanged( int index );
57+
void mUnitsComboBox_currentIndexChanged( int index );
58+
void mLineJoinStyleCombo_currentIndexChanged( int index );
59+
void mLineCapStyleCombo_currentIndexChanged( int index );
60+
void mMinWidthSpinBox_valueChanged( double d );
61+
void mMaxWidthSpinBox_valueChanged( double d );
62+
63+
private slots:
64+
void setGuiElements();
65+
void segmentSizeRadioChanged( QAbstractButton *radio );
66+
void mapChanged( QgsLayoutItem *item );
67+
void fontChanged();
68+
69+
private:
70+
QgsLayoutItemScaleBar *mScalebar = nullptr;
71+
QgsLayoutItemPropertiesWidget *mItemPropertiesWidget = nullptr;
72+
73+
QButtonGroup mSegmentSizeRadioGroup;
74+
75+
//! Enables/disables the signals of the input gui elements
76+
void blockMemberSignals( bool enable );
77+
78+
//! Enables/disables controls based on scale bar style
79+
void toggleStyleSpecificControls( const QString &style );
80+
81+
void connectUpdateSignal();
82+
void disconnectUpdateSignal();
83+
};
84+
85+
#endif //QGSLAYOUTSCALEBARWIDGET_H

‎src/core/layout/qgslayoutitem.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ class CORE_EXPORT QgsLayoutItem : public QgsLayoutObject, public QGraphicsRectIt
138138
UndoLegendGroupFont, //!< Legend group font
139139
UndoLegendLayerFont, //!< Legend layer font
140140
UndoLegendItemFont, //!< Legend item font
141+
UndoScaleBarLineWidth, //!< Scalebar line width
142+
UndoScaleBarSegmentSize, //!< Scalebar segment size
143+
UndoScaleBarSegmentsLeft, //!< Scalebar segments left
144+
UndoScaleBarSegments, //!< Scalebar number of segments
145+
UndoScaleBarHeight, //!< Scalebar height
146+
UndoScaleBarFontColor, //!< Scalebar font color
147+
UndoScaleBarFillColor, //!< Scalebar fill color
148+
UndoScaleBarFillColor2, //!< Scalebar secondary fill color
149+
UndoScaleBarStrokeColor, //!< Scalebar stroke color
150+
UndoScaleBarUnitText, //!< Scalebar unit text
151+
UndoScaleBarMapUnitsSegment, //!< Scalebar map units per segment
152+
UndoScaleBarLabelBarSize, //!< Scalebar label bar size
153+
UndoScaleBarBoxContentSpace, //!< Scalebar box context space
154+
141155
UndoCustomCommand, //!< Base id for plugin based item undo commands
142156
};
143157

‎src/core/layout/qgslayoutitemregistry.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ bool QgsLayoutItemRegistry::populate()
6060
addLayoutItemType( new QgsLayoutItemMetadata( LayoutPicture, QStringLiteral( "Picture" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddImage.svg" ) ), QgsLayoutItemPicture::create ) );
6161
addLayoutItemType( new QgsLayoutItemMetadata( LayoutLabel, QStringLiteral( "Label" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionLabel.svg" ) ), QgsLayoutItemLabel::create ) );
6262
addLayoutItemType( new QgsLayoutItemMetadata( LayoutLegend, QStringLiteral( "Legend" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddLegend.svg" ) ), QgsLayoutItemLegend::create ) );
63-
addLayoutItemType( new QgsLayoutItemMetadata( LayoutScaleBar, QStringLiteral( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddScalebar.svg" ) ), QgsLayoutItemScaleBar::create ) );
63+
addLayoutItemType( new QgsLayoutItemMetadata( LayoutScaleBar, QStringLiteral( "Scale Bar" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionScaleBar.svg" ) ), QgsLayoutItemScaleBar::create ) );
6464
addLayoutItemType( new QgsLayoutItemMetadata( LayoutShape, QStringLiteral( "Shape" ), QgsApplication::getThemeIcon( QStringLiteral( "/mActionAddBasicRectangle.svg" ) ), []( QgsLayout * layout )
6565
{
6666
QgsLayoutItemShape *shape = new QgsLayoutItemShape( layout );

‎src/core/layout/qgslayoutitemscalebar.cpp

Lines changed: 44 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,11 @@
4242

4343
QgsLayoutItemScaleBar::QgsLayoutItemScaleBar( QgsLayout *layout )
4444
: QgsLayoutItem( layout )
45-
, mSegmentMillimeters( 0.0 )
4645
{
4746
applyDefaultSettings();
4847
applyDefaultSize();
4948
}
5049

51-
QgsLayoutItemScaleBar::~QgsLayoutItemScaleBar()
52-
{
53-
delete mStyle;
54-
}
55-
5650
int QgsLayoutItemScaleBar::type() const
5751
{
5852
return QgsLayoutItemRegistry::LayoutScaleBar;
@@ -68,6 +62,11 @@ QgsLayoutItemScaleBar *QgsLayoutItemScaleBar::create( QgsLayout *layout )
6862
return new QgsLayoutItemScaleBar( layout );
6963
}
7064

65+
QgsLayoutSize QgsLayoutItemScaleBar::minimumSize() const
66+
{
67+
return QgsLayoutSize( mStyle->calculateBoxSize( mSettings, createScaleContext() ), QgsUnitTypes::LayoutMillimeters );
68+
}
69+
7170
void QgsLayoutItemScaleBar::draw( QgsRenderContext &context, const QStyleOptionGraphicsItem * )
7271
{
7372
if ( !mStyle )
@@ -87,6 +86,7 @@ void QgsLayoutItemScaleBar::setNumberOfSegments( int nSegments )
8786
mSettings.setNumberOfSegments( nSegments );
8887
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
8988
correctXPositionAlignment( width, widthAfter );
89+
refreshItemSize();
9090
emit changed();
9191
}
9292

@@ -102,6 +102,7 @@ void QgsLayoutItemScaleBar::setUnitsPerSegment( double units )
102102
refreshSegmentMillimeters();
103103
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
104104
correctXPositionAlignment( width, widthAfter );
105+
refreshItemSize();
105106
emit changed();
106107
}
107108

@@ -117,6 +118,7 @@ void QgsLayoutItemScaleBar::setSegmentSizeMode( QgsScaleBarSettings::SegmentSize
117118
refreshSegmentMillimeters();
118119
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
119120
correctXPositionAlignment( width, widthAfter );
121+
refreshItemSize();
120122
emit changed();
121123
}
122124

@@ -132,6 +134,7 @@ void QgsLayoutItemScaleBar::setMinimumBarWidth( double minWidth )
132134
refreshSegmentMillimeters();
133135
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
134136
correctXPositionAlignment( width, widthAfter );
137+
refreshItemSize();
135138
emit changed();
136139
}
137140

@@ -147,6 +150,7 @@ void QgsLayoutItemScaleBar::setMaximumBarWidth( double maxWidth )
147150
refreshSegmentMillimeters();
148151
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
149152
correctXPositionAlignment( width, widthAfter );
153+
refreshItemSize();
150154
emit changed();
151155
}
152156

@@ -161,6 +165,7 @@ void QgsLayoutItemScaleBar::setNumberOfSegmentsLeft( int nSegmentsLeft )
161165
mSettings.setNumberOfSegmentsLeft( nSegmentsLeft );
162166
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
163167
correctXPositionAlignment( width, widthAfter );
168+
refreshItemSize();
164169
emit changed();
165170
}
166171

@@ -175,6 +180,7 @@ void QgsLayoutItemScaleBar::setBoxContentSpace( double space )
175180
mSettings.setBoxContentSpace( space );
176181
double widthAfter = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
177182
correctXPositionAlignment( width, widthAfter );
183+
refreshItemSize();
178184
emit changed();
179185
}
180186

@@ -247,6 +253,7 @@ void QgsLayoutItemScaleBar::refreshDataDefinedProperty( const QgsLayoutObject::D
247253
}
248254
if ( forceUpdate )
249255
{
256+
refreshItemSize();
250257
update();
251258
}
252259

@@ -353,14 +360,15 @@ QgsScaleBarRenderer::ScaleBarContext QgsLayoutItemScaleBar::createScaleContext()
353360
void QgsLayoutItemScaleBar::setAlignment( QgsScaleBarSettings::Alignment a )
354361
{
355362
mSettings.setAlignment( a );
356-
update();
363+
refreshItemSize();
357364
emit changed();
358365
}
359366

360367
void QgsLayoutItemScaleBar::setUnits( QgsUnitTypes::DistanceUnit u )
361368
{
362369
mSettings.setUnits( u );
363370
refreshSegmentMillimeters();
371+
refreshItemSize();
364372
emit changed();
365373
}
366374

@@ -391,8 +399,7 @@ void QgsLayoutItemScaleBar::setLineCapStyle( Qt::PenCapStyle style )
391399
void QgsLayoutItemScaleBar::applyDefaultSettings()
392400
{
393401
//style
394-
delete mStyle;
395-
mStyle = new QgsSingleBoxScaleBarRenderer();
402+
mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
396403

397404
//default to no background
398405
setBackgroundEnabled( false );
@@ -409,6 +416,7 @@ void QgsLayoutItemScaleBar::applyDefaultSettings()
409416
mSettings.setFont( f );
410417

411418
mSettings.setUnits( QgsUnitTypes::DistanceUnknownUnit );
419+
refreshItemSize();
412420

413421
emit changed();
414422
}
@@ -483,76 +491,16 @@ void QgsLayoutItemScaleBar::applyDefaultSize( QgsUnitTypes::DistanceUnit units )
483491
}
484492

485493
refreshSegmentMillimeters();
486-
adjustBoxSize();
494+
refreshItemSize();
487495
emit changed();
488496
}
489497

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-
550498
void QgsLayoutItemScaleBar::update()
551499
{
552500
//Don't adjust box size for numeric scale bars:
553501
if ( mStyle && mStyle->name() != QLatin1String( "Numeric" ) )
554502
{
555-
adjustBoxSize();
503+
refreshItemSize();
556504
}
557505
QgsLayoutItem::update();
558506
}
@@ -563,31 +511,31 @@ void QgsLayoutItemScaleBar::updateSegmentSize()
563511
{
564512
return;
565513
}
566-
double width = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
514+
double widthMM = mStyle->calculateBoxSize( mSettings, createScaleContext() ).width();
567515
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 );
570521
update();
571522
emit changed();
572523
}
573524

574525
void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
575526
{
576-
delete mStyle;
577-
mStyle = nullptr;
578-
579527
//switch depending on style name
580528
if ( styleName == QLatin1String( "Single Box" ) )
581529
{
582-
mStyle = new QgsSingleBoxScaleBarRenderer();
530+
mStyle = qgis::make_unique< QgsSingleBoxScaleBarRenderer >();
583531
}
584532
else if ( styleName == QLatin1String( "Double Box" ) )
585533
{
586-
mStyle = new QgsDoubleBoxScaleBarRenderer();
534+
mStyle = qgis::make_unique< QgsDoubleBoxScaleBarRenderer >();
587535
}
588536
else if ( styleName == QLatin1String( "Line Ticks Middle" ) || styleName == QLatin1String( "Line Ticks Down" ) || styleName == QLatin1String( "Line Ticks Up" ) )
589537
{
590-
QgsTicksScaleBarRenderer *tickStyle = new QgsTicksScaleBarRenderer();
538+
std::unique_ptr< QgsTicksScaleBarRenderer > tickStyle = qgis::make_unique< QgsTicksScaleBarRenderer >();
591539
if ( styleName == QLatin1String( "Line Ticks Middle" ) )
592540
{
593541
tickStyle->setTickPosition( QgsTicksScaleBarRenderer::TicksMiddle );
@@ -600,12 +548,13 @@ void QgsLayoutItemScaleBar::setStyle( const QString &styleName )
600548
{
601549
tickStyle->setTickPosition( QgsTicksScaleBarRenderer::TicksUp );
602550
}
603-
mStyle = tickStyle;
551+
mStyle = std::move( tickStyle );
604552
}
605553
else if ( styleName == QLatin1String( "Numeric" ) )
606554
{
607-
mStyle = new QgsNumericScaleBarRenderer();
555+
mStyle = qgis::make_unique< QgsNumericScaleBarRenderer >();
608556
}
557+
refreshItemSize();
609558
emit changed();
610559
}
611560

@@ -629,7 +578,7 @@ QFont QgsLayoutItemScaleBar::font() const
629578
void QgsLayoutItemScaleBar::setFont( const QFont &font )
630579
{
631580
mSettings.setFont( font );
632-
update();
581+
refreshItemSize();
633582
emit changed();
634583
}
635584

@@ -705,7 +654,7 @@ bool QgsLayoutItemScaleBar::writePropertiesToElement( QDomElement &composerScale
705654
return true;
706655
}
707656

708-
bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext &context )
657+
bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemElem, const QDomDocument &, const QgsReadWriteContext & )
709658
{
710659
mSettings.setHeight( itemElem.attribute( QStringLiteral( "height" ), QStringLiteral( "5.0" ) ).toDouble() );
711660
mSettings.setLabelBarSpace( itemElem.attribute( QStringLiteral( "labelBarSpace" ), QStringLiteral( "3.0" ) ).toDouble() );
@@ -831,10 +780,8 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
831780
}
832781

833782
//style
834-
delete mStyle;
835-
mStyle = nullptr;
836783
QString styleString = itemElem.attribute( QStringLiteral( "style" ), QLatin1String( "" ) );
837-
setStyle( tr( styleString.toLocal8Bit().data() ) );
784+
setStyle( styleString.toLocal8Bit().data() );
838785

839786
if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
840787
{
@@ -897,23 +844,28 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
897844
return true;
898845
}
899846

900-
void QgsLayoutItemScaleBar::correctXPositionAlignment( double width, double widthAfter )
847+
void QgsLayoutItemScaleBar::correctXPositionAlignment( double widthMM, double widthAfterMM )
901848
{
902849
//Don't adjust position for numeric scale bars:
903850
if ( mStyle->name() == QLatin1String( "Numeric" ) )
904851
{
905852
return;
906853
}
907-
#if 0 //TODO
908854

855+
QgsLayoutPoint currentPos = positionWithUnits();
856+
857+
double deltaMM = 0.0;
909858
if ( mSettings.alignment() == QgsScaleBarSettings::AlignMiddle )
910859
{
911-
move( -( widthAfter - width ) / 2.0, 0 );
860+
deltaMM = -( widthAfterMM - widthMM ) / 2.0;
912861
}
913862
else if ( mSettings.alignment() == QgsScaleBarSettings::AlignRight )
914863
{
915-
move( -( widthAfter - width ), 0 );
864+
deltaMM = -( widthAfterMM - widthMM );
916865
}
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 );
918870
}
919871

‎src/core/layout/qgslayoutitemscalebar.h

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
4242
* Constructor for QgsLayoutItemScaleBar, with the specified parent \a layout.
4343
*/
4444
QgsLayoutItemScaleBar( QgsLayout *layout );
45-
~QgsLayoutItemScaleBar();
4645

4746
int type() const override;
4847
QString stringType() const override;
@@ -53,6 +52,7 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
5352
* The caller takes responsibility for deleting the returned object.
5453
*/
5554
static QgsLayoutItemScaleBar *create( QgsLayout *layout ) SIP_FACTORY;
55+
QgsLayoutSize minimumSize() const override;
5656

5757
/**
5858
* Returns the number of segments included in the scalebar.
@@ -402,19 +402,11 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
402402
*/
403403
QString style() const;
404404

405-
/**
406-
* Sets the scale bar box size to a size suitable for the scalebar content.
407-
*/
408-
void adjustBoxSize();
409-
410405
/**
411406
* Adjusts the scale bar box size and updates the item.
412407
*/
413408
void update();
414-
#if 0 //TODO
415-
//overridden to apply minimum size
416-
void setSceneRect( const QRectF &rectangle ) override;
417-
#endif
409+
418410
void refreshDataDefinedProperty( const QgsLayoutObject::DataDefinedProperty property = QgsLayoutObject::AllProperties ) override;
419411

420412
protected:
@@ -436,13 +428,13 @@ class CORE_EXPORT QgsLayoutItemScaleBar: public QgsLayoutItem
436428
QgsScaleBarSettings mSettings;
437429

438430
//! Scalebar style
439-
QgsScaleBarRenderer *mStyle = nullptr;
431+
std::unique_ptr< QgsScaleBarRenderer > mStyle;
440432

441433
//! Width of a segment (in mm)
442-
double mSegmentMillimeters;
434+
double mSegmentMillimeters = 0.0;
443435

444436
//! Moves scalebar position to the left / right depending on alignment and change in item width
445-
void correctXPositionAlignment( double width, double widthAfter );
437+
void correctXPositionAlignment( double widthMM, double widthAfterMM );
446438

447439
//! Calculates with of a segment in mm and stores it in mSegmentMillimeters
448440
void refreshSegmentMillimeters();

‎src/ui/layout/qgslayoutscalebarwidgetbase.ui

Lines changed: 778 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.