File: test_ui_panel.py

package info (click to toggle)
python-traitsui 8.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 18,232 kB
  • sloc: python: 58,982; makefile: 113
file content (287 lines) | stat: -rw-r--r-- 8,844 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

import unittest

from traits.api import Enum, HasTraits, Int, Str, Instance
from traitsui.api import HGroup, Item, Group, VGroup, View
from traitsui.menu import ToolBar, Action
from traitsui.testing.api import Index, IsVisible, MouseClick, UITester
from traitsui.tests._tools import (
    create_ui,
    requires_toolkit,
    process_cascade_events,
    ToolkitName,
)


class FooPanel(HasTraits):

    my_int = Int(2)
    my_str = Str("I am a panel/subpanel")
    toolbar = Instance(ToolBar)

    def default_traits_view(self):
        view = View(
            Item(name="my_int"),
            Item(name="my_str"),
            title="FooPanel",
            buttons=["OK", "Cancel"],
            toolbar=self.toolbar,
        )
        return view

    def _toolbar_default(self):
        return ToolBar(Action(name="Open file"))


class FooDialog(HasTraits):

    panel1 = Instance(FooPanel)
    panel2 = Instance(FooPanel)

    view = View(
        Group(Item("panel1"), Item("panel2"), layout="split", style="custom")
    )

    def _panel1_default(self):
        return FooPanel()

    def _panel2_default(self):
        return FooPanel()


class ScrollableGroupExample(HasTraits):

    my_int = Int(2)

    my_str = Str("The group is scrollable")


scrollable_group_view = View(
    Group(
        Item(name="my_int"),
        Item(name="my_str"),
        scrollable=True,
    ),
    title="FooPanel",
    kind='subpanel',
)

non_scrollable_group_view = View(
    Group(
        Item(name="my_int"),
        Item(name="my_str"),
        scrollable=False,
    ),
    title="FooPanel",
    kind='subpanel',
)

scrollable_group_box_view = View(
    Group(
        Item(name="my_int"),
        Item(name="my_str"),
        scrollable=True,
        label="Scrollable View",
        show_border=True,
    ),
    title="FooPanel",
    kind='subpanel',
)

scrollable_labelled_group_view = View(
    Group(
        Item(name="my_int"),
        Item(name="my_str"),
        scrollable=True,
        label="Scrollable View",
    ),
    title="FooPanel",
    kind='subpanel',
)


class ScrollableGroupVisibleWhen(HasTraits):
    bar = Str("bar!")
    baz = Str("Baz?")
    enabled = Enum("Yes", "No")

    def default_traits_view(self):
        view = View(
            Item("enabled"),
            HGroup(
                VGroup(
                    Item("bar"),
                    scrollable=True,
                    visible_when="enabled=='Yes'",
                    id='bar_group',
                ),
                VGroup(
                    Item("baz"),
                    scrollable=True,
                    visible_when="enabled=='No'",
                    id='baz_group',
                ),
            ),
        )
        return view


