File: action_set_manager.py

package info (click to toggle)
python-envisageplugins 3.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,600 kB
  • sloc: python: 6,968; sh: 11; makefile: 8; lisp: 1
file content (118 lines) | stat: -rw-r--r-- 3,525 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
""" Manages a collection of action sets. """


# Enthought library imports.
from enthought.traits.api import HasTraits, List

# Local imports.
from action_set import ActionSet


class ActionSetManager(HasTraits):
    """ Manages a collection of action sets. """

    #### 'ActionSetManager' interface #########################################

    # The action sets that this manager manages.
    action_sets = List(ActionSet)

    ###########################################################################
    # 'ActionSetManager' interface.
    ###########################################################################

    def get_actions(self, root):
        """ Return all action definitions for a root. """

        return self._get_items(self.action_sets, 'actions', root)

    def get_groups(self, root):
        """ Return all group definitions for a root. """

        return self._get_items(self.action_sets, 'groups', root)

    def get_menus(self, root):
        """ Return all menu definitions for a root. """

        return self._get_items(self.action_sets, 'menus', root)

    def get_tool_bars(self, root):
        """ Return all tool bar definitions for a root. """

        return self._get_items(self.action_sets, 'tool_bars', root)

    ###########################################################################
    # 'Private' interface.
    ###########################################################################

    def _get_items(self, action_sets, attribute_name, root):
        """ Return all actions, groups or menus for a particular root.

        e.g. To get all of the groups::

            self._get_items(action_sets, 'groups', root)

        """

        items = []
        for action_set in action_sets:
            for item in getattr(action_set, attribute_name):
                if self._get_root(item.path, action_set.aliases) == root:
                    items.append(item)

                    # fixme: Hacky, but the model needs to maintain the
                    # action set that contributed the item.
                    item._action_set_ = action_set

                    # fixme: Even hackier if this is a menu then we need to
                    # tag the action set onto all of the groups.
                    if attribute_name in ['menus', 'toolbars']:
                        for group in item.groups:
                            group._action_set_ = action_set

        return items

    def _get_root(self, path, aliases):
        """ Return the effective root for a path.

        If the first component of the path matches an alias, then we return
        the value of the alias.
        
        e.g. If the aliases are::

            {'MenuBar' : 'enthought.envisage.ui.workbench.menubar'}

        and the path is::

           'MenuBar/File/New'

        Then the effective root is::

            'enthought.envisage.ui.workbench.menubar'

        If the first component of the path does *not* match an alias, then it
        is returned as is.

        e.g. If the aliases are::

            {'ToolBar' : 'enthought.envisage.ui.workbench.toolbar'}

        and the path is::

           'MenuBar/File/New'

        Then the effective root is::

            'MenuBar'
        
        """

        components = path.split('/')
        if components[0] in aliases:
            root = aliases[components[0]]

        else:
            root = components[0]

        return root

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