Node.pm

filter: Statementstmt 18.5% 15 / 81 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 29.4% 5 / 17 Podpod n/a totaltotal 15.3% 20 / 130 SCAR 50.7
CC
15
Cov
14%
relative
42.0
_deregisterEdge
12.0
_checkloop
12.0
CRAP 159.3
« Label/NodeLabel.pm ↑ Summary Label.pm »
1 package yEd::Node;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use yEd::PropertyBasedObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use yEd::Label::NodeLabel;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 yEd::Node - Base class for Nodes
12  
13 =head1 DESCRIPTION
14  
15 This is the base class for Nodes.
16 It may not be instanciated, instead use one of the specialized types as described in the SUPPORTED FEATURES section.
17  
18 =head1 SUPPORTED FEATURES
19  
20 The following Node types are currently supported:
21  
22 =over 4
23
24 =item *
25
26 L<yEd::Node::ShapeNode> basic shape type nodes
27
28 =item *
29
30 L<yEd::Node::GenericNode> see link for a list of which Nodes of yEd actually are GenericNodes
31
32 =back
33  
34 The following types and features are currently not supported (and it's not sure they'll ever be):
35  
36 =over 4
37  
38 =item *
39  
40 Group/Folder type Nodes (e.g. group 'Group Nodes')
41  
42 =item *
43  
44 Table type Nodes (e.g. group "Swimlane Nodes and Tables")
45  
46 =item *
47  
48 SVG type Nodes (e.g. group "People")
49  
50 =item *
51  
52 Adding meta data to Nodes or edit the present ones (e.g. URL, description)
53  
54 =back
55  
56 All other basic features of Nodes (yEd Version 3.13) are supported.
57  
58 Support beyond yEd:
59  
60 =over 4
61
62 =item *
63
64 You may specify Nodes to be relative to other Nodes (see relative property)
65  
66 =item *
67  
68 You may add multiple Labels to a single Node (yEd will handle it properly)
69  
70 =back
71  
72 =head1 PROPERTIES
73  
74 =head2 layer
75  
76 Type: uint
77  
78 Default: 0
79  
80 The drawing pane layer this Node is on.
81  
82 =head2 relative
83  
84 Type: yEd::Node or 0 to erase relation
85  
86 Default: 0
87  
88 If a Node reference is set here, this Nodes x and y properties will be interpreted as being relative to the given Node.
89  
90 Beware of creating loops !
91  
92 =head2 id
93  
94 This property is read only and returns the Nodes ID.
95  
96 =head2 x
97  
98 Type: float
99  
100 Default: 0
101  
102 The x position (upper left corner).
103  
104 =head2 y
105  
106 Type: float
107  
108 Default: 0
109  
110 The y position (upper left corner).
111  
112 =head2 height
113  
114 Type: ufloat
115  
116 Default: 30
117  
118 The height of the Node.
119  
120 =head2 width
121  
122 Type: ufloat
123  
124 Default: 30
125  
126 The width of the Node.
127  
128 =head2 fillColor
129  
130 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
131  
132 Default: '#ffcc00'
133  
134 The background color.
135  
136 =head2 fillColor2
137  
138 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
139  
140 Default: 'none'
141  
142 The second background color (will be ignored if fillColor is 'none').
143  
144 =head2 borderColor
145  
146 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
147  
148 Default: '#000000'
149  
150 The color for the border line.
151  
152 =head2 borderType
153  
154 Type: descrete values ( line | dotted | dashed | dashed_dotted )
155  
156 Default: 'line'
157  
158 Linetype of the border.
159  
160 =head2 borderWidth
161  
162 Type: ufloat
163  
164 Default: 1
165  
166 The width of the border.
167  
168 =head1 SUBROUTINES/METHODS
169  
170 =head2 new
171  
172 Creates a new instance of a Node.
173  
174 An ID must be provided as first parameter.
175 This value may be any string or number but must be unique among the whole L<yEd::Document>.
176 If you don't want to bother with IDs use the addNewNode function of L<yEd::Document> instead, it will take care of the IDs.
177  
178 Further parameters to set properties are optional (property => value, ...).
179  
180 =cut
181  
182 sub new {
183 0 ▶ confess 'you may not instantiate a yEd::Node base class object';
Subroutine new: not called CC=1 SCAR=6.9
184 }
185  
186 =head2 copy
187  
188 Creates a copy of this Node and returns it.
189  
190 Also copies all attached Labels.
191  
192 An ID must be given as first parameter as described in the Node classes constructors, it will be applied to the copy.
193  
194 You may optionally specify properties in the form 'property => value, ...' to change these properties for the returned copy.
195  
196 If this Node is relative to another one you will want to set a new relation partner for the copy or make it absolute.
197  
198 =cut
199  
200 sub copy {
201 0 ▶ my ($self, $id, @p) = @_;
Subroutine copy: not called CC=2 SCAR=17.9
202 0 my $ref = ref $self;
203 0 my $o = $ref->new($id);
204 0 $o->setProperties($self->getProperties());
205 0 $o->setProperties(@p);
206 0 foreach my $l ($self->labels()) {
207 0 $o->addLabel($l->copy());
208 }
209 0 return $o;
210 }
211  
212 sub _init {
213 0 ▶ my ($self, $id, @properties) = @_;
Subroutine _init: not called CC=2 SCAR=17.9
214 0 ▶ confess 'node id missing for node creation' unless (defined $id);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
215 0 $self->{'properties'}{'id'} = $id;
216 0 $self->{'datakeys'} = [ 'd4', 'd5' ];
217 0 $self->{'rootkey'} = 'd6';
218 # defaults
219 0 $self->relative(0);
220 0 $self->x(0);
221 0 $self->y(0);
222 0 $self->height(30);
223 0 $self->width(30);
224 0 $self->fillColor('#ffcc00');
225 0 $self->fillColor2('none');
226 0 $self->borderColor('#000000');
227 0 $self->borderType('line');
228 0 $self->borderWidth(1);
229 0 $self->clearLabels();
230 0 $self->layer(0);
231 0 $self->{'edges'} = {};
232 # user values
233 0 $self->setProperties(@properties);
234 0 return $self;
235 }
236  
237 # "virtual" properties
238 sub layer {
239 0 ▶ return _PROPERTY($match{'uint'}, @_);
Subroutine layer: not called CC=1 SCAR=6.9
240 }
241 sub relative {
242 0 ▶ my ($self, $value) = @_;
Subroutine relative: not called CC=6 SCAR=37.4
243 0 my $key = 'relative';
244 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 0 0 0
245 0 ▶ confess "value must be a yEd::Node (or one of its subtypes) (given value: $value)" unless ($value == 0 or $value->isa('yEd::Node'));
Branch: 1 branch
BranchTrueFalseTotal
unless $value == 0 or $value->isa("yEd::Node") 0 0 0
Condition0%0/3
$value == 0 or $value->isa("yEd::Node")
l!l&&r!l&&!r
000
MC/DC0%0/2
$value == 0 or $value->isa("yEd::Node")
$value == 0 $value->isa("yEd::Node")
Truth table0%0/3
A or B
A= $value == 0B= $value->isa("yEd::Node")
ABresult
1X1
011
000
246 0 ▶ confess "Did you really think making a Node relative to itself would be a good idea?" if ($value and $value == $self);
Branch: 1 branch
BranchTrueFalseTotal
if $value and $value == $self 0 0 0
Condition0%0/3
$value and $value == $self
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $value == $self
$value $value == $self
Truth table0%0/3
A and B
A= $valueB= $value == $self
ABresult
0X0
100
111
247 0 $self->{'properties'}{$key} = $value;
248 0 return;
249 } else {
250 0 return $self->{'properties'}{$key};
251 }
252 }
253  
254 # node
255 sub id {
256 0 ▶ return _PROPERTY('ro', @_);
Subroutine id: not called CC=1 SCAR=6.9
257 }
258  
259 sub _registerEdge {
260 0 ▶ my ($self, $edge) = @_;
Subroutine _registerEdge: not called CC=2 SCAR=17.9
261 0 ▶ if ($edge->isa('yEd::Edge')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($edge->isa("yEd::Edge")) 0 0 0
262 0 $self->{'edges'}{$edge}{'count'}++;
263 0 $self->{'edges'}{$edge}{'ref'} = $edge;
264 }
265 }
266 sub _deregisterEdge {
267 0 ▶ my ($self, $edge) = @_;
Subroutine _deregisterEdge: not called CC=3 SCAR=24.8
268 0 ▶ if ($edge->isa('yEd::Edge')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($edge->isa("yEd::Edge")) 0 0 0
269 0 $self->{'edges'}{$edge}{'count'}--;
270 0 ▶ delete $self->{'edges'}{$edge} unless ($self->{'edges'}{$edge}{'count'});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"edges"}{$edge}{"count"} 0 0 0
271 }
272 }
273  
274 =head2 absX
275  
276 Returns the absolute x value for this Node which equals $node->x() unless $node->relative($node2) was set.
277 In this case $node->absX() is $node2->absX() + $node->x().
278  
279 =cut
280  
281 sub _checkloop {
282 0 ▶ my $self = shift;
Subroutine _checkloop: not called CC=3 SCAR=24.8
283 0 my $node = $self;
284 0 my $count = 0;
285 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$node and do { do { $node = $node->relative; ++$count; confess("loop detected: relative node $self (id: " . $self->id . ") found itself $count nodes later in hierarchy") if $node == $self } } 0 0 0
286 0 $node = $node->relative();
287 0 $count++;
288 0 ▶ confess "loop detected: relative node $self (id: " . $self->id() . ") found itself $count nodes later in hierarchy" if ($node == $self);
Branch: 1 branch
BranchTrueFalseTotal
if $node == $self 0 0 0
289 } while ($node);
290 }
291  
292 sub absX {
293 0 ▶ my $self = shift;
Subroutine absX: not called CC=2 SCAR=17.9
294 0 $self->_checkloop();
295 0 my $x = $self->x();
296 0 ▶ $x += $self->relative()->absX() if ($self->relative());
Branch: 1 branch
BranchTrueFalseTotal
if $self->relative 0 0 0
297 0 return $x;
298 }
299  
300 =head2 absY
301  
302 Returns the absolute y value for this Node which equals $node->y() unless $node->relative($node2) was set.
303 In this case $node->absY() is $node2->absY() + $node->y().
304  
305 =cut
306  
307 sub absY {
308 0 ▶ my $self = shift;
Subroutine absY: not called CC=2 SCAR=17.9
309 0 $self->_checkloop();
310 0 my $y = $self->y();
311 0 ▶ $y += $self->relative()->absY() if ($self->relative());
Branch: 1 branch
BranchTrueFalseTotal
if $self->relative 0 0 0
312 0 return $y;
313 }
314  
315 =head2 absCenter
316  
317 Returns the absolute (x,y) values for this Node's center (as an array of 2 elements: x and y).
318 Note that normal coords indicate the upper left corner of a Node or better its surrounding rectangle (circles, ...).
319 So it is better to say: This returns the center of the surrounding rectangle.
320  
321 =cut
322  
323 sub absCenter {
324 0 ▶ my $self = shift;
Subroutine absCenter: not called CC=1 SCAR=6.9
325 return (
326 0 $self->absX() + 0.5 * $self->width(),
327 $self->absY() + 0.5 * $self->height()
328 );
329 }
330  
331 =head2 getEdges
332  
333 Returns an array of all Edges (as references) connected to this Node.
334  
335 =cut
336  
337 sub getEdges {
338 my $self = shift;
339 my @edges;
340 foreach my $edge (keys $self->{'edges'}) {
341 push @edges, $self->{'edges'}{$edge}{'ref'};
342 }
343 return @edges;
344 }
345  
346 # Labels
347  
348 =head2 addNewLabel
349  
350 Takes a value for the text property of Labels followed by optional Label properties ('property' => 'value', ...) and creates and adds a new Label from it.
351  
352 Returns a ref to the Label object.
353  
354 =cut
355  
356 sub addNewLabel {
357 my $self = shift;
358 my $label = yEd::Label::NodeLabel->new(@_);
359 $self->addLabel($label);
360 return $label;
361 }
362  
363 =head2 addLabel
364  
365 Takes yEd::Label::NodeLabel object and adds it.
366  
367 =cut
368  
369 sub addLabel {
370 my ($self, $label) = @_;
371 confess "only yEd::Label::NodeLabel's can be added" unless ($label->isa('yEd::Label::NodeLabel'));
372 push @{$self->{'labels'}}, $label;
373 return;
374 }
375  
376 =head2 labels
377  
378 Acts as a getter with no parameters provided and returns an array of all Labels attached to this Node.
379  
380 If an array of yEd::Label::NodeLabel objects is provided, this nodes Labels are replaced by the given Labels.
381  
382 =cut
383  
384 sub labels {
385 my ($self, @labels) = @_;
386 if (@labels) {
387 foreach my $label (@labels) {
388 confess "only yEd::Label::NodeLabel's can be added" unless ($label->isa('yEd::Label::NodeLabel'));
389 }
390 $self->{'labels'} = \@labels;
391 return;
392 } else {
393 return @{$self->{'labels'}};
394 }
395 }
396  
397 =head2 clearLabels
398  
399 Removes all Labels from this Node.
400  
401 =cut
402  
403 sub clearLabels {
404 $_[0]->{'labels'} = [];
405 return;
406 }
407  
408 =head2 getLabelsByProperties
409  
410 Takes arguments of the form 'property1 => value, property2 => value2, ...'.
411  
412 Returns a list of all Labels that matches the given filter.
413  
414 =cut
415  
416 sub getLabelsByProperties {
417 my ($self, @properties) = @_;
418 my @labels;
419 foreach my $label ($self->labels()) {
420 push @labels, $label if $label->hasProperties(@properties);
421 }
422 return @labels;
423 }
424  
425 # Geometry
426 sub x {
427 return _PROPERTY($match{'float'}, @_);
428 }
429 sub y {
430 return _PROPERTY($match{'float'}, @_);
431 }
432 sub height {
433 return _PROPERTY($match{'ufloat'}, @_);
434 }
435 sub width {
436 return _PROPERTY($match{'ufloat'}, @_);
437 }
438  
439 # Fill
440 sub fillColor {
441 return _PROPERTY($match{'color'}, @_);
442 }
443 sub fillColor2 {
444 return _PROPERTY($match{'color'}, @_);
445 }
446  
447 # BorderStyle
448 sub borderColor {
449 return _PROPERTY($match{'color'}, @_);
450 }
451 sub borderType {
452 return _PROPERTY($match{'linetype'}, @_);
453 }
454 sub borderWidth {
455 return _PROPERTY($match{'ufloat'}, @_);
456 }
457  
458 sub _build {
459 my $self = shift;
460 my $node = $self->_createNodeNode();
461 my $root = $self->_addRootNode($node);
462 my $type = $self->_addTypeNode($root);
463 $self->_addGeometryNode($type);
464 $self->_addFillNode($type);
465 $self->_addBorderNode($type);
466 $self->_addLabelsNode($type);
467 $self->_addAdditionalNodes($type);
468 return $node;
469 }
470 sub _createNodeNode {
471 my $self = shift;
472 my $node = XML::LibXML::Element->new('node');
473 $node->setAttribute('id', $self->id());
474 foreach my $dk (@{$self->{'datakeys'}}) {
475 $node->addNewChild('', 'data')->setAttribute('key', $dk);
476 }
477 return $node;
478 }
479 sub _addRootNode {
480 my ($self, $node) = @_;
481 my $root = $node->addNewChild('', 'data');
482 $root->setAttribute('key', $self->{'rootkey'});
483 return $root;
484 }
485 sub _addTypeNode {
486 # add type node here and return it (e.g. <y:ShapeNode>)
487 confess 'you may not build a yEd::Node base class object, override this method in a specialized subclass';
488 }
489 sub _addGeometryNode {
490 my ($self, $node) = @_;
491 my $geo = $node->addNewChild('', 'y:Geometry');
492 $geo->setAttribute('height', $self->height());
493 $geo->setAttribute('width', $self->width());
494 $geo->setAttribute('x', $self->absX());
495 $geo->setAttribute('y', $self->absY());
496 }
497 sub _addFillNode {
498 my ($self, $node) = @_;
499 my $fill = $node->addNewChild('', 'y:Fill');
500 if ($self->fillColor() eq 'none') {
501 $fill->setAttribute('hasColor', 'false');
502 } else {
503 $fill->setAttribute('color', $self->fillColor());
504 $fill->setAttribute('color2', $self->fillColor2()) unless ($self->fillColor2() eq 'none');
505 }
506 $fill->setAttribute('transparent', 'false');
507 }
508 sub _addBorderNode {
509 my ($self, $node) = @_;
510 my $border = $node->addNewChild('', 'y:BorderStyle');
511 if ($self->borderColor() eq 'none') {
512 $border->setAttribute('hasColor', 'false');
513 } else {
514 $border->setAttribute('color', $self->borderColor());
515 }
516 $border->setAttribute('type', $self->borderType());
517 $border->setAttribute('width', $self->borderWidth());
518 }
519 sub _addLabelsNode {
520 my ($self, $node) = @_;
521 foreach my $label ($self->labels()) {
522 $node->addChild($label->_build());
523 }
524 }
525 sub _addAdditionalNodes {
526 # add type specific data here (e.g. <y:Shape ...)
527 }
528  
529 =head2 setProperties getProperties hasProperties
530  
531 As described at L<yEd::PropertyBasedObject>
532  
533 =head1 SEE ALSO
534  
535 L<yEd::Document> for further informations about the whole package
536  
537 L<yEd::PropertyBasedObject> for further basic information about properties and their additional functions
538  
539 The specialized Nodes:
540  
541 L<yEd::Node::ShapeNode>
542  
543 L<yEd::Node::GenericNode>
544  
545 =cut
546  
547 1;
« Label/NodeLabel.pm ↑ Summary Label.pm »