Tk/NodeEditor.pm untested

filter: Statementstmt 0.0% 0 / 135 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 198 CC 14 SCAR 35.2
CC
14
Cov
0%
fill_pane
90.0
add_accept_entry
20.0
Populate
12.0
CRAP 33.9
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::NodeEditor 1.382;
11  
12 use strict;
13 use warnings;
14 use Carp;
15  
16 use Tk::Pane;
17 use Tk::Balloon;
18 use Text::Wrap;
19 use Config::Model::Tk::NoteEditor;
20  
21 use base qw/Config::Model::Tk::NodeViewer/;
22 use subs qw/menu_struct/;
23  
24 Construct Tk::Widget 'ConfigModelNodeEditor';
25  
26 my @fbe1 = qw/-fill both -expand 1/;
27 my @fxe1 = qw/-fill x -expand 1/;
28 my @fx = qw/-fill x -expand 0/;
29  
30 my $logger = Log::Log4perl::get_logger("Tk::NodeEditor");
31  
32 sub ClassInit {
33 my ( $cw, $args ) = @_;
34  
35 # ClassInit is often used to define bindings and/or other
36 # resources shared by all instances, e.g., images.
37  
38 # cw->Advertise(name=>$widget);
39 }
40  
41 sub Populate {
42 my ( $cw, $args ) = @_;
43 my $node = $cw->{node} = delete $args->{-item}
44 || die "NodeViewer: no -item, got ", keys %$args;
45 $cw->{path} = delete $args->{-path};
46 $cw->{store_cb} = delete $args->{-store_cb} || die __PACKAGE__, "no -store_cb";
47 my $cme_font = $cw->{my_font} = delete $args->{-font};
48  
49 $cw->add_header( Edit => $node )->pack(@fx);
50  
51 $cw->Label( -text => $node->composite_name . ' node elements' )->pack();
52  
53 $cw->{pane} = $cw->Scrolled(qw/Pane -scrollbars osow -sticky senw/);
54 $cw->{pane}->pack(@fbe1);
55  
56 $cw->fill_pane;
57  
58 # insert a widget for "accepted" elements
59 my @rexp = $node->accept_regexp;
60 if (@rexp) {
61 $cw->add_accept_entry(@rexp);
62 }
63  
64 $cw->ConfigModelNoteEditor( -object => $node )->pack;
65 $cw->add_info_button()->pack( @fxe1, qw/-anchor n/ );
66  
67 if ( $node->parent ) {
68 $cw->add_summary($node)->pack(@fx);
69 $cw->add_description($node)->pack(@fbe1);
70 }
71 else {
72 $cw->add_help( class => $node->get_help )->pack(@fx);
73 }
74  
75 $cw->ConfigSpecs(-font => [['SELF','DESCENDANTS'], 'font','Font', $cme_font ],);
76  
77 # don't call directly SUPER::Populate as it's NodeViewer's populate
78 $cw->Tk::Frame::Populate($args);
79  
80 # TODO: above is a hack. The required methods of NodeViewer and
81 # AnyViewer should be moved into a role. Question: how can roles
82 # be done with Tk ??
83 }
84  
85 sub reload {
86 goto &fill_pane;
87 }
88  
89 sub fill_pane {
90 my $cw = shift;
91  
92 my $node = $cw->{node};
93 my $elt_pane = $cw->{pane};
94  
95 my %is_elt_drawn = map { ( $_ => 1 ) } keys %{ $cw->{elt_widgets} || {} };
96  
97 my $prev_elt;
98 my $font = $cw->{my_font}; #cget('-font');
99  
100 foreach my $c ( $node->get_element_name() ) {
101 if ( delete $is_elt_drawn{$c} ) {
102 $prev_elt = $c;
103 next;
104 }
105  
106 my $type = $node->element_type($c);
107 my $elt_path = $cw->{path} . '.' . $c;
108  
109 my @after = defined $prev_elt ? ( -after => $cw->{elt_widgets}{$prev_elt} ) : ();
110 $prev_elt = $c;
111 my $f = $elt_pane->Frame( -relief => 'groove', -borderwidth => 1 );
112 $f->pack( -side => 'top', @fx, @after );
113  
114 $cw->{elt_widgets}{$c} = $f;
115 my $label = $f->Label( -text => $c, -font => $font, -width => 22, -anchor => 'w' );
116 $label->pack(qw/-side left -fill x -anchor w/);
117  
118 my $help = $node->get_help_as_text( summary => $c ) || $node->get_help_as_text( description => $c );
119 $cw->Balloon( -state => 'balloon' )->attach( $label, -msg => wrap( '', '', $help ) );
120  
121 if ( $type eq 'leaf' ) {
122 my $leaf = $node->fetch_element($c);
123 my $v = $node->fetch_element_value( name => $c, check => 'no' );
124 my $store_sub = sub {
125 $leaf->store($v);
126 $cw->{store_cb}->($elt_path);
127 $cw->fill_pane;
128 };
129 my $v_type = $leaf->value_type;
130  
131 if ( $v_type =~ /integer|number|uniline/ ) {
132 my $e = $f->Entry( -textvariable => \$v )->pack( qw/-side left -anchor w/, @fxe1 );
133 $e->bind( "<Return>" => $store_sub );
134 $e->bind( "<FocusOut>" => $store_sub );
135 next;
136 }
137  
138 if ( $v_type =~ /boolean/ ) {
139 my $e = $f->Checkbutton( -variable => \$v, -font => $font, -command => $store_sub )
140 ->pack(qw/-side left -anchor w/);
141 next;
142 }
143  
144 if ( $v_type =~ /enum|reference/ ) {
145 my @choices = $leaf->get_choice;
146 require Tk::BrowseEntry;
147 my $e = $f->BrowseEntry(
148 -variable => \$v,
149 -font => $font,
150 -browsecmd => $store_sub,
151 -choices => \@choices
152 )->pack( qw/-side left -anchor w/, @fxe1 );
153 next;
154 }
155 }
156  
157 # add button to launch dedicated editor
158 my $obj = $node->fetch_element($c);
159 my $edit_sub = sub {
160 # it would be better for tkui ui to pass this as a callback
161 # note that storing tkui object in a sub widget creates issues with tk
162 $cw->parent->parent->parent->parent->create_element_widget( 'edit', $elt_path, $obj );
163 };
164 my $edb = $f->Button(
165 -text => '...',
166 -font => $font,
167 -command => $edit_sub
168 );
169 $edb->pack( -anchor => 'w' );
170  
171 my $content =
172 $type eq 'leaf' ? $obj->fetch( check => 'no' ) || ''
173 : $type eq 'node' ? $node->config_class_name
174 : $type;
175 $cw->Balloon( -state => 'balloon' )->attach( $edb, -msg => wrap( '', '', $content ) );
176 }
177  
178 # destroy leftover widgets (may occur with warp mechanism)
179 foreach my $widget ( keys %is_elt_drawn) {
180 my $w = delete $cw->{elt_widgets}{$widget};
181 $w->destroy;
182 }
183 }
184  
185 sub add_accept_entry {
186 my ( $cw, @rexp ) = @_;
187  
188 my $node = $cw->{node};
189 my $f = $cw->Frame( -relief => 'groove', -borderwidth => 1 );
190 $f->pack( -side => 'top', @fx );
191  
192 my $font = $cw->{my_font}; #cget('-font');
193  
194 my $accepted = '';
195 my $accept_label = $f->Label(
196 -text => 'accept : /' . join( '/, /', @rexp ) . '/',
197 -font => $font
198 );
199 $cw->Balloon(-state => 'balloon')->attach(
200 $accept_label,
201 -msg => qq!Add a parameter not yet known by the model and hit <Return>.\n!
202 . qq!This parameter must satisfy the Perl regexp shown after "accept:".!
203 );
204 $accept_label->pack;
205  
206 my $e = $f->Entry( -textvariable => \$accepted, -font => $font)->pack( qw/-side left -anchor w/, @fxe1 );
207 my $sub = sub {
208 return unless $accepted;
209 my $ok = 0;
210 map { $ok++ if $accepted =~ /^$_$/ } @rexp;
211 if ( not $ok ) {
212 die "Cannot accept $accepted, it does not match any accepted regexp\n";
213 }
214 $node->fetch_element($accepted);
215 $cw->{store_cb}->();
216 $cw->fill_pane;
217 $cw->{pane}->yview( moveto => 1 );
218 };
219  
220 $e->bind( "<Return>" => $sub );
221 }
222  
223 1;