lib/App/SeismicUnixGui/misc/flow_widgets.pm untested

filter: Statementstmt 0.0% 0 / 249 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 30 Podpod 0.0% 0 / 25 totaltotal 0.0% 0 / 351 CC 22 SCAR 21.1
CC
22
Cov
0%
delete_selection
20.0
get_current_program
20.0
clear_flow_item
12.0
CRAP 8.3
1 package App::SeismicUnixGui::misc::flow_widgets;
2  
3 =head1 DOCUMENTATION
4  
5 =head2 SYNOPSIS
6  
7 PACKAGE NAME:
8  
9 AUTHOR: Juan Lorenzo
10  
11 DATE:
12  
13 DESCRIPTION:
14 Version:
15  
16 =head2 USE
17  
18 =head3 NOTES
19  
20 =head4 Examples
21  
22 =head2 SYNOPSIS
23  
24 =head3 SEISMIC UNIX NOTES
25  
26 =head2 CHANGES and their DATES
27 V0.1.2 refactoring superflows and user-built flows
28 August 3 2018
29 V0.1.3 Dec 2 2018 reordered methods alphabetically
30 added get_num_indices
31  
32 =cut
33  
34 use Moose;
35 our $VERSION = '0.1.3';
36 use Tk;
37 use Tk::DragDrop;
38 use Tk::DropSite;
39 use aliased 'App::SeismicUnixGui::misc::L_SU_global_constants';
40  
41 my $get = L_SU_global_constants->new();
42 my $default_param_specs = $get->param();
43  
44 my $var = $get->var();
45 my $empty_string = $var->{_empty_string};
46  
47 =head2 private hash references
48  
49 16
50  
51 =cut
52  
53 my $flow_widgets = {
54 _current_get_flow_selection_index => '',
55 _first_index => '',
56 _first_item => '',
57 _flow_listbox_wref => '',
58 _index => '',
59 _index2delete => '',
60 _labels_aref => '',
61 _labels_w_aref => '',
62 _last_index => '',
63 _last_item => '',
64 _num_items => '',
65 _all_items_aref => '',
66 };
67  
68 my $true = 1;
69 my $false = 0;
70  
71 =head2 p sub _reset
72  
73 16
74  
75 =cut
76  
77 sub _reset {
78
79 my ($self) = @_;
80
81 $flow_widgets->{_current_get_flow_selection_index} = '';
82 $flow_widgets->{_first_index} = '';
83 $flow_widgets->{_first_item } = '';
84 $flow_widgets->{_flow_listbox_wref} = '';
85 $flow_widgets->{_index} = '';
86 $flow_widgets->{_index2delete} = '';
87 $flow_widgets->{_labels_aref} = '';
88 $flow_widgets->{_labels_w_aref} = '';
89 $flow_widgets->{_last_index} = '';
90 $flow_widgets->{_last_item} = '';
91 $flow_widgets->{_num_items} = '';
92 $flow_widgets->{_all_items_aref} = '';
93
94 return();
95 };
96  
97 =head2 sub clear
98  
99 delete ALL items
100 from the listbox
101  
102 =cut
103  
104 sub clear {
105  
106 my ( $self, $flow_listbox_w ) = @_;
107  
108 if ($flow_listbox_w) {
109
110 my $first_index = 0;
111 $flow_listbox_w->delete( $first_index, 'end');
112 _reset();
113  
114 } else {
115 print("2. flow_widgets, clear, missing flow_listbox_w\n");
116 }
117 return ();
118 }
119  
120 =head2 sub clear_flow_item
121  
122 empty flow item hash
123  
124 =cut
125  
126 sub clear_flow_item {
127  
128 my ( $self, $flow_listbox_w ) = @_;
129  
130 if ($flow_listbox_w) {
131  
132 my $flow_listbox = $flow_listbox_w;
133 my $num_items = $flow_listbox->size();
134 my $first_index = 0;
135  
136 $flow_widgets->{_first_index} = $first_index;
137 $flow_widgets->{_flow_listbox_wref} = $flow_listbox;
138 $flow_widgets->{_num_items} = $num_items;
139  
140 # must become an empty listbox
141 if ( $num_items == 1 ) {
142  
143 my $last_item = $flow_listbox->get('end');
144 $flow_widgets->{_last_item} = $last_item;
145 my $first_item = $flow_listbox->get($first_index);
146 my $last_index = $num_items - 1;
147 my $all_items_aref = $flow_listbox->get( $first_index, 'end' );
148
149 $flow_listbox_w->delete( $first_index, $last_index );
150 $flow_widgets->{_all_items_aref} = $all_items_aref;
151 $flow_widgets->{_last_index} = $last_index;
152 $flow_widgets->{_first_item} = $first_item;
153  
154 print("flow_widgets,clear_flow_items,last item: is $last_item\n");
155 print("flow_widgets,clear_flow_items,num_items: is $num_items\n");
156 print("flow_widgets,clear_flow_items,first_item: is $first_item\n");
157  
158 return ();
159  
160 } else {
161  
162 # print ("flow_widgets,clear_flow_items, $num_items is unexpected--NADA\n");
163 return ();
164 }
165 } else {
166 print("flow_widgets,clear_flow_items, missing flow_listbox_w\n");
167 return ();
168 }
169 }
170  
171 =head2 sub clear_items_versions_aref
172  
173 =cut
174  
175 sub clear_items_versions_aref {
176  
177 my ($self) = @_;
178  
179 my @version;
180 my ( $length, $clear_items_versions_aref );
181 my (@items_aref);
182  
183 # print("flow_widgets,clear_items_versions_aref,flow_widgets->{_all_items_aref} }= @{ $flow_widgets->{_all_items_aref} }\n");
184  
185 @items_aref = @{ $flow_widgets->{_all_items_aref} };
186 $length = $flow_widgets->{_last_index} + 1;
187  
188  
189 # initialize an array of zeroes
190 for ( my $i = 0; $i < $length; $i++ ) {
191  
192 $version[$i] = 0;
193 }
194  
195 # print("flow_widgets,clear_items_versions_aref:versions= @version\n");
196  
197 #loop through all items in the flow listbox
198 for ( my $current = 0; $current < $length; $current++ ) {
199  
200 $version[$current] = $version[$current] + 1;
201  
202 # print("flow_widgets,clear_items_versions_aref,current: @{$flow_widgets->{_all_items_aref}}[$current]\n");
203  
204 #loop from current to last item
205 for ( my $next = $current + 1; $next < $length; $next++ ) {
206  
207 my $instances = 1;
208  
209 if ( $items_aref[$current] eq $items_aref[$next] ) {
210 $version[$next] = $version[$next] + 1;
211 }
212  
213 # print("flow_widgets,clear_items_versions_aref,next: @{$flow_widgets->{_all_items_aref}}[$next]\n");
214 }
215  
216 } # end loop over all items in flow listbox
217  
218 # print("flow_widgets,clear_items_versions_aref:versions @version\n");
219 # print("flow_widgets,clear_items_versions_aref,next: @{$flow_widgets->{_all_items_aref}}\n");
220  
221 return ( \@version );
222 }
223  
224 #=head2
225 #
226 # silly limits because of weird drag and drop behavior
227 # counter can increase ( impleis deleting) but can not
228 # decrease .. imy arbitrary limit
229 #
230 #=cut
231 #
232 #sub dec_vigil_on_delete_counter {
233 # my ( $self, $value ) = @_;
234 # my $new_counter = $flow_widgets->{_leave_dropsite} - $value;
235 # $flow_widgets->{_leave_dropsite} = $new_counter;
236 #
237 # # print("flow_widgets, dec_vigil_on_delete_counter, count=$flow_widgets->{_leave_dropsite}\n");
238 # return ();
239 #}
240  
241 sub delete_selection {
242 my ( $self, $flow_listbox_w ) = @_;
243  
244 # print("flow_widgets, delete_selection, delete is active\n");
245 # print("Before deletion flow_widgets, delete_selection, \n last item: $flow_widgets->{_last_item}\n num_items:$flow_widgets->{_num_items}\n last_index:$flow_widgets->{_last_index}\n");
246  
247 # for index to delete
248 $flow_widgets->{_index2delete} = $flow_widgets->{_current_get_flow_selection_index};
249  
250 # print("flow_widgets,delete_selection,index2delete:$flow_widgets->{_index2delete}\n");
251 my $index2delete = $flow_widgets->{_index2delete};
252  
253 # move selection one up
254 if ( $index2delete >= 0 ) { # move selection one up
255 if ( $index2delete > 0 ) {
256  
257 # print("1. flow_widgets, delete_selection deleted index is $index2delete\n");
258 $flow_listbox_w->delete($index2delete);
259  
260 ; # last item to delete
261 # update private variables
262 $flow_widgets->{_last_item} = $flow_listbox_w->get('end');
263 $flow_widgets->{_num_items} = $flow_widgets->{_num_items} - 1;
264 $flow_widgets->{_last_index} = $flow_widgets->{_last_index} - 1;
265  
266 } # index to delete is 0 but not the last item
267 elsif ( $flow_widgets->{_last_index} > $index2delete ) {
268 $flow_listbox_w->delete($index2delete);
269 $flow_listbox_w->selectionSet($index2delete); # at index=0
270 # update private variables
271 $flow_widgets->{_last_item} = $flow_listbox_w->get('end');
272 $flow_widgets->{_num_items} = $flow_widgets->{_num_items} - 1;
273 $flow_widgets->{_last_index} = $flow_widgets->{_last_index} - 1;
274  
275 } # index to delete is 0 and it is the last item
276 else {
277 $flow_listbox_w->delete($index2delete);
278 $flow_widgets->{_last_item} = 'nu';
279 $flow_widgets->{_num_items} = $flow_widgets->{_num_items} - 1; #=0
280 $flow_widgets->{_last_index} = $flow_widgets->{_last_index} - 1; #=-1
281 # print("2. flow_widgets, delete_selection, deleted index: is $index2delete..\n");
282 }
283 }
284  
285 # print("3.After deletion flow_widgets,delete_selection,\nlast item: $flow_widgets->{_last_item}\n num_items,$flow_widgets->{_num_items}\n last_index $flow_widgets->{_last_index}\n");
286  
287 return ();
288 }
289  
290 sub destination_index {
291 my $self = @_;
292 my $destination_index = $flow_widgets->{_chosen_index2drop};
293 return ($destination_index);
294 }
295  
296 =head2 sub get_index2delete
297  
298  
299 =cut
300  
301 sub get_index2delete {
302  
303 my ($self) = @_;
304 return ( $flow_widgets->{_index2delete} );
305  
306 }
307  
308 =head2 sub get_current_program
309  
310 from the current listbox
311 returns a scalar reference to the element selected
312 fromt the list box
313  
314 =cut
315  
316 sub get_current_program {
317  
318 my ( $self, $widget_ref ) = @_;
319  
320 my @selection_index = $$widget_ref->curselection;
321  
322 # print("flow_widgets,get_current_program, selection index is @selection_index \n");
323  
324 if (@selection_index) { # if defined
325  
326 if ( @selection_index >= 0 ) {
327  
328 my $prog_name = $$widget_ref->get( $selection_index[0] );
329  
330 # print("1. flow_widgets get_current_program program name is $prog_name\n");
331 return ( \$prog_name );
332  
333 } else {
334 print("flow_widgets,get_current_program, unexpected selection index:@selection_index \n");
335 return ();
336 }
337  
338 } else { # no item selected or no items exist
339  
340 # make sure that the first flow item exists,
341 # (TODO) and that you have not changed color
342 my $flow_listbox_color_w = $$widget_ref;
343 my $flow_num_items = $flow_listbox_color_w->size();
344  
345 # print("2. flow_widgets,get_current_program, flow_num_items=$flow_num_items\n");
346  
347 if ( $flow_num_items > 0 ) {
348 my $index = $flow_num_items - 1; # unfounded assumption.. possible future error
349 my $program_name = $flow_listbox_color_w->get($index);
350 my $result = $program_name;
351 return ( \$result );
352  
353 } else {
354  
355 # print("flow_widgets,get_current_program, unexpected num_items=$flow_num_items\n");
356  
357 # print("flow_widgets,get_current_program, Perhaps null selection index\n");
358 return ();
359 }
360  
361 }
362  
363 }
364  
365 =head2 sub get_flow_selection
366  
367 estimates the index lselected in the listbox
368 the first item has an index=0
369
370  
371  
372 =cut
373  
374 sub get_flow_selection {
375 my ( $self, $flow_listbox_w ) = @_;
376  
377 if ( $flow_listbox_w ne $empty_string ) {
378  
379 # print("flow_widgets,get_flow_selection, flow_listbox_w: $flow_listbox_w\n");
380  
381 my @selection_index = $flow_listbox_w->curselection;
382  
383 if (@selection_index) {
384  
385 # print("flow_widgets,get_flow_selection,selection_index= @selection_index\n");
386 $flow_widgets->{_flow_listbox_wref} = $flow_listbox_w;
387 my $current_selected_index = $selection_index[0];
388  
389 $flow_widgets->{_current_get_flow_selection_index} = $current_selected_index;
390  
391 # print("flow_widgets,get_flow_selection--is integer: $current_selected_index\n");
392  
393 return ($current_selected_index);
394  
395 } else {
396  
397 # print("flow_widgets,get_flow_selection,selection_index is missing, need to select an item again NADA\n");
398 return ();
399 }
400  
401 } else {
402  
403 # print("flow_widgets,get_flow_selection, missing flow_listbox widget returns -1\n");
404 my $result = -1;
405 return ($result);
406 }
407  
408 }
409  
410 =head2 get_num_indices
411  
412 how many (items-1) are there in a listbox
413 input is a widget
414 output is a scalar
415  
416 =cut
417  
418 sub get_num_indices {
419  
420 my ( $self, $flow_listbox_w ) = @_;
421  
422 if ($flow_listbox_w) {
423  
424 my $num_indices = ( $$flow_listbox_w->size() - 1 );
425  
426 # print("2. flow_widgets,get_num_indices,num_indices=$num_indices\n");
427 return ($num_indices);
428  
429 } else {
430 print("2. flow_widgets,get_num_indices,missing flow_listbox_w\n");
431 return ();
432 }
433  
434 }
435  
436 =head2 get_num_items
437  
438 how many items are there in a listbox
439 input is a widget from the main flow
440  
441 =cut
442  
443 sub get_num_items {
444  
445 my ( $self, $flow_listbox_w ) = @_;
446  
447 if ($flow_listbox_w) {
448  
449 my $num_items = $flow_listbox_w->size();
450  
451 # print("2. flow_widgets,get_num_items,num_items=$num_items\n");
452 return ($num_items);
453  
454 } else {
455 print("2. flow_widgets,get_num_items,missing flow_listbox_w\n");
456 return ();
457 }
458  
459 }
460  
461 sub inc_vigil_on_delete_counter {
462 my ($self) = @_;
463 $flow_widgets->{_leave_dropsite}++;
464  
465 # print("flow_widgets, inc_vigil_on_delete_counter, count=$flow_widgets->{_leave_dropsite}\n");
466 }
467  
468 sub local_get_chosen_index2drag {
469 my ($self) = @_;
470  
471 # print("flow_widgets,local_get_chosen_index2drag is $flow_widgets->{_chosen_index2drag}\n");
472 return ( $flow_widgets->{_chosen_index2drag} );
473 }
474  
475 sub local_get_vigil_on_delete {
476 my ($self) = @_;
477  
478 # print("flow_widgets,local_get_vigil_on_delete, count=$flow_widgets->{_leave_dropsite}\n");
479 return ( $flow_widgets->{_leave_dropsite} );
480 }
481  
482 sub get_vigil_on_delete {
483 my ($self) = @_;
484  
485 # print("flow_widgets,get_vigil_on_delete, count=$flow_widgets->{_leave_dropsite}\n");
486 return ( $flow_widgets->{_leave_dropsite} );
487 }
488  
489 sub index2move {
490 my $self = @_;
491 my $index2move = $flow_widgets->{_chosen_index2drag};
492 return ($index2move);
493 }
494  
495 =head2 sub is_drag_deleted
496  
497 =cut
498  
499 sub is_drag_deleted {
500  
501 my ( $self, $flow_listbox_grey_w_w ) = @_;
502  
503 # print("flow_widgets, is_drag_deleted,flow_listbox_grey_w=$flow_listbox_grey_w_w \n");
504 my $num_items;
505 my $check;
506 my $true = 1;
507 my $false = 0;
508 my $is_deleted = 0;
509  
510 $num_items = local_get_num_items($flow_listbox_grey_w_w);
511 $check = local_get_vigil_on_delete();
512  
513 # print("1.flow_widgets, is_drag_deleted, #num_items=$num_items\n ");
514 # print("1.flow_widgets, is_drag_deleted vigil_on_delete count is $check\n ");
515 if ( $num_items >= 1 ) {
516 if ( $check > -1 && $check <= 3 ) {
517 $is_deleted = $true;
518  
519 # print("2.flow_widgets, is_drag_deleted vigil_on_delete count is $check\n ");
520 # print("2.flow_widgets, is_drag_deleted?, yes, deletion occurred\n");
521 local_set_vigil_on_delete();
522 }
523 return ($is_deleted);
524 }
525 }
526  
527 =head2 sub local_set_vigil_on_delete
528  
529 If $leave_dropsite >-1 , < 2 then we have deleted an item
530 TODO consider when (1) new flow item is selected or
531 (2)new sunix box item is selected
532  
533 leave_dropsite will increment once when item is moved (=0)
534 and two more times (=2)if it is dropeed withint he listbox
535 however it is only incremented twice (=1) if drop FAILS
536  
537 leave_dropsite is a counter that is used to see if
538 during DnD the mouse left the listbox window
539  
540 =cut
541  
542 sub local_set_vigil_on_delete {
543 my ($self) = @_;
544 $flow_widgets->{_leave_dropsite} = -1;
545  
546 # print("flow_widgets, local_set_vigil_on_delete, RESTART (=-1)\n");
547 return ();
548 }
549  
550 sub set_vigil_on_delete {
551 my ($self) = @_;
552 $flow_widgets->{_leave_dropsite} = -1;
553  
554 # print("flow_widgets, set_vigil_on_delete, RESTART (=-1)\n");
555 return ();
556 }
557  
558 =head2 sub items_aref
559  
560 =cut
561  
562 sub items_aref {
563  
564 my ($self) = @_;
565  
566 my $items_aref = $flow_widgets->{_all_items_aref};
567  
568 #print("flow_widgets,items_aref: all items @$items_aref\n");
569  
570 return ($items_aref);
571 }
572  
573 =head2 sub items_versions_aref
574  
575 =cut
576  
577 sub items_versions_aref {
578  
579 my ($self) = @_;
580  
581 my @version;
582 my ( $length, $items_versions_aref );
583 my (@items_aref);
584  
585 @items_aref = @{ $flow_widgets->{_all_items_aref} };
586 $length = $flow_widgets->{_last_index} + 1;
587  
588 # print("flow_widgets,items_versions_aref,length is $length\n");
589 # initialize an array of zeroes
590 for ( my $i = 0; $i < $length; $i++ ) {
591 $version[$i] = 0;
592 }
593  
594 #print("flow_widgets,items_versions_aref:versions @version\n");
595  
596 #loop through all items in the flow listbox
597 for ( my $current = 0; $current < $length; $current++ ) {
598 $version[$current] = $version[$current] + 1;
599  
600 #print("flow_widgets,items_versions_aref,current: @{$flow_widgets->{_all_items_aref}}[$current]\n");
601  
602 #loop from current to last item
603 for ( my $next = $current + 1; $next < $length; $next++ ) {
604 my $instances = 1;
605  
606 if ( $items_aref[$current] eq $items_aref[$next] ) {
607 $version[$next] = $version[$next] + 1;
608 }
609  
610 #print("flow_widgets,items_versions_aref,next: @{$flow_widgets->{_all_items_aref}}[$next]\n");
611 }
612  
613 } # end loop over all items in flow listbox
614  
615 # print("flow_widgets,items_versions_aref:versions @version\n");
616 # print("flow_widgets,items_versions_aref,next: @{$flow_widgets->{_all_items_aref}}\n");
617  
618 return ( \@version );
619 }
620  
621 =head2 sub set_flow_items
622  
623 collect listbox widget reference
624  
625 =cut
626  
627 sub set_flow_items {
628  
629 my ( $self, $widget_ref ) = @_;
630 if ($widget_ref) {
631 my $flow_listbox = $widget_ref;
632 my $num_items = $flow_listbox->size();
633 my $first_index = 0;
634 $flow_widgets->{_first_index} = $first_index;
635 $flow_widgets->{_flow_listbox_wref} = $flow_listbox;
636 $flow_widgets->{_num_items} = $num_items;
637  
638 # not an empty listbox
639 if ( $num_items > 0 ) {
640 my $last_item = $flow_listbox->get('end');
641 $flow_widgets->{_last_item} = $last_item;
642 my $first_item = $flow_listbox->get($first_index);
643 my $last_index = $num_items - 1;
644 my $all_items_aref = $flow_listbox->get( $first_index, 'end' );
645 $flow_widgets->{_all_items_aref} = $all_items_aref;
646 $flow_widgets->{_last_index} = $last_index;
647 $flow_widgets->{_first_item} = $first_item;
648  
649 # print("flow_widgets,set_flow_items,last item: is $last_item\n");
650 # print("flow_widgets,set_flow_items,num_items: is $num_items\n");
651 # print("flow_widgets,set_flow_items,first_item: is $first_item\n");
652 }
653 return ();
654 }
655 }
656  
657 =head2 local_get_num_items
658  
659 how many items are there in a listbox
660 input is a widget
661 output is a scalar
662  
663 =cut
664  
665 sub local_get_num_items {
666  
667 my ($flow_listbox_w) = @_;
668  
669 my $num_items = $flow_listbox_w->size();
670  
671 # print("flow_widgets,local_get_num_items,num_items=$num_items\n");
672  
673 return ($num_items);
674  
675 }
676  
677 =head2 sub num_items
678  
679 collect listbox widget reference
680 print("flow_widgets,flow_times: last_item $last_item\n");
681 print("flow_widgets,flow_times: first_item $first_item\n");
682 print("flow_widgets,flow_times: size $num_items\n");
683  
684 =cut
685  
686 sub num_items {
687  
688 my ( $self, $widget_ref ) = @_;
689 if ($widget_ref) {
690 my $flow_listbox = $widget_ref;
691 my $num_items = $flow_listbox->size();
692 $flow_widgets->{_num_items} = $num_items;
693  
694 return ($num_items);
695 }
696 }
697  
698 =head2 sub set_num_items
699  
700 =cut
701  
702 sub set_num_items {
703 my ( $self, $widget_ref ) = @_;
704 if ($widget_ref) {
705 my $flow_listbox = $widget_ref;
706 my $num_items = $flow_listbox->size();
707 $flow_widgets->{_num_items} = $num_items;
708 return ();
709 }
710 return ();
711 }
712  
713 #
714 #
715 #=head2
716 #
717 # $TkPl_SU->{_listbox_size} = $flow_listbox->size;
718 # print("main,delete_from_flow_commands,listbox size before ->delete is $TkPl_SU->{_listbox_size}\n\n\n");
719 # $TkPl_SU->{_listbox_size} = $flow_listbox->size;
720 # print("main,delete_from_flow_select_commands,listbox size after ->delete is $TkPl_SU->{_listbox_size}\n\n\n");
721 #
722 #=cut
723  
724 1;