File: action.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 (140 lines) | stat: -rw-r--r-- 4,423 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
# (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!

""" The base class for all actions. """

from functools import partial


from traits.api import Bool, Callable, Enum, HasTraits, Str

from pyface.ui_traits import Image


class Action(HasTraits):
    """ The base class for all actions.

    An action is the non-UI side of a command which can be triggered by the end
    user.  Actions are typically associated with buttons, menu items and tool
    bar tools.

    When the user triggers the command via the UI, the action's 'perform'
    method is invoked to do the actual work.

    """

    # 'Action' interface ---------------------------------------------------

    #: Keyboard accelerator (by default the action has NO accelerator).
    accelerator = Str()

    #: Is the action checked?  This is only relevant if the action style is
    #: 'radio' or 'toggle'.
    checked = Bool(False)

    #: A longer description of the action (used for context sensitive help etc).
    #: If no description is specified, the tooltip is used instead (and if there
    #: is no tooltip, then well, maybe you just hate your users ;^).
    description = Str()

    #: Is the action enabled?
    enabled = Bool(True)

    #: Is the action visible?
    visible = Bool(True)

    #: The action's unique identifier (may be None).
    id = Str()

    #: The action's image (displayed on tool bar tools etc).
    image = Image

    #: The action's name (displayed on menus/tool bar tools etc).
    name = Str()

    #: An (optional) callable that will be invoked when the action is performed.
    on_perform = Callable

    #: The action's style.
    style = Enum("push", "radio", "toggle", "widget")

    #: A short description of the action used for tooltip text etc.
    tooltip = Str()

    #: An (optional) callable to create the toolkit control for widget style.
    control_factory = Callable

    # ------------------------------------------------------------------------
    # 'Action' interface.
    # ------------------------------------------------------------------------

    # Initializers ---------------------------------------------------------

    def _id_default(self):
        """ Initializes the 'id' trait.

        The default is the ``name`` trait.
        """
        return self.name

    # Methods -------------------------------------------------------------#

    def create_control(self, parent):
        """ Called when creating a "widget" style action.

        By default this will call whatever callable is supplied via the
        'control_factory' trait which is a callable that should take the parent
        control and the action as arguments and return an appropriate toolkit
        control.  Some operating systems (Mac OS in particular) may limit what
        widgets can be displayed in menus.

        This method is only used when the 'style' is "widget" and is ignored by
        other styles.

        Parameters
        ----------
        parent : toolkit control
            The toolkit control, usually a toolbar.

        Returns
        -------
        control : toolkit control
            A toolkit control or None.
        """
        if self.style == "widget" and self.control_factory is not None:
            return self.control_factory(parent, self)
        return None

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

        By default this method does nothing, but this would be a great place to
        unhook trait listeners etc.
        """

    def perform(self, event):
        """ Performs the action.

        Parameters
        ----------
        event : ActionEvent
            The event which triggered the action.
        """
        if self.on_perform is not None:
            self.on_perform()

    @classmethod
    def factory(cls, *args, **kwargs):
        """ Create a factory for an action with the given arguments.

        This is particularly useful for passing context to Tasks schema
        additions.
        """
        return partial(cls, *args, **kwargs)