GraphItems/Examples/gi_circles_graph.pl untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 84 CC 4 SCAR 15.5
CC
4
Cov
0%
init_bindings
12.0
toggle_edge
6.0
new_node
2.0
CRAP 4.7
1 #!/usr/bin/perl -w
2  
3 use strict;
4 use warnings;
5 use Tk;
6 require Tk::GraphItems::Circle;
7 require Tk::GraphItems::Connector;
8 use Graph 0.70 ;
9 use Data::Dumper;
10 my $mw = tkinit();
11  
12 # our Graph has to be refvertexed to use
13 # Tk::GraphItems::Circle instances for the nodes
14  
15 my $graph = Graph->new(refvertexed=>1);
16 my $scrolled_can = $mw -> Scrolled('Canvas',
17 -width => 500,
18 -height => 500,
19 -scrollregion =>[0,0,500,500],
20 )->pack(-fill => 'both',
21 -expand => 1);
22  
23 # for use with Tk::GraphItems we have to extract the
24 # 'real' canvas out of the Scrolled widget :
25  
26 my $can = $scrolled_can->Subwidget('scrolled');
27 my $text =<<'TEXT'
28 Mouse bindings are as follows:
29 Shift-Button-1 create a new vertex here
30 Shift-Button3 delete this vertex
31 Button1-move drag this vertex
32 Control-Button1 select/unselect this vertex
33 Control-Button1 if another vertex is selected:
34 create an edge from the selected
35 vertex to this one or delete the
36 edge if it is present.
37 TEXT
38 ;
39 $can-> createText(20,20,
40 -font => ['Courier',10],
41 -text => $text,
42 -anchor => 'nw',
43 );
44 init_bindings($can);
45  
46 MainLoop;
47  
48  
49 # create Tk::GraphItems bindings for the canvas instance
50  
51 sub init_bindings{
52 my ($can) = @_;
53  
54 # create a dummy node on our canvas to call bind_class with.
55 # A call of 'bind_class' on this 'Circle' instance installs
56 # a binding which will be valid for every 'Circle' item
57 # on the same canvas.
58 my $node = Tk::GraphItems::Circle->new(canvas => $can,
59 'x' => 0,
60 'y' => 0
61 );
62  
63 # Deleting a node:
64 $node->bind_class("<Shift-Button-3>",
65 sub {
66 my $item = shift;
67 $graph->delete_vertex($item);
68 }
69 );
70  
71 # Adding and removing edges:
72 my ($selected, $old_colour);
73 $node->bind_class("<Control-Button-1>",
74 sub { my $item = shift;
75 if ( !$selected ) {
76 $selected = $item;
77 $old_colour = $item->colour;
78 $item -> colour('red');
79 } elsif ( $selected == $item ) {
80 $item -> colour($old_colour);
81 $selected = undef;
82 } else {
83 toggle_edge( $selected,$item );
84 $selected -> colour($old_colour);
85 $selected = undef;
86 }
87 }
88 );
89  
90  
91 # A Tk-binding to create new nodes:
92  
93 $can->Tk::bind("<Shift-Button-1>", sub {
94 my $e = $can->XEvent;
95 my ($mx, $my)=($e->x, $e->y);
96 new_node( $can,$mx,$my);
97 } );
98
99 }# end init_bindings
100  
101 sub new_node{
102 # Create a new Circle instance and use it as vertex in
103 # our Graph. The Circle will be destroyed when its vertex
104 # gets deleted.
105  
106 my ( $can,$x,$y ) = @_;
107 my $v = Tk::GraphItems::Circle->new(canvas => $can,
108 colour => 'green',
109 size => 20,
110 'x' => $x,
111 'y' => $y);
112 $graph->add_vertex($v);
113 }
114  
115 sub new_edge{
116 # Create a Connector with 'autodestroy' set to true so we don't
117 # need to 'detach' it to have it destroyed.
118  
119 my ( $source,$target) = @_;
120 my $conn = Tk::GraphItems::Connector->new( source => $source,
121 target => $target,
122 autodestroy => 1,
123 );
124
125 # create a new edge in the Graph and store our new Connector in
126 # the edges attribute data. That way the Connector will be destroyed
127 # when its edge gets deleted.
128  
129 $graph->add_edge( $source , $target);
130 $graph->set_edge_attribute($source, $target, 'Connector', $conn);
131 }
132  
133 sub toggle_edge{
134 my ( $source,$target ) = @_;
135 if ($graph->has_edge( $source, $target )){
136 $graph->delete_edge( $source, $target );
137 }else{
138 new_edge( $source, $target );
139 }
140 }
141  
142