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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
#------------------------------------------------------------------------------
# Copyright (c) 2005, 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!
#
# Author: Enthought, Inc.
# Description: <Enthought pyface package component>
#------------------------------------------------------------------------------
""" A tree control with extensible node types. """
# Standard library imports.
import inspect
# Enthought library imports.
from enthought.pyface.action.api import ActionEvent
from enthought.traits.api import Instance, List, Property
# Local imports.
from node_manager import NodeManager
from node_type import NodeType
from node_tree_model import NodeTreeModel
from tree import Tree
class NodeTree(Tree):
""" A tree control with extensible node types. """
#### 'Tree' interface #####################################################
# The model that provides the data for the tree.
model = Instance(NodeTreeModel, ())
#### 'NodeTree' interface #################################################
# The node manager looks after all node types.
node_manager = Property(Instance(NodeManager))
# The node types in the tree.
node_types = Property(List(NodeType))
###########################################################################
# 'NodeTree' interface.
###########################################################################
#### Properties ###########################################################
# node_manager
def _get_node_manager(self):
""" Returns the root node of the tree. """
return self.model.node_manager
def _set_node_manager(self, node_manager):
""" Sets the root node of the tree. """
self.model.node_manager = node_manager
return
# node_types
def _get_node_types(self):
""" Returns the node types in the tree. """
return self.model.node_manager.node_types
def _set_node_types(self, node_types):
""" Sets the node types in the tree. """
self.model.node_manager.node_types = node_types
return
###########################################################################
# 'Tree' interface.
###########################################################################
#### Trait event handlers #################################################
def _node_activated_changed(self, obj):
""" Called when a node has been activated (i.e., double-clicked). """
default_action = self.model.get_default_action(obj)
if default_action is not None:
self._perform_default_action(default_action, obj)
return
def _node_right_clicked_changed(self, (obj, point)):
""" Called when the right mouse button is clicked on the tree. """
# Add the node that the right-click occurred on to the selection.
self.select(obj)
# fixme: This is a hack to allow us to attach the node that the
# right-clicked occurred on to the action event.
self._context = obj
# Ask the model for the node's context menu.
menu_manager = self.model.get_context_menu(obj)
if menu_manager is not None:
self._popup_menu(menu_manager, obj, point)
return
###########################################################################
# 'ActionController' interface.
###########################################################################
def add_to_menu(self, menu_item):
""" Adds a menu item to a menu bar. """
pass
def add_to_toolbar(self, toolvar_item):
""" Adds a tool bar item to a tool bar. """
pass
def can_add_to_menu(self, action):
""" Returns True iff an action can be added to the menu. """
return True
def perform(self, action, event):
""" Perform an action. """
# fixme: We need a more formal event structure!
event.widget = self
event.context = self._context
# fixme: the 'perform' method without taking an event is deprecated!
args, varargs, varkw, defaults = inspect.getargspec(action.perform)
# If the only argument is 'self' then this is the DEPRECATED
# interface.
if len(args) == 1:
action.perform()
else:
action.perform(event)
return
###########################################################################
# Private interface.
###########################################################################
def _create_action_event(self, obj):
""" Return a new action event for the specified object. """
return ActionEvent(widget=self, context=obj)
def _perform_default_action(self, action, obj):
""" Perform the default action on the specified object. """
action.perform(self._create_action_event(obj))
return
def _popup_menu(self, menu_manager, obj, point):
""" Popup the menu described by the menu manager. """
# Create the actual menu control.
menu = menu_manager.create_menu(self.control, self)
if not menu.is_empty():
# Show the menu. If an action is selected it will be performed
# *before* this call returns.
menu.show(*point)
# This gives the actions in the menu manager a chance to cleanup
# any event listeners etc.
menu_manager.destroy()
return
#### EOF ######################################################################
|