File: task_action.py

package info (click to toggle)
python-pyface 6.1.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 11,756 kB
  • sloc: python: 39,728; makefile: 79
file content (148 lines) | stat: -rw-r--r-- 4,967 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
# Copyright (c) 2010-18, 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!

# Enthought library imports.
from traits.api import Bool, Instance, Property, Str, cached_property

# Local imports.
from pyface.tasks.api import Editor, Task, TaskPane
from pyface.action.listening_action import ListeningAction


class TaskAction(ListeningAction):
    """ An Action that makes a callback to a Task.

    Note that this is a convenience class. Actions associated with a Task need
    not inherit TaskAction, although they must, of course, inherit Action.
    """

    #### ListeningAction interface ############################################

    object = Property(depends_on='task')

    #### TaskAction interface #################################################

    # The Task with which the action is associated. Set by the framework.
    task = Instance(Task)

    ###########################################################################
    # Protected interface.
    ###########################################################################

    def _get_object(self):
        return self.task

    def destroy(self):
        # Disconnect listeners to task and dependent properties.
        self.task = None
        super(TaskAction, self).destroy()


class TaskWindowAction(TaskAction):
    """ An Action that makes a callback to a Task's window.
    """

    #### ListeningAction interface ############################################

    object = Property(depends_on='task.window')

    ###########################################################################
    # Protected interface.
    ###########################################################################

    def _get_object(self):
        if self.task:
            return self.task.window
        return None


class CentralPaneAction(TaskAction):
    """ An Action that makes a callback to a Task's central pane.
    """

    #### ListeningAction interface ############################################

    object = Property(depends_on='central_pane')

    #### CentralPaneAction interface ##########################################

    # The central pane with which the action is associated.
    central_pane = Property(Instance(TaskPane), depends_on='task')

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_central_pane(self):
        if self.task and self.task.window is not None:
            return self.task.window.get_central_pane(self.task)
        return None

    def _get_object(self):
        return self.central_pane


class DockPaneAction(TaskAction):
    """ An Action the makes a callback to one of a Task's dock panes.
    """

    #### ListeningAction interface ############################################

    object = Property(depends_on='dock_pane')

    #### DockPaneAction interface #############################################

    # The dock pane with which the action is associated. Set by the framework.
    dock_pane = Property(Instance(TaskPane), depends_on='task')

    # The ID of the dock pane with which the action is associated.
    dock_pane_id = Str

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_dock_pane(self):
        if self.task and self.task.window is not None:
            return self.task.window.get_dock_pane(self.dock_pane_id, self.task)
        return None

    def _get_object(self):
        return self.dock_pane


class EditorAction(CentralPaneAction):
    """ An action that makes a callback to the active editor in an editor pane.
    """

    #### ListeningAction interface ############################################

    object = Property(depends_on='active_editor')

    #### EditorAction interface ###############################################

    # The active editor in the central pane with which the action is associated.
    active_editor = Property(
        Instance(Editor), depends_on='central_pane.active_editor'
    )

    ###########################################################################
    # Protected interface.
    ###########################################################################

    @cached_property
    def _get_active_editor(self):
        if self.central_pane is not None:
            return self.central_pane.active_editor
        return None

    def _get_object(self):
        return self.active_editor