| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
""" Manages a collection of action sets. """ |
| 11 |
|
| 12 |
|
| 13 |
# Enthought library imports. |
| 14 |
from traits.api import HasTraits, List |
| 15 |
|
| 16 |
# Local imports. |
| 17 |
from .action_set import ActionSet |
| 18 |
|
| 19 |
|
| 20 |
class ActionSetManager(HasTraits): |
| 21 |
"""Manages a collection of action sets.""" |
| 22 |
|
| 23 |
#### 'ActionSetManager' interface ######################################### |
| 24 |
|
| 25 |
# The action sets that this manager manages. |
| 26 |
action_sets = List(ActionSet) |
| 27 |
|
| 28 |
########################################################################### |
| 29 |
# 'ActionSetManager' interface. |
| 30 |
########################################################################### |
| 31 |
|
| 32 |
def get_actions(self, root): |
| 33 |
"""Return all action definitions for a root.""" |
| 34 |
|
| 35 |
return self._get_items(self.action_sets, "actions", root) |
| 36 |
|
| 37 |
def get_groups(self, root): |
| 38 |
"""Return all group definitions for a root.""" |
| 39 |
|
| 40 |
return self._get_items(self.action_sets, "groups", root) |
| 41 |
|
| 42 |
def get_menus(self, root): |
| 43 |
"""Return all menu definitions for a root.""" |
| 44 |
|
| 45 |
return self._get_items(self.action_sets, "menus", root) |
| 46 |
|
| 47 |
def get_tool_bars(self, root): |
| 48 |
"""Return all tool bar definitions for a root.""" |
| 49 |
|
| 50 |
return self._get_items(self.action_sets, "tool_bars", root) |
| 51 |
|
| 52 |
########################################################################### |
| 53 |
# 'Private' interface. |
| 54 |
########################################################################### |
| 55 |
|
| 56 |
def _get_items(self, action_sets, attribute_name, root): |
| 57 |
"""Return all actions, groups or menus for a particular root. |
| 58 |
|
| 59 |
e.g. To get all of the groups:: |
| 60 |
|
| 61 |
self._get_items(action_sets, 'groups', root) |
| 62 |
|
| 63 |
""" |
| 64 |
|
| 65 |
items = [] |
| 66 |
for action_set in action_sets: |
| 67 |
for item in getattr(action_set, attribute_name): |
| 68 |
if self._get_root(item.path, action_set.aliases) == root: |
| 69 |
items.append(item) |
| 70 |
|
| 71 |
# fixme: Hacky, but the model needs to maintain the |
| 72 |
# action set that contributed the item. |
| 73 |
item._action_set_ = action_set |
| 74 |
|
| 75 |
# fixme: Even hackier if this is a menu then we need to |
| 76 |
# tag the action set onto all of the groups. |
| 77 |
if attribute_name in ["menus", "toolbars"]: |
| 78 |
for group in item.groups: |
| 79 |
group._action_set_ = action_set |
| 80 |
|
| 81 |
return items |
| 82 |
|
| 83 |
def _get_root(self, path, aliases): |
| 84 |
"""Return the effective root for a path. |
| 85 |
|
| 86 |
If the first component of the path matches an alias, then we return |
| 87 |
the value of the alias. |
| 88 |
|
| 89 |
e.g. If the aliases are:: |
| 90 |
|
| 91 |
{'MenuBar' : 'envisage.ui.workbench.menubar'} |
| 92 |
|
| 93 |
and the path is:: |
| 94 |
|
| 95 |
'MenuBar/File/New' |
| 96 |
|
| 97 |
Then the effective root is:: |
| 98 |
|
| 99 |
'envisage.ui.workbench.menubar' |
| 100 |
|
| 101 |
If the first component of the path does *not* match an alias, then it |
| 102 |
is returned as is. |
| 103 |
|
| 104 |
e.g. If the aliases are:: |
| 105 |
|
| 106 |
{'ToolBar' : 'envisage.ui.workbench.toolbar'} |
| 107 |
|
| 108 |
and the path is:: |
| 109 |
|
| 110 |
'MenuBar/File/New' |
| 111 |
|
| 112 |
Then the effective root is:: |
| 113 |
|
| 114 |
'MenuBar' |
| 115 |
|
| 116 |
""" |
| 117 |
|
| 118 |
components = path.split("/") |
| 119 |
if components[0] in aliases: |
| 120 |
root = aliases[components[0]] |
| 121 |
|
| 122 |
else: |
| 123 |
root = components[0] |
| 124 |
|
| 125 |
return root |