Skip to content

Commit

Permalink
[layouts] Fix legends do not respect reference point when auto sizing
Browse files Browse the repository at this point in the history
Fixes #18269
  • Loading branch information
nyalldawson committed Mar 1, 2018
1 parent 75f91e5 commit 0f93e55
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/core/layout/qgslayoutitemlegend.cpp
Expand Up @@ -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 );
}
}

Expand Down
23 changes: 22 additions & 1 deletion tests/src/python/test_qgslayoutlegend.py
Expand Up @@ -26,7 +26,10 @@
QgsProject,
QgsLayoutObject,
QgsProperty,
QgsLayoutMeasurement)
QgsLayoutMeasurement,
QgsLayoutItem,
QgsLayoutPoint,
QgsLayoutSize)
from qgis.testing import (start_app,
unittest
)
Expand Down Expand Up @@ -84,6 +87,24 @@ def testInitialSizeSymbolMapUnits(self):
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):
Expand Down

0 comments on commit 0f93e55

Please sign in to comment.