File: example_workbench_window.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (169 lines) | stat: -rw-r--r-- 5,495 bytes parent folder | download | duplicates (2)
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
""" A simple example of using the workbench window. """


from pyface.action.api import Action, MenuManager
from pyface.workbench.api import EditorManager, WorkbenchWindow
from pyface.workbench.api import Perspective, PerspectiveItem
from pyface.workbench.action.api import MenuBarManager
from pyface.workbench.action.api import ToolBarManager
from pyface.workbench.action.api import ViewMenuManager
from traits.api import Callable, List, Instance


from black_view import BlackView
from blue_view import BlueView
from green_view import GreenView
from red_view import RedView
from yellow_view import YellowView
from person import Person


class ExampleEditorManager(EditorManager):
    """ An editor manager that supports the editor memento protocol. """

    # ---------------------------------------------------------------------
    # 'IEditorManager' interface.
    # ---------------------------------------------------------------------

    def get_editor_memento(self, editor):
        """ Return the state of the editor contents. """

        # Return the data attributes as a tuple.
        return (editor.obj.name, editor.obj.age)

    def set_editor_memento(self, memento):
        """ Restore an editor from a memento and return it. """

        # Create a new data object.
        name, age = memento
        person = Person(name=name, age=age)

        # Create an editor for the data.
        return self.create_editor(self.window, person, None)


class ExampleWorkbenchWindow(WorkbenchWindow):
    """ A simple example of using the workbench window. """

    # 'WorkbenchWindow' interface ------------------------------------------

    # The available perspectives.
    perspectives = [
        Perspective(
            name="Foo",
            contents=[
                PerspectiveItem(id="Black", position="bottom", height=0.1),
                PerspectiveItem(id="Debug", position="left", width=0.25),
            ],
        ),
        Perspective(
            name="Bar",
            contents=[
                PerspectiveItem(id="Black", position="top"),
                PerspectiveItem(id="Blue", position="bottom"),
                PerspectiveItem(id="Green", position="left"),
                PerspectiveItem(id="Red", position="right"),
                PerspectiveItem(id="Debug", position="left"),
            ],
        ),
    ]

    # 'ExampleWorkbenchWindow' interface -----------------------------------

    # The view factories.
    #
    # fixme: This should be part of the standadr 'WorkbenchWindow'!
    view_factories = List(Callable)

    # Private interface ----------------------------------------------------

    # The Exit action.
    _exit_action = Instance(Action)

    # The New Person action.
    _new_person_action = Instance(Action)

    # ------------------------------------------------------------------------
    # 'ApplicationWindow' interface.
    # ------------------------------------------------------------------------

    # Trait initializers ---------------------------------------------------

    def _editor_manager_default(self):
        """ Trait initializer.

        Here we return the replacement editor manager.
        """

        return ExampleEditorManager()

    def _menu_bar_manager_default(self):
        """ Trait initializer. """

        file_menu = MenuManager(
            self._new_person_action,
            self._exit_action,
            name="&File",
            id="FileMenu",
        )
        view_menu = ViewMenuManager(name="&View", id="ViewMenu", window=self)

        return MenuBarManager(file_menu, view_menu, window=self)

    def _tool_bar_managers_default(self):
        """ Trait initializer. """

        # Add multiple (albeit identical!) tool bars just to show that it is
        # allowed!
        tool_bar_managers = [
            ToolBarManager(
                self._exit_action, show_tool_names=False, name=str(i)
            )
            for i in range(5)
        ]

        return tool_bar_managers

    # ------------------------------------------------------------------------
    # 'WorkbenchWindow' interface.
    # ------------------------------------------------------------------------

    # Trait initializers ---------------------------------------------------

    def _view_factories_default(self):
        """ Trait initializer. """

        from pyface.workbench.debug.api import DebugView

        return [DebugView, BlackView, BlueView, GreenView, RedView, YellowView]

    def _views_default(self):
        """ Trait initializer. """

        # Using an initializer makes sure that every window instance gets its
        # own view instances (which is necessary since each view has a
        # reference to its toolkit-specific control etc.).
        return [factory(window=self) for factory in self.view_factories]

    # ------------------------------------------------------------------------
    # Private interface.
    # ------------------------------------------------------------------------

    def __exit_action_default(self):
        """ Trait initializer. """

        return Action(name="E&xit", on_perform=self.workbench.exit)

    def __new_person_action_default(self):
        """ Trait initializer. """

        return Action(name="New Person", on_perform=self._new_person)

    def _new_person(self):
        """ Create a new person. """

        from person import Person

        self.workbench.edit(Person(name="New", age=100))

        return