| 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 |
""" An action set is a collection of menus, groups, and actions. """ |
| 11 |
|
| 12 |
|
| 13 |
# Standard library imports. |
| 14 |
import logging |
| 15 |
|
| 16 |
# Enthought library imports. |
| 17 |
from traits.api import Bool, Dict, HasTraits, List, provides, Str |
| 18 |
from traits.util.camel_case import camel_case_to_words |
| 19 |
|
| 20 |
# Local imports. |
| 21 |
from .action import Action |
| 22 |
from .group import Group |
| 23 |
from .i_action_set import IActionSet |
| 24 |
from .menu import Menu |
| 25 |
from .tool_bar import ToolBar |
| 26 |
|
| 27 |
# Logging. |
| 28 |
logger = logging.getLogger(__name__) |
| 29 |
|
| 30 |
|
| 31 |
@provides(IActionSet) |
| 32 |
class ActionSet(HasTraits): |
| 33 |
"""An action set is a collection of menus, groups, and actions.""" |
| 34 |
|
| 35 |
# The action set's globally unique identifier. |
| 36 |
id = Str |
| 37 |
|
| 38 |
# The action set's name. |
| 39 |
# |
| 40 |
# fixme: This is not currently used, but in future it will be the name that |
| 41 |
# is shown to the user when they are customizing perspectives by adding or |
| 42 |
# removing action sets etc. |
| 43 |
name = Str |
| 44 |
|
| 45 |
# The actions in this set. |
| 46 |
actions = List(Action) |
| 47 |
|
| 48 |
# The groups in this set. |
| 49 |
groups = List(Group) |
| 50 |
|
| 51 |
# The menus in this set. |
| 52 |
menus = List(Menu) |
| 53 |
|
| 54 |
# The tool bars in this set. |
| 55 |
tool_bars = List(ToolBar) |
| 56 |
|
| 57 |
# Are the actions and menus in this set enabled (if they are disabled they |
| 58 |
# will be greyed out). Tool bars are generally not greyed out themselves, |
| 59 |
# but the actions within them are. |
| 60 |
enabled = Bool(True) |
| 61 |
|
| 62 |
# Are the actions, menus and tool bars in this set visible? |
| 63 |
visible = Bool(True) |
| 64 |
|
| 65 |
# A mapping from human-readable names to globally unique IDs. |
| 66 |
# |
| 67 |
# This mapping is used when interpreting the first item in a location path |
| 68 |
# (i.e., the **path** trait of a **Location** instance). |
| 69 |
# |
| 70 |
# When the path is intepreted, the first component (i.e., the first item |
| 71 |
# before any '/') is checked to see if it is in the mapping, and if so it |
| 72 |
# is replaced with the value in the map. |
| 73 |
# |
| 74 |
# This technique allows paths to start with human readable names, as |
| 75 |
# opposed to IDs (which are required in order to manage the namespace of |
| 76 |
# all action sets). |
| 77 |
# |
| 78 |
# For example, in the Envisage workbench, the menu bar ID is: |
| 79 |
# |
| 80 |
# ``'envisage.workbench.menubar'`` |
| 81 |
# |
| 82 |
# Without aliases, you must specify a location like this: |
| 83 |
# |
| 84 |
# ``Location(path='envisage.workbench.menubar/ASubMenu/AGroup')`` |
| 85 |
# |
| 86 |
# This is a bit long-winded! Instead, you can define an alias: |
| 87 |
# |
| 88 |
# ``aliases = { 'MenuBar' : 'envisage.workbench.menubar' }`` |
| 89 |
# |
| 90 |
# In that case, you can specify a location like this: |
| 91 |
# |
| 92 |
# ``Location(path='MenuBar/ASubMenu/AGroup')`` |
| 93 |
# |
| 94 |
aliases = Dict(Str, Str) |
| 95 |
|
| 96 |
#### Trait initializers ################################################### |
| 97 |
|
| 98 |
def _id_default(self): |
| 99 |
"""Trait initializer.""" |
| 100 |
|
| 101 |
id = "%s.%s" % (type(self).__module__, type(self).__name__) |
| 102 |
logger.warning("action set %s has no Id - using <%s>" % (self, id)) |
| 103 |
|
| 104 |
return id |
| 105 |
|
| 106 |
def _name_default(self): |
| 107 |
"""Trait initializer.""" |
| 108 |
|
| 109 |
name = camel_case_to_words(type(self).__name__) |
| 110 |
logger.warning("action set %s has no name - using <%s>" % (self, name)) |
| 111 |
|
| 112 |
return name |