def testRectWithFrame(self): layout = QgsLayout(QgsProject.instance()) item = QgsLayoutItemMap(layout) item.attemptMove(QgsLayoutPoint(6, 10, QgsUnitTypes.LayoutMillimeters)) item.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) item.setFrameEnabled(False) self.assertEqual(item.rectWithFrame(), QRectF(0, 0, 18, 12)) item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutMillimeters)) item.setFrameEnabled(False) self.assertEqual(item.rectWithFrame(), QRectF(0, 0, 18, 12)) item.setFrameEnabled(True) self.assertEqual(item.rectWithFrame(), QRectF(-5.0, -5.0, 28.0, 22.0)) item.setFrameStrokeWidth(QgsLayoutMeasurement(10, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item.rectWithFrame(), QRectF(-50.0, -50.0, 118.0, 112.0))
def testResize(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(7, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize(QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove(QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize(QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) view.resizeSelectedItems(QgsLayoutAligner.ResizeNarrowest) item1.setSelected(True) item2.setSelected(True) item3.setSelected(True) view.resizeSelectedItems(QgsLayoutAligner.ResizeNarrowest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(10, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.0, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeWidest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(18, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeShortest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 0.9, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeTallest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() item2.attemptResize(QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters)) view.resizeSelectedItems(QgsLayoutAligner.ResizeToSquare) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))
def testLayoutItemAt(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(6, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove(QgsLayoutPoint(8, 12, QgsUnitTypes.LayoutMillimeters)) item3.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) item3.setLocked(True) l.addItem(item3) self.assertIsNone(l.layoutItemAt(QPointF(0, 0))) self.assertIsNone(l.layoutItemAt(QPointF(100, 100))) self.assertEqual(l.layoutItemAt(QPointF(5, 9)), item1) self.assertEqual(l.layoutItemAt(QPointF(25, 23)), item3) self.assertIsNone(l.layoutItemAt(QPointF(25, 23), True)) self.assertEqual(l.layoutItemAt(QPointF(7, 11)), item2) self.assertEqual(l.layoutItemAt(QPointF(9, 13)), item3) self.assertEqual(l.layoutItemAt(QPointF(9, 13), True), item2) self.assertEqual(l.layoutItemAt(QPointF(9, 13), item3), item2) self.assertEqual(l.layoutItemAt(QPointF(9, 13), item2), item1) self.assertIsNone(l.layoutItemAt(QPointF(9, 13), item1)) item2.setLocked(True) self.assertEqual(l.layoutItemAt(QPointF(9, 13), item3, True), item1)
def testSnapPointsToItems(self): p = QgsProject() l = QgsLayout(p) page = QgsLayoutItemPage(l) page.setPageSize('A4') #l.pageCollection().addPage(page) s = QgsLayoutSnapper(l) guides = l.guides() s.setSnapToItems(True) s.setSnapTolerance(1) # no items point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, []) self.assertFalse(snapped) line = QGraphicsLineItem() line.setVisible(True) point, snapped = s.snapPointsToItems([0.5], Qt.Horizontal, 1, [], line) self.assertFalse(line.isVisible()) guides.addGuide(QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(1), page)) # add an item item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) point, snapped = s.snapPointsToItems([3.5], Qt.Horizontal, 1, [], line) self.assertTrue(snapped) self.assertEqual(point, 0.5) self.assertTrue(line.isVisible()) point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, -0.5) point, snapped = s.snapPointsToItems([4.6, 4.5], Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, -0.5) point, snapped = s.snapPointsToItems([4.6, 4.5, 3.7], Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertAlmostEqual(point, 0.3, 5) # ignoring item point, snapped = s.snapPointsToItems([4.5], Qt.Horizontal, 1, [item1]) self.assertFalse(snapped) # outside tolerance point, snapped = s.snapPointsToItems([5.5], Qt.Horizontal, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # snap to center point, snapped = s.snapPointsToItems([12.5], Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, 0.5) # snap to right point, snapped = s.snapPointsToItems([22.5], Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, -0.5) #snap to top point, snapped = s.snapPointsToItems([7.5], Qt.Vertical, 1, [], line) self.assertTrue(snapped) self.assertEqual(point, 0.5) self.assertTrue(line.isVisible()) point, snapped = s.snapPointsToItems([8.5], Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, -0.5) # outside tolerance point, snapped = s.snapPointsToItems([5.5], Qt.Vertical, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # snap to center point, snapped = s.snapPointsToItems([13.5], Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, 0.5) # snap to bottom point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, -0.5) # snapping off s.setSnapToItems(False) line.setVisible(True) point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # with different pixel scale s.setSnapToItems(True) point, snapped = s.snapPointsToItems([20.5], Qt.Vertical, 3, []) self.assertFalse(snapped)
def testAlign(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(6, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize(QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove(QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize(QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignLeft) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(4, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignHCenter) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(8, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignRight) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(12, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignTop) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(12, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignVCenter) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(12, 11.5, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.alignItems(l, [item1, item2, item3], QgsLayoutAligner.AlignBottom) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(12, 15, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
def testDistribute(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize(QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(7, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize(QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove(QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize(QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeLeft) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeHCenter) self.assertAlmostEqual(item1.positionWithUnits().x(), 5.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeRight) self.assertAlmostEqual(item1.positionWithUnits().x(), 3.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeTop) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 10.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeVCenter) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 12.5, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) QgsLayoutAligner.distributeItems(l, [item1, item2, item3], QgsLayoutAligner.DistributeBottom) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 15.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual(item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
def testSnapPointToItems(self): p = QgsProject() l = QgsLayout(p) page = QgsLayoutItemPage(l) page.setPageSize('A4') #l.pageCollection().addPage(page) s = QgsLayoutSnapper(l) guides = l.guides() s.setSnapToItems(True) s.setSnapTolerance(1) # no items point, snapped = s.snapPointToItems(0.5, Qt.Horizontal, 1, []) self.assertFalse(snapped) line = QGraphicsLineItem() line.setVisible(True) point, snapped = s.snapPointToItems(0.5, Qt.Horizontal, 1, [], line) self.assertFalse(line.isVisible()) guides.addGuide( QgsLayoutGuide(Qt.Vertical, QgsLayoutMeasurement(1), page)) # add an item item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize( QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) point, snapped = s.snapPointToItems(3.5, Qt.Horizontal, 1, [], line) self.assertTrue(snapped) self.assertEqual(point, 4) self.assertTrue(line.isVisible()) point, snapped = s.snapPointToItems(4.5, Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, 4) # ignoring item point, snapped = s.snapPointToItems(4.5, Qt.Horizontal, 1, [item1]) self.assertFalse(snapped) # outside tolerance point, snapped = s.snapPointToItems(5.5, Qt.Horizontal, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # snap to center point, snapped = s.snapPointToItems(12.5, Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, 13) # snap to right point, snapped = s.snapPointToItems(22.5, Qt.Horizontal, 1, []) self.assertTrue(snapped) self.assertEqual(point, 22) #snap to top point, snapped = s.snapPointToItems(7.5, Qt.Vertical, 1, [], line) self.assertTrue(snapped) self.assertEqual(point, 8) self.assertTrue(line.isVisible()) point, snapped = s.snapPointToItems(8.5, Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, 8) # outside tolerance point, snapped = s.snapPointToItems(5.5, Qt.Vertical, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # snap to center point, snapped = s.snapPointToItems(13.5, Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, 14) # snap to bottom point, snapped = s.snapPointToItems(20.5, Qt.Vertical, 1, []) self.assertTrue(snapped) self.assertEqual(point, 20) # snapping off s.setSnapToItems(False) line.setVisible(True) point, snapped = s.snapPointToItems(20.5, Qt.Vertical, 1, [], line) self.assertFalse(snapped) self.assertFalse(line.isVisible()) # with different pixel scale s.setSnapToItems(True) point, snapped = s.snapPointToItems(20.5, Qt.Vertical, 3, []) self.assertFalse(snapped)
def testResize(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize( QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(7, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize( QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove( QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize( QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) view.resizeSelectedItems(QgsLayoutAligner.ResizeNarrowest) item1.setSelected(True) item2.setSelected(True) item3.setSelected(True) view.resizeSelectedItems(QgsLayoutAligner.ResizeNarrowest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(10, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.0, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeWidest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(18, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeShortest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 0.9, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() view.resizeSelectedItems(QgsLayoutAligner.ResizeTallest) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 16, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 16, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.undoStack().stack().undo() item2.attemptResize( QgsLayoutSize(10, 19, QgsUnitTypes.LayoutMillimeters)) view.resizeSelectedItems(QgsLayoutAligner.ResizeToSquare) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 18, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(19, 19, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.8, QgsUnitTypes.LayoutCentimeters))
def testDistribute(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize( QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(7, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize( QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove( QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize( QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) view.distributeSelectedItems(QgsLayoutAligner.DistributeLeft) item1.setSelected(True) item2.setSelected(True) item3.setSelected(True) view.distributeSelectedItems(QgsLayoutAligner.DistributeLeft) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.distributeSelectedItems(QgsLayoutAligner.DistributeHCenter) self.assertAlmostEqual(item1.positionWithUnits().x(), 5.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.distributeSelectedItems(QgsLayoutAligner.DistributeRight) self.assertAlmostEqual(item1.positionWithUnits().x(), 3.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().x(), 6.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().x(), 0.8, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.distributeSelectedItems(QgsLayoutAligner.DistributeTop) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 10.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.distributeSelectedItems(QgsLayoutAligner.DistributeVCenter) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 12.5, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.distributeSelectedItems(QgsLayoutAligner.DistributeBottom) self.assertAlmostEqual(item1.positionWithUnits().y(), 8.0, 3) self.assertEqual(item1.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item2.positionWithUnits().y(), 15.0, 3) self.assertEqual(item2.positionWithUnits().units(), QgsUnitTypes.LayoutMillimeters) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertAlmostEqual(item3.positionWithUnits().y(), 1.2, 3) self.assertEqual(item3.positionWithUnits().units(), QgsUnitTypes.LayoutCentimeters) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))
def testAlign(self): p = QgsProject() l = QgsLayout(p) # add some items item1 = QgsLayoutItemMap(l) item1.attemptMove(QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) item1.attemptResize( QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) l.addItem(item1) item2 = QgsLayoutItemMap(l) item2.attemptMove(QgsLayoutPoint(6, 10, QgsUnitTypes.LayoutMillimeters)) item2.attemptResize( QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) l.addItem(item2) item3 = QgsLayoutItemMap(l) item3.attemptMove( QgsLayoutPoint(0.8, 1.2, QgsUnitTypes.LayoutCentimeters)) item3.attemptResize( QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) l.addItem(item3) view = QgsLayoutView() view.setCurrentLayout(l) view.alignSelectedItems(QgsLayoutAligner.AlignLeft) item1.setSelected(True) item2.setSelected(True) item3.setSelected(True) view.alignSelectedItems(QgsLayoutAligner.AlignLeft) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(4, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.alignSelectedItems(QgsLayoutAligner.AlignHCenter) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(8, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.alignSelectedItems(QgsLayoutAligner.AlignRight) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item2.positionWithUnits(), QgsLayoutPoint(12, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 1.2, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.alignSelectedItems(QgsLayoutAligner.AlignTop) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.positionWithUnits(), QgsLayoutPoint(12, 8, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.alignSelectedItems(QgsLayoutAligner.AlignVCenter) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 10, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item2.positionWithUnits(), QgsLayoutPoint(12, 11.5, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters)) view.alignSelectedItems(QgsLayoutAligner.AlignBottom) self.assertEqual(item1.positionWithUnits(), QgsLayoutPoint(4, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item1.sizeWithUnits(), QgsLayoutSize(18, 12, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item2.positionWithUnits(), QgsLayoutPoint(12, 15, QgsUnitTypes.LayoutMillimeters)) self.assertEqual(item2.sizeWithUnits(), QgsLayoutSize(10, 9, QgsUnitTypes.LayoutMillimeters)) self.assertEqual( item3.positionWithUnits(), QgsLayoutPoint(0.4, 0.8, QgsUnitTypes.LayoutCentimeters)) self.assertEqual( item3.sizeWithUnits(), QgsLayoutSize(1.8, 1.6, QgsUnitTypes.LayoutCentimeters))