GraphItems/Examples/gi_easy.pl untested

filter: Statementstmt 0.0% 0 / 93 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 122 CC 5 SCAR 15.8
CC
5
Cov
0%
init_bindings
12.0
toggle_edge
6.0
menu_handle_leave
6.0
CRAP 4.9
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 gi_easy - Demonstrates how to set up mouse bindings to create nodes and edges
6
7 =cut
8
9 use strict;
10 use warnings;
11 use Tk;
12 require Tk::GraphItems::TextBox;
13 require Tk::GraphItems::Connector;
14 require Tk::LabEntry;
15 my $mw = tkinit();
16
17 my %nodes; # the simplest way to hold a 'model'
18 my %connectors;
19
20 my $scrolled_can = $mw -> Scrolled('Canvas',
21 -width => 500,
22 -height => 500,
23 -scrollregion => [0,0,500,500],
24 )->pack(-fill => 'both',
25 -expand => 1);
26
27 # Extract the 'real' canvas out of the Scrolled widget :
28
29 my $can = $scrolled_can->Subwidget('scrolled');
30 my $text =<<'TEXT'
31 Mouse bindings:
32 Shift-Button-1 create a new vertex here
33 Shift-Button3 delete this vertex
34 Button1-move drag this vertex
35 Control-Button1 select/unselect this vertex
36 Control-Button1 if another vertex is selected:
37 create an edge from the selected
38 vertex to this one or delete the
39 edge if it is present.
40 Button3 Popup a context menu displaying
41 text, colour and coords of a vertex.
42 Edit text or colour fields to
43 change these properties
44 TEXT
45 ;
46 $can-> createText(20,20,
47 -font => ['Courier',10],
48 -text => $text,
49 -anchor => 'nw',
50 );
51
52 init_context_menu($can);
53 init_bindings($can);
54
55
56 MainLoop;
57
58
59 # create Tk::GraphItems bindings for the canvas instance
60 sub init_bindings{
61 my ($can) = @_;
62
63 # create a dummy node on our canvas to call bind_class with.
64 # A call of 'bind_class' on this 'TextBox' instance installs
65 # a binding which will be valid for every 'TextBox' item
66 # on the same canvas.
67 my $node = Tk::GraphItems::TextBox->new(canvas => $can,
68 'x' => 0,
69 'y' => 0
70 );
71
72 # Deleting a node:
73 $node->bind_class("<Shift-Button-3>",
74 sub {
75 my $item = shift;
76 delete $nodes{$item};
77 delete $connectors{$item};
78 for (values %connectors){
79 delete $_->{$item}
80 }
81 }
82 );
83 # Context Menu:
84 $node->bind_class("<Button-3>",
85 sub {
86 my $item = shift;
87 my $menu = $can->{gi_easy_menu};
88 $menu->{properties}[1] = $item->text;
89 $menu->{properties}[2] = $item->colour;
90 my ($x,$y) = $item->get_coords;
91 $menu->{properties}[3] = $x;
92 $menu->{properties}[4] = $y;
93 $menu->Popup(-popover=>'cursor');
94 $menu->waitVariable(\$menu->{state});
95 $item->text($menu->{properties}[1]);
96 $item->colour($menu->{properties}[2]);
97 }
98 );
99
100 # Adding and removing edges:
101 my $selected;
102 $node->bind_class("<Control-Button-1>",
103 sub { my $item = shift;
104 if ( !$selected ) {
105 $selected = $item;
106 $item -> colour('red');
107 } elsif ( $selected == $item ) {
108 $item -> colour('white');
109 $selected = undef;
110 } else {
111 toggle_edge( $selected,$item );
112 $selected -> colour('white');
113 $selected = undef;
114 }
115 }
116 );
117
118
119 # A Tk-binding to create new nodes on Shift-B1:
120
121 $can->Tk::bind("<Shift-Button-1>", sub {
122 my $e = $can->XEvent;
123 my ($mx, $my)=($e->x, $e->y);
124 new_node( $can,$mx,$my);
125 } );
126
127 }# end init_bindings
128
129 sub new_node{
130 # Create a new TextBox instance and store a reference to it.
131 # The TextBox will be destroyed when its reference gets deleted.
132
133 my ( $can,$x,$y,$text ) = @_;
134 my $v = Tk::GraphItems::TextBox->new(canvas=>$can,
135 text=> $text||'node',
136 # font=> ['Courier',10],
137 'x'=> $x,
138 'y'=> $y);
139 $nodes{$v}=$v;
140 return $v;
141 }
142
143 sub new_edge{
144 # Create a Connector with 'autodestroy' set to true so we don't
145 # need to 'detach' it to have it destroyed.
146
147 my ( $source,$target) = @_;
148 my $conn = Tk::GraphItems::Connector->new( source => $source,
149 target => $target,
150 autodestroy => 1,
151 );
152 $connectors{$source}{$target} = $conn;
153 }
154
155 sub toggle_edge{
156 my ( $source,$target ) = @_;
157 if ($connectors{$source}{$target}){
158 delete $connectors{$source}{$target};
159 }else{
160 new_edge( $source, $target );
161 }
162 }
163
164
165 sub init_context_menu{
166 my $canvas = shift;
167 my $ctm = $canvas->Toplevel;
168 $ctm->configure(-borderwidth => 2,
169 -relief => 'groove',
170 );
171 $ctm->overrideredirect(1);
172 $ctm->bind('<Leave>',[\&menu_handle_leave,$ctm]);
173 my $properties;
174 my $n = 1;
175 for (qw/text colour x y/){
176 $ctm->LabEntry(-label => "$_",
177 -textvariable => \$properties->[$n++],
178 -bg => 'white'
179 )->pack;
180 }
181 $ctm->{properties} = $properties;
182 $ctm->withdraw;
183 $canvas->{gi_easy_menu} = $ctm;
184 }
185
186 sub menu_handle_leave{
187 my ($w,$ctm) = @_;
188 return unless $w == $ctm;# are we leaving the contextmenu
189 # or a subwidget?
190
191 $ctm->{state} ++; # a flag to signal withdrawing
192 $ctm->withdraw;
193 }