File: schema.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 (230 lines) | stat: -rw-r--r-- 6,696 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
# (C) Copyright 2005-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!

""" Schema class definitions

This module defines the Schema
"""

from traits.api import (
    Bool,
    Callable,
    Enum,
    HasTraits,
    Instance,
    List,
    Property,
    Str,
    Tuple,
    Union,
)

from pyface.util.id_helper import get_unique_id

# NOTE
# This module should never import directly or indirectly any toolkit-dependent
# code at the top level.  This permits it to be used declaratively in a
# toolkit-agnostic way if needed.

# Trait definitions.
SubSchema = Union(
    None,
    Instance("pyface.action.action.Action"),
    Instance("pyface.action.action_item.ActionItem"),
    Instance("pyface.action.group.Group"),
    Instance("pyface.action.menu_manager.MenuManager"),
    Instance("pyface.action.schema.schema.GroupSchema"),
    Instance("pyface.action.schema.schema.MenuSchema"),
    Instance("pyface.action.schema.schema.Schema"),
)


class Schema(HasTraits):
    """ The abstract base class for all action schemas.
    """

    #: The schema's identifier (unique within its parent schema).
    id = Str()

    #: The list of sub-items in the schema. These items can be other
    #: (non-top-level) schema or concrete instances from the Pyface API.
    items = List(SubSchema)

    def __init__(self, *items, **traits):
        """ Creates a new schema.
        """
        super().__init__(**traits)
        self.items.extend(items)

    def create(self, children):
        """ Create the appropriate pyface.action instance with the specified
            child items.
        """
        raise NotImplementedError()

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

    def _id_default(self):
        return get_unique_id(self)


class ActionSchema(Schema):
    """ Action schema for Pyface Actions.

    An action schema cannot have children. It is used as an action factory
    to make sure a larger schema (e.g., a menu schema) can be used multiple
    times. Without using an ActionSchema, a reference to the action is added
    to every menu created from the schema. When one of the menus is destroyed,
    the action is also destroyed and is made unusable.
    """

    #: A factory for the Action instance.
    action_factory = Callable()

    #: Items is overwritten to be empty and read-only to avoid assigning to
    #: it by mistake.
    items = Property()

    def _get_items(self):
        return []

    def create(self, children):
        """ Create the appropriate Pyface Action instance. """

        traits = dict(id=self.id)
        return self.action_factory(**traits)

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

    def _action_factory_default(self):
        from pyface.action.action import Action
        return Action


class GroupSchema(Schema):
    """ A schema for a Pyface Group.
    """

    #: A factory for instantiating a pyface Group.
    group_factory = Callable()

    #: Does the group require a separator when it is visualized?
    separator = Bool(True)

    def create(self, children):
        traits = dict(id=self.id, separator=self.separator)
        return self.group_factory(*children, **traits)

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

    def _group_factory_default(self):
        from pyface.action.group import Group
        return Group


class MenuSchema(Schema):
    """ A schema for a Pyface MenuManager.
    """

    #: The menu's user visible name.
    name = Str()

    #: Does the menu require a separator before the menu item?
    separator = Bool(False)

    #: The default action for tool button when shown in a toolbar (Qt only)
    action = Instance("pyface.action.action.Action")

    #: A factory for instantiating a pyface MenuManager.
    menu_manager_factory = Callable()

    def create(self, children):
        traits = dict(id=self.id, name=self.name, separator=self.separator)
        if self.action:
            traits["action"] = self.action
        return self.menu_manager_factory(*children, **traits)

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

    def _menu_manager_factory_default(self):
        from pyface.action.menu_manager import MenuManager
        return MenuManager


class MenuBarSchema(Schema):
    """ A schema for a Pyface MenuBarManager.
    """

    #: Assign a default ID for menu bar schemas.
    id = "MenuBar"

    #: A factory for instantiating a pyface MenuBarManager.
    menu_bar_manager_factory = Callable()

    def create(self, children):
        traits = dict(id=self.id)
        return self.menu_bar_manager_factory(*children, **traits)

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

    def _menu_bar_manager_factory_default(self):
        from pyface.action.menu_bar_manager import MenuBarManager
        return MenuBarManager


class ToolBarSchema(Schema):
    """ A schema for a Pyface ToolBarManager.
    """

    #: Assign a default ID for tool bar schemas.
    id = "ToolBar"

    #: The tool bar's user visible name. Note that this name may not be used on
    #: all platforms.
    name = Str("Tool Bar")

    #: The size of tool images (width, height).
    image_size = Tuple((16, 16))

    #: The orientation of the toolbar.
    orientation = Enum("horizontal", "vertical")

    #: Should we display the horizontal divider?
    show_divider = Bool(True)

    #: Should we display the name of each tool bar tool under its image?
    show_tool_names = Bool(True)

    #: A factory for instantiating a pyface ToolBarManager
    tool_bar_manager_factory = Callable()

    def create(self, children):
        traits = dict(
            id=self.id,
            name=self.name,
            image_size=self.image_size,
            orientation=self.orientation,
            show_divider=self.show_divider,
            show_tool_names=self.show_tool_names,
        )
        return self.tool_bar_manager_factory(*children, **traits)

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

    def _tool_bar_manager_factory_default(self):
        from pyface.action.tool_bar_manager import ToolBarManager
        return ToolBarManager


# Convenience abbreviations.
SGroup = GroupSchema
SMenu = MenuSchema
SMenuBar = MenuBarSchema
SToolBar = ToolBarSchema