DTree.pm untested

filter: Statementstmt 0.0% 0 / 265 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 35 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 465 CC 54 SCAR 39.9
CC
54
Cov
0%
sort
240.0
reverse
42.0
add
30.0
CRAP 54.1
1 ######################################## SOH ###########################################
2 ## Function : Additional Tk Class for List-type Trees with Data per Item, Sorting
3 ##
4 ## Copyright (c) 2002-2004 Michael Krause. All rights reserved.
5 ## This program is free software; you can redistribute it and/or modify it
6 ## under the same terms as Perl itself.
7 ##
8 ## History : V1.00 15-Dec-2002 Class adopted from DHList. MK
9 ## V1.01 19-Jan-2004 Added numeric sorting for column 2. MK
10 ##
11 ######################################## EOH ###########################################
12 package Tk::DTree;
13  
14 ##############################################
15 ### Use
16 ##############################################
17 use Tk::Tree;
18 use Tk::ItemStyle;
19 use Tk qw(Ev);
20  
21 use strict;
22 use Carp;
23  
24 use vars qw ($VERSION);
25 $VERSION = '1.01';
26  
27 use base qw (Tk::Derived Tk::Tree);
28  
29 ########################################################################
30 Construct Tk::Widget 'DTree';
31  
32 #---------------------------------------------
33 # internal Setup function
34 #---------------------------------------------
35 sub ClassInit
36 {
37 my ($class, $window) = @_;
38  
39 $class->SUPER::ClassInit($window);
40  
41 # Note these keyboard-Keys are only usable, if the widget gets 'focus'
42 $window->bind ($class, '<Control-Key-bracketleft>', [\&viewtype, 'withdata']);
43 $window->bind ($class, '<Control-Key-bracketright>', [\&viewtype, 'normal']);
44 # Update the view after mapping
45 $window->bind ($class, '<Map>', \&map_cb);
46 }
47  
48 #---------------------------------------------
49 # internal Setup function
50 #---------------------------------------------
51 sub CreateArgs
52 {
53 my ($class, $window, $args) = @_;
54
55 # Necessarily Patch Columns
56 my %args;
57 (%args) = ( -columns => '2') unless defined $args->{-columns};
58 ($class->SUPER::CreateArgs($window, $args), %args);
59 }
60  
61 #---------------------------------------------
62 # internal Setup function
63 #---------------------------------------------
64 sub Populate
65 {
66 my ($this, $args) = @_;
67  
68 my $data_background = delete $args->{-databackground};
69 $data_background = $this->cget ('-background') unless defined $data_background;
70 my $style = delete $args->{-datastyle};
71 $style = $this->toplevel->ItemStyle ('text' ,
72 -anchor => 'e',
73 -background => $data_background,
74 ) unless defined $style;
75  
76 # Check whether we want numeric sorting
77 $this->{m_numeric_primary_sort} = delete $args->{-numeric_primary_sort} || 0;
78 $this->{m_numeric_secondary_sort} = delete $args->{-numeric_secondary_sort} || 0;
79
80 # Reroute any size_call_back
81 my $sizecmd = delete $args->{-sizecmd};
82 $sizecmd = sub { return 1 } unless defined $sizecmd;
83 $args->{-sizecmd} = [\&resize_cb, $this ];
84  
85 #INvoke Superclass fill func
86 $this->SUPER::Populate($args);
87  
88 #
89 $this->ConfigSpecs(
90 #default listbox options
91 -pady => [['SELF', 'PASSIVE'], 'pady', 'Pad', '0'],
92 -sizecmd_cb => ['CALLBACK',undef, undef, $sizecmd],
93 # new, additional optiona
94 -viewtype => ['METHOD', 'ViewType', 'viewType', undef],
95 -datastyle => [['SELF', 'PASSIVE'], 'datastyle', 'datastyle', $style],
96 );
97
98 # Internal Presets
99 $this->{m_firstpath} = undef;
100 $this->{m_datastyle} = $style;
101 $this->{m_viewtype} = '---';
102 }
103  
104 #---------------------------------------------
105 # OVERRIDE: new ADD function
106 #---------------------------------------------
107 sub add
108 {
109 # Locals
110 my ($this, $path, %args, $data, $datastyle);
111  
112 # Parameters
113 $this = shift;
114 $path = shift;
115 %args = @_;
116
117 # Do we have anything at all to insert ?
118 return unless @_;
119  
120 # Prepare the data and it's style
121 $data = (defined $args{-data}) ? delete $args{-data} : 'undef';
122 $datastyle = (defined $args{-datastyle}) ? delete $args{-datastyle} : $this->{m_datastyle};
123
124  
125 # now add it as desired
126 $this->SUPER::add($path, -data => $data);
127 $this->SUPER::itemCreate($path, 0, %args);
128 # and a second column for the data
129 $this->SUPER::itemCreate($path, 1,
130 -itemtype => 'text',
131 -text => $data,
132 -style => $datastyle,
133 );
134 # store it internally
135 $this->{m_firstpath} = $path unless defined $this->{m_firstpath};
136 # store it internally
137 $this->{m_style}{$path} = $args{-style};
138 $this->{m_datastyle}{$path} = $datastyle;
139
140 # Install the 'normal view after we have something on the screen..
141 $this->viewtype($this->{m_viewtype});
142 }
143  
144 #---------------------------------------------
145 # OVERRIDE: new DELETE function
146 #---------------------------------------------
147 sub delete
148 {
149 # Parameters
150 my ($this, $what, $path) = @_;
151  
152 if ($what eq 'all') {
153 # Clear the internal storage
154 $this->{m_firstpath} = undef;
155 # Delete it
156 $this->SUPER::delete($what);
157 }
158 else {
159 # Clear the internal storage
160 if (defined $this->{m_firstpath}) {
161 if ($path eq $this->{m_firstpath}) {
162 $this->{m_firstpath} = undef;
163 }
164 }
165 # Delete it
166 $this->SUPER::delete($what, $path);
167 }
168 }
169  
170 #---------------------------------------------
171 # ADD-ON: reordering function
172 #---------------------------------------------
173 sub reverse
174 {
175 # Locals
176 my ($this, @paths, $path, @allitems, $opt, $value, %items, %data, %hidden);
177  
178 # Parameters
179 $this = shift;
180  
181 # safety check
182 return unless defined $this->{m_firstpath};
183 # Retrieve the contents
184 $path = $this->{m_firstpath};
185 while (defined $path && $path ne "") {
186 push @paths, $path;
187 #----------------------------------
188 if ($this->{m_style}{$path}) { # avoid silly error-msgs
189 $this->itemConfigure($path, 0, -style => undef);
190 $this->itemConfigure($path, 1, -style => undef);
191 }
192 @allitems = $this->itemConfigure($path, 0);
193 my %args = ();
194 foreach (@allitems) {
195 $opt = $_->[0]; $value = $_->[4];
196 if (defined $value && $value ne "") {
197 $args{$opt} = $value;
198 }
199 }
200 $items{$path} = \%args;
201 #----------------------------------
202 my $data = $this->infoData($path);
203 #print "retrieved data $data<\n";
204 $data{$path} = $data;
205 #----------------------------------
206 my $hidden = $this->infoHidden($path);
207 #print "retrieved hidden $hidden<\n";
208 $hidden{$path} = $hidden;
209 #----------------------------------
210 $path = $this->infoNext($path);
211 }
212 # delete it
213 $this->delete('all');
214  
215 $this->{m_firstpath} = undef;
216  
217 my @rpaths = reverse @paths;
218 my @revpaths = $this->reorder_hierarchy(@rpaths);
219  
220  
221 # Reverse, refill it
222 foreach $path ( @revpaths ) {
223 $this->add( $_, -data => $data{$path},
224 %{$items{$path}},
225 -style => $this->{m_style}{$path},
226 -datastyle => $this->{m_datastyle}{$path},
227 );
228 if ($hidden{$path} == 1) {
229 $this->hide('entry', $path);
230 }
231 $this->{m_firstpath} = $path unless defined $this->{m_firstpath};
232 }
233 $this->autosetmode();
234 }
235 sub reorder_hierarchy
236 {
237 # Parameters
238 my $this = shift;
239  
240 # Parameters
241 my @paths_in = @_;
242
243 # Locals
244 my (@paths_out, $sepcnt, $found, $max, $sepchar, $cnt);
245
246 # Initialization
247 $sepcnt = 1;
248 $found = 0;
249 $max = scalar @paths_in;
250 $sepchar = $this->cget(-separator);
251 #Loop
252 while (scalar @paths_in != $found) {
253 foreach (@paths_in) {
254 my @dummy = split(/$sepchar/, $_);
255 $cnt = @dummy;
256 if ($cnt == $sepcnt) {
257 push @paths_out, $_ ;
258 $found++;
259 }
260 }
261 $sepcnt++;
262 }
263 return @paths_out;
264 }
265 #---------------------------------------------
266 # ADD-ON: sorting function
267 #---------------------------------------------
268 sub sort
269 {
270 # Locals
271 my ($this, $mode, @paths, $path, @allitems, $opt, $value, %items, %data, %hidden,
272 @sortarray, @tmparray);
273 #print "begin of sort\n";
274  
275 # Parameters
276 $this = shift;
277 $mode = shift; $mode = 'ascending' unless defined $mode;
278  
279 # safety check
280 return unless defined $this->{m_firstpath};
281  
282 # Retrieve the contents
283 $path = $this->{m_firstpath};
284 while (defined $path && $path ne "") {
285 push @paths, $path;
286 #----------------------------------
287 if ($this->{m_style}{$path}) { # avoid silly error-msgs
288 $this->itemConfigure($path, 0, -style => undef);
289 $this->itemConfigure($path, 1, -style => undef);
290 }
291 @allitems = $this->itemConfigure($path, 0);
292 my %args = ();
293 foreach (@allitems) {
294 $opt = $_->[0]; $value = $_->[4];
295 #$opt = 'undef' unless defined $opt;
296 #$value = 'undef' unless defined $value;
297 #print "retrieved \$opt = >$opt<, \$value = >", defined $value ? $value : 'undef', ,"<\n";
298 if (defined $value && $value ne "") {
299 $args{$opt} = $value;
300 }
301 }
302 $items{$path} = \%args;
303 #----------------------------------
304 my $data = $this->infoData($path);
305 #print "retrieved data $data<\n";
306 $data{$path} = $data;
307 #----------------------------------
308 my $hidden = $this->infoHidden($path);
309 #print "retrieved hidden $hidden<\n";
310 $hidden{$path} = $hidden;
311 #----------------------------------
312 $path = $this->infoNext($path);
313 }
314 # delete it
315 $this->delete('all');
316
317 foreach (@paths) {
318 push @sortarray, [ $_, $items{$_}->{-text}, $data{$_}];
319 }
320  
321 # sort it
322 if ($mode =~ /ascending/i) {
323 if ($mode =~ /secondary/i) {
324 if ($this->{m_numeric_secondary_sort}) {
325 @tmparray = sort secondary_numeric @sortarray;
326 }
327 else {
328 @tmparray = sort secondary @sortarray;
329 }
330 }
331 else {
332 if ($this->{m_numeric_primary_sort}) {
333 @tmparray = sort primary_numeric @sortarray;
334 }
335 else {
336 @tmparray = sort primary @sortarray;
337 }
338 }
339 }
340 elsif ($mode =~ /descending/i) {
341 if ($mode =~ /secondary/i) {
342 if ($this->{m_numeric_secondary_sort}) {
343 @tmparray = sort rev_secondary_numeric @sortarray;
344 }
345 else {
346 @tmparray = sort rev_secondary @sortarray;
347 }
348 }
349 else {
350 if ($this->{m_numeric_primary_sort}) {
351 @tmparray = sort rev_primary_numeric @sortarray;
352 }
353 else {
354 @tmparray = sort rev_primary @sortarray;
355 }
356 }
357 }
358 else {
359 return;
360 }
361  
362 #
363 $this->{m_firstpath} = undef;
364  
365 # Retrieve the sorted path-array
366 my @rpaths;
367 foreach ( @tmparray ) {
368 push @rpaths, $_->[0];
369 }
370 my @reordered_paths = $this->reorder_hierarchy(@rpaths);
371
372 # refill it
373 foreach ( @reordered_paths ) {
374 $path = $_;
375 $this->add( $path, -data => $data{$path},
376 %{$items{$path}},
377 -style => $this->{m_style}{$path},
378 -datastyle => $this->{m_datastyle}{$path},
379 );
380 if ($hidden{$path} == 1) {
381 $this->hide('entry', $path);
382 }
383 $this->{m_firstpath} = $path unless defined $this->{m_firstpath};
384 }
385 $this->autosetmode();
386 }
387 sub secondary
388 {
389 $a->[2] cmp $b->[2];
390 }
391 sub rev_secondary
392 {
393 $b->[2] cmp $a->[2];
394 }
395 sub secondary_numeric
396 {
397 $a->[2] <=> $b->[2];
398 }
399 sub rev_secondary_numeric
400 {
401 $b->[2] <=> $a->[2];
402 }
403 sub primary
404 {
405 $a->[1] cmp $b->[1];
406 }
407 sub rev_primary
408 {
409 $b->[1] cmp $a->[1];
410 }
411 sub primary_numeric
412 {
413 $a->[1] <=> $b->[1];
414 }
415 sub rev_primary_numeric
416 {
417 $b->[1] <=> $a->[1];
418 }
419  
420 #---------------------------------------------
421 # ADD-ON: trace any viewtype updates
422 # supported values are 'normal', 'withdata'
423 #---------------------------------------------
424 sub viewtype
425 {
426 # Parameters
427 my ($this, $viewtype) = @_;
428 # if we're not in a cget we might consider changing the value
429 if (defined $viewtype) {
430 #print "we're in viewtype with $viewtype\n";
431 # allow only 'normal' & 'withdata'...
432 if ($viewtype =~ /withdata/i ) {
433 $viewtype = 'withdata';
434 }
435 else {
436 $viewtype = 'normal';
437 }
438 #
439 #print "internal viewtype is $this->{m_viewtype}\n";
440 if ($this->{m_viewtype} ne $viewtype) {
441 $this->{m_viewtype} = $viewtype;
442 $this->setup_view();
443 }
444 }
445 # needed for cget
446 return $this->{m_viewtype};
447 }
448  
449 #---------------------------------------------
450 # ADD-ON: setup_view: display function
451 #---------------------------------------------
452 sub setup_view
453 {
454 # Parameters
455 my $this = shift;
456  
457 # Locals
458 my (@bb, $needsize);
459 #print "firstpath = ", (defined $this->{m_firstpath}) ? $this->{m_firstpath} : "undef";
460 return unless defined $this->{m_firstpath};
461 #print "in setupview\n";
462 if ($this->{m_viewtype} eq 'withdata') {
463 $this->columnWidth(1, '');
464 $needsize = $this->columnWidth(1);
465 }
466 else {
467 $this->columnWidth(1, 0);
468 $needsize = 0;
469 }
470 @bb = $this->infoBbox($this->{m_firstpath});
471 # Fast hack to avoid empty bbox due to invisible first item
472 if (@bb == 0) {
473 my $path = $this->nearest(10);
474 @bb = $this->infoBbox($path);
475 }
476 #print "bbox is @bb<, needsize is $needsize\n";
477 if (@bb) {
478 $this->columnWidth(0, $bb[2] - $bb[0] - $needsize);
479 }
480 }
481  
482 #---------------------------------------------
483 # ADD-ON: new resizxing function
484 #---------------------------------------------
485 sub resize_cb
486 {
487 my ($this) = shift;
488 return unless defined $this->{m_firstpath};
489 return unless $this->viewable;
490
491 my $path0 = $this->{m_firstpath};
492 my @bb = $this->infoBbox($path0);
493 #print "bbox is >@bb<\n";
494 return if ((scalar @bb == 0) || ($bb[2] - $bb[0] == 0));
495 my $needsize = $this->columnWidth(1);
496 $this->columnWidth(0, $bb[2] - $bb[0] - $needsize);
497  
498 # invoke any given callback
499 my @args = ( $this );
500 $this->Callback(-sizecmd_cb => @args);
501 }
502  
503  
504 #---------------------------------------------
505 # CALLBACK: update the view (type 'normal')
506 # means Data-Column is invisible
507 #---------------------------------------------
508 sub map_cb
509 {
510 # Parameters
511 my $this = shift;
512 $this->setup_view();
513 }
514  
515 #---------------------------------------------
516 # ADD-ON: get Item and / or ItemData
517 # returns a scalar(first only) or an array
518 #---------------------------------------------
519 sub get_item
520 {
521 # Parameters
522 my ($this, $path) = @_;
523  
524 # get all information
525 my @items_out = $this->_get_item(3, $path);
526  
527 return wantarray ? @items_out : $items_out[0];
528 }
529 #---------------------------------------------
530 sub get_item_text
531 {
532 # Parameters
533 my ($this, $path) = @_;
534  
535 # get all information
536 my @items_out = $this->_get_item(1, $path);
537  
538 return wantarray ? @items_out : $items_out[0];
539 }
540 #---------------------------------------------
541 sub get_item_value
542 {
543 # Parameters
544 my ($this, $path) = @_;
545  
546 # get all information
547 my @items_out = $this->_get_item(2, $path);
548  
549 return wantarray ? @items_out : $items_out[0];
550 }
551 #---------------------------------------------
552 sub _get_item
553 {
554 # Parameters
555 my ($this, $mode, $path) = @_;
556  
557 # Locals
558 my (@items_out);
559  
560 if ($mode & 1) {
561 push @items_out, $this->itemCget($path, 0, '-text');
562 }
563 if ($mode & 2) {
564 push @items_out, $this->infoData($path);
565 }
566 return wantarray ? @items_out : $items_out[0];
567 }
568  
569 #---------------------------------------------
570 # ADD-ON: Get Current Selected Text AND/OR associated Data
571 #---------------------------------------------
572 sub getcurselection
573 {
574 # Parameters
575 my $this = shift;
576  
577 # get all information
578 my @items_out = $this->_getcurselection(3);
579  
580 return wantarray ? @items_out : $items_out[0];
581 }
582 #---------------------------------------------
583 sub getcurselection_text
584 {
585 # Parameters
586 my $this = shift;
587  
588 # get all information
589 my @items_out = $this->_getcurselection(1);
590  
591 return wantarray ? @items_out : $items_out[0];
592 }
593 #---------------------------------------------
594 sub getcurselection_value
595 {
596 # Parameters
597 my $this = shift;
598  
599 # get all information
600 my @items_out = $this->_getcurselection(2);
601  
602 return wantarray ? @items_out : $items_out[0];
603 }
604  
605 #---------------------------------------------
606 sub _getcurselection
607 {
608 # Parameters
609 my ($this) = shift;
610 my ($mode) = shift;
611  
612 # Locals
613 my (@items_out, @selitems, $path);
614  
615 # get index information
616 @selitems = $this->infoSelection;
617 foreach $path (@selitems) {
618 if ($mode & 1) {
619 push @items_out, $this->itemCget($path, 0, '-text' );
620 }
621 if ($mode & 2) {
622 push @items_out, $this->infoData($path);
623 }
624 }
625 return wantarray ? @items_out : $items_out[0];
626 }
627  
628  
629 ########################################################################
630 1;
631 __END__