1from AnyQt.QtWidgets import QAction, QToolButton
2
3from .. import test
4from ..toolgrid import ToolGrid
5
6
7class TestToolGrid(test.QAppTestCase):
8    def test_tool_grid(self):
9        w = ToolGrid()
10
11        w.show()
12        self.app.processEvents()
13
14        def buttonsOrderedVisual():
15            # Process layout events so the buttons have right positions
16            self.app.processEvents()
17            buttons = w.findChildren(QToolButton)
18            return list(sorted(buttons, key=lambda b: (b.y(), b.x())))
19
20        def buttonsOrderedLogical():
21            return list(map(w.buttonForAction, w.actions()))
22
23        def assertOrdered():
24            self.assertSequenceEqual(buttonsOrderedLogical(),
25                                     buttonsOrderedVisual())
26
27        action_a = QAction("A", w)
28        action_b = QAction("B", w)
29        action_c = QAction("C", w)
30        action_d = QAction("D", w)
31
32        w.addAction(action_b)
33        w.insertAction(0, action_a)
34        self.assertSequenceEqual(w.actions(),
35                                 [action_a, action_b])
36        assertOrdered()
37
38        w.addAction(action_d)
39        w.insertAction(action_d, action_c)
40
41        self.assertSequenceEqual(w.actions(),
42                                 [action_a, action_b, action_c, action_d])
43        assertOrdered()
44
45        w.removeAction(action_c)
46        self.assertSequenceEqual(w.actions(),
47                                 [action_a, action_b, action_d])
48
49        assertOrdered()
50
51        w.removeAction(action_a)
52        self.assertSequenceEqual(w.actions(),
53                                 [action_b, action_d])
54
55        assertOrdered()
56
57        w.insertAction(0, action_a)
58        self.assertSequenceEqual(w.actions(),
59                                 [action_a, action_b, action_d])
60
61        assertOrdered()
62
63        w.setColumnCount(2)
64        self.assertSequenceEqual(w.actions(),
65                                 [action_a, action_b, action_d])
66
67        assertOrdered()
68
69        w.insertAction(2, action_c)
70        self.assertSequenceEqual(w.actions(),
71                                 [action_a, action_b, action_c, action_d])
72        assertOrdered()
73
74        w.clear()
75        # test no 'before' action edge case
76        w.insertAction(0, action_a)
77        self.assertIs(action_a, w.actions()[0])
78        w.insertAction(1, action_b)
79        self.assertSequenceEqual(w.actions(),
80                                 [action_a, action_b])
81
82        w.clear()
83        w.setActions([action_a, action_b, action_c, action_d])
84        self.assertSequenceEqual(w.actions(),
85                                 [action_a, action_b, action_c, action_d])
86        assertOrdered()
87
88        triggered_actions = []
89
90        def p(action):
91            print(action.text())
92
93        w.actionTriggered.connect(p)
94        w.actionTriggered.connect(triggered_actions.append)
95        action_a.trigger()
96
97        w.show()
98        self.qWait()
99