def test_left_space(self): layout = QTilingLayout(max_span=3) widgets = [Widget(0), Widget(1)] layout.addWidget(widgets[0], 0, 2, 1, 1) layout.addWidget(widgets[1], 2, 0, 1, 2) layout._drop_hanging_widgets(RecBlock(layout, False, 0, 0, 3, 3)) self.assertEqual(layout._get_item_position(widgets[0], False), (2, 2, 1, 1)) self.assertEqual(layout._get_item_position(widgets[1], False), (2, 0, 1, 2))
def test_right_block(self): layout = QTilingLayout(max_span=3) widgets = [Widget(0), Widget(1), Widget(2)] layout.addWidget(widgets[0], 0, 0, 2, 1) layout.addWidget(widgets[1], 0, 1, 1, 2) layout.addWidget(widgets[2], 1, 2, 1, 1) layout._fill_spaces(RecBlock(layout, False, 0, 0, 3, 3)) self.assertEqual(layout._get_item_position(widgets[0], False), (0, 0, 3, 1)) self.assertEqual(layout._get_item_position(widgets[1], False), (0, 1, 2, 2)) self.assertEqual(layout._get_item_position(widgets[2], False), (2, 1, 1, 2))
class RemoveTestCase(unittest.TestCase): # ┌───┬──────────┐ # │ │ 1 │ # │ 0 ├───┬──────┤ # │ │ 2 │ │ # ├───┴───┤ │ # │ │ 3 │ # │ 4 │ │ # │ │ │ # └───────┴──────┘ def setUp(self): self.app = QApplication([]) self.widgets = [Widget(i) for i in range(5)] self.layout = QTilingLayout(max_span=4) self.layout.addWidget(self.widgets[0], 0, 0, 2, 1) self.layout.addWidget(self.widgets[1], 0, 1, 1, 3) self.layout.addWidget(self.widgets[2], 1, 1, 1, 1) self.layout.addWidget(self.widgets[3], 1, 2, 3, 2) self.layout.addWidget(self.widgets[4], 2, 0, 2, 2) def test_horizontal_remove(self): self.layout.remove_widget(self.widgets[0]) self.assertTrue(self.widgets[0].isHidden()) self.assertEqual( self.layout._get_item_position(self.widgets[1], False), (0, 0, 2, 4)) self.assertEqual( self.layout._get_item_position(self.widgets[2], False), (2, 0, 1, 2)) self.assertEqual( self.layout._get_item_position(self.widgets[3], False), (2, 2, 2, 2)) self.assertEqual( self.layout._get_item_position(self.widgets[4], False), (3, 0, 1, 2)) def test_vertical_remove(self): self.layout.remove_widget(self.widgets[4]) self.assertTrue(self.widgets[4].isHidden()) self.assertEqual( self.layout._get_item_position(self.widgets[0], False), (0, 0, 4, 2)) self.assertEqual( self.layout._get_item_position(self.widgets[1], False), (0, 2, 2, 2)) self.assertEqual( self.layout._get_item_position(self.widgets[2], False), (2, 2, 2, 1)) self.assertEqual( self.layout._get_item_position(self.widgets[3], False), (2, 3, 2, 1))
class StateTestCase(unittest.TestCase): # ┌───┬──────────┐ # │ │ 1 │ # │ 0 ├───┬──────┤ # │ │ 2 │ │ # ├───┴───┤ │ # │ │ 3 │ # │ 4 │ │ # │ │ │ # └───────┴──────┘ def setUp(self): self.app = QApplication([]) self.widgets = [Widget(i) for i in range(5)] self.layout = QTilingLayout(max_span=4) self.layout.addWidget(self.widgets[0], 0, 0, 2, 1) self.layout.addWidget(self.widgets[1], 0, 1, 1, 3) self.layout.addWidget(self.widgets[2], 1, 1, 1, 1) self.layout.addWidget(self.widgets[3], 1, 2, 3, 2) self.layout.addWidget(self.widgets[4], 2, 0, 2, 2) def test_get_state(self): self.assertEqual(self.layout._get_state(), [(self.widgets[0], (0, 0, 2, 1)), (self.widgets[1], (0, 1, 1, 3)), (self.widgets[2], (1, 1, 1, 1)), (self.widgets[3], (1, 2, 3, 2)), (self.widgets[4], (2, 0, 2, 2))]) def test_restore_state(self): state = [(self.widgets[0], (0, 0, 3, 5)), (self.widgets[1], (3, 0, 2, 5))] self.layout._restore_state(state) self.assertEqual( self.layout._get_item_position(self.widgets[0], False), (0, 0, 3, 5)) self.assertEqual( self.layout._get_item_position(self.widgets[1], False), (3, 0, 2, 5))
class SplitsTestCase(unittest.TestCase): # ┌───────┐ # │ │ # │ 0 │ # │ │ # └───────┘ def setUp(self): self.app = QApplication([]) self.ws = [Widget(i) for i in range(2)] self.layout = QTilingLayout(max_span=2) self.layout.addWidget(self.ws[0], 0, 0, 2, 2) def test_hsplit_after(self): self.layout.hsplit(self.ws[0], self.ws[1]) self.assertEqual(self.layout._get_item_position(self.ws[0], False), (0, 0, 1, 2)) self.assertEqual(self.layout._get_item_position(self.ws[1], False), (1, 0, 1, 2)) def test_hsplit_before(self): self.layout.hsplit(self.ws[0], self.ws[1], True) self.assertEqual(self.layout._get_item_position(self.ws[0], False), (1, 0, 1, 2)) self.assertEqual(self.layout._get_item_position(self.ws[1], False), (0, 0, 1, 2)) def test_vsplit_after(self): self.layout.vsplit(self.ws[0], self.ws[1]) self.assertEqual(self.layout._get_item_position(self.ws[0], False), (0, 0, 2, 1)) self.assertEqual(self.layout._get_item_position(self.ws[1], False), (0, 1, 2, 1)) def test_vsplit_before(self): self.layout.vsplit(self.ws[0], self.ws[1], True) self.assertEqual(self.layout._get_item_position(self.ws[0], False), (0, 1, 2, 1)) self.assertEqual(self.layout._get_item_position(self.ws[1], False), (0, 0, 2, 1)) def test_split_limit(self): self.layout.hsplit(self.ws[0], self.ws[1]) with self.assertRaises(SplitLimitException): self.layout.hsplit(self.ws[0], Widget(2))
class RecBlockTestCase(unittest.TestCase): # ┌───────┬───────────────────────┐ # │ │ │ # │ │ 1 │ # │ 0 │ │ # │ ├───┬───┬───────────────┤ # │ │ 2 │░░░│ │ # ├───────┴───┴───┤ │ # │ │ │ # │ │ 3 │ # │ │ │ # │ │ │ # │ 4 │ │ # │ ├───────────────┤ # │ │ │ # │ │ 5 │ # │ │ │ # └───────────────┴───────────────┘ def setUp(self): self.app = QApplication([]) self.layout = QTilingLayout(max_span=8) self.ws = [Widget(i) for i in range(6)] self.layout.addWidget(self.ws[0], 0, 0, 3, 2) self.layout.addWidget(self.ws[1], 0, 2, 2, 6) self.layout.addWidget(self.ws[2], 2, 2, 1, 1) self.layout.addWidget(self.ws[3], 2, 4, 4, 4) self.layout.addWidget(self.ws[4], 3, 0, 5, 4) self.layout.addWidget(self.ws[5], 6, 4, 2, 4) def test_valid_block(self): p = (0, 0, 8, 8) block = RecBlock(self.layout, False, *p) self.assertEqual((block.i, block.j, block.rowspan, block.colspan), p) def test_non_rectangular_blocks(self): with self.assertRaises(NonRectangularRecBlockException): RecBlock(self.layout, False, 2, 0, 6, 8) with self.assertRaises(NonRectangularRecBlockException): RecBlock(self.layout, False, 0, 0, 3, 3) with self.assertRaises(NonRectangularRecBlockException): RecBlock(self.layout, False, 0, 2, 3, 6) with self.assertRaises(NonRectangularRecBlockException): RecBlock(self.layout, False, 2, 3, 4, 5) def test_get_widgets(self): self.assertEqual( list(RecBlock(self.layout, False, 0, 0, 8, 8).get_widgets()), [(self.ws[0], (0, 0, 3, 2)), (self.ws[1], (0, 2, 2, 6)), (self.ws[2], (2, 2, 1, 1)), (self.ws[3], (2, 4, 4, 4)), (self.ws[4], (3, 0, 5, 4)), (self.ws[5], (6, 4, 2, 4))]) def test_virtualization(self): block = RecBlock(self.layout, False, 0, 0, 8, 8) l = self.ws self.assertEqual(block._virtualize(), [(l[0], l[0], l[1], l[1], l[1], l[1], l[1], l[1]), (l[0], l[0], l[1], l[1], l[1], l[1], l[1], l[1]), (l[0], l[0], l[2], None, l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[5], l[5], l[5], l[5]), (l[4], l[4], l[4], l[4], l[5], l[5], l[5], l[5])]) def test_subset_virtualization(self): block = RecBlock(self.layout, False, 2, 4, 6, 4) self.assertEqual(block._virtualize(), [(self.ws[3], self.ws[3], self.ws[3], self.ws[3]), (self.ws[3], self.ws[3], self.ws[3], self.ws[3]), (self.ws[3], self.ws[3], self.ws[3], self.ws[3]), (self.ws[3], self.ws[3], self.ws[3], self.ws[3]), (self.ws[5], self.ws[5], self.ws[5], self.ws[5]), (self.ws[5], self.ws[5], self.ws[5], self.ws[5])]) def test_materialization(self): block = RecBlock(self.layout, False, 0, 0, 8, 8) virtual = block._virtualize() self.assertEqual( set(RecBlock._materialize_virtual_block(0, 0, virtual)), {(w, self.layout._get_item_position(w, False)) for w in self.ws}) def test_displaced_materialization(self): block = RecBlock(self.layout, False, 0, 0, 8, 8) virtual = block._virtualize() offset = (1, 2) expected = [(w, self.layout._get_item_position(w, False)) for w in self.ws] for i in range(0, len(expected)): widget, pos = expected[i] expected[i] = (widget, (pos[0] + offset[0], pos[1] + offset[1], pos[2], pos[3])) self.assertEqual( set(RecBlock._materialize_virtual_block(*offset, virtual)), set(expected)) def test_shrink_failure(self): block = RecBlock(self.layout, False, 0, 0, 8, 8) with self.assertRaises(SplitLimitException): block.displace_and_resize(0, -5) def test_displace_and_resize(self): l = self.ws block = RecBlock(self.layout, False, 0, 0, 8, 8) block.displace_and_resize(4, -4) block = RecBlock(self.layout, False, 0, 0, 8, 8) self.assertEqual(block._virtualize(), [(None, None, None, None, None, None, None, None), (None, None, None, None, None, None, None, None), (None, None, None, None, None, None, None, None), (None, None, None, None, None, None, None, None), (l[0], l[0], l[1], l[1], l[1], l[1], l[1], l[1]), (l[0], l[0], l[2], None, l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[5], l[5], l[5], l[5])]) block.displace_and_resize(-2, 2) block = RecBlock(self.layout, False, 0, 0, 8, 8) self.assertEqual(block._virtualize(), [(None, None, None, None, None, None, None, None), (None, None, None, None, None, None, None, None), (l[0], l[0], l[1], l[1], l[1], l[1], l[1], l[1]), (l[0], l[0], l[1], l[1], l[1], l[1], l[1], l[1]), (l[0], l[0], l[2], None, l[3], l[3], l[3], l[3]), (l[0], l[0], l[2], None, l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[3], l[3], l[3], l[3]), (l[4], l[4], l[4], l[4], l[5], l[5], l[5], l[5])])
class TransposedMethodsTestCase(unittest.TestCase): # ┌───┬──────────┐ # │ │ 1 │ # │ 0 ├───┬──────┤ # │ │ 2 │ │ # ├───┴───┤ │ # │ │ 3 │ # │ 4 │ │ # │ │ │ # └───────┴──────┘ def setUp(self): self.app = QApplication([]) self.widgets = [Widget(i) for i in range(5)] self.layout = QTilingLayout(max_span=4) self.layout.addWidget(self.widgets[0], 0, 0, 2, 1) self.layout.addWidget(self.widgets[1], 0, 1, 1, 3) self.layout.addWidget(self.widgets[2], 1, 1, 1, 1) self.layout.addWidget(self.widgets[3], 1, 2, 3, 2) self.layout.addWidget(self.widgets[4], 2, 0, 2, 2) def test_add_widget(self): widget = self.widgets[3] self.layout.removeWidget(widget) self.layout._add_widget(widget, 1, 2, 3, 2, False) self.assertEqual( self.layout.getItemPosition(self.layout.indexOf(widget)), (1, 2, 3, 2)) self.layout.removeWidget(widget) self.layout.removeWidget(self.widgets[2]) self.layout.removeWidget(self.widgets[4]) self.layout._add_widget(widget, 1, 2, 3, 2, True) self.assertEqual( self.layout.getItemPosition(self.layout.indexOf(widget)), (2, 1, 2, 3)) self.layout.removeWidget(widget) def test_failed_add_widget(self): widget = Widget('new') with self.assertRaises(WidgetOverlapException): self.layout._add_widget(widget, 1, 1, 2, 2, False) with self.assertRaises(WidgetOverlapException): self.layout._add_widget(widget, -1, -1, 2, 2, False) def test_item_at_position(self): for i in range(self.layout.rowCount()): for j in range(self.layout.columnCount()): self.assertEqual(self.layout.itemAtPosition(i, j), self.layout._item_at_position(i, j, False)) self.assertEqual(self.layout.itemAtPosition(j, i), self.layout._item_at_position(i, j, True)) def test_failed_item_at_position(self): with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(-1, 0, False) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(0, -1, False) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(self.layout.max_span, 0, False) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(0, self.layout.max_span, False) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(-1, 0, True) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(0, -1, True) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(self.layout.max_span, 0, True) with self.assertRaises(PointOutsideGridException): self.layout._item_at_position(0, self.layout.max_span, True) def test_get_item_position(self): for widget in self.widgets: pos = self.layout.getItemPosition(self.layout.indexOf(widget)) self.assertEqual(pos, self.layout._get_item_position(widget, False)) self.assertEqual((pos[1], pos[0], pos[3], pos[2]), self.layout._get_item_position(widget, True)) self.assertRaises(WidgetNotInLayoutException, self.layout._get_item_position, QWidget(), False) self.assertRaises(WidgetNotInLayoutException, self.layout._get_item_position, QWidget(), True)