[layouts] Fix legends do not respect reference point when auto sizing

Fixes #18269
This commit is contained in:
Nyall Dawson 2018-03-01 16:08:06 +10:00
parent 75f91e53c5
commit 0f93e55fce
2 changed files with 24 additions and 3 deletions

View File

@ -182,9 +182,9 @@ void QgsLayoutItemLegend::adjustBoxSize()
QgsDebugMsg( QString( "width = %1 height = %2" ).arg( size.width() ).arg( size.height() ) );
if ( size.isValid() )
{
QRectF targetRect = QRectF( pos().x(), pos().y(), size.width(), size.height() );
QgsLayoutSize newSize = mLayout->convertFromLayoutUnits( size, sizeWithUnits().units() );
//set new rect, respecting position mode and data defined size/position
attemptSetSceneRect( targetRect );
attemptResize( newSize );
}
}

View File

@ -26,7 +26,10 @@ from qgis.core import (QgsLayoutItemLegend,
QgsProject,
QgsLayoutObject,
QgsProperty,
QgsLayoutMeasurement)
QgsLayoutMeasurement,
QgsLayoutItem,
QgsLayoutPoint,
QgsLayoutSize)
from qgis.testing import (start_app,
unittest
)
@ -84,6 +87,24 @@ class TestQgsLayoutItemLegend(unittest.TestCase, LayoutItemTestCase):
result, message = checker.testLayout()
self.assertTrue(result, message)
# resize with non-top-left reference point
legend.setResizeToContents(False)
legend.setReferencePoint(QgsLayoutItem.LowerRight)
legend.attemptMove(QgsLayoutPoint(120, 90))
legend.attemptResize(QgsLayoutSize(50, 60))
self.assertEqual(legend.positionWithUnits().x(), 120.0)
self.assertEqual(legend.positionWithUnits().y(), 90.0)
self.assertAlmostEqual(legend.pos().x(), 70, -1)
self.assertAlmostEqual(legend.pos().y(), 30, -1)
legend.setResizeToContents(True)
legend.updateLegend()
self.assertEqual(legend.positionWithUnits().x(), 120.0)
self.assertEqual(legend.positionWithUnits().y(), 90.0)
self.assertAlmostEqual(legend.pos().x(), 91, -1)
self.assertAlmostEqual(legend.pos().y(), 71, -1)
QgsProject.instance().removeMapLayers([point_layer.id()])
def testResizeWithMapContent(self):