ObjEditor.pm untested

filter: Statementstmt 0.0% 0 / 203 Branchbran 0.0% 0 / 56 Conditioncond 0.0% 0 / 43 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 368 CC 29 SCAR 37.8
CC
29
Cov
0%
add_entry
156.0
modify_menu
30.0
update_item
30.0
CRAP 44.0
1 #
2 # This file is part of Tk-ObjEditor
3 #
4 # This software is copyright (c) 2014 by Dominique Dumont.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 package Tk::ObjEditor;
10 $Tk::ObjEditor::VERSION = '2.010';
11 use Carp;
12 use Tk::Derived;
13 use Tk::Frame;
14 use Tk::ObjScanner 2.010;
15 use Tk::Dialog;
16 use Tk::DialogBox;
17 use warnings;
18 use strict;
19 use 5.10.1;
20 use Scalar::Util 1.01 qw(reftype);
21  
22 use vars qw/$VERSION @ISA/;
23  
24 use Storable qw(dclone);
25  
26 use base qw(Tk::Derived Tk::ObjScanner);
27  
28 Tk::Widget->Construct('ObjEditor');
29  
30 sub _isa {
31 return (reftype($_[0]) // '') eq $_[1] ;
32 }
33  
34 sub edit_object {
35 require Tk;
36 import Tk;
37 my $object = shift;
38  
39 my $mw = MainWindow->new;
40 $mw->geometry('+10+10');
41 my $s = $mw->ObjEditor(
42 '-caller' => $object,
43 -direct => 1,
44 -title => 'object editor'
45 );
46  
47 $s->pack( -expand => 1, -fill => 'both' );
48 $s->OnDestroy( sub { $mw->destroy; } );
49  
50 &MainLoop; # Tk's
51 }
52  
53 sub InitObject {
54 my ( $cw, $args ) = @_;
55  
56 my $data = delete $args->{'caller'} || delete $args->{'-caller'};
57 $cw->{direct} =
58 delete $args->{'direct'} || delete $args->{'-direct'} || 0;
59  
60 $cw->{user_data} = $data;
61  
62 my $edited_data = $cw->{direct} ? $data : dclone($data);
63  
64 $args->{'-caller'} = $edited_data; # to pass to ObjScanner
65 $args->{'-show_tied'} = 0; # do not show tied data internal
66  
67 $args->{title} = ref($data) . ' editor'
68 unless ( defined $args->{title} || defined $args->{-title} );
69  
70 $cw->SUPER::InitObject($args);
71  
72 $cw->Subwidget('hlist')->bind( '<B3-ButtonRelease>', sub { $cw->modify_menu() } );
73  
74 $cw->{actions} = [];
75  
76 return $cw;
77 }
78  
79 sub modify_menu {
80 my $cw = shift;
81 my $item = shift; # reserved for tests
82  
83 unless ( defined $item ) {
84  
85 # pointery and rooty are common widget method and must called on
86 # the right widget to give accurate results
87 my $hlist = $cw->Subwidget('hlist');
88 $item = $cw->nearest( $hlist->pointery - $hlist->rooty );
89 }
90  
91 $cw->selectionClear(); # clear all
92 $cw->selectionSet($item);
93  
94 #print "item $item to modify\n";
95  
96 my $menu = $cw->Menu;
97  
98 my $ref = $cw->info( "data", $item )->{item_ref};
99 my @children = $cw->infoChildren($item);
100  
101 if ( not ref($$ref) ) {
102 $menu->add(
103 'command',
104 '-label' => 'modify element',
105 '-command' => sub { $cw->modify_entry($item); } );
106 }
107  
108 if ( $item eq 'root' ) {
109 $menu->add(
110 'command',
111 '-label' => 'reset',
112 '-command' => sub { $cw->reset; } ) unless $cw->{direct};
113 }
114 else {
115 $menu->add(
116 'command',
117 '-label' => 'delete',
118 '-command' => sub { $cw->delete_entry($item); } );
119 }
120  
121 $menu->Popup( -popover => 'cursor', -popanchor => 'nw' );
122  
123 return $menu;
124 }
125  
126 sub reset {
127 my $cw = shift;
128  
129 $cw->{chief} = dclone( $cw->{user_data} );
130 $cw->updateListBox();
131 }
132  
133 # returns the edited data (a clone in case of no-direct option)
134 sub get_data {
135 my $cw = shift;
136  
137 return $cw->{chief};
138 }
139  
140 sub get_orig_data {
141 my $cw = shift;
142  
143 return $cw->{user_data};
144 }
145  
146 sub modify_entry {
147 my $cw = shift;
148 my $item = shift;
149 my $is_text = shift || 0;
150  
151 my $text = $cw->entrycget( $item, '-text' );
152 my ($item_key) = ( $text =~ m/^[\[\{](.*?)[\]\}]->/ );
153  
154 my $c_data = $cw->entrycget( $item, '-data' );
155 my $ref = $c_data->{item_ref};
156  
157 my $modified = $cw->modify_widget( $ref, $is_text );
158  
159 return unless $modified;
160  
161 # replace value in parent sdata structure
162 $cw->update_item($item);
163  
164 }
165  
166 sub update_item {
167 my ( $cw, $item ) = @_[ 0, 1 ];
168 my $direction = $_[2] || '';
169  
170 my $c_data = $cw->entrycget( $item, '-data' );
171 my $parent_item = $cw->info( "parent", $item );
172 my $parent_c_data = $cw->entrycget( $parent_item, '-data' );
173 my $parent_ref =
174 $item eq 'root' ? $cw->{chief}
175 : $parent_c_data->{tied_display} ? \tied( ${ $parent_c_data->{item_ref} } )
176 : $parent_c_data->{item_ref};
177  
178 # update HList display
179 my $text = $cw->describe_element( $parent_ref, $c_data->{index} );
180 $cw->entryconfigure( $item, -text => $text );
181  
182 # update parent if necessary
183 if ( $parent_c_data->{tied_display} and $direction ne 'down' ) {
184 $cw->update_item( $parent_item, 'up' );
185 }
186  
187 # update below if necessary
188 if ( $c_data->{tied_display} and $direction ne 'up' ) {
189 my $h = $cw->Subwidget('hlist');
190 foreach my $child ( $h->infoChildren($item) ) {
191 $cw->update_item( $child, 'down' );
192 }
193 }
194 }
195  
196 sub modify_widget {
197 my $cw = shift;
198 my $ref = shift;
199 my $is_text = shift;
200  
201 # construct popup dialog to change item value.
202 my $db = $cw->DialogBox(
203 -title => 'modify element',
204 -buttons => [ "OK", "Cancel" ] );
205 $cw->{current_dialog} = $db;
206  
207 # Note: focus is taken over by DialogBox and given to "OK"
208  
209 $db->add( 'Label', -text => 'Please enter new value' )->pack;
210  
211 my $textw;
212 if ( $is_text or ( defined $$ref and $$ref =~ /\n/ ) ) {
213 $textw = $db->add('Text')->pack( -expand => 1, -fill => 'both' );
214 $textw->insert( 'end', $$ref );
215 $db->bind( '<Return>', '' ); # remove Dialog Box binding on return
216 $db->Advertise( 'Entry' => $textw );
217 }
218 else {
219 my $entry = $db->add( 'Entry', -textvariable => $ref )->pack( -expand => 1, -fill => 'x' );
220 $db->Advertise( 'Entry' => $entry );
221 }
222  
223 # Show method should be enhanced to accept a "focus" parameter
224 # so focus could be given to the actual editing widget
225 my $answer = $db->Show;
226  
227 return 0 unless $answer eq "OK";
228  
229 # the '- 1c' skips the newline erroneously added by the text widget
230 # Thanks Slaven
231 $$ref = $textw->get( '1.0', 'end - 1c' ) if defined $textw;
232 return 1;
233 }
234  
235 sub add_entry {
236 my $cw = shift;
237 my $item = shift;
238  
239 # construct popup dialog to change item value.
240 my $db = $cw->DialogBox(
241 -title => 'add element',
242 -buttons => [ "OK", "Cancel" ] );
243  
244 my $ok = $db->Subwidget('B_OK');
245  
246 # enforce that a type is choosen by the user and that a new key is used
247 my ( $key_ok, $type_ok ) = ( 0, 0 );
248 my $check = sub {
249 my $what = $key_ok && $type_ok ? 'normal' : 'disabled';
250 $ok->configure( -state => $what );
251 };
252  
253 &$check; # for fun and for test
254  
255 my $ref_ref = $cw->entrycget( $item, '-data' )->{item_ref};
256 my $ref = $$ref_ref;
257  
258 my $is_hash_like = _isa( $ref, 'HASH' );
259  
260 my $what = $is_hash_like ? 'key' : 'index';
261 $db->add( 'Label', -text => "enter new $what" )->pack;
262  
263 my $key = $is_hash_like ? '' : scalar(@$ref);
264  
265 $db->add(
266 'Entry',
267 -textvariable => \$key,
268 -validate => 'key',
269 -validatecommand => sub {
270 my $prop = shift;
271  
272 #print "key: '$prop'\n";
273 if ( ( $is_hash_like and not exists $ref->{$prop} )
274 or ( _isa( $ref, 'ARRAY' ) and not defined $ref->[$prop] ) ) {
275 $key_ok = 1;
276 }
277 else { $key_ok = 0; }
278 &$check;
279 1;
280 } )->pack;
281  
282 $db->add( 'Label', -text => "Specify new element type" )->pack;
283 my $type = 'undef';
284 my $mb = $db->add(
285 'Menubutton',
286 -textvariable => \$type,
287 qw/-indicatoron 1 -relief raised/
288 );
289  
290 my $menu = $mb->Menu( -tearoff => 0 );
291 $mb->configure( -menu => $menu );
292  
293 foreach (qw/array hash scalar text/) {
294 $mb->radiobutton(
295 -label => $_,
296 -value => $_,
297 -variable => \$type,
298 -indicatoron => 0,
299 -command => sub { $type_ok = 1; &$check; } );
300 }
301  
302 $mb->pack;
303 return if $db->Show ne 'OK';
304  
305 # update data structure
306 my $new;
307 if ( $type eq 'hash' ) { $new = {}; }
308 elsif ( $type eq 'array' ) { $new = []; }
309 elsif ( $type eq 'text' ) { $cw->modify_widget( \$new, 1 ); }
310 else { $cw->modify_widget( \$new, 0 ); }
311  
312 return unless defined $new;
313  
314 $ref->{$key} = $new if _isa( $ref, 'HASH' );
315 $ref->[$key] = $new if _isa( $ref, 'ARRAY' );
316  
317 #recompute the text for parent widget
318 my $text = $cw->element( \$ref );
319 $cw->entryconfigure( $item, '-text', $text );
320  
321 #(re)display the children
322 $cw->deleteOffsprings($item);
323 $cw->displaySubItem($item); # by default do not display tied internals
324 }
325  
326 sub delete_entry {
327 my $cw = shift;
328 my $item = shift;
329  
330 my $item_key = $cw->entrycget( $item, '-data' )->{index} || '';
331  
332 my $dialog = $cw->Dialog(
333 -title => 'WARNING',
334 -text => "Delete the '$item_key' element and all its children ?",
335 -buttons => [ "Yes", "No" ] );
336 my $answer = $dialog->Show();
337  
338 return if $answer eq "No";
339  
340 my $parent_item = $cw->info( "parent", $item );
341 my $text_parent = $cw->entrycget( $parent_item, "-text" );
342 my $parent_ref = $cw->entrycget( $parent_item, '-data' )->{item_ref};
343  
344 delete $$parent_ref->{$item_key} if _isa( $$parent_ref, 'HASH' );
345 splice @$$parent_ref, $item_key, 1 if _isa( $$parent_ref, 'ARRAY' );
346  
347 $cw->entryconfigure( $parent_item, "-text", $cw->element($parent_ref) );
348  
349 $cw->deleteEntry($item);
350 }
351  
352 # used for tests
353 sub get_current_dialog {
354 my $self = shift;
355 return $self->{current_dialog};
356 }
357  
358 1;
359  
360 __END__