Skip to content

Commit 0f93e55

Browse files
committed
[layouts] Fix legends do not respect reference point when auto sizing
Fixes #18269
1 parent 75f91e5 commit 0f93e55

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/core/layout/qgslayoutitemlegend.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,9 @@ void QgsLayoutItemLegend::adjustBoxSize()
182182
QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
183183
if ( size.isValid() )
184184
{
185-
QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
185+
QgsLayoutSize newSize = mLayout->convertFromLayoutUnits( size, sizeWithUnits().units() );
186186
//set new rect, respecting position mode and data defined size/position
187-
attemptSetSceneRect( targetRect );
187+
attemptResize( newSize );
188188
}
189189
}
190190

tests/src/python/test_qgslayoutlegend.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
QgsProject,
2727
QgsLayoutObject,
2828
QgsProperty,
29-
QgsLayoutMeasurement)
29+
QgsLayoutMeasurement,
30+
QgsLayoutItem,
31+
QgsLayoutPoint,
32+
QgsLayoutSize)
3033
from qgis.testing import (start_app,
3134
unittest
3235
)
@@ -84,6 +87,24 @@ def testInitialSizeSymbolMapUnits(self):
8487
result, message = checker.testLayout()
8588
self.assertTrue(result, message)
8689

90+
# resize with non-top-left reference point
91+
legend.setResizeToContents(False)
92+
legend.setReferencePoint(QgsLayoutItem.LowerRight)
93+
legend.attemptMove(QgsLayoutPoint(120, 90))
94+
legend.attemptResize(QgsLayoutSize(50, 60))
95+
96+
self.assertEqual(legend.positionWithUnits().x(), 120.0)
97+
self.assertEqual(legend.positionWithUnits().y(), 90.0)
98+
self.assertAlmostEqual(legend.pos().x(), 70, -1)
99+
self.assertAlmostEqual(legend.pos().y(), 30, -1)
100+
101+
legend.setResizeToContents(True)
102+
legend.updateLegend()
103+
self.assertEqual(legend.positionWithUnits().x(), 120.0)
104+
self.assertEqual(legend.positionWithUnits().y(), 90.0)
105+
self.assertAlmostEqual(legend.pos().x(), 91, -1)
106+
self.assertAlmostEqual(legend.pos().y(), 71, -1)
107+
87108
QgsProject.instance().removeMapLayers([point_layer.id()])
88109

89110
def testResizeWithMapContent(self):

0 commit comments

Comments
 (0)