File: group.py

package info (click to toggle)
enthought-traits-ui 2.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 15,204 kB
  • ctags: 9,623
  • sloc: python: 45,547; sh: 32; makefile: 19
file content (216 lines) | stat: -rw-r--r-- 5,753 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
#------------------------------------------------------------------------------
# Copyright (c) 2005, Enthought, Inc.
# All rights reserved.
# 
# This software is provided without warranty under the terms of the BSD
# license included in enthought/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!
# 
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A group of action manager items. """


# Enthought library imports.
from enthought.pyface.action.api import Action
from enthought.traits.api import Bool, HasTraits, Instance, List, Property
from enthought.traits.api import Str
from enthought.traits.trait_base import user_name_for

# Local imports.
from action_item import ActionItem
from action_manager_item import ActionManagerItem


class Group(HasTraits):
    """ A group of action manager items.

    By default, a group declares itself as requiring a separator when it is
    visualized, but this can be changed by setting its 'separator' trait to
    False.

    """

    #### 'Group' interface ####
    
    # The group's unique identifier (only needs to be unique within the action
    # manager that the group belongs to).
    id = Str

    # All of the items in the group.
    items = Property
    
    # The action manager that the group belongs to.
    parent = Instance(HasTraits)#('enthought.pyface.action.ActionManager')
    
    # Does this group require a separator when it is visualized?
    separator = Bool(True)

    #### Private interface ####

    # All of the items in the group.
    _items = List#(ActionManagerItem)

    ###########################################################################
    # 'object' interface.
    ###########################################################################

    def __init__(self, *items,  **traits):
        """ Creates a new menu manager. """

        # Base class constructor.
        super(Group, self).__init__(**traits)

        # Add any specified items.
        for item in items:
            self.append(item)
            
        return
    
    ###########################################################################
    # 'Group' interface.
    ###########################################################################

    #### Properties ###########################################################
    
    # items
    def _get_items(self):
        """ Returns the items in the group. """

        return self._items[:]

    #### Methods ##############################################################

    def append(self, item):
        """ Appends an item to the group.

        See the documentation for 'insert'.

        """

        return self.insert(len(self._items), item)

    def clear(self):
        """ Remove all items from the group. """

        self._items = []

        return

    def destroy(self):
        """ Called when the manager is no longer required.

        By default this method simply calls 'destroy' on all items in the
        group.

        """

        for item in self.items:
            item.destroy()

        return

    def insert(self, index, item):
        """ Inserts an item into the group at the specified index.

        1) An 'ActionManagerItem' instance.

            In which case the item is simply inserted into the group.

        2) An 'Action' instance.
        
            In which case an 'ActionItem' instance is created with the action
            and then inserted into the group.

        3) A Python callable (ie.'callable(item)' returns True).

            In which case an 'Action' is created that calls the callable when
            it is performed, and the action is then wrapped as in 2).
        
        """

        if isinstance(item, Action):
            item = ActionItem(action=item)

        elif callable(item):
            text = user_name_for(item.func_name)
            item = ActionItem(action=Action(text=text, on_perform=item))
            
        item.parent = self
        self._items.insert(index, item)

        return item

    def remove(self, item):
        """ Removes an item from the group. """

        self._items.remove(item)
        item.parent = None

        return

    def insert_before(self, before, item):
        """ Inserts an item into the group before the specified item.

        See the documentation for 'insert'.

        """

        index = self._items.index(before)
        
        self.insert(index, item)

        return (index, item)

    def insert_after(self, after, item):
        """ Inserts an item into the group after the specified item.

        See the documentation for 'insert'.

        """

        index = self._items.index(after)
        
        self.insert(index + 1, item)

        return (index, item)

    def find(self, id):
        """ Returns the item with the specified Id.

        Returns None if no such item exists.

        """

        for item in self._items:
            if item.id == id:
                break

        else:
            item = None
            
        return item


class Separator(Group):
    """ A convenience class.

    This is only used in 'cheap and cheerful' applications that create menus
    like::

        file_menu = MenuManager(
            CopyAction(),
            Separator(),
            ExitAction()
        )

    Hopefully, 'Separator' is more readable than 'Group'...

    """
    
    pass

#### EOF ######################################################################