|
22 | 22 | QgsLayoutPoint,
|
23 | 23 | QgsLayoutObject,
|
24 | 24 | QgsProject,
|
| 25 | + QgsMargins, |
25 | 26 | QgsProperty,
|
| 27 | + QgsLayoutGuide, |
| 28 | + QgsLayoutMeasurement, |
26 | 29 | QgsLayoutPageCollection,
|
27 | 30 | QgsSimpleFillSymbolLayer,
|
| 31 | + QgsLayoutItemShape, |
28 | 32 | QgsFillSymbol,
|
29 | 33 | QgsReadWriteContext)
|
30 | 34 | from qgis.PyQt.QtCore import Qt, QCoreApplication, QEvent, QPointF, QRectF
|
@@ -741,6 +745,68 @@ def testUndoRedo(self):
|
741 | 745 | self.assertEqual(collection.pageCount(), 1)
|
742 | 746 | self.assertEqual(collection.page(0).pageSize().width(), 148)
|
743 | 747 |
|
| 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 | + |
744 | 810 |
|
745 | 811 | if __name__ == '__main__':
|
746 | 812 | unittest.main()
|
0 commit comments