Dominique Dumont > Tk-TreeGraph-1.030 > Tk::TreeGraph

Download:
Tk-TreeGraph-1.030.tar.gz

Dependencies

Annotate this POD

Related Modules

XML::Parser
Net::SSLeay
VCS::SCCS
Digest::HMAC
Date::Manip
Convert::IBM390
Crypt::Blowfish
Archive::Zip
HTTP::Proxy
Image::Info
more...
By perlmonks.org
View/Report Bugs
Module Version: 1.030   Source  

NAME ^

Tk::TreeGraph - Tk widget to draw a tree in a Canvas

SYNOPSIS ^

 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

DESCRIPTION ^

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 :

CAVEATS ^

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.

About Scrolling ^

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.

Widget Options ^

-nodeColor:

Color of the node rectangle.

-nodeTextColor:

Color of the text within the nodes

-labelColor

Color of the label.

-arrowColor

Color of the arrow.

-shortcutColor:

Color of the shortcut arrow (default 'orange')

-shortcutStyle:

Style of the shortcut arrow. The arrow can be drawn as a 'straight' arrow or a 'spline'. (default 'straight')

-nodeTag:

Boolean. By default the nodeId is added at the beginning of the node text.

-arrowDeltaY:

Length of direct arrows (downward). default 40

-branchSeparation:

Minimum width between 2 branches of the tree (default 120)

-x_start:

x coordinate of the root of the tree. (default 100)

-y_start:

y coordinate of the root of the tree.(default 100)

-animation:

Delay (in ms), if delay is positive, TreeGraph will update the canvas widget any time the addNode method is called and wait delay ms. This enable to make pretty animation when drawing complex trees. This feature will work only if a scrollregion is set before calling addNode.

Drawing Methods added to Canvas ^

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.

Management methods ^

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
 ) 

Private methods ^

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.

draw_tree_from_ref( $tree_aref )

Draws a tree from a structure like this: my $tree_aref = [['Root', ['Terminal', ['Nonterminal', ['Terminal']]]]];

_draw_tree_from_ref_recursive( $tree_aref, $root_node_id )

This is the private method we use to draw the tree recursively. It is called by draw_tree_from_ref($tree_aref). As this method is private, it is not indended to be called directly.

$tree_aref has to be a tree-structure as the following expamle:

 my $tree_aref = [['Root', ['Terminal', ['Nonterminal', ['Terminal']]]]] ;

_calcNewTreeId( $motherId, $pos_in_tree_level )

Method used in draw_tree_from_ref. Calculates the id of a child in a subtree-level and returns it. Needs the id of the root-node of the sub-tree and the position of the element.

AUTHOR ^

Dominique Dumont, ddumont at cpan dot org.

Copyright (c) 1998-2004,2007 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.

Thanks to Alexander Becker for the method to draw a tree from an array ref.

SEE ALSO ^

Tk, Tk::Canvas

syntax highlighting: