Label.pm

filter: Statementstmt 70.6% 65 / 92 Branchbran 16.6% 3 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.6% 26 / 29 Podpod 100% 23 / 23 totaltotal 72.2% 117 / 162 CC 13 SCAR 31.5
CC
13
Cov
62%
getTextString
83.3
allInsets
3.0
new
2.0
CRAP 23.3
↑ Summary Label/NodeLabel.pm »
1 package yEd::Label;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use yEd::PropertyBasedObject;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 yEd::Label - Textlabels for Nodes and Edges
11  
12 =head1 DESCRIPTION
13  
14 This is the base class for Labels.
15 It may not be instanciated, instead use one of the specialized types:
16  
17 =over 4
18  
19 =item *
20  
21 L<yEd::Label::EdgeLabel> Label for Edges, see documentation for additional EdgeLabel properties
22  
23 =item *
24  
25 L<yEd::Label::NodeLabel> Label for Nodes, see documentation for additional NodeLabel properties
26  
27 =back
28  
29 Have a look at the C<addNewLabel()> function of L<yEd::Node> and L<yEd::Edge>, it is the preferred way to create Labels.
30  
31 For saving Label templates see L<yEd::Document>'s C<addNewLabelTemplate()>, C<getTemplateLabel()> and the related functions.
32  
33 =head1 SUPPORTED FEATURES
34  
35 Labels are supported for both, Nodes and Edges.
36 However there are some features which are currently not supported:
37  
38 =over 4
39  
40 =item *
41  
42 Smart Label positioning: Because it offers few advantages over the other positioning modells and is much more complex to implement.
43  
44 =item *
45  
46 configuration of the Preferred Placement Descriptors: As they seem to only have an effect on Smart Labels (and do only exist for Labels on Edges).
47  
48 =item *
49  
50 SVG content (icons) for Labels
51  
52 =back
53  
54 All other features of Labels (yEd Version 3.13) are supported.
55  
56 For the available positioning modells see the documentation of the specialized Labels.
57  
58 Other than in yEd itself you may add more than one Label to a single Node or Edge, regardless of its type.
59 In fact some special Nodes in yEd have multiple Labels per default (e.g. tables), so yEd will handle this correctly.
60  
61 =head1 PROPERTIES
62  
63 =head2 text
64  
65 Type: anything
66  
67 Default: ... must be supplied
68  
69 The text to be displayed by the Label.
70  
71 If it is a blessed ref it will try to find a C<toString()> or C<to_string()> method with the fallback of standard perl stringification.
72  
73 If it is an array ref each entry will be treated as a line of text.
74  
75 If it is a hash ref its content will be formated like so:
76  
77 key1: value
78 key2: val2
79 another key: val3
80  
81 Use a monospace font and left alignment (e.g. 'alignment' => 'left', 'fontFamily' => 'Monospaced') for hashes.
82 Output will be sorted by keys, you can make your keys like _01_keyfirst , _02_keysecond, ... to order them (the ^_\d+_ portion will be removed for printing).
83  
84 Also see the C<getTextString()> function which returns this property as the printed text.
85  
86 =head2 visible
87  
88 Type: bool
89  
90 Default: true
91  
92 Whether the Label is visible or not (rather useless).
93  
94 =head2 x
95  
96 Type: float
97  
98 Default: 0
99  
100 The x position of the Label (ignored in most positioning modells).
101  
102 =head2 y
103  
104 Type: float
105  
106 Default: 0
107  
108 The y position of the Label (ignored in most positioning modells).
109  
110 =head2 height
111  
112 Type: ufloat
113  
114 Default: 20
115  
116 The height of the Label (mostly ignored depending on autoSizePolicy).
117  
118 =head2 width
119  
120 Type: ufloat
121  
122 Default: 30
123  
124 The width of the Label (mostly ignored depending on autoSizePolicy).
125  
126 =head2 backgroundColor
127  
128 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
129  
130 Default: 'none'
131  
132 The background color of the Label.
133  
134 =head2 lineColor
135  
136 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
137  
138 Default: 'none'
139  
140 The border color of the Label.
141  
142 =head2 bottomInset
143  
144 Type: uint
145  
146 Default: 0
147  
148 The bottom inset between Label border and text.
149  
150 =head2 topInset
151  
152 Type: uint
153  
154 Default: 0
155  
156 The top inset between Label border and text.
157  
158 =head2 leftInset
159  
160 Type: uint
161  
162 Default: 0
163  
164 The left inset between Label border and text.
165  
166 =head2 rightInset
167  
168 Type: uint
169  
170 Default: 0
171  
172 The right inset between Label border and text.
173  
174 =head2 rotationAngle
175  
176 Type: float
177  
178 Default: 0
179  
180 Rotation of the whole Label.
181  
182 =head2 alignment
183  
184 Type: descrete values ( center | right | left )
185  
186 Default: 'center'
187  
188 The text alignment.
189  
190 =head2 fontFamily
191  
192 Type: Fontstring
193  
194 Default: 'Dialog'
195  
196 The font for the Label text.
197  
198 As fonts differ on systems and platforms, this is not a descrete values property, be sure to choose a proper value (e.g. look into a yEd created graphml).
199 The default font 'Dialog' seems to be always present.
200  
201 =head2 fontSize
202  
203 Type: uint
204  
205 Default: 12
206  
207 The font size.
208  
209 =head2 fontStyle
210  
211 Type: descrete values ( plain | bold | italic | bolditalic )
212  
213 Default: 'plain'
214  
215 The font style.
216  
217 =head2 textColor
218  
219 Type: '#0000fa' (rgb) or '#000000cc' (rgb + transparency) java.awt.Color hex form or 'none'
220  
221 Default: '#000000'
222  
223 The text color.
224  
225 =head2 underlinedText
226  
227 Type: bool
228  
229 Default: false
230  
231 Whether the text is underlined or not.
232  
233 =head1 SUBROUTINES/METHODS
234  
235 =head2 new
236  
237 Creates a new instance of a Label typ type.
238  
239 A value for the C<text> property must be provided as first parameter.
240  
241 Further parameters to set properties are optional (C<property1 =E<gt> value, property2 =E<gt> value2, ...>).
242  
243 =head3 EXAMPLE
244  
245 my $label = yEd::Label::NodeLabel->new('hello world', 'underlinedText' => 1, 'textColor' => '#ff0000');
246  
247 =cut
248  
249 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
250 0 confess 'you may not instantiate a yEd::Label base class object';
251 }
252  
253 =head2 copy
254  
255 Creates a copy of this Label and returns it.
256  
257 You may optionally specify properties in the form C<property1 =E<gt> value, property2 =E<gt> value2, ...> to change these properties for the returned copy.
258  
259 =head3 EXAMPLE
260  
261 my $newlabel = $label->copy();
262 my $newlabel2 = $label->copy('text' => 'my new text');
263  
264 =cut
265  
266 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
267 0 my ($self, @p) = @_;
268 0 my $ref = ref $self;
269 0 my $o = $ref->new();
270 0 $o->setProperties($self->getProperties());
271 0 $o->setProperties(@p);
272 0 return $o;
273 }
274  
275 2 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
276 2 my ($self, $text, @properties) = @_;
277 2 $self->text($text);
278 # defaults
279 2 $self->x(0);
280 2 $self->y(0);
281 2 $self->height(20);
282 2 $self->width(30);
283 2 $self->alignment('center');
284 2 $self->fontFamily('Dialog');
285 2 $self->fontSize(12);
286 2 $self->fontStyle('plain');
287 2 $self->textColor('#000000');
288 2 $self->visible(1);
289 2 $self->backgroundColor('none');
290 2 $self->lineColor('none');
291 2 $self->underlinedText(0);
292 2 $self->allInsets(0);
293 2 $self->rotationAngle(0);
294 # user values
295 2 $self->setProperties(@properties);
296 2 return $self;
297 }
298  
299 4 ▶sub text {
Subroutine text: called CC=1 SCAR=0.0
Pod documented
300 4 return _PROPERTY(0, @_);
301 }
302  
303 =head2 getTextString
304  
305 While the C<text> property will return the set value (which may be a ref), this will return the text form of the property as it will be shown in yEd.
306  
307 Have a look at the C<text> property description for details.
308  
309 =head3 EXAMPLE
310  
311 print $label->getTextString();
312  
313 =cut
314  
315 3 my @knownStringifier = ('toString', 'to_String');
316 2 ▶sub getTextString {
Subroutine getTextString: called CC=11 SCAR=44.2
Pod documented
317 2 my $self = shift;
318 2 my $text = $self->text();
319 2 my $ref = ref $text;
320 2 ▶ if ($ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref) 0 2 2
321 0 ▶ if ($ref eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 0 0 0
elsif (UNIVERSAL::isa($text, 'UNIVERSAL')) { } 0 0 0
elsif ($ref eq 'HASH') { } 0 0 0
322 0 return join "\n", @{$text};
323 } elsif (UNIVERSAL::isa($text,'UNIVERSAL')) {
324 0 foreach my $toStr (@knownStringifier) {
325 0 ▶ return $text->$toStr() if $text->can($toStr);
Branch: 1 branch
BranchTrueFalseTotal
if $text->can($toStr) 0 0 0
326 0 $toStr = lc $toStr;
327 0 ▶ return $text->$toStr() if $text->can($toStr);
Branch: 1 branch
BranchTrueFalseTotal
if $text->can($toStr) 0 0 0
328 }
329 } elsif ($ref eq 'HASH') {
330 0 my @keys = sort keys %{$text};
331 0 my $l = 0;
332 0 foreach my $k (@keys) {
333 0 $k =~ m/^(?:_\d+_)?(.*)$/;
334 0 ▶ $l = length($1) if (length($1) > $l);
Branch: 1 branch
BranchTrueFalseTotal
if length $1 > $l 0 0 0
335 }
336 0 $l++;
337 0 my $t = '';
338 0 foreach my $k (@keys) {
339 0 $k =~ m/^(?:_\d+_)?(.*)$/;
340 0 $t .= sprintf("%-${l}s %s\n", $1 . ':', $text->{$k});
341 }
342 0 return $t;
343 }
344 }
345 2 return "$text"; # stringify everthing else by using perl defaults
346 }
347  
348 # Labelarea
349 4 ▶sub visible {
Subroutine visible: called CC=1 SCAR=0.0
Pod documented
350 4 return _PROPERTY('bool', @_);
351 }
352 4 ▶sub x {
Subroutine x: called CC=1 SCAR=0.0
Pod documented
353 4 return _PROPERTY($match{'float'}, @_);
354 }
355 4 ▶sub y {
Subroutine y: called CC=1 SCAR=0.0
Pod documented
356 4 return _PROPERTY($match{'float'}, @_);
357 }
358 4 ▶sub height {
Subroutine height: called CC=1 SCAR=0.0
Pod documented
359 4 return _PROPERTY($match{'ufloat'}, @_);
360 }
361 4 ▶sub width {
Subroutine width: called CC=1 SCAR=0.0
Pod documented
362 4 return _PROPERTY($match{'ufloat'}, @_);
363 }
364 4 ▶sub backgroundColor {
Subroutine backgroundColor: called CC=1 SCAR=0.0
Pod documented
365 4 return _PROPERTY($match{'color'}, @_);
366 }
367 4 ▶sub lineColor {
Subroutine lineColor: called CC=1 SCAR=0.0
Pod documented
368 4 return _PROPERTY($match{'color'}, @_);
369 }
370 4 ▶sub bottomInset {
Subroutine bottomInset: called CC=1 SCAR=0.0
Pod documented
371 4 return _PROPERTY($match{'uint'}, @_);
372 }
373 4 ▶sub leftInset {
Subroutine leftInset: called CC=1 SCAR=0.0
Pod documented
374 4 return _PROPERTY($match{'uint'}, @_);
375 }
376 4 ▶sub rightInset {
Subroutine rightInset: called CC=1 SCAR=0.0
Pod documented
377 4 return _PROPERTY($match{'uint'}, @_);
378 }
379 4 ▶sub topInset {
Subroutine topInset: called CC=1 SCAR=0.0
Pod documented
380 4 return _PROPERTY($match{'uint'}, @_);
381 }
382  
383 =head2 allInsets
384  
385 This is an alternative setter for the C<inset...> properties.
386  
387 It takes one or two parameters, where the first one will set bottom and top (or all insets if there is no second parameter) and the second will set left and right.
388  
389 =head3 EXAMPLE
390  
391 $label->allInsets(10);
392 $label->allInsets(4, 8);
393  
394 =cut
395  
396 2 ▶sub allInsets {
Subroutine allInsets: called CC=3 SCAR=11.1
Pod documented
397 2 my ($self, $i1, $i2) = @_;
398 2 ▶ confess 'allInsets will only set insets, specify 1 or 2 args (top/bottom , left/right)' unless (defined $i1);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i1 0 2 2
399 2 ▶ $i2 = $i1 unless (defined $i2);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $i2 2 0 2
400 2 $self->bottomInset($i1);
401 2 $self->topInset($i1);
402 2 $self->rightInset($i2);
403 2 $self->leftInset($i2);
404 2 return;
405 }
406 4 ▶sub rotationAngle {
Subroutine rotationAngle: called CC=1 SCAR=0.0
Pod documented
407 4 return _PROPERTY($match{'float'}, @_);
408 }
409  
410 # Font
411 4 ▶sub alignment {
Subroutine alignment: called CC=1 SCAR=0.0
Pod documented
412 4 return _PROPERTY($match{'alignment'}, @_);
413 }
414 4 ▶sub fontFamily {
Subroutine fontFamily: called CC=1 SCAR=0.0
Pod documented
415 4 return _PROPERTY(0, @_);
416 }
417 4 ▶sub fontSize {
Subroutine fontSize: called CC=1 SCAR=0.0
Pod documented
418 4 return _PROPERTY($match{'uint'}, @_);
419 }
420 4 ▶sub fontStyle {
Subroutine fontStyle: called CC=1 SCAR=0.0
Pod documented
421 4 return _PROPERTY($match{'fontstyle'}, @_);
422 }
423 6 ▶sub textColor {
Subroutine textColor: called CC=1 SCAR=0.0
Pod documented
424 6 return _PROPERTY($match{'color'}, @_);
425 }
426 4 ▶sub underlinedText {
Subroutine underlinedText: called CC=1 SCAR=0.0
Pod documented
427 4 return _PROPERTY('bool', @_);
428 }
429  
430 0 ▶sub _build {
Subroutine _build: not called CC=1 SCAR=6.9
431 0 confess 'you may not build a yEd::Label base class object, override this method in a specialized subclass';
432 }
433  
434 =head2 setProperties getProperties hasProperties
435  
436 As described at L<yEd::PropertyBasedObject>
437  
438 =head1 SEE ALSO
439  
440 L<yEd::Document> for further informations about the whole package
441  
442 L<yEd::PropertyBasedObject> for further basic information about properties and their additional functions
443  
444 L<yEd::Label::EdgeLabel> for information about specialized Label elements for Edges
445  
446 L<yEd::Label::NodeLabel> for information about specialized Label elements for Nodes
447  
448 =cut
449  
450  
451 3 1;
↑ Summary Label/NodeLabel.pm »