Tk/Wizard.pm untested

filter: Statementstmt 0.0% 0 / 152 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 212 CC 5 SCAR 16.5
CC
5
Cov
0%
start_wizard
20.0
destroy_wizard
6.0
ClassInit
2.0
CRAP 5.2
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::Wizard 1.382;
11  
12 use strict;
13 use warnings;
14 use Carp;
15 use Try::Tiny;
16  
17 use base qw/Tk::Toplevel/;
18 use vars qw/$icon_path/;
19 use Log::Log4perl;
20  
21 use Config::Model::Tk::LeafEditor;
22 use Config::Model::Tk::CheckListEditor;
23 use Config::Model::Tk::ListEditor;
24 use Config::Model::Tk::HashEditor;
25  
26 Construct Tk::Widget 'ConfigModelWizard';
27  
28 my $logger = Log::Log4perl::get_logger('Tk::Wizard');
29  
30 my @fbe1 = qw/-fill both -expand 1/;
31 my @fxe1 = qw/-fill x -expand 1/;
32 my @fx = qw/-fill x /;
33  
34 sub ClassInit {
35 my ( $class, $mw ) = @_;
36  
37 # ClassInit is often used to define bindings and/or other
38 # resources shared by all instances, e.g., images.
39  
40 # cw->Advertise(name=>$widget);
41 }
42  
43 sub Populate {
44 my ( $cw, $args ) = @_;
45  
46 foreach my $parm (qw/-root/) {
47 my $attr = $parm;
48 $attr =~ s/^-//;
49 $cw->{$attr} = delete $args->{$parm}
50 or croak "Missing $parm arg\n";
51 }
52  
53 foreach my $parm (qw/-from_widget -stop_on_important -store_cb -show_cb -end_cb -font/) {
54 my $attr = $parm;
55 $attr =~ s/^-//;
56 $cw->{$attr} = delete $args->{$parm};
57 }
58  
59 $logger->info("Creating wizard widget");
60 $cw->{show_cb} ||= sub { };
61 $cw->{store_cb} ||= sub { };
62 $cw->{has_stopped} = 0;
63  
64 my $title = delete $args->{'-title'}
65 || "config wizard " . $cw->{root}->config_class_name;
66  
67 $cw->Label(
68 -text => "Configuration of " . $cw->{root}->config_class_name,
69 )->pack;
70  
71 my $ed = $cw->{ed_frame} = $cw->Frame->pack(qw/-pady 0 -fill both -expand 1 -anchor n/);
72 $cw->{ed_frame}->packPropagate(0);
73  
74 $args->{-title} = $title;
75  
76 $cw->Advertise( ed_frame => $ed, );
77  
78 $cw->ConfigSpecs(
79 -font => [['SELF','DESCENDANTS'], 'font','Font', $cw->{font} ],
80  
81 #-background => ['DESCENDANTS', 'background', 'Background', $background],
82 #-selectbackground => [$hlist, 'selectBackground', 'SelectBackground',
83 # $selectbackground],
84 -width => [ $ed, undef, undef, 600 ],
85 -height => [ $ed, undef, undef, 400 ],
86 DEFAULT => [$ed]
87 );
88  
89 $cw->{font} //= $cw->cget('-font');
90  
91 $cw->SUPER::Populate($args);
92 }
93  
94 sub save {
95 my $cw = shift;
96  
97 $cw->check();
98  
99 $logger->info("Saving data in default directory with instance write_back");
100 $cw->{root}->instance->write_back();
101 }
102  
103 sub leaf_cb {
104 my ( $cw, $scanner, $data_ref, $node, $element_name, $index, $leaf_object ) = @_;
105 $cw->{has_stopped} = 1;
106  
107 # cleanup existing widget contained in this frame
108 $cw->{show_cb}->($leaf_object);
109 $cw->{ed_w} = $cw->{ed_frame}->ConfigModelLeafEditor(
110 -item => $leaf_object,
111 -store_cb => $cw->{store_cb},
112 -font => $cw->{font},
113 )->pack(@fbe1);
114 }
115  
116 sub list_element_cb {
117 my ( $cw, $scanner, $data_ref, $node, $element_name, @indexes ) = @_;
118 $cw->{has_stopped} = 1;
119  
120 # cleanup existing widget contained in this frame
121 my $obj = $node->fetch_element($element_name);
122 $cw->{show_cb}->($obj);
123 $cw->{ed_w} = $cw->{ed_frame}->ConfigModelListEditor(
124 -item => $obj,
125 -store_cb => $cw->{store_cb},
126 -font => $cw->{font},
127 )->pack(@fbe1);
128 }
129  
130 sub hash_element_cb {
131 my ( $cw, $scanner, $data_ref, $node, $element_name, @keys ) = @_;
132 $cw->{has_stopped} = 1;
133  
134 # cleanup existing widget contained in this frame
135 my $obj = $node->fetch_element($element_name);
136 $cw->{show_cb}->($obj);
137 $cw->{ed_w} = $cw->{ed_frame}->ConfigModelHashEditor(
138 -item => $obj,
139 -store_cb => $cw->{store_cb},
140 -font => $cw->{font},
141 )->pack(@fbe1);
142 }
143  
144 sub check_list_element_cb {
145 my ( $cw, $scanner, $data_ref, $node, $element_name, @items ) = @_;
146 $cw->{has_stopped} = 1;
147  
148 # cleanup existing widget contained in this frame
149 my $obj = $node->fetch_element($element_name);
150 $cw->{show_cb}->($obj);
151 $cw->{ed_w} = $cw->{ed_frame}->ConfigModelCheckListEditor(
152 -item => $obj,
153 -store_cb => $cw->{store_cb},
154 -font => $cw->{font},
155 )->pack(@fbe1);
156 }
157  
158 sub prepare_wizard {
159 my ( $cw, %args ) = @_;
160  
161 my $text =
162 'The wizard will scan all configuration items and stop on '
163 . '"important" items or on error (like missing mandatory values). If no '
164 . '"important" item and no error are found, the wizard will exit immediately';
165  
166 my $edf = $cw->{ed_frame};
167  
168 my $textw =
169 $edf->ROText( qw/-relief flat -wrap word -height 8/, -font => $cw->{font});
170 $textw->insert( end => $text );
171 $textw->pack( qw/-side top -anchor n/, @fxe1 );
172  
173 my $stop_on_warn = 0;
174 $edf->Checkbutton( -text => 'stop on warning', -variable => \$stop_on_warn , -font => $cw->{font})
175 ->pack(qw/-side top -anchor w/);
176  
177 $edf->Button(
178 -text => 'OK',
179 -font => $cw->{font},
180 -command => sub { $cw->start_wizard(stop_on_warning => $stop_on_warn) }
181 )->pack(qw/-side right -anchor e/);
182  
183 $edf->Button(
184 -text => 'cancel',
185 -font => $cw->{font},
186 -command => sub { $cw->destroy_wizard() }
187 )->pack(qw/-side left -anchor w/);
188 }
189  
190 sub start_wizard {
191 my ( $cw, %args ) = @_;
192  
193 my $button_f = $cw->Frame->pack(qw/-pady 0 -fill x -expand 1/);
194 $cw->{has_stopped} = 0;
195  
196 my $back = $button_f->Button(
197 -text => 'Back',
198 -font => $cw->{font},
199 -command => sub {
200 $cw->{keep_wiz_editor} = 0;
201 $cw->{wizard}->go_backward;
202 } );
203 $back->pack(qw/-side left -fill x -expand 1/);
204  
205 my $stop = $button_f->Button(
206 -text => 'Store and stop',
207 -font => $cw->{font},
208 -command => sub {
209 $cw->{ed_w}->store if $cw->{ed_w}->can('store');
210 $cw->{keep_wiz_editor} = 0;
211 $cw->{wizard}->bail_out;
212 } );
213 $stop->pack(qw/-side left -fill x -expand 1/);
214  
215 my $quit = $button_f->Button(
216 -text => 'quit wizard',
217 -font => $cw->{font},
218 -command => sub {
219 $cw->{keep_wiz_editor} = 0;
220 $cw->{wizard}->bail_out;
221 } );
222 $quit->pack(qw/-side left -fill x -expand 1/);
223  
224 my $forw = $button_f->Button(
225 -text => 'Next',
226 -font => $cw->{font},
227 -command => sub {
228 $cw->{keep_wiz_editor} = 0;
229 $cw->{wizard}->go_forward;
230 } );
231 $forw->pack(qw/-side right -fill x -expand 1/);
232  
233 $cw->{keep_wiz_editor} = 1;
234  
235 my %cb_table;
236  
237 # a local event loop is run within the call-back
238 foreach my $cb_key (
239 qw/leaf_cb check_list_element_cb
240 list_element_cb hash_element_cb/
241 ) {
242 $cb_table{$cb_key} = sub {
243 my ( $scanner, $data_ref, $node, $element_name ) = @_;
244 my @all_args = @_; # @_ does not work in try blocks
245 $logger->info( "$cb_key (element $element_name) called on '",
246 $node->name, "'->'$element_name'" );
247 map { $_->destroy if Tk::Exists($_) } $cw->{ed_frame}->children;
248 $cw->{keep_wiz_editor} = 1;
249 try {
250 $cw->$cb_key(@all_args);
251 }
252 catch {
253 $cw->{keep_wiz_editor} = 0; # destroy wizard in case of error
254 };
255 my $loop_c = 0;
256 $logger->debug( "$cb_key wizard entered local loop ", ++$loop_c );
257 $cw->DoOneEvent() while $cw->{keep_wiz_editor};
258 $logger->debug( "$cb_key wizard exited local loop ", $loop_c );
259 };
260 }
261  
262 my @wiz_args = ( %cb_table );
263  
264 foreach (qw/warning important/) {
265 push @wiz_args, "call_back_on_$_" => $args{"stop_on_$_"}
266 if defined $args{"stop_on_$_"};
267 }
268  
269 #Tk::ObjScanner::scan_object(\@wiz_args) ;
270 $cw->{wizard} = $cw->{root}->instance->iterator(@wiz_args);
271  
272 # exits when wizard is done
273 $cw->{wizard}->start;
274 $cw->destroy_wizard;
275 }
276  
277 sub destroy_wizard {
278 my $cw = shift;
279  
280 delete $cw->{ed_w};
281 delete $cw->{wizard};
282  
283 # print "Destroying wizard\n" ;
284 $logger->debug("Destroying wizard");
285 $cw->destroy;
286  
287 if ( defined $cw->{end_cb} ) {
288 $logger->debug("Calling end_cb");
289 $cw->{end_cb}->( $cw->{has_stopped} );
290 }
291 }
292  
293 1;
294