Tk/HashEditor.pm untested

filter: Statementstmt 0.0% 0 / 325 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 27 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 0.0% 0 / 31 Podpod 0.0% 0 / 18 totaltotal 0.0% 0 / 470 CC 28 SCAR 31.4
CC
28
Cov
0%
Populate
90.0
add_entry
30.0
move_selected_to
30.0
CRAP 23.1
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::HashEditor 1.382;
11  
12 use strict;
13 use warnings;
14 use Carp;
15 use Log::Log4perl;
16  
17 use base qw/Config::Model::Tk::HashViewer/;
18 use vars qw/$icon_path/;
19 use subs qw/menu_struct/;
20 use Tk::Dialog;
21 use Tk::Photo;
22 use Tk::Balloon;
23 use List::MoreUtils qw/apply/;
24 use Config::Model::Tk::NoteEditor;
25 use Config::Model::Tk::CmeDialog;
26  
27 Construct Tk::Widget 'ConfigModelHashEditor';
28  
29 my @fbe1 = qw/-fill both -expand 1/;
30 my @fxe1 = qw/-fill x -expand 1/;
31 my @fx = qw/-fill x /;
32 my $logger = Log::Log4perl::get_logger("Tk::HashEditor");
33  
34 my $entry_width = 15;
35  
36 my ( $up_img, $down_img, $add_img, $eraser_img, $remove_img, $rename_img, $copy_img );
37 *icon_path = *Config::Model::TkUI::icon_path;
38  
39 sub ClassInit {
40 my ( $cw, $args ) = @_;
41  
42 # ClassInit is often used to define bindings and/or other
43 # resources shared by all instances, e.g., images.
44  
45 # cw->Advertise(name=>$widget);
46 }
47  
48 # list of widget that must be activated (0 in this table) when
49 # item is selected in listbox or entry is not empty
50  
51 my %widget_activation_table = (
52 add => { tklist => 1, entry => 0 },
53 mv => { tklist => 0, entry => 0 },
54 cp => { tklist => 0, entry => 0 },
55 up => { tklist => 0, entry => 1 },
56 down => { tklist => 0, entry => 1 },
57 del => { tklist => 0, entry => 1 },
58 );
59  
60 sub Populate {
61 my ( $cw, $args ) = @_;
62 my $hash = $cw->{hash} = delete $args->{-item}
63 || die "HashEditor: no -item, got ", keys %$args;
64 delete $args->{-path};
65 $cw->{store_cb} = delete $args->{-store_cb} || die __PACKAGE__, "no -store_cb";
66 my $cme_font = delete $args->{-font};
67  
68 unless ( defined $up_img ) {
69 $add_img = $cw->Photo( -file => $icon_path . 'add.png' );
70 $up_img = $cw->Photo( -file => $icon_path . 'up.png' );
71 $down_img = $cw->Photo( -file => $icon_path . 'down.png' );
72 $eraser_img = $cw->Photo( -file => $icon_path . 'eraser.png' );
73 $remove_img = $cw->Photo( -file => $icon_path . 'remove.png' );
74 $rename_img = $cw->Photo( -file => $icon_path . 'rotate_cw.png' );
75 $copy_img = $cw->Photo( -file => $icon_path . 'fontsizeup.png' );
76 }
77  
78 $cw->add_header( Edit => $hash )->pack( @fx, -anchor => 'n' );
79  
80 my $inst = $hash->instance;
81  
82 # frame for element list
83 my $elt_frame = $cw->Frame(qw/-relief raised -borderwidth 2/)->pack(@fbe1);
84  
85 $elt_frame->Label( -text => $hash->element_name . ' elements' )->pack(@fx);
86  
87 # element list
88 my $tklist = $elt_frame->Scrolled(
89 'Listbox',
90 -selectmode => 'single',
91 -scrollbars => 'oe',
92 -height => 6,
93 );
94 $tklist->pack( @fbe1, -side => 'left' );
95 $cw->Advertise( tklist => $tklist );
96  
97 $cw->reset_value;
98  
99 my $item_frame = $cw->Frame(qw/-borderwidth 1 -relief groove/)->pack( @fx, -anchor => 'n' );
100  
101 my $balloon = $cw->Balloon( -state => 'balloon' );
102  
103 my $label_keep_frame = $item_frame->Frame->pack(@fxe1);
104 my $item = '';
105 my $keep = 0;
106 $label_keep_frame->Label( -text => 'Item:' )->pack( -side => 'left', -anchor => 'w' );
107  
108 # clear entry
109 my $clear_b = $label_keep_frame->Button(
110 -command => sub { $item = ''; },
111 -text => 'clear'
112 )->pack(qw/-side right -anchor e/);
113 $balloon->attach( $clear_b, -msg => 'clear entry below' );
114  
115 # copy select entry
116 my $copy_cb = sub {
117 my $sel = $tklist->curselection;
118 $item = $tklist->get($sel) if $sel;
119 };
120 my $copy_b = $label_keep_frame->Button(
121 -command => $copy_cb,
122 -text => 'copy'
123 )->pack(qw/-side right -anchor e/);
124 $balloon->attach( $copy_b, -msg => 'copy selected item in entry below' );
125  
126 my $keep_b = $label_keep_frame->Checkbutton(
127 -variable => \$keep,
128 -text => 'keep'
129 )->pack(qw/-side right -anchor e/);
130 $balloon->attach( $keep_b, -msg => 'keep content of entry below after add, move or copy' );
131  
132 # Entry
133 my $entry = $item_frame->Entry( -textvariable => \$item );
134 $entry->pack( @fxe1, qw/-side top -anchor n/ );
135 $balloon->attach( $entry, -msg => 'enter item name to add, copy to, or move to' );
136 $cw->Advertise( entry => $entry );
137  
138 # bind both entries to update correctly the state of all buttons
139 my $bound_sub = sub {
140 $cw->update_state(
141 entry => $item,
142 tklist => $tklist->curselection || 0
143 );
144 };
145 $entry->bind( '<KeyPress>', $bound_sub );
146 $entry->bind( '<B2-ButtonRelease>', $bound_sub );
147 $tklist->bind( '<<ListboxSelect>>', $bound_sub );
148  
149 # frame for all buttons
150 my $button_frame = $item_frame->Frame->pack( @fxe1, qw/-anchor n/ );
151  
152 # add button
153 my $addb = $button_frame->Button(
154 -image => $add_img,
155 -command => sub {
156 $cw->add_entry($item);
157 $item = '' unless $keep;
158 &$bound_sub;
159 },
160 );
161 $addb->pack( @fxe1, qw/-side left/ );
162 $cw->Advertise( add => $addb );
163 my $add_str = $hash->ordered ? " after selection" : '';
164 $balloon->attach( $addb, -msg => "fill field above and click to add new entry" . $add_str );
165  
166 # copy button
167 my $cp_b = $button_frame->Button(
168 -image => $copy_img,
169 -command => sub {
170 $cw->copy_selected_in($item);
171 $item = '' unless $keep;
172 &$bound_sub;
173 },
174 );
175 $cp_b->pack( @fxe1, qw/-side right/ );
176 $cw->Advertise( @fxe1, cp => $cp_b );
177 $balloon->attach( $cp_b, -msg => "copy selected item in entry" );
178  
179 # rename button
180 my $rename_b = $button_frame->Button(
181 -image => $rename_img,
182 -command => sub {
183 $cw->move_selected_to($item);
184 $item = '' unless $keep;
185 &$bound_sub,;
186 },
187 );
188 $rename_b->pack( @fxe1, -side => 'left' );
189 $cw->Advertise( mv => $rename_b );
190 $balloon->attach( $rename_b, -msg => "rename selected key in entry" );
191  
192 if ( $hash->ordered ) {
193 my $up_b = $button_frame->Button(
194 -image => $up_img,
195 -command => sub { $cw->move_selected_up; },
196 );
197  
198 my $down_b = $button_frame->Button(
199 -image => $down_img,
200 -command => sub { $cw->move_selected_down; },
201 );
202 $up_b->pack( -side => 'left', @fxe1 );
203 $down_b->pack( -side => 'left', @fxe1 );
204  
205 $cw->Advertise( up => $up_b );
206 $cw->Advertise( down => $down_b );
207 }
208  
209 my $eraser_b = $button_frame->Button(
210 -image => $eraser_img,
211 -command => sub {
212 $cw->delete_selection;
213 $item = '' unless $keep;
214 &$bound_sub;
215 },
216 );
217 $balloon->attach( $eraser_b, -msg => 'Remove selected key' );
218 $eraser_b->pack( -side => 'left', @fxe1 );
219 $cw->Advertise( del => $eraser_b );
220  
221 my $rm_all_b = $button_frame->Button(
222 -image => $remove_img,
223 -command => sub { $cw->remove_all_elements; $item = ''; },
224 )->pack( -side => 'left', @fxe1 );
225 $balloon->attach( $rm_all_b, -msg => 'Remove all keys' );
226  
227 $cw->ConfigModelNoteEditor( -object => $hash )->pack(qw/-anchor n/);
228  
229 # set all buttons to their default state
230 $cw->update_state( tklist => '', entry => '' );
231  
232 $cw->add_warning( $hash, 'edit' )->pack(@fx);
233 $cw->add_info_button()->pack( @fx, qw/-anchor n/ );
234 $cw->add_summary($hash)->pack(@fx);
235 $cw->add_description($hash)->pack(@fbe1);
236  
237 $cw->ConfigSpecs(-font => [['SELF','DESCENDANTS'], 'font','Font', $cme_font ],);
238  
239 $cw->Tk::Frame::Populate($args);
240 }
241  
242 sub reset_value {
243 my $cw = shift ;
244 $cw->Subwidget('tklist')->delete( 0, 'end' );
245 $cw->insert( end => $cw->{hash}->fetch_all_indexes );
246 }
247  
248  
249 # the following function is used to make multi-line keys (like Files
250 # entries in Debian copyright files) more manageable: LF are replaced
251 # by \n to enable editing a multi line entry in a Tk::Entry.
252 sub insert {
253 my $cw = shift ;
254 my $where = shift ;
255 my @what = escape_keys(@_);
256 $cw->Subwidget('tklist')->insert($where => @what);
257 }
258  
259 sub escape_keys {
260 return apply {
261 s/\\n/\\\\n/g; # literal \n -> \\n
262 s/\n/\\n/g; # LF -> \n
263 $_;
264 } @_ ;
265 }
266  
267 # this function (not a method) restore the LF in a multi line key
268 # (reverse the operation done above
269 sub restore_keys {
270 return apply {
271 s/(?<!\\)\\n/\n/g; # \n -> LF, leave \\n alone
272 s/\\\\n/\\n/g; # \\n -> literal \n
273 $_;
274 } @_ ;
275 }
276  
277 sub remove_all_elements {
278 my $cw = shift;
279 my $dialog = $cw->Dialog(
280 -title => "Delete ?",
281 -text => "Are you sure you want to delete all elements ?",
282 -buttons => [qw/Yes No/],
283 -default_button => 'Yes',
284 );
285 my $answer = $dialog->Show;
286 return unless $answer eq 'Yes';
287 $cw->{hash}->clear;
288 $cw->Subwidget('tklist')->delete( 0, 'end' );
289 $cw->reload_tree();
290 }
291  
292 # update buttons state according to entry and list widget
293 # this method is called whenever one of them changes its content
294 sub update_state {
295 my ( $cw, %content ) = @_;
296  
297 my $wat = \%widget_activation_table;
298  
299 foreach my $button ( keys %$wat ) {
300 my $new = 1;
301 foreach my $c ( keys %content ) {
302 $new &&= $wat->{$button}{$c} || $content{$c};
303 }
304 my $subwidget = $cw->Subwidget($button) || next;
305 $subwidget->configure( -state => length($new) > 0 ? 'normal' : 'disabled' );
306 }
307 }
308  
309 sub add_entry {
310 my $cw = shift;
311 my $add = shift;
312 my $tklist = $cw->Subwidget('tklist');
313 my $hash = $cw->{hash};
314  
315 $logger->debug("add_entry: $add");
316  
317 if ( $hash->exists(restore_keys($add)) ) {
318 $cw->Dialog(
319 -title => "Add item error",
320 -text => "Entry $add already exists",
321 )->Show();
322 return 0;
323 }
324  
325 # add entry in hash
326 eval { $hash->fetch_with_id(restore_keys($add)) };
327  
328 if ($@) {
329 $cw->CmeDialog(
330 -title => 'Hash index error',
331 -text => $@->as_string,
332 )->Show;
333 return 0;
334 }
335  
336 $logger->debug( "new hash idx: " . join( ',', $hash->fetch_all_indexes ) );
337  
338 # ensure correct order for ordered hash
339 my @selected = $tklist->curselection();
340  
341 $tklist->selectionClear( 0, 'end' );
342  
343 if ( @selected and $hash->ordered ) {
344 my $idx = $tklist->get( $selected[0] );
345 $logger->debug("add_entry on ordered hash: swap $idx and $add");
346 $hash->move_after( restore_keys($add, $idx) );
347 $logger->debug( "new hash idx: " . join( ',', $hash->fetch_all_indexes ) );
348 my $new_idx = $selected[0] + 1;
349 $cw->insert( $new_idx, $add );
350 $tklist->selectionSet($new_idx);
351 $tklist->see($new_idx);
352 }
353 elsif ( $hash->ordered ) {
354  
355 # without selection on ordered hash, items are simply pushed
356 $cw->insert( 'end', $add );
357 $tklist->selectionSet('end');
358 $tklist->see('end');
359 }
360 else {
361 $cw->add_and_sort_item($add);
362 }
363  
364 # trigger redraw of Tk Tree
365 $cw->reload_tree;
366 return 1;
367 }
368  
369 sub add_and_sort_item {
370 my $cw = shift;
371 my $add = shift;
372  
373 my $tklist = $cw->Subwidget('tklist');
374 my $idx = 0;
375 my $added = 0;
376  
377 $tklist->selectionClear( 0, 'end' );
378 foreach my $item ( $tklist->get( 0, 'end' ) ) {
379 if ( $add lt $item ) {
380 $cw->insert( $idx, $add );
381 $tklist->selectionSet($idx);
382 $tklist->see($idx);
383 $added = 1;
384 last;
385 }
386 $idx++;
387 }
388  
389 if ( not $added ) {
390 $cw->insert( 'end', $add ); # last entry
391 $tklist->selectionSet('end');
392 $tklist->see('end');
393 }
394 }
395  
396 sub add_item {
397 my $cw = shift;
398 my $add = shift;
399  
400 my $hash = $cw->{hash};
401 my $tklist = $cw->Subwidget('tklist');
402  
403 # add entry in tklist
404 if ( $hash->ordered ) {
405 $logger->debug("add_item: adding $add in ordered hash");
406 $tklist->selectionClear( 0, 'end' );
407 $cw->insert( 'end', $add );
408 $tklist->selectionSet('end');
409 $tklist->see('end');
410 }
411 else {
412  
413 # add the item so that items are ordered alphabetically
414 $logger->debug("add_item: adding $add in plain hash");
415 $cw->add_and_sort_item($add);
416 }
417 }
418  
419 sub get_selection {
420 my $cw = shift;
421 my $what = shift;
422 my $tklist = $cw->Subwidget('tklist');
423 my @from_idx = $tklist->curselection();
424 if ( not @from_idx ) {
425 $cw->Dialog(
426 -title => "$what selection error",
427 -text => " Please select an item to $what",
428 )->Show();
429 }
430 return @from_idx;
431 }
432  
433 sub copy_selected_in {
434 my $cw = shift;
435 my $to_name = shift;
436 my $tklist = $cw->Subwidget('tklist');
437 my @from_idx = $cw->get_selection('copy') or return 0;
438 my $from_name = $tklist->get(@from_idx);
439  
440 if ( $from_name eq $to_name ) {
441 $cw->Dialog(
442 -title => "copy item error",
443 -text => "Cannot copy in the same item ($to_name)",
444 )->Show();
445 return 0;
446 }
447  
448 my $hash = $cw->{hash};
449  
450 my $new_idx = $hash->exists(restore_keys($to_name)) ? 0 : 1;
451 $logger->debug( "copy_selected_to: from $from_name to $to_name (is new index: $new_idx)" );
452 $hash->copy( restore_keys($from_name, $to_name) );
453  
454 if ($new_idx) {
455 $logger->debug("copy_selected_to: add_item $to_name");
456 $cw->add_item($to_name);
457 }
458  
459 $cw->reload_tree;
460 }
461  
462 sub move_selected_to {
463 my $cw = shift;
464 my $to_name = shift;
465 my $tklist = $cw->Subwidget('tklist');
466 my @from_idx = $cw->get_selection('move') or return 0;
467 my $from_name = $tklist->get(@from_idx);
468  
469 if ( $from_name eq $to_name ) {
470 $cw->Dialog(
471 -title => "move item error",
472 -text => "Cannot move in the same item ($to_name)",
473 )->Show();
474 return 0;
475 }
476  
477 $logger->debug("move_selected_to: from $from_name to $to_name");
478 my $hash = $cw->{hash};
479 $tklist->delete(@from_idx);
480  
481 my $new_idx = $hash->exists(restore_keys($to_name)) ? 0 : 1;
482 $hash->move( restore_keys($from_name, $to_name) );
483  
484 if ($new_idx) {
485 if ( $hash->ordered ) {
486 $tklist->selectionClear( 0, 'end' );
487 $cw->insert( $from_idx[0], $to_name );
488 $tklist->selectionSet( $from_idx[0] );
489 }
490 else {
491  
492 # add the item so that items are ordered alphabetically
493 $cw->add_and_sort_item($to_name);
494 }
495 }
496  
497 $cw->reload_tree;
498 }
499  
500 sub move_selected_up {
501 my $cw = shift;
502 my $tklist = $cw->Subwidget('tklist');
503 my @idx = $tklist->curselection();
504  
505 return unless @idx and $idx[0] > 0;
506  
507 my $name = $tklist->get(@idx);
508  
509 $logger->debug("move_selected_up: $name (@idx)");
510 $tklist->delete(@idx);
511 my $new_idx = $idx[0] - 1;
512 $cw->insert( $new_idx, $name );
513 $tklist->selectionSet($new_idx);
514 $tklist->see($new_idx);
515  
516 my $hash = $cw->{hash};
517 $hash->move_up(restore_keys($name));
518 $logger->debug( "move_up new hash idx: " . join( ',', $hash->fetch_all_indexes ) );
519  
520 $cw->reload_tree;
521 }
522  
523 sub move_selected_down {
524 my $cw = shift;
525 my $tklist = $cw->Subwidget('tklist');
526 my @idx = $tklist->curselection();
527 my $hash = $cw->{hash};
528 my @h_idx = $hash->fetch_all_indexes;
529  
530 return unless @idx and $idx[0] < $#h_idx;
531  
532 my $name = $tklist->get(@idx);
533  
534 $logger->debug("move_selected_down: $name (@idx)");
535 $tklist->delete(@idx);
536 my $new_idx = $idx[0] + 1;
537 $cw->insert( $new_idx, $name );
538 $tklist->selectionSet($new_idx);
539 $tklist->see($new_idx);
540  
541 $hash->move_down(restore_keys($name));
542 $logger->debug( "move_down new hash idx: " . join( ',', $hash->fetch_all_indexes ) );
543  
544 $cw->reload_tree;
545 }
546  
547 sub delete_selection {
548 my $cw = shift;
549 my $tklist = $cw->Subwidget('tklist');
550 my $hash = $cw->{hash};
551  
552 foreach ( $tklist->curselection() ) {
553 my $idx = $tklist->get($_);
554 $hash->delete(restore_keys($idx));
555 $tklist->delete($_);
556 $cw->reload_tree;
557 }
558 }
559  
560 sub reload_tree {
561 my $cw = shift;
562 $cw->update_warning( $cw->{hash} );
563 $cw->{store_cb}->();
564 }
565  
566 1;