File: base_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 (191 lines) | stat: -rw-r--r-- 6,063 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
# (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!

from pyface.action.api import ActionController
from traits.api import Any, Instance
from traitsui.menu import Action

# Set of all predefined system button names:
SystemButtons = {"Undo", "Redo", "Apply", "Revert", "OK", "Cancel", "Help"}


class BasePanel(ActionController):
    """Base class for Traits UI panels and dialog boxes.

    Concrete subclasses of BasePanel are the Python-side owners of the
    top-level toolkit control for a UI.  They also implement the Pyface
    ActionController API for menu and toolbar action handling.
    """

    #: The top-level toolkit control of the UI.
    control = Any()

    #: The UI instance for the view.
    ui = Instance("traitsui.ui.UI")

    def default_icon(self):
        """Return a default icon for a TraitsUI dialog."""
        from pyface.image_resource import ImageResource

        return ImageResource("frame.png")

    def check_button(self, buttons, action):
        """Adds *action* to the system buttons list for this dialog, if it is
        not already in the list.
        """
        name = action.name
        for button in buttons:
            if self.is_button(button, name):
                return
        buttons.append(action)

    def is_button(self, action, name):
        """Returns whether a specified action button is a system button."""
        if isinstance(action, str):
            return action == name
        return action.name == name

    def coerce_button(self, action):
        """Coerces a string to an Action if necessary."""
        if isinstance(action, str):
            return Action(
                name=action, action="" if action in SystemButtons else "?"
            )
        return action

    # Button handlers --------------------------------------------------------

    def _on_undo(self, event=None):
        """Handles an "Undo" change request."""
        self.ui.history.undo()

    def _on_redo(self, event=None):
        """Handles a "Redo" change request."""
        self.ui.history.redo()

    def _on_revert(self, event=None):
        """Handles a request to revert all changes."""
        ui = self.ui
        if ui.history is not None:
            ui.history.revert()
        ui.handler.revert(ui.info)

    def _on_help(self, event=None):
        """Handles the user clicking the Help button."""
        self.ui.handler.show_help(self.ui.info)

    # ------------------------------------------------------------------------
    # ActionController interface
    # ------------------------------------------------------------------------

    def perform(self, action, event):
        """Dispatches the action to be handled by the handler.

        Parameters
        ----------
        action : Action instance
            The action to perform.
        event : ActionEvent instance
            The event that triggered the action.

        Returns
        -------
        result : any
            The result of the action's perform method (usually None).
        """
        handler = self.ui.handler
        self.ui.do_undoable(handler.perform, self.ui.info, action, event)

    def add_to_menu(self, menu_item):
        """Adds a menu item to the menu bar being constructed.

        The bulk of the back-end work is done in Pyface.  This code is simply
        responsible for hooking up radio groups, checkboxes, and enabled
        status.

        This routine is also used to add items to the toolbar, as logic and
        APIs are identical.

        Parameters
        ----------

        menu_item : toolkit MenuItem
            The Pyface toolkit-level item to add to the menu.
        """
        item = menu_item.item
        action = item.action

        if action.id != "":
            self.ui.info.bind(action.id, menu_item)

        if action.enabled_when != "":
            self.ui.add_enabled(action.enabled_when, menu_item)

        if action.visible_when != "":
            self.ui.add_visible(action.visible_when, menu_item)

        if action.checked_when != "":
            self.ui.add_checked(action.checked_when, menu_item)

    def add_to_toolbar(self, toolbar_item):
        """Adds a menu item to the menu bar being constructed.

        The bulk of the back-end work is done in Pyface.  This code is simply
        responsible for hooking up radio groups, checkboxes, and enabled
        status.

        This simply calls the analagous menu as logic and APIs are identical.

        Parameters
        ----------

        toolbar_item : toolkit Tool
            The Pyface toolkit-level item to add to the toolbar.
        """
        self.add_to_menu(toolbar_item)

    def can_add_to_menu(self, action):
        """Should the toolbar action be defined in the user interface.

        This simply calls the analagous menu as logic and APIs are identical.

        Parameters
        ----------

        action : Action
            The Action to add to the toolbar.

        Returns
        -------
        defined : bool
            Whether or not the action should be added to the menu.
        """
        if action.defined_when == "":
            return True

        return self.ui.eval_when(action.defined_when)

    def can_add_to_toolbar(self, action):
        """Should the toolbar action be defined in the user interface.

        This simply calls the analagous menu as logic and APIs are identical.

        Parameters
        ----------

        action : Action
            The Action to add to the toolbar.

        Returns
        -------
        defined : bool
            Whether or not the action should be added to the toolbar.
        """
        return self.can_add_to_menu(action)