GraphItems/Circle.pm

filter: Statementstmt 19.7% 19 / 96 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 35.7% 5 / 14 Podpod 57.1% 4 / 7 totaltotal 17.5% 28 / 160 CC 21 SCAR 36.9
CC
21
Cov
14%
initialize
110.0
connector_coords
30.0
colour
12.0
CRAP 40.2
« GraphItems/LabeledConnector.pm ↑ Summary GraphItems/Examples/moving_items.pl »
1 package Tk::GraphItems::Circle;
2  
3  
4 =head1 NAME
5  
6 Tk::GraphItems::Circle - Display nodes of relation-graphs on a Tk::Canvas
7  
8 =head1 SYNOPSIS
9  
10  
11 require Tk::GraphItems::Circle;
12 ...
13 my $node = Tk::GraphItems::Circle->new(canvas => $can,
14 colour => $a_TkColour,
15 size => $points,
16 'x' => 50,
17 'y' => 50);
18 $node->move(10,0);
19 $node->set_coords(50,50);
20 $node->text($node->text()."\nanother_line");
21 $node->colour('red');
22  
23  
24  
25 =head1 DESCRIPTION
26  
27 Tk::GraphItems::Circle provides objects to display nodes of relation-graphs on a Tk::Canvas widget.
28  
29 =head1 METHODS
30  
31 B<Tk::GraphItems::Circle> supports the following methods:
32  
33 =over 4
34  
35 =item B<new(> canvas => $a_canvas,
36 colour => $a_TkColour,
37 x => $x_coord,
38 y => $y_coord,
39 size => $points B<)>
40  
41 Return a new Circle instance and display it on the given 'Canvas'. The canvas-items will be destroyed with the Circle-instance when it goes out of scope.
42  
43 =item B<set_coords(> $x, $y B<)>
44  
45 Set the (center)coordinates of this node.
46 If two references are given as arguments, the referenced Scalar-variables will get tied to the coordinates properties of the node.
47  
48 =item B<get_coords>
49  
50 Return the (center)coordinates of this node.
51  
52 =item B<move(> $d_x, $d_y B<)>
53  
54 Move the node by ( $d_x, $d_y ) points.
55  
56 =item B<size(> $size B<)>
57  
58 Resize the node to $size points. Returns the current size, if called without an argument.
59  
60 =item B<colour(> $a_Tk_colour B<)>
61  
62 Sets the Circles colour to $a_Tk_colour, if the argument is given. Returns the current colour, if called without an argument.
63  
64 =item B<bind_class(> 'event', $coderef B<)>
65  
66 Binds the given 'event' sequence to $coderef. This binding will exist for all Circle instances on the Canvas displaying the invoking object. The binding will not exist for Circles that are displayed on other Canvas instances. The Circle instance which is the 'current' one at the time the event is triggered will be passed to $coderef as an argument. If $coderef contains an empty string, the binding for 'event' is deleted.
67  
68  
69 =item B<was_dragged>
70  
71 Returns a true value in case a <B1-Motion> occured after the last <B1>. You may want to check this when binding to <B1-Release>, to make sure the action was a 'click' and not a 'drag'.
72  
73 =back
74  
75 =head1 SEE ALSO
76  
77 Documentation of Tk::GraphItems::Connector.
78 Examples in Tk/GraphItems/Examples.
79  
80 =head1 AUTHOR
81  
82 Christoph Lamprecht, ch.l.ngre@online.de
83  
84 =head1 COPYRIGHT AND LICENSE
85  
86 Copyright (C) 2007 by Christoph Lamprecht
87  
88 This library is free software; you can redistribute it and/or modify
89 it under the same terms as Perl itself, either Perl version 5.8.7 or,
90 at your option, any later version of Perl 5 you may have available.
91  
92  
93  
94  
95 =cut
96  
97 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
98 4 our $VERSION = '0.12';
99  
100 #use Data::Dumper;
101 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
102 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
103 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
104 4 ▶use Scalar::Util qw/looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
105 4 require Tk::GraphItems::Node;
106 4 require Tk::GraphItems::TiedCoord;
107 4 our @ISA = ('Tk::GraphItems::Node');
108  
109  
110 0 ▶sub initialize{
Subroutine initialize: not called CC=10 SCAR=47.0
Pod undocumented
111 0 my $self = shift;
112  
113 0 ▶ if (@_%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) 0 0 0
114 0 croak "wrong number of args! ";
115 }
116 0 my %args = @_;
117 0 my ($can,$x,$y,$size,$colour) = @args{qw/canvas x y size colour/};
118 0 eval {$can->isa('Tk::Canvas')};
119 0 ▶ croak "this is not a 'Canvas':<$can> $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
120 0 ▶ unless ($can->Exists){croak "This Canvas does not Exist:<$can>"};
Branch: 1 branch
BranchTrueFalseTotal
unless ($can->Exists) 0 0 0
121 0 my $text_id;
122 0 ▶ my @center = map {ref($_)?$$_:$_} ($x,$y);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
123 0 ▶ $size ||= 10;
Condition0%0/2
$size ||= 10
l!l
00
MC/DC0%0/1
$size ||= 10
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
124 0 my @coords = ($center[0] - $size/2,
125 $center[1] - $size/2,
126 $center[0] + $size/2,
127 $center[1] + $size/2);
128 0 ▶ my @colour = (-fill => $colour) if ($colour);
Branch: 1 branch
BranchTrueFalseTotal
if $colour 0 0 0
129 0 eval{$text_id = $can->createOval(@coords,
130 -tags =>['Circle',
131 'CircleBind'],
132 @colour,
133 );
134 };
135 0 ▶ croak "could not create Circle at coords <$x>,<$y>: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
136  
137 0 $self->{circle_id} = $text_id;
138 0 $self->{dependents} = {};
139 0 $self->{canvas} = $can;
140 0 $self->{size} = $size;
141
142 0 $self->SUPER::initialize;
143 0 $self->_create_canvas_layers;
144 0 $self->_set_layer(2);
145 0 $self->_set_canvas_bindings;
146 0 ▶ if (ref $x and ref $y) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $x and ref $y) 0 0 0
Condition0%0/3
ref $x and ref $y
!ll&&!rl&&r
000
MC/DC0%0/2
ref $x and ref $y
ref $x ref $y
Truth table0%0/3
A and B
A= ref $xB= ref $y
ABresult
0X0
100
111
147 0 $self->_tie_coords($x,$y);
148 }
149 0 return $self;
150  
151 } #end new
152  
153  
154 0 ▶sub _set_canvas_bindings{
Subroutine _set_canvas_bindings: not called CC=2 SCAR=17.9
155 0 my ($self) = @_;
156 0 ▶ return if $self->{canvas}{CircleBindings_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"canvas"}{"CircleBindings_done"} 0 0 0
157  
158 0 $self->_set_canvas_bindings_for_tag('Circle');
159  
160 0 $self->{canvas}{CircleBindings_done}= 1;
161 }
162  
163 0 ▶sub bind_class{
Subroutine bind_class: not called CC=1 SCAR=6.9
Pod documented
164 0 my ($self,$event,$code) = @_;
165 0 my $can = $self->{canvas};
166 0 $self->_bind_this_class($event,'CircleBind',$code);
167 }
168  
169  
170 0 ▶sub canvas_items{
Subroutine canvas_items: not called CC=1 SCAR=6.9
Pod undocumented
171 0 my $self = shift;
172 0 return (@$self{qw/ circle_id /});
173 }
174  
175 0 ▶sub connector_coords{
Subroutine connector_coords: not called CC=5 SCAR=34.0
Pod undocumented
176 0 my ($self,$dependent) = @_;
177 0 my ($x,$y) = $self->get_coords;
178 0 ▶ if (!defined $dependent) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dependent) 0 0 0
179 0 return($x,$y);
180 }
181 0 my $where = $dependent->{master}{$self};
182 0 ▶ my $other = $where eq 'source'? 'target':'source';
Branch: 1 branch
BranchTrueFalseTotal
$where eq 'source' ? : 0 0 0
183 0 my $c_c = $dependent->get_coords($other);
184 0 ▶ my $c_r= ($c_c->[1]-$y)/(($c_c->[0]-$x)||0.01);
Condition0%0/2
$c_c->[0] - $x || 0.01
l!l
00
MC/DC0%0/1
$c_c->[0] - $x || 0.01
$c_c->[0] - $x
Truth table0%0/2
A
A= $c_c->[0] - $x
Aresult
11
00
185 0 my $radius = $self->{ size } / 2;
186  
187 0 my $dx = sqrt($radius**2 /(1+$c_r**2));
188 0 ▶ $dx = - $dx if ($c_c->[0] > $x);
Branch: 1 branch
BranchTrueFalseTotal
if $c_c->[0] > $x 0 0 0
189 0 my $dy = $dx * $c_r;
190  
191 0 return ( $x-$dx , $y - $dy );
192  
193 }
194  
195 0 ▶sub _set_coords{
Subroutine _set_coords: not called CC=2 SCAR=17.9
196 0 my ($self,$x,$y)=@_;
197 0 my ($can,$circle_id,$size) = @$self{qw/canvas circle_id size/};
198 0 my $radius = $size/2;
199 0 $can->coords($circle_id,
200 $x - $radius,
201 $y - $radius,
202 $x + $radius,
203 $y + $radius);
204  
205 0 for ($self->dependents){
206 0 $_->position_changed($self);
207 }
208 }
209  
210 0 ▶sub colour{
Subroutine colour: not called CC=3 SCAR=24.8
Pod documented
211 0 my $self = shift;
212 0 my $can = $self->get_canvas;
213 0 ▶ if (@_){
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
214 0 eval{$can->itemconfigure($self->{circle_id},-fill=>$_[0]);};
215 0 ▶ croak " setting colour to <$_[0]> not possible: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
216 0 return $self;
217 }else{
218 0 return $can->itemcget($self->{circle_id},'-fill');
219 }
220 }
221  
222 0 ▶sub size{
Subroutine size: not called CC=3 SCAR=24.8
Pod documented
223 0 my $self = shift;
224 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
225 0 ▶ looks_like_number($_[0])||
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($_[0]) or croak("method 'size' failed:\n" . "arg <$_[0]> has to be a number!") 0 0 0
226 croak "method 'size' failed:\n"
227 ."arg <$_[0]> has to be a number!";
228 0 $self->{size} = $_[0];
229 0 $self->set_coords( $self->get_coords );
230 } else {
231 0 return $self->{size};
232 }
233 }
234 0 ▶sub get_coords{
Subroutine get_coords: not called CC=2 SCAR=17.9
Pod documented
235 0 my$self = shift;
236 0 my $can = $self->get_canvas;
237 0 my @circle_co = $can->coords($self->{circle_id});
238 0 my @coords = (( $circle_co[0] + $circle_co[2] )/2 ,
239 ( $circle_co[1] + $circle_co[3] )/2 );
240 0 ▶ return wantarray ? @coords:\@coords;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
241 }
242  
243  
244 4 1;
245  
246 __END__
« GraphItems/LabeledConnector.pm ↑ Summary GraphItems/Examples/moving_items.pl »