Tk/ListEditor.pm untested

filter: Statementstmt 0.0% 0 / 277 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 0.0% 0 / 28 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 405 CC 27 SCAR 27.9
CC
27
Cov
0%
Populate
56.0
move_up
30.0
set_entry
20.0
CRAP 16.3
1 #
2 # This file is part of Config-Model-TkUI
3 #
4 # This software is Copyright (c) 2008-2021 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::Tk::ListEditor 1.382;
11  
12 use strict;
13 use warnings;
14 use Carp;
15 use Log::Log4perl;
16  
17 use base qw/Config::Model::Tk::ListViewer/;
18 use subs qw/menu_struct/;
19 use vars qw/$icon_path/;
20 use Config::Model::Tk::NoteEditor;
21 use Config::Model::Tk::CmeDialog;
22  
23 Construct Tk::Widget 'ConfigModelListEditor';
24  
25 my @fbe1 = qw/-fill both -expand 1/;
26 my @fxe1 = qw/-fill x -expand 1/;
27 my @fx = qw/-fill x /;
28 my $logger = Log::Log4perl::get_logger("Tk::ListEditor");
29  
30 my ( $up_img, $down_img, $eraser_img, $remove_img, $sort_img );
31 *icon_path = *Config::Model::TkUI::icon_path;
32  
33 my $entry_width = 20;
34  
35 sub ClassInit {
36 my ( $cw, $args ) = @_;
37  
38 # ClassInit is often used to define bindings and/or other
39 # resources shared by all instances, e.g., images.
40 # cw->Advertise(name=>$widget);
41 }
42  
43 sub Populate {
44 my ( $cw, $args ) = @_;
45  
46 my $list = $cw->{list} = delete $args->{-item}
47 || die "ListEditor: no -item, got ", keys %$args;
48  
49 delete $args->{-path};
50 my $cme_font = delete $args->{-font};
51  
52 $cw->{store_cb} = delete $args->{-store_cb}
53 or die __PACKAGE__, "no -store_cb";
54  
55 unless ( defined $up_img ) {
56 $up_img = $cw->Photo( -file => $icon_path . 'up.png' );
57 $down_img = $cw->Photo( -file => $icon_path . 'down.png' );
58 $eraser_img = $cw->Photo( -file => $icon_path . 'eraser.png' );
59 $remove_img = $cw->Photo( -file => $icon_path . 'remove.png' );
60 $sort_img = $cw->Photo( -file => $icon_path . 'dbgrun.png' );
61 }
62  
63 $cw->add_header( Edit => $list )->pack(@fx);
64  
65 my $balloon = $cw->Balloon( -state => 'balloon' );
66  
67 my $inst = $list->instance;
68  
69 my $value_type = $list->get_cargo_info('value_type'); # may be undef
70  
71 my $elt_button_frame = $cw->Frame(qw/-relief raised -borderwidth 2/)->pack(@fbe1);
72 my $frame_title = $list->element_name;
73 $frame_title .= ( defined $value_type and $value_type =~ /node/ ) ? ' elements' : ' list';
74 $elt_button_frame->Label( -text => $frame_title )->pack();
75  
76 my $tklist = $elt_button_frame->Scrolled(
77 'Listbox',
78 -selectmode => 'single',
79 -scrollbars => 'oe',
80 -height => 8,
81 )->pack(@fbe1);
82  
83 $balloon->attach(
84 $tklist,
85 -msg => 'select an element and perform an action with one of the buttons below'
86 );
87  
88 my $right_frame = $elt_button_frame->Frame->pack( @fxe1, qw/-side right -anchor n/ );
89  
90 $cw->ConfigModelNoteEditor( -object => $list )->pack;
91 $cw->add_summary($list)->pack(@fx);
92 $cw->add_description($list)->pack(@fbe1);
93 $cw->add_warning( $list, 'edit' )->pack(@fx);
94 $cw->add_info_button($cw)->pack(@fx);
95  
96 my $mv_rm_frame = $right_frame->Frame->pack(@fx);
97  
98 my $move_up_b = $mv_rm_frame->Button(
99 -image => $up_img,
100 -command => sub { $cw->move_up; },
101 )->pack( -side => 'left', @fxe1 );
102 $balloon->attach( $move_up_b, -msg => 'Move selected element up the list' );
103  
104 my $move_down_b = $mv_rm_frame->Button(
105 -image => $down_img,
106 -command => sub { $cw->move_down; },
107 )->pack( -side => 'left', @fxe1 );
108 $balloon->attach( $move_down_b, -msg => 'Move selected element down the list' );
109  
110 my $eraser_b = $mv_rm_frame->Button(
111 -image => $eraser_img,
112 -command => sub { $cw->remove_selection; },
113 )->pack( -side => 'left', @fxe1 );
114 $balloon->attach( $eraser_b, -msg => 'Remove selected element from the list' );
115  
116 my $rm_all_b = $mv_rm_frame->Button(
117 -image => $remove_img,
118 -command => sub {
119 $list->clear;
120 $tklist->delete( 0, 'end' );
121 $cw->{store_cb}->();
122 },
123 )->pack( -side => 'left', @fxe1 );
124 $balloon->attach( $rm_all_b, -msg => 'Remove all elements from the list' );
125  
126 my $cargo_type = $list->cargo_type;
127  
128 if ( $cargo_type eq 'leaf' ) {
129 my $sort_b = $mv_rm_frame->Button(
130 -image => $sort_img,
131 -command => sub { $cw->sort_content } )->pack( -side => 'left', @fxe1 );
132 $balloon->attach( $sort_b, -msg => 'Sort all elements in the list' );
133 }
134  
135 if ( $cargo_type eq 'leaf'
136 and $value_type ne 'enum'
137 and $value_type ne 'reference' ) {
138 my $set_push_b_entry_frame =
139 $right_frame->Frame( -borderwidth => 2, -relief => 'groove' )->pack(@fxe1);
140 my $user_value;
141 my $value_entry = $set_push_b_entry_frame->Entry(
142 -textvariable => \$user_value,
143 -width => $entry_width
144 );
145  
146 my $set_push_b_frame = $set_push_b_entry_frame->Frame->pack(@fxe1);
147  
148 $cw->add_set_entry( $set_push_b_frame, $balloon, $tklist, \$user_value )->pack(@fxe1);
149 $cw->add_insort_entry( $set_push_b_frame, $balloon, \$user_value )->pack(@fxe1);
150 $cw->add_insert_entry( $set_push_b_frame, $balloon, \$user_value )->pack(@fxe1);
151 $cw->add_set_all_b( $set_push_b_entry_frame, $set_push_b_frame, $balloon, \$user_value )
152 ->pack(@fxe1);
153  
154 $value_entry->pack(@fxe1);
155 $cw->add_warning( $list, 'edit' )->pack(@fx);
156 }
157 else {
158 my $elt_name = $list->element_name;
159 my $disp = "$elt_name ( $cargo_type ";
160 $disp .= $list->config_class_name . ' )' if $cargo_type eq 'node';
161 $disp .= " $value_type )" if defined $value_type;
162 my $b = $right_frame->Button(
163 -text => "Push new $disp",
164 -command => sub { $cw->push_entry(''); },
165 )->pack(@fxe1);
166 $balloon->attach( $b, -msg => "add a new $elt_name at the end of the list" );
167 }
168  
169 $cw->{tklist} = $tklist;
170 $cw->reset_value;
171  
172 $cw->ConfigSpecs(-font => [['SELF','DESCENDANTS'], 'font','Font', $cme_font ],);
173  
174 $cw->Tk::Frame::Populate($args);
175 }
176  
177 #
178 # New subroutine "reset_value" extracted - Wed Sep 21 11:33:51 2011.
179 #
180 sub reset_value {
181 my $cw = shift;
182 my $list = $cw->{list};
183  
184 my $cargo_type = $list->cargo_type;
185 $cw->{tklist}->delete( 0, 'end' );
186 my @insert =
187 $cargo_type eq 'leaf'
188 ? $list->fetch_all_values( check => 'no' )
189 : $list->fetch_all_indexes;
190 $cw->{tklist}->insert( end => @insert );
191  
192 return ( $cargo_type, \@insert );
193 }
194  
195 sub add_set_entry {
196 my ( $cw, $frame, $balloon, $tklist, $user_value_r ) = @_;
197  
198 my $set_sub = sub { $cw->set_entry($$user_value_r); };
199  
200 my $set_b = $frame->Button(
201 -text => "set selected",
202 -command => $set_sub,
203 )->pack( -side => 'left', @fxe1 );
204  
205 $balloon->attach( $set_b,
206 -msg => 'enter a value, select an element on the left '
207 . 'and click the button to replace the selected '
208 . 'element with this value.' );
209  
210 my $b_sub = sub {
211 my $idx = $tklist->curselection;
212 $$user_value_r = $tklist->get($idx) if $idx;
213 };
214  
215 $tklist->bind( '<<ListboxSelect>>', $b_sub );
216  
217 return $set_b;
218 }
219  
220 sub add_push_entry {
221 my ( $cw, $frame, $balloon, $user_value_r ) = @_;
222  
223 my $push_sub = sub { $cw->push_entry($$user_value_r); $$user_value_r = ''; };
224 my $push_b = $frame->Button(
225 -text => "push item",
226 -command => $push_sub,
227 )->pack( -side => 'left', @fxe1 );
228  
229 $balloon->attach( $push_b,
230 -msg => 'enter a value, and click the push button to add '
231 . 'this value at the end of the list' );
232 return $push_b;
233 }
234  
235 sub push_entry {
236 my $cw = shift;
237 my $add = shift;
238 my $tklist = $cw->{tklist};
239 my $list = $cw->{list};
240  
241 $logger->debug("push_entry: $add");
242  
243 # create new item in list (may auto create node object)
244 my @idx = $list->fetch_all_indexes;
245 eval { $list->fetch_with_id( scalar @idx ) };
246  
247 if ($@) {
248 $cw->CmeDialog(
249 -title => "List index error",
250 -text => $@->as_string,
251 )->Show;
252 }
253 else {
254 # trigger redraw of Tk Tree
255 $cw->{store_cb}->();
256  
257 my @new_idx = $list->fetch_all_indexes;
258 $logger->debug( "new list idx: " . join( ',', @new_idx ) );
259  
260 my $insert = length($add) ? $add : $#new_idx;
261 $tklist->insert( 'end', $insert );
262 }
263  
264 return 1;
265 }
266  
267 sub add_insert_entry {
268 my ( $cw, $frame, $balloon, $user_value_r ) = @_;
269  
270 my $insert_sub = sub { $cw->insert_entry($$user_value_r); $$user_value_r = ''; };
271 my $insert_b = $frame->Button(
272 -text => "insert item",
273 -command => $insert_sub,
274 )->pack( -side => 'left', @fxe1 );
275  
276 $balloon->attach( $insert_b,
277 -msg => 'enter a value, and click the insert button to add '
278 . 'this value before the selected item or at the end of the list (push)' );
279 return $insert_b;
280 }
281  
282 sub insert_entry {
283 my $cw = shift;
284 my $add = shift;
285 my $tklist = $cw->{tklist};
286 my $list = $cw->{list};
287  
288 my $idx_ref = $tklist->curselection || [];
289 my $idx = $idx_ref->[0];
290  
291 $logger->debug( "insert_entry: $add insert at index ", $idx || 'end' );
292 print( "insert_entry: $add insert at index ", $idx || 'end', "\n" );
293  
294 return unless length($add);
295 my $try_sub =
296 defined $idx ? sub { $list->insert_at( $idx, $add ); } : sub { $list->push($add) };
297 $cw->try_and_redraw($try_sub);
298  
299 }
300  
301 sub set_entry {
302 my $cw = shift;
303 my $data = shift;
304  
305 my $tklist = $cw->{tklist};
306 my $idx_ref = $tklist->curselection();
307 return unless defined $idx_ref;
308 return unless @$idx_ref;
309  
310 my $idx = $idx_ref->[0];
311 return unless $idx;
312 $tklist->delete($idx);
313 $tklist->insert( $idx, $data );
314 $tklist->selectionSet($idx);
315 $cw->{list}->fetch_with_id($idx)->store($data);
316 $cw->{store_cb}->();
317 }
318  
319 sub add_insort_entry {
320 my ( $cw, $frame, $balloon, $user_value_r ) = @_;
321  
322 my $insort_sub = sub { $cw->insort_entry($$user_value_r); $$user_value_r = ''; };
323 my $insort_b = $frame->Button(
324 -text => "insort",
325 -command => $insort_sub,
326 )->pack( -side => 'left', @fxe1 );
327  
328 $balloon->attach( $insort_b,
329 -msg => 'enter a value, and click the insort button to insert '
330 . 'this value while keeping the list sorted' );
331 return $insort_b;
332 }
333  
334 sub insort_entry {
335 my $cw = shift;
336 my $add = shift;
337  
338 $logger->debug("insort_entry: $add");
339  
340 return unless length($add);
341 $cw->try_and_redraw( sub { $cw->{list}->insort($add); } );
342 }
343  
344 sub try_and_redraw {
345 my $cw = shift;
346 my $to_try = shift;
347 my $tklist = $cw->{tklist};
348 my $list = $cw->{list};
349  
350 eval { $to_try->(); };
351  
352 if ($@) {
353 $cw->CmeDialog(
354 -title => "List index error",
355 -text => $@->as_string,
356 )->Show;
357 }
358 else {
359 # trigger redraw of Tk Tree
360 $cw->{store_cb}->();
361  
362 my @list = $list->fetch_all_values;
363  
364 $tklist->delete( 0, 'end' );
365 $tklist->insert( 0, @list );
366 }
367  
368 return 1;
369 }
370  
371 sub add_set_all_b {
372 my ( $cw, $frame, $b_frame, $balloon, $user_value_r ) = @_;
373  
374 my $regexp = '\s*,\s*';
375 my $set_all_sub = sub { $cw->set_all_items( $$user_value_r, $regexp ); };
376  
377 #my $set_all_frame = $frame->Frame;
378 #my $set_top = $set_all_frame->Frame->pack(@fxe1);
379 my $set_bottom = $frame->Frame->pack( @fxe1, -side => 'bottom' );
380  
381 my $set_b = $b_frame->Button(
382 -text => "set all",
383 -command => $set_all_sub,
384 )->pack( -side => 'left', @fx );
385  
386 $balloon->attach( $set_b,
387 -msg => 'set all elements with a single string that '
388 . 'will be split by the regexp displayed below' );
389  
390 my $split_lb = $set_bottom->Label( -text => 'split regexp' )->pack( -side => 'left', @fxe1 );
391 $set_bottom->Entry( -textvariable => \$regexp )->pack( -side => 'left', @fxe1 );
392  
393 $balloon->attach( $split_lb,
394 -msg => 'regexp used to split the entry above when "set all" button is pressed' );
395  
396 return $set_bottom;
397 }
398  
399 sub set_all_items {
400 my $cw = shift;
401 my $data = shift;
402 my $regexp = shift;
403  
404 return unless $data;
405 my $tklist = $cw->{tklist};
406  
407 my @list = split /$regexp/, $data;
408  
409 $tklist->delete( 0, 'end' );
410 $tklist->insert( 0, @list );
411 $cw->{list}->load_data( \@list );
412 $cw->{store_cb}->();
413 }
414  
415 sub sort_content {
416 my $cw = shift;
417  
418 my $tklist = $cw->{tklist};
419 my $list = $cw->{list};
420 $list->sort;
421  
422 my @list = $list->fetch_all_values;
423  
424 $tklist->delete( 0, 'end' );
425 $tklist->insert( 0, @list );
426 $cw->{store_cb}->();
427 }
428  
429 sub move_up {
430 my $cw = shift;
431  
432 my $tklist = $cw->{tklist};
433 my $from_idx_ref = $tklist->curselection();
434  
435 return unless defined $from_idx_ref;
436 return unless @$from_idx_ref;
437  
438 my $from_idx = $from_idx_ref->[0];
439 return unless $from_idx;
440 return unless $from_idx > 0;
441  
442 $cw->swap( $from_idx, $from_idx - 1 );
443 }
444  
445 sub move_down {
446 my $cw = shift;
447  
448 my $tklist = $cw->{tklist};
449 my $from_idx_ref = $tklist->curselection();
450  
451 return unless defined $from_idx_ref;
452 return unless @$from_idx_ref;
453  
454 my $from_idx = $from_idx_ref->[0];
455 my $max_idx = $cw->{list}->fetch_size - 1;
456 return unless $from_idx < $max_idx;
457  
458 $cw->swap( $from_idx, $from_idx + 1 );
459 }
460  
461 sub swap {
462 my ( $cw, $ida, $idb ) = @_;
463  
464 my $tklist = $cw->{tklist};
465  
466 my $list = $cw->{list};
467 $list->swap( $ida, $idb );
468  
469 my $cargo_type = $list->cargo_type;
470  
471 $tklist->selectionClear($ida);
472  
473 if ( $cargo_type ne 'node' ) {
474 my $old = $tklist->get($ida);
475 $tklist->delete($ida);
476  
477 while ( $idb > $tklist->size ) {
478 $tklist->insert( 'end', '<undef>' );
479 }
480 $tklist->insert( $idb, $old );
481 }
482  
483 $tklist->selectionSet($idb);
484 $cw->{store_cb}->();
485 }
486  
487 sub remove_selection {
488 my $cw = shift;
489 my $tklist = $cw->{tklist};
490 my $list = $cw->{list};
491  
492 foreach ( $tklist->curselection() ) {
493 $logger->debug("remove_selection: removing index $_");
494 $list->remove($_);
495 }
496 $cw->{store_cb}->();
497  
498 # redraw the list content
499 $cw->reset_value;
500  
501 $cw->update_warning($list);
502 }
503  
504 1;