@requires_toolkit([ToolkitName.qt])
class TestUIPanel(unittest.TestCase):
    def setup_qt_dock_window(self):
        from pyface.qt import QtGui

        # set up the dock window for qt
        main_window = QtGui.QMainWindow()
        self.addCleanup(process_cascade_events)
        self.addCleanup(main_window.close)
        dock = QtGui.QDockWidget("testing", main_window)
        dock.setWidget(QtGui.QMainWindow())
        return main_window, dock

    def test_panel_has_toolbar_buttons_qt(self):
        from pyface.qt import QtGui

        _, dock = self.setup_qt_dock_window()

        # add panel
        panel = FooPanel()
        with create_ui(panel, dict(parent=dock.widget(), kind="panel")) as ui:
            dock.widget().setCentralWidget(ui.control)

            # There should be a toolbar for the panel
            self.assertIsNotNone(dock.findChild(QtGui.QToolBar))

            # There should be buttons too
            # Not searching from dock because the dock panel has buttons for
            # popping up and closing the panel
            self.assertIsNotNone(ui.control.findChild(QtGui.QPushButton))

    def test_subpanel_has_toolbar_no_buttons_qt(self):
        from pyface.qt import QtGui

        _, dock = self.setup_qt_dock_window()

        # add panel
        panel = FooPanel()
        parent = dock.widget()
        with create_ui(panel, dict(parent=parent, kind="subpanel")) as ui:
            dock.widget().setCentralWidget(ui.control)

            # There should be a toolbar for the subpanel
            self.assertIsNotNone(dock.findChild(QtGui.QToolBar))

            # Buttons should not be shown for subpanel
            # Not searching from dock because the dock panel has buttons for
            # popping up and closing the panel
            self.assertIsNone(ui.control.findChild(QtGui.QPushButton))

    def test_subpanel_no_toolbar_nor_button_in_widget(self):
        from pyface.qt import QtGui

        # FooDialog uses a QWidget to contain the panels
        # No attempt should be made for adding the toolbars
        foo_window = FooDialog()
        with create_ui(foo_window) as ui:
            # No toolbar for the dialog
            self.assertIsNone(ui.control.findChild(QtGui.QToolBar))

            # No button
            self.assertIsNone(ui.control.findChild(QtGui.QPushButton))

    # regression test for enthought/traitsui#1512
    def test_scrollable_group_visible_when(self):
        from pyface.qt import QtGui

        obj = ScrollableGroupVisibleWhen()
        tester = UITester()
        with tester.create_ui(obj) as ui:
            bar_group = tester.find_by_id(ui, 'bar_group')
            baz_group = tester.find_by_id(ui, 'baz_group')

            # for a scrollable group the GroupEditors control should be a
            # QScrollArea not just the QWidget.  We want the full area to be
            # not visible, not just the text box widget.
            self.assertIsInstance(bar_group._target.control, QtGui.QScrollArea)
            self.assertIsInstance(baz_group._target.control, QtGui.QScrollArea)

            self.assertTrue(bar_group.inspect(IsVisible()))
            self.assertFalse(baz_group.inspect(IsVisible()))

            enabled_box = tester.find_by_name(ui, 'enabled')
            baz_item = enabled_box.locate(Index(1))
            baz_item.perform(MouseClick())

            self.assertTrue(baz_group.inspect(IsVisible()))
            self.assertFalse(bar_group.inspect(IsVisible()))


@requires_toolkit([ToolkitName.qt])
class TestPanelLayout(unittest.TestCase):
    def test_scrollable_group_typical(self):
        from pyface.qt import QtGui

        example = ScrollableGroupExample()

        ui = example.edit_traits(view=scrollable_group_view)
        try:
            mainwindow = ui.control.layout().itemAt(0).widget()
            scroll_area = mainwindow.centralWidget()
            self.assertIsInstance(scroll_area, QtGui.QScrollArea)
            content = scroll_area.widget()
            self.assertEqual(type(content), QtGui.QWidget)
        finally:
            ui.dispose()

    def test_scrollable_group_box(self):
        from pyface.qt import QtGui

        example = ScrollableGroupExample()

        ui = example.edit_traits(view=scrollable_group_box_view)
        try:
            mainwindow = ui.control.layout().itemAt(0).widget()
            scroll_area = mainwindow.centralWidget()
            self.assertIsInstance(scroll_area, QtGui.QScrollArea)
            group_box = scroll_area.widget()
            self.assertIsInstance(group_box, QtGui.QGroupBox)
            self.assertEqual(group_box.title(), "Scrollable View")
        finally:
            ui.dispose()

    def test_scrollable_labelled_group(self):
        from pyface.qt import QtGui

        example = ScrollableGroupExample()

        ui = example.edit_traits(view=scrollable_labelled_group_view)
        try:
            mainwindow = ui.control.layout().itemAt(0).widget()
            scroll_area = mainwindow.centralWidget()
            self.assertIsInstance(scroll_area, QtGui.QScrollArea)
            content = scroll_area.widget()
            self.assertEqual(type(content), QtGui.QWidget)
        finally:
            ui.dispose()

    def test_non_scrollable_group_typical(self):
        from pyface.qt import QtGui

        example = ScrollableGroupExample(my_str="The group is not scrollable")

        ui = example.edit_traits(view=non_scrollable_group_view)
        try:
            mainwindow = ui.control.layout().itemAt(0).widget()
            content = mainwindow.centralWidget()
            self.assertEqual(type(content), QtGui.QWidget)
        finally:
            ui.dispose()