Tk::TreeGraph - Tk widget to draw a tree in a Canvas |
addNode(...)
modifyNode(...)
viewNode(nodeId)
flashNode(...)
addDirectArrow(...)
addSlantedArrow(...)
addLabel(...)
addShortcutInfo(...)
addAllShortcuts()
clear()
nodeBind(...)
arrowBind(...)
unselectAllNodes()
getSelectedNodes()
getNodeRectangle(...)
command(...)
Tk::TreeGraph - Tk widget to draw a tree in a Canvas
use Tk ; use Tk::TreeGraph ;
use strict ;
my $mw = MainWindow-> new ;
my $tg = $mw -> Scrolled('TreeGraph') -> pack(-expand => 1, -fill => 'both');
$tg -> addLabel (text => 'some tree');
my $ref = [qw/some really_silly text/];
$tg -> addNode ( nodeId => '1.0', text => $ref ) ;
# EITHER add the arrow and the node $tg -> addDirectArrow ( from => '1.0', to => '1.1' ) ;
$tg->addNode ( nodeId => '1.1', text => ['some','text'] ) ;
# OR add a node after another one, in this case the widget # will draw the arrow $tg->addNode ( after =>'1.0', nodeId => '1.1', text => ['some','text'] );
$tg->arrowBind ( button => '<1>', color => 'orange', command => sub{my %h = @_; warn "clicked 1 arrow $h{from} -> $h{to}\n";} );
$tg->nodeBind ( button => '<2>', color => 'red', command => sub {my %h = @_; warn "clicked 2 node $h{nodeId}\n";} );
$tg->command( on => 'arrow', label => 'dummy 2', command => sub{warn "arrow menu dummy2\n";});
$tg->arrowBind(button => '<3>', color => 'green', command => sub{$tg->popupMenu(@_);});
$tg->command(on => 'node', label => 'dummy 1', command => sub{warn "node menu dummy1\n";});
$tg->nodeBind(button => '<3>', color => 'green', command => sub{$tg->popupMenu(@_);});
# adjust scrolled area with some margin my @array = $tg->bbox("all") ; $tg->configure(-scrollregion => [0, 0, $array[2] + 50, $array[3] + 50 ]);
MainLoop ; # Tk's
Tk::TreeGraph is a Canvas specialized to draw trees on a Canvas using arrows and nodes. A node is simply some text imbedded in a rectangular shape.
TreeGraph is able to draw the following items:
TreeGraph also provides :
You might say that the tree is a weird tree since it is drawn downward and assymetric and adding branches leaves a some void between them.
You'd be right. I'm not a specialist in tree drawing algorithms but the crude algorithm used here works quite fine with drawing id trees for VCS system. But as usual, I'm always listening for suggestions or even better, patches ;-) .
Note that the tree MUST be drawn from top to bottom and from left to right. Unless you may get a very confusing drawing of a tree.
First versions of TreeGraph used to tinker with a -scrollregion option each time addNode was called. This was not consistent since the scrollbars are added by the user when calling TreeGraph (using Scrolled('TreeGraph')). Hence from now on, it will be the responsability of the user to set a satisfying -scrollregion.
The user may write this after all nodes are drawn to set the scrollregion :
my @array = $tg->bbox("all") ; $tg->configure(-scrollregion => [0, 0, $array[2] + 50, $array[3] + 50 ]);
Furthermore, since configure will called only once, the resulting code will be faster.
You draw the tree node after node with addNode using the 'after' parameter. Then the object will infer the kind of arrow needed between the 2 nodes. Using the 'after' parameter, you no longer need to call youself the addSlantedArrow or addDirectArrow methods.
addNode(...)
Parameters are:
Will add a new node (made of a rectangle with the text inside).
Note that this method will add the nodeId on top of the passed text ('text' parameter).
modifyNode(...)
Parameters are:
Will modify an existing node. Note that the geometry of the node will not be changed.
viewNode(nodeId)
Will move the canvas so that the node is visible within the scrolled area. (do nothing if the scroll region is not defined TBD XXX)
flashNode(...)
Parameters are:
Will make an existing node flash. Calling a second time this method on a node will make the flashing stop.
addDirectArrow(...)
You can use this method if you want to change the default aspect of
the direct arrow. In this case do not use the 'after' parameter of the
addNode()
method. Parameters are:
Add a new straight (i.e. vertical) arrow starting from a node. Note that the 'from' nodeId must be defined. The 'to' nodeId must NOT be defined. (Remember that you must draw the tree from top to bottom)
addSlantedArrow(...)
You can use this method if you want to change the default aspect of
the slanted arrow. In this case do not use the 'after' parameter of the
addNode()
method.
Parameters are:
Add a new branch connecting node 'id' to node 'id2'. Note that the 'from' nodeId must be defined. The 'to' nodeId must NOT be defined. (Remember that you must draw the tree from left to right)
addLabel(...)
Put some text on the top of the graph. Parameters are:
addShortcutInfo(...)
Parameters are:
Declare that a shortcut arrow will be drawn from node 'arrow_start' and 'arrow_end'.
addAllShortcuts()
This method is to be called once all nodes, direct arrow and branch arrows are drawn and all relevant calls to addShortcutInfo are done.
It will draw shortcut arrows between the ids declared with the addShortcutInfo method.
clear()
Clear the graph.
nodeBind(...)
Parameters are:
Bind the 'button' on all nodes. When 'button' is clicked, the node text color will change to 'color' and the callback sub will be called with these parameters:
(on => 'node', nodeId => $nodeId)
arrowBind(...)
Parameters are:
Bind the 'button' on arrows. When 'button' is clicked, the arrow color will change to 'color' and the callback sub will be called with these parameters:
( on => 'arrow', from => nodeId_on_arrow_start, to => nodeId_on_arrow_tip )
unselectAllNodes()
Unselect all previously selected nodes (see button <1> binding)
getSelectedNodes()
Return an array containing nodeIds of all nodes currently selected.
getNodeRectangle(...)
Returns the rectangle reference of the passed nodeId or of the node selected by the user.
Parameters are :
command(...)
This will add a new entry on a Popup menu which can be raised on a node or an arrow.
Parameters are :
The callback will be invoked with these parameters when the command is set for nodes :
(on => 'node', nodeId => $nodeId)
The callback will be invoked with these parameters when the command is set for arrows :
( on => 'arrow', from => nodeId_on_arrow_start, to => nodeId_on_arrow_tip )
These functions are documented only for people wanting to improve or inherit this widget.
setArrow(...)
Parameters are:
Reset any previously selected arrow to default color and set the current arrow to the color. This function should be used with a bind.
Returns (from => $endNodeId, to => $tipNodeId) to specify the nodes the arrow is attached to.
setNode()
Parameters are:
Set node either from passed nodeId or from the mouse pointer. When a node is set, only the text is highlighted
Returns the nodeId of the current node (i.e. the node clicked by the user if this function was used in a bind)
toggleNode(...)
Parameters are:
Will toggle the node rectangle between 'color' and default.
Dominique Dumont, Dominique_Dumont@grenoble.hp.com
Copyright (c) 1998-2001 Dominique Dumont. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
the Tk manpage, the Tk::Canvas manpage
Tk::TreeGraph - Tk widget to draw a tree in a Canvas |