Skip to content

Commit d7e179c

Browse files
committedDec 17, 2017
Add unit test for resizing pages
1 parent 082733a commit d7e179c

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
 

‎tests/src/python/test_qgslayoutpagecollection.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@
2222
QgsLayoutPoint,
2323
QgsLayoutObject,
2424
QgsProject,
25+
QgsMargins,
2526
QgsProperty,
27+
QgsLayoutGuide,
28+
QgsLayoutMeasurement,
2629
QgsLayoutPageCollection,
2730
QgsSimpleFillSymbolLayer,
31+
QgsLayoutItemShape,
2832
QgsFillSymbol,
2933
QgsReadWriteContext)
3034
from qgis.PyQt.QtCore import Qt, QCoreApplication, QEvent, QPointF, QRectF
@@ -741,6 +745,68 @@ def testUndoRedo(self):
741745
self.assertEqual(collection.pageCount(), 1)
742746
self.assertEqual(collection.page(0).pageSize().width(), 148)
743747

748+
def testResizeToContents(self):
749+
p = QgsProject()
750+
l = QgsLayout(p)
751+
752+
shape1 = QgsLayoutItemShape(l)
753+
shape1.attemptResize(QgsLayoutSize(90, 50))
754+
shape1.attemptMove(QgsLayoutPoint(90, 50))
755+
shape1.setItemRotation(45, False)
756+
l.addLayoutItem(shape1)
757+
shape2 = QgsLayoutItemShape(l)
758+
shape2.attemptResize(QgsLayoutSize(110, 50))
759+
shape2.attemptMove(QgsLayoutPoint(100, 150), True, False, 0)
760+
l.addLayoutItem(shape2)
761+
shape3 = QgsLayoutItemShape(l)
762+
l.addLayoutItem(shape3)
763+
shape3.attemptResize(QgsLayoutSize(50, 100))
764+
shape3.attemptMove(QgsLayoutPoint(210, 250), True, False, 0)
765+
shape4 = QgsLayoutItemShape(l)
766+
l.addLayoutItem(shape4)
767+
shape4.attemptResize(QgsLayoutSize(50, 30))
768+
shape4.attemptMove(QgsLayoutPoint(10, 340), True, False, 0)
769+
shape4.setVisibility(False)
770+
771+
# resize with no existing pages
772+
l.pageCollection().resizeToContents(QgsMargins(1, 2, 3, 4), QgsUnitTypes.LayoutCentimeters)
773+
self.assertEqual(l.pageCollection().pageCount(), 1)
774+
775+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().width(), 290.3, 2)
776+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().height(), 380.36, 2)
777+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters)
778+
779+
self.assertAlmostEqual(shape1.positionWithUnits().x(), 90.15, 2)
780+
self.assertAlmostEqual(shape1.positionWithUnits().y(), 20.21, 2)
781+
self.assertAlmostEqual(shape2.positionWithUnits().x(), 100.15, 2)
782+
self.assertAlmostEqual(shape2.positionWithUnits().y(), 120.21, 2)
783+
self.assertAlmostEqual(shape3.positionWithUnits().x(), 210.15, 2)
784+
self.assertAlmostEqual(shape3.positionWithUnits().y(), 220.21, 2)
785+
self.assertAlmostEqual(shape4.positionWithUnits().x(), 10.15, 2)
786+
self.assertAlmostEqual(shape4.positionWithUnits().y(), 310.21, 2)
787+
788+
# add a second page
789+
page2 = QgsLayoutItemPage(l)
790+
page2.setPageSize("A4", QgsLayoutItemPage.Landscape)
791+
l.pageCollection().addPage(page2)
792+
793+
# add some guides
794+
g1 = QgsLayoutGuide(Qt.Horizontal, QgsLayoutMeasurement(2.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
795+
l.guides().addGuide(g1)
796+
g2 = QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(4.5, QgsUnitTypes.LayoutCentimeters), l.pageCollection().page(0))
797+
l.guides().addGuide(g2)
798+
799+
# second page should be removed
800+
l.pageCollection().resizeToContents(QgsMargins(0, 0, 0, 0), QgsUnitTypes.LayoutCentimeters)
801+
self.assertEqual(l.pageCollection().pageCount(), 1)
802+
803+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().width(), 250.3, 2)
804+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().height(), 320.36, 2)
805+
self.assertAlmostEqual(l.pageCollection().page(0).sizeWithUnits().units(), QgsUnitTypes.LayoutMillimeters)
806+
807+
self.assertAlmostEqual(g1.position().length(), 0.5, 2)
808+
self.assertAlmostEqual(g2.position().length(), 3.5, 2)
809+
744810

745811
if __name__ == '__main__':
746812
unittest.main()

0 commit comments

Comments
 (0)