Wizard/Tasks.pm untested

filter: Statementstmt 0.0% 0 / 129 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 214 CC 20 SCAR 51.0
CC
20
Cov
0%
Tk::Wizard::_page_taskList
342.0
BEGIN
12.0
Tk::Wizard::addTaskListPage
2.0
CRAP 163.8
1 package Tk::Wizard::Tasks;
2  
3 use strict;
4 use warnings;
5 use warnings::register;
6 use lib "../../";
7 use vars '$VERSION';
8 $VERSION = do { my @r = ( q$Revision: 2.80 $ =~ /\d+/g ); sprintf "%d." . "%03d" x $#r, @r };
9  
10 use vars qw(@ISA @EXPORT);
11 BEGIN {
12 eval { require Log::Log4perl; };
13  
14 # No Log4perl so bluff: see Log4perl FAQ
15 if($@) {
16 no strict qw"refs";
17 *{__PACKAGE__."::$_"} = sub { } for qw(TRACE DEBUG INFO WARN ERROR FATAL);
18 }
19  
20 # Setup log4perl
21 else {
22 no warnings;
23 no strict qw"refs";
24 require Log::Log4perl::Level;
25 Log::Log4perl::Level->import(__PACKAGE__);
26 Log::Log4perl->import(":easy");
27 if ($Log::Log4perl::VERSION < 1.11){
28 *{__PACKAGE__."::TRACE"} = *DEBUG;
29 }
30 }
31  
32 require Exporter; # Exporting Tk's MainLoop so that
33 @ISA = ( "Exporter", ); # I can just use strict and Tk::Wizard without
34 @EXPORT = ("MainLoop"); # having to use Tk
35 }
36  
37 use Carp ();
38 use Tk::LabFrame;
39 use Tk::DirTree;
40 use Tk::Wizard::Image;
41  
42 my $WINDOZE = ($^O =~ m/MSWin32/i);
43 my $dir_term = $WINDOZE ? 'folder' : 'directory';
44 my $dir_term_ucf = ucfirst $dir_term;
45  
46  
47 =head1 NAME
48  
49 Tk::Wizard::Tasks - C<Tk::Wizard> pages to perform sequential tasks
50  
51 =head1 SYNOPSIS
52  
53 Currently automatically loaded by C<Tk::Wizard>, though this
54 behaviour is deprecated and is expected to change in 2008.
55  
56 =head1 DESCRIPTION
57  
58 Adds a number of methods to C<Tk::Wizard>, to allow the end-user to access
59 the filesystem.
60  
61 =head1 METHODS
62  
63  
64 =head2 addTaskListPage
65  
66 Adds a page to the Wizard that will perform a series of tasks, keeping the user
67 informed by ticking-off a list as each task is accomplished.
68  
69 Whilst the task list is being executed, both the I<Back> and I<Next> buttons
70 are disabled.
71  
72 Parameters are as for L</blank_frame>, plus:
73  
74 =over 4
75  
76 =item -tasks
77  
78 The tasks to perform, supplied as a reference to an array, where each
79 entry is a pair (i.e. a two-member list), the first of which is a text
80 string to display, the second a reference to code to execute.
81  
82 =item -delay
83  
84 The length of the delay, in milliseconds, after the page has been
85 displayed and before execution the task list is begun.
86 Default is 1000 milliseconds (1 second).
87 See L<Tk::after>.
88  
89 =item -continue
90  
91 Display the next Wizard page once the job is done: invokes the
92 callback of the I<Next> button at the end of the task.
93  
94 =item -todo_photo
95  
96 =item -doing_photo
97  
98 =item -ok_photo
99  
100 =item -error_photo
101  
102 =item -na_photo
103  
104 Optional: all L<Tk::Photo|Tk::Photo> objects, displayed as appropriate.
105 C<-na_photo> is displayed if the task code reference returns an undef value, otherwise:
106 C<-ok_photo> is displayed if the task code reference returns a true value, otherwise:
107 C<-error_photo> is displayed.
108 These have defaults taken from L<Tk::Wizard::Image|Tk::Wizard::Image>.
109  
110 =item -label_frame_title
111  
112 The label above the L<Tk::LabFrame|Tk::LabFrame> object which
113 contains the task list. Default label is the boring C<Performing Tasks:>.
114  
115 =item -frame_args
116  
117 Optional: the arguments to pass in the creation of the C<Frame> object used to contain the list.
118  
119 =item -frame_pack
120  
121 Optional: array-refernce to pass to the C<pack> method of the C<Frame> containing the list.
122  
123 =back
124  
125 =head3 TASK LIST EXAMPLE
126  
127 $wizard->addTaskListPage(
128 -title => "Toy example",
129 -tasks => [
130 "Wait five seconds" => sub { sleep 5; 1; },
131 "Wait ten seconds!" => sub { sleep 10; 1; },
132 ],
133 );
134  
135 =cut
136  
137 sub Tk::Wizard::addTaskListPage {
138 my $self = shift;
139 my $args = {@_};
140  
141 $self->addPage( sub { $self->_page_taskList($args) } );
142 }
143  
144 sub Tk::Wizard::_page_taskList {
145 my $self = shift;
146 my $args = shift;
147 my @tasks;
148 my @states = qw[ todo doing ok error na ];
149 my $photos = {};
150 foreach my $state (@states) {
151 my $sArg = "-" . $state . "_photo";
152 if ( !$args->{$sArg} ) {
153 $photos->{$state} = $self->Photo( $state, -data => $Tk::Wizard::Image::TASK_LIST{$state} );
154 }
155 # elsif (!-r $args->{$sArg}
156 # || !$self->Photo( $state, -file => $args->{$sArg} ) )
157 # {
158 # warn "# Could not read $sArg from " . $args->{$sArg};
159 # }
160 elsif ( ref($args->{$sArg}) eq 'SCALAR' ) {
161 $photos->{$state} = $self->Photo(
162 $state,
163 -data => ${$args->{$sArg}}
164 ) || WARN "Could not read $sArg from referenced data " . ${$args->{$sArg}};
165 }
166 elsif (-r $args->{$sArg}) {
167 $photos->{$state} = $self->Photo(
168 $state,
169 -file => $args->{$sArg}
170 ) || WARN "Could not read $sArg from file " . $args->{$sArg};
171 }
172 else {
173 WARN "Could not read $sArg from " . $args->{$sArg};
174 }
175 }
176  
177 $args->{-frame_pack} = [qw/-expand 1 -fill x -padx 30 -pady 10/]
178 unless $args->{-frame_pack};
179  
180 $args->{-frame_args} = [
181 -background => $self->{background},
182 -relief => "flat",
183 -bd => 0,
184 -label => $args->{-label_frame_title} || "Performing Tasks: ",
185 -labelside => "acrosstop"
186 ] unless $args->{-frame_args};
187  
188 my $frame = $self->blank_frame(
189 -title => $args->{-title} || "Performing Tasks",
190 -subtitle => $args->{-subtitle} || "Please wait whilst the Wizard performs these tasks.",
191 -text => $args->{-text} || "",
192 -wait => $args->{ -wait },
193 );
194  
195 if ( $#{ $args->{-tasks} } > -1 ) {
196 my $task_frame =
197 $frame->LabFrame( @{ $args->{-frame_args} }, -background => $self->{background}, )
198 ->pack( @{ $args->{-frame_pack} }, );
199  
200 foreach ( my $i = 0 ; $i <= $#{ $args->{-tasks} } ; $i += 2 ) {
201 my $icn = "-1";
202 my $p = $task_frame->Frame( -background => $self->{background}, )->pack( -side => 'top', -anchor => "w" );
203 if ( exists $photos->{todo} ) {
204 $icn = $p->Label(
205 -image => "todo",
206 -anchor => "w",
207 -background => $self->{background},
208 )->pack( -side => "left" );
209 }
210 $p->Label(
211 -font => $self->{defaultFont},
212 -text => @{ $args->{-tasks} }[$i],
213 -anchor => "w",
214 -background => $self->{background},
215 )->pack( -side => "left" );
216 push @tasks, [ $icn, @{ $args->{-tasks} }[ $i + 1 ] ];
217 }
218  
219 }
220  
221 else {
222 $args->{-delay} = 1;
223 }
224  
225 if ( $args->{ -wait } ) {
226 # If we got a non-zero -wait argument, we must be part of an
227 # automated test. In any case, this page is going to auto-flip to
228 # the next page soon (via a call to $widget->after). We do NOT
229 # want to start executing our tasks, only to have the Wizard flip
230 # to the next page while we're still executing, because then we'll
231 # be trying to update Photos that no longer exist (or worse).
232 }
233  
234 else {
235 # Do not let the user click any buttons while we're working:
236 $self->{nextButton}->configure( -state => "disabled" )
237 if Tk::Exists( $self->{nextButton} );
238  
239 $self->{backButton}->configure( -state => "disabled" )
240 if Tk::Exists( $self->{backButton} );
241  
242 $frame->after(
243 $args->{-delay} || 1000,
244  
245 sub {
246 foreach my $task (@tasks) {
247 if ( Tk::Exists( $task->[0] ) ) {
248 $task->[0]->configure( -image => "doing" );
249 $task->[0]->update;
250 }
251 my $result = &{ $task->[1] };
252 if ( Tk::Exists( $task->[0] ) ) {
253 $task->[0]->configure(
254 -image => defined($result)
255 ? $result
256 ? 'ok'
257 : 'error'
258 : 'na'
259 );
260 $task->[0]->update;
261 }
262 }
263  
264 # We're all done, the user can click buttons again:
265 $self->{backButton}->configure( -state => "normal" ) if Tk::Exists( $self->{backButton} );
266  
267 if ( Tk::Exists( $self->{nextButton} ) ) {
268 $self->{nextButton}->configure( -state => "normal" );
269 # RT#54904
270 $self->{nextButton}->invoke if $args->{ -continue };
271 }
272 },
273  
274 );
275 }
276 return $frame;
277 }
278  
279  
280  
281  
282 1;
283  
284 =head1 AUTHOR
285  
286 Lee Goddard (lgoddard@cpan.org).
287  
288 =head1 COPYRIGHT
289  
290 Copyright (C) Lee Goddard, 11/2002 - 01/2008 ff.
291  
292 Made available under the same terms as Perl itself.