Skip to content
Browse files
When reading scalebar from XML re-apply stored size after changing st…
…yle (#36173)

This PR fixes the issue that when restoring a numeric scale bar layout item from an XML (such as when opening a saved project or pasting a copied item) the height of the scale bar layout item is set to a value larger than the one stored in the XML (see #26931 and #32379).

The issue arises from the fact that restoring a scale bar layout item initially creates a QgsLayoutItemScaleBar with the default style of Single Box, which has larger minimum dimensions as calculated by calculateBoxSize. The call to attemptResize in QgsLayoutItem::readXml, which tries to apply the dimensions stored in the XML, cannot set the dimensions of the scale bar layout item to be smaller than these minimum dimensions, so the dimensions are only applied correctly if they happen to be larger.

To fix this issue, this PR re-applies the stored size by calling attemptResize in QgsLayoutItemScaleBar::readPropertiesFromElement after the scale bar style has been set.

Additionally, the width of the numeric scale bar was not restored correctly because QgsLayoutItemScaleBar::finalizeRestoreFromXml calls QgsLayoutItemScaleBar::updateScale, which calls resizeToMinimumWidth(). This PR adds a check to skip resizeToMinimumWidth when the style is Numeric.

Fixes #26931, fixes #32379
  • Loading branch information
jdugge committed May 5, 2020
1 parent d3b0b89 commit 0888d377e6f8d3010aac3d55064228468cc5ba74
Showing with 8 additions and 1 deletion.
  1. +8 −1 src/core/layout/qgslayoutitemscalebar.cpp
@@ -577,7 +577,11 @@ void QgsLayoutItemScaleBar::update()
void QgsLayoutItemScaleBar::updateScale()
//Don't adjust box size for numeric scale bars:
if ( mStyle && mStyle->id() != QLatin1String( "Numeric" ) )

@@ -1063,6 +1067,9 @@ bool QgsLayoutItemScaleBar::readPropertiesFromElement( const QDomElement &itemEl
setStyle( itemElem.attribute( QStringLiteral( "style" ), QString() ) );

//call attemptResize after setStyle to ensure the appropriate size limitations are applied
attemptResize( QgsLayoutSize::decodeSize( itemElem.attribute( QStringLiteral( "size" ) ) ) );

if ( itemElem.attribute( QStringLiteral( "unitType" ) ).isEmpty() )
QgsUnitTypes::DistanceUnit u = QgsUnitTypes::DistanceUnknownUnit;

0 comments on commit 0888d37

Please sign in to comment.