Exemple #1
0
 def _perform_operation(self, max_span, positions, widget, operation):
     self.trace.append((positions[widget], operation))
     layout = QTilingLayout(max_span=max_span)
     for i in range(len(positions)):
         layout._add_widget(self.widgets[i], *positions[i], False)
     try:
         if operation == 'hsplit':
             layout.hsplit(self.widgets[widget], self.new_widget)
         elif operation == 'vsplit':
             layout.vsplit(self.widgets[widget], self.new_widget)
         else:
             layout.remove_widget(self.widgets[widget])
     except SplitLimitException:
         return
     except Exception as e:
         for entry in self.trace:
             print(entry)
         raise e
     new_positions = [
         layout.getItemPosition(i) for i in range(layout.count())
     ]
     new_operations = (('vsplit', 'hsplit') if layout.count() == 1 else
                       ('vsplit', 'hsplit', 'remove'))
     self._perform_operation(max_span, new_positions,
                             random.randrange(len(new_positions)),
                             random.choice(new_operations))
Exemple #2
0
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)