Skip to content

Commit

Permalink
ENH: TreeEditor: Added on_activated trait to TreeNode.
Browse files Browse the repository at this point in the history
This complements the activated trait of TreeEditor, and allows
TreeNodes and related classes (TreeNodeObject etc.) to implement
functionality when a TreeEditor's node is activated
(double clicked or Enter key is pressed when the node has focus).
Also fixed a bug in vetoing to double click events on wx backend.
  • Loading branch information
Pankaj Pandey committed Dec 29, 2011
1 parent 5210f65 commit 84da03a
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 3 deletions.
3 changes: 3 additions & 0 deletions traitsui/editors/tree_editor.py
Expand Up @@ -101,6 +101,9 @@ class ToolkitEditorFactory ( EditorFactory ):
# Called when a node is double-clicked
on_dclick = Any

# Called when a node is activated
on_activated = Any

# Call when the mouse hovers over a node
on_hover = Any

Expand Down
7 changes: 7 additions & 0 deletions traitsui/qt4/tree_editor.py
Expand Up @@ -1001,6 +1001,13 @@ def _on_item_activated(self, nid, col):
"""
_, node, object = self._get_node_data(nid)

if node.activated(object) is True:
if self.factory.on_activated is not None:
self.ui.evaluate(self.factory.on_activated, object)
self._veto = True
else:
self._veto = True

# Fire the 'activated' event with the clicked on object as value:
self.activated = object

Expand Down
62 changes: 62 additions & 0 deletions traitsui/tree_node.py
Expand Up @@ -119,6 +119,10 @@ class TreeNode ( HasPrivateTraits ):
# Function for handling double-clicking an object
on_dclick = Callable

# Function for handling activation of an object
# (double-click or Enter key press when node is in focus)
on_activated = Callable

# View to use for editing the object
view = AView

Expand Down Expand Up @@ -622,6 +626,19 @@ def dclick ( self, object ):

return True

#---------------------------------------------------------------------------
# Handles an object being activated:
#---------------------------------------------------------------------------

def activated ( self, object ):
""" Handles an object being activated.
"""
if self.on_activated is not None:
self.on_activated( object )
return None

return True

#---------------------------------------------------------------------------
# Returns whether or not a specified object class can be added to the node:
#---------------------------------------------------------------------------
Expand Down Expand Up @@ -820,6 +837,10 @@ def dclick ( self ):
""" Handles an object being double-clicked.
"""

def activated ( self ):
""" Handles an object being activated.
"""

#-------------------------------------------------------------------------------
# 'ITreeNodeAdapter' class
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1054,6 +1075,11 @@ def dclick ( self ):
"""
pass

def activated ( self ):
""" Handles an object being activated.
"""
pass

#-------------------------------------------------------------------------------
# 'ITreeNodeAdapterBridge' class
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -1279,6 +1305,11 @@ def dclick ( self, object ):
"""
return self.adapter.dclick()

def activated ( self, object ):
""" Handles an object being activated.
"""
return self.adapter.activated()


# FIXME RTK: add the column_labels API to the following TreeNodes, too.

Expand Down Expand Up @@ -1630,6 +1661,15 @@ def dclick ( self, object ):
"""
return object.tno_dclick( self )

#---------------------------------------------------------------------------
# Handles an object being activated:
#---------------------------------------------------------------------------

def activated ( self, object ):
""" Handles an object being activated.
"""
return object.tno_activated( self )

#-------------------------------------------------------------------------------
# 'TreeNodeObject' class:
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -2042,6 +2082,19 @@ def tno_dclick ( self, node ):

return True

#---------------------------------------------------------------------------
# Handles an object being activated:
#---------------------------------------------------------------------------

def tno_activated ( self, node ):
""" Handles an object being activated.
"""
if node.on_activated is not None:
node.on_activated( self )
return None

return True

#-------------------------------------------------------------------------------
# 'MultiTreeNode' object:
#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -2334,3 +2387,12 @@ def dclick ( self, object ):
"""
return self.root_node.dclick( object )

#---------------------------------------------------------------------------
# Handles an object being activated:
#---------------------------------------------------------------------------

def activated ( self, object ):
""" Handles an object being activated.
"""
return self.root_node.activated( object )

16 changes: 13 additions & 3 deletions traitsui/wx/tree_editor.py
Expand Up @@ -1277,6 +1277,13 @@ def _on_tree_item_activated ( self, event ):
"""
expanded, node, object = self._get_node_data( event.GetItem() )

if node.activated( object ) is True:
if self.factory.on_activated is not None:
self.ui.evaluate( self.factory.on_activated, object )
self._veto = True
else:
self._veto = True

# Fire the 'activated' event with the clicked on object as value:
self.activated = object

Expand Down Expand Up @@ -1371,9 +1378,12 @@ def _on_left_dclick ( self, event ):

# If the mouse is over a node, then process the click:
if node is not None:
if ((node.dclick( object ) is True) and
(self.factory.on_dclick is not None)):
self.ui.evaluate( self.factory.on_dclick, object )
if node.dclick( object ) is True:
if self.factory.on_dclick is not None:
self.ui.evaluate( self.factory.on_dclick, object )
self._veto = True
else:
self._veto = True

# Fire the 'dclick' event with the object as its value:
# FIXME: This is instead done in _on_item_activated for backward
Expand Down

0 comments on commit 84da03a

Please sign in to comment.