GraphItems/Examples/create_and_autolayout.pl untested

filter: Statementstmt 0.0% 0 / 123 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 161 CC 5 SCAR 14.3
CC
5
Cov
0%
init_bindings
12.0
stop_cb
6.0
toggle_edge
6.0
CRAP 4.2
1 #!/usr/bin/perl -w
2
3
4
5 =head1 NAME
6
7 create_and_autolayout - Create a 'Graph' object by clicking/dragging and let Graph::Layout::Aesthetic do the layout.
8
9 =cut
10
11
12
13
14
15 use strict;
16 use warnings;
17 use Tk;
18 require Tk::GraphItems::Circle;
19 require Tk::GraphItems::Connector;
20 use Graph 0.70 ;
21 use Graph::Layout::Aesthetic::Topology;
22 use Graph::Layout::Aesthetic;
23
24
25 package main;
26 my $mw = tkinit();
27
28 # our Graph has to be refvertexed to use
29 # Tk::GraphItems::Circle instances for the nodes
30
31 my $graph = Graph->new( refvertexed => 1 );
32 my $scrolled_can = $mw -> Scrolled('Canvas',
33 -width => 500,
34 -height => 500,
35 -scrollregion =>[0,0,500,500],
36 )->pack(-fill => 'both',
37 -expand => 1);
38
39 # for use with Tk::GraphItems we have to extract the
40 # 'real' canvas out of the Scrolled widget :
41
42 my $can = $scrolled_can->Subwidget('scrolled');
43 my $text =<<'TEXT'
44 Mouse bindings:
45 Shift-Button-1 create a new vertex here
46 Shift-Button3 delete this vertex
47 Button1-move drag this vertex
48 Control-Button1 select/unselect this vertex
49 Control-Button1 if another vertex is selected:
50 create an edge from the selected
51 vertex to this one or delete the
52 edge if it is present.
53 Control-D delete all vertices
54 TEXT
55 ;
56 $can-> createText(20,20,
57 -font => ['Courier',10],
58 -text => $text,
59 -anchor => 'nw',
60 );
61 init_bindings($can);
62
63
64 my $repeat;
65 my $stop_button;
66 my ($temp,$centrip,$rep,$min_len)=(10,1,10000,0.01);
67
68 my $f1 = $mw->Frame()->pack;
69 my @frames= map {$f1->Frame()->pack(-side=>'left');} (0..2);
70 $frames[0]->Label(-text=>$_)->pack for ('temperature',
71 'centripetal',
72 'node_repulsion',
73 'min_edge_length');
74
75 $frames[1]->Entry(-textvariable=>$_)->pack for (\$temp,
76 \$centrip,
77 \$rep,
78 \$min_len);
79
80 {
81 my $aglo;
82
83 $frames[2]->Button(-text=>'start',
84 -width=>20,
85 -command=>sub{
86 $aglo = convert($graph);
87 set_aglo_coords($aglo,$graph);
88 if ($repeat){$repeat ->cancel}
89 $repeat = $mw->repeat(100,sub{iterate($aglo,$graph)})
90 }
91 )->pack;
92 $frames[2]->Button(-text=>'continue',
93 -width=>20,
94 -command=>sub{
95 set_aglo_coords($aglo,$graph);
96 if ($repeat){$repeat ->cancel}
97 $repeat = $mw->repeat(100,sub{iterate($aglo,$graph)})
98 }
99 )->pack;
100 $stop_button = $frames[2]->Button(-text => 'stop',
101 -width => 20,
102 -command => \&stop_cb,
103 )->pack;
104 }#end scope of $g, $aglo
105
106
107 MainLoop;
108
109 sub stop_cb{
110 if ($repeat){$repeat ->cancel;
111 undef $repeat;
112 }
113 my @bb = $scrolled_can->bbox('all');
114 $scrolled_can->configure(-scrollregion => \@bb);
115 }
116
117 sub iterate{
118 my ($aglo,$g) = @_;
119 $aglo->_gloss(0);
120 $aglo->coordinates_to_graph( $g,
121 pos_attribute => ["x_end", "y_end"]);
122 }
123 sub convert{
124 my $topo = Graph::Layout::Aesthetic::Topology->from_graph($_[0]);
125 my $aglo = Graph::Layout::Aesthetic->new($topo);
126 $aglo->add_force(node_repulsion => $rep);
127 $aglo->add_force(min_edge_length => $min_len);
128 $aglo->add_force("Centripetal", => $centrip);
129 $aglo->init_gloss($temp,0.0001,1000,0);
130 return $aglo;
131 }
132 sub set_aglo_coords{
133 my ($aglo,$graph) = @_;
134 for my $v($graph->vertices){
135 my $id = $graph->get_vertex_attribute($v,'layout_id');
136 $aglo->coordinates($id,$v->get_coords);
137 }
138 }
139
140 # create Tk::GraphItems bindings for the canvas instance
141
142 sub init_bindings{
143 my ($can) = @_;
144
145 # create a dummy node on our canvas to call bind_class with.
146 # A call of 'bind_class' on this 'Circle' instance installs
147 # a binding which will be valid for every 'Circle' item
148 # on the same canvas.
149 my $node = Tk::GraphItems::Circle->new(canvas => $can,
150 'x' => 0,
151 'y' => 0
152 );
153
154 # Deleting a node:
155 $node->bind_class("<Shift-Button-3>",
156 sub {
157 my $item = shift;
158 $graph->delete_vertex($item);
159 }
160 );
161
162 # Adding and removing edges:
163 my ($selected, $old_colour);
164 $node->bind_class("<Control-Button-1>",
165 sub { my $item = shift;
166 if ( !$selected ) {
167 $selected = $item;
168 $old_colour = $item->colour;
169 $item -> colour('red');
170 } elsif ( $selected == $item ) {
171 $item -> colour($old_colour);
172 $selected = undef;
173 } else {
174 toggle_edge( $selected,$item );
175 $selected -> colour($old_colour);
176 $selected = undef;
177 }
178 }
179 );
180
181
182 # A Tk-binding to create new nodes:
183
184 $can->Tk::bind("<Shift-Button-1>", sub {
185 my $e = $can->XEvent;
186 my ($wx, $wy)=($e->x, $e->y);
187 my $x = $can->canvasx($wx);
188 my $y = $can->canvasy($wy);
189 new_node( $can,$x,$y);
190 } );
191
192 my $mw = $can->MainWindow;
193 $mw->bind('<Control-d>',\&delete_all_vertices);
194
195 }# end init_bindings
196
197 sub new_node{
198 # Create a new Circle instance and use it as vertex in
199 # our Graph. The Circle will be destroyed when its vertex
200 # gets deleted.
201
202 my ( $can,$x,$y ) = @_;
203 # my $v = Tk::GraphItems::Circle->new(canvas => $can,
204 my $v = ColoredCircle->new(canvas => $can,
205 colour => 'green',
206 size => 20,
207 'x' => $x,
208 'y' => $y);
209 $graph->add_vertex($v);
210 $graph->set_vertex_attribute($v,$_,0)for qw/x_end y_end/;
211
212 # yes, I know! the following line is a dirty trick and it should
213 # *never* be done that way!
214 $v->set_coords(\$graph->[2][4]{$v}[2]{x_end},\$graph->[2][4]{$v}[2]{y_end});
215 $graph->set_vertex_attribute($v,'x_end',$x);
216 $graph->set_vertex_attribute($v,'y_end',$y);
217
218 return $v;
219 }
220
221 sub new_edge{
222 # Create a Connector with 'autodestroy' set to true so we don't
223 # need to 'detach' it to have it destroyed.
224
225 my ( $source,$target) = @_;
226 my $conn = Tk::GraphItems::Connector->new( source => $source,
227 target => $target,
228 autodestroy => 1,
229 );
230
231 # create a new edge in the Graph and store our new Connector in
232 # the edges attribute data. That way the Connector will be destroyed
233 # when its edge gets deleted.
234
235 $graph->add_edge( $source , $target);
236 $graph->set_edge_attribute($source, $target, 'Connector', $conn);
237 }
238
239 sub toggle_edge{
240 my ( $source,$target ) = @_;
241 if ($graph->has_edge( $source, $target )){
242 $graph->delete_edge( $source, $target );
243 }else{
244 new_edge( $source, $target );
245 }
246 }
247
248 sub delete_all_vertices{
249 $stop_button->Invoke;
250 $graph->delete_vertices($graph->vertices);
251 }