blib/lib/PDL/Demos/Prima.pm untested

filter: Statementstmt 0.0% 0 / 141 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 33 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 238 CC 14 SCAR 35.2
CC
14
Cov
0%
run
90.0
setup_slide
20.0
keypress_handler
12.0
CRAP 33.9
1 use strict;
2 use warnings;
3  
4 ############################################################################
5 package PDL::Demos::Prima;
6 ############################################################################
7  
8 use PDL;
9  
10 =head1 NAME
11  
12 PDL::Demos::Prima - PDL demo for PDL::Graphics::Prima
13  
14 =head1 SYNOPSIS
15  
16 You can enjoy this demo in any number of ways. First, you can invoke the
17 demo from the command line by saying
18  
19 perl -MPDL::Demos::Prima
20  
21 Second, you can invoke the demo from with the pdl shell by saying
22  
23 pdl> demo prima
24  
25 Finally, all of the content is in the pod documentation, so you can simply
26 read this, though it won't be quite so interactive. :-)
27  
28 perldoc PDL::Demos::Prima
29 podview PDL::Demos::Prima
30  
31 =head1 DESCRIPTION
32  
33 The documentation in this module is meant to give a short, hands-on
34 introduction to L<PDL::Graphics::Prima|PDL::Graphics::Prima/>, a plotting
35 library written on top of the L<Prima|Prima/> GUI toolkit.
36  
37 =cut
38  
39 ##############################
40 # Check load status of Prima #
41 ##############################
42  
43 my $min_version = 0.13;
44 my $loaded_prima = eval {
45 require PDL::Graphics::Prima;
46 return 0 if $PDL::Graphics::Prima::VERSION < $min_version;
47 require PDL::Graphics::Prima::Simple;
48 PDL::Graphics::Prima::Simple->import();
49 require Prima::Application;
50 Prima::Application->import();
51 1;
52 };
53  
54 ###########################################
55 # Pull the demo pod into a data structure #
56 ###########################################
57  
58 # Pull the pod apart into the following sort of array structure
59 # @demo = (
60 # 'Introduction' => $first_paragraph => $first_code,
61 # 'Introduction' => $second_paragraph => $second_code,
62 # ...
63 # 'First steps' => $first_paragraph => $first_code,
64 # ...
65 # );
66  
67 my (@demo, $curr_section, $curr_par, $curr_code);
68 my $curr_state = 'section_title';
69 while(my $line = <DATA>) {
70 # Only =head2s in this documentation
71 last if $line =~ /=head1/;
72 if ($line =~ /^=head2 (.*)/) {
73 # Add the current section's name and an empty arrayref
74 $curr_section = $1;
75 }
76 elsif ($line =~ /^\n/) {
77 if (defined $curr_par and defined $curr_code) {
78 push @demo, $curr_section, $curr_par, $curr_code;
79 $curr_par = $curr_code = undef;
80 }
81 }
82 elsif (not defined $curr_par) {
83 $curr_par = $line;
84 }
85 elsif (not defined $curr_code and $line !~ /^\s/) {
86 $curr_par .= $line;
87 }
88 elsif ($line =~ /^\s/) {
89 # Accumulate code lines, stripping off the leading space
90 $line =~ s/^\s//;
91 $curr_code .= $line;
92 }
93 }
94  
95 # Add some extra content for Prima viewing only
96 if ($loaded_prima) {
97 unshift @demo, 'Introduction',
98 'This is the demo for L<PDL::Graphics::Prima|PDL::Graphics::Prima/>. Explanatory
99 text will appear here; code samples will appear below. Tip: you can modify and
100 re-run the code samples. When you are done, simply close the window.',
101 '### HEY, EDIT ME! ###
102 use Prima::MsgBox;
103 Prima::MsgBox::message( "Hello, this is the PDL::Graphics::Prima demo.", mb::Ok);'
104 }
105  
106 ##################################
107 # The command that runs the demo #
108 ##################################
109  
110 # These are widgts I will need across multiple functions, so they are globals.
111 my ($section_title_label, $text_pod, $code_eval, $prev_button, $next_button,
112 $run_button, $help_window, $window, $is_evaling);
113 sub run {
114
115 # Make sure they have it. Otherwise, bail out.
116 if (not $loaded_prima) {
117 my $reason =
118 "I couldn't load the library, either because it's not installed on your
119 machine or it's broken.";
120 $reason =
121 "your version of PDL::Graphics::Prima (v$PDL::Graphics::Prima::VERSION) is out of date. This demo
122 requires at least v$min_version." if defined $loaded_prima;
123 print <<SORRY;
124  
125 Thanks for trying to learn more about PDL::Graphics::Prima. Unfortunately,
126 $reason
127  
128 If you really want to get this working, the fastest way to get help is to
129 join the live chat on the PDL irc channel. If you have an IRC client, check
130 out
131  
132 irc.perl.org#pdl
133  
134 If you don't have an IRC client, you can join the discussion via mibbit:
135  
136 http://www.mibbit.com/chat/?url=irc://irc.perl.org/pdl
137  
138 If you would rather, you can send an email to the mailing list:
139  
140 http://pdl.perl.org/?page=mailing-lists
141  
142 For more information about PDL::Graphics::Prima, check out
143  
144 http://p3rl.org/PDL::Graphics::Prima.
145  
146  
147 Thanks, and keep trying! I promise it's worth it.
148  
149 SORRY
150 return;
151 }
152
153 # Note that by the time we reach here, $::application is defined.
154 require Prima::Label;
155 require Prima::PodView;
156 require Prima::Buttons;
157 require Prima::Utils;
158 require Prima::Edit;
159
160 my $current_slide = 0;
161
162 # ---( Build the Demo Window )--- #
163
164 # Window
165 $window = Prima::Window->create(
166 place => {
167 relx => 0.15, relwidth => 0.7, relheight => 0.7, rely => 0.15,
168 anchor => 'sw',
169 },
170 sizeMax => [600, 800],
171 sizeMin => [600, 800],
172 text => 'PDL::Graphics::Prima Demo',
173 onDestroy => sub {
174 require Prima::Utils;
175 # Throw an exception after destruction is complete so that we
176 # break out of the $::application->go loop.
177 Prima::Utils::post(sub { die 'time to exit the event loop' });
178 },
179 onKeyUp => \&keypress_handler,
180 );
181 $window->font->size(12);
182 # Title
183 # ---( Build list of windows that we don't want to close )---
184 my @dont_touch = $::application->get_widgets;
185
186 my $title_height = 50;
187 $section_title_label = $window->insert(Label =>
188 place => {
189 x => 0, relwidth => 1, anchor => 'sw',
190 y => -$title_height, rely => 1, height => $title_height,
191 },
192 text => '',
193 height => $title_height,
194 alignment => ta::Center(),
195 valignment => ta::Center(),
196 backColor => cl::White(),
197 font => {
198 size => 24,
199 },
200 onKeyUp => \&keypress_handler,
201 );
202 # Buttons
203 my $button_height = 35;
204 $prev_button = $window->insert(Button =>
205 place => {
206 x => 0, relwidth => 0.333, anchor => 'sw',
207 y => 0, height => $button_height,
208 },
209 height => $button_height,
210 text => 'Previous',
211 enabled => 0,
212 onClick => sub {
213 $current_slide-- unless $current_slide == 0;
214 setup_slide($current_slide);
215 },
216 );
217 $run_button = $window->insert(Button =>
218 place => {
219 relx => 0.333, relwidth => 0.333, anchor => 'sw',
220 y => 0, height => $button_height,
221 },
222 height => $button_height,
223 text => 'Run',
224 onClick => sub {
225 # Clear out old windows
226 for my $curr_window ($::application->get_widgets) {
227 next if grep { $curr_window == $_ } @dont_touch
228 or defined $help_window and $curr_window == $help_window;
229 $curr_window->destroy;
230 }
231
232 # Disable the buttons
233 my $prev_state = $prev_button->enabled;
234 $prev_button->enabled(0);
235 $run_button->enabled(0);
236 my $next_state = $next_button->enabled;
237 $next_button->enabled(0);
238
239 # Run the eval
240 eval 'no strict; no warnings; ' . $code_eval->text;
241 if ($@ and $@ !~ /time to exit the event loop/ ) {
242 warn $@;
243 Prima::MsgBox::message($@);
244 }
245
246 $prev_button->enabled($prev_state);
247 $run_button->enabled(1);
248 $next_button->enabled($next_state);
249 },
250 );
251 $next_button = $window->insert(Button =>
252 place => {
253 relx => 0.666, relwidth => 0.333, anchor => 'sw',
254 y => 0, height => $button_height,
255 },
256 height => $button_height,
257 text => 'Next',
258 onClick => sub {
259 $current_slide++ unless $current_slide == @demo/3;
260 setup_slide($current_slide);
261 },
262 );
263 # Text
264 my $par_container = $window->insert(Widget =>
265 place => {
266 x => 0, relwidth => 1, anchor => 'sw',
267 rely => 0.6, relheight => 0.4, height => -$title_height-1,
268 },
269 backColor => cl::White(),
270 );
271 my $padding = 10;
272 $text_pod = $par_container->insert(PodView =>
273 place => {
274 x => $padding, relwidth => 1, width => -2*$padding,
275 y => $padding, relheight => 1, height => -2*$padding - 15,
276 anchor => 'sw',
277 },
278 # This Event does not appear to be documented!!! Beware!!!
279 # Modify link clicking so that it opens the help window instead
280 # of following the link.
281 onLink => sub {
282 my ($self, $link) = @_;
283 # $link is a reference to the link that should be opened; deref
284 $::application->open_help($$link);
285 # Store the help window so we can close it on exit later
286 $help_window = $::application->get_active_window;
287 # Bring the help window to the fore
288 $::application->get_active_window->bring_to_front
289 if $::application->get_active_window;
290 # Clear the event so that it doesn't follow the link in this
291 # renderer
292 $self->clear_event;
293 },
294 backColor => cl::White(),
295 borderWidth => 0,
296 autoVScroll => 1,
297 onKeyUp => \&keypress_handler,
298 );
299
300 # Code
301 my $code_container = $window->insert(Widget =>
302 place => {
303 x => 0, relwidth => 1, anchor => 'sw',
304 y => $button_height+1, relheight => 0.6, height => -$button_height-2,
305 },
306 backColor => cl::White(),
307 );
308 $code_eval = $code_container->insert(Edit =>
309 place => {
310 x => $padding, relwidth => 1, width => -2*$padding,
311 y => $padding, relheight => 1, height => -2*$padding,
312 anchor => 'sw',
313 },
314 borderWidth => 0,
315 backColor => cl::White(),
316 tabIndent => 4,
317 syntaxHilite => 1,
318 wantTabs => 1,
319 wantReturns => 1,
320 wordWrap => 0,
321 autoIndent => 1,
322 cursorWrap => 1,
323 font => { name => 'monospace', size => 12 },
324 );
325
326 $window->bring_to_front;
327 setup_slide(0);
328
329 # Run this sucker
330 local $@;
331 eval { $::application->go };
332 $help_window->close if defined $help_window and $help_window->alive;
333 }
334  
335 sub keypress_handler {
336 my ($self, $code, $key, $mod) = @_;
337 if ($key == kb::Down() or $key == kb::Right() or $key == kb::PgDn()) {
338 $next_button->notify('Click');
339 }
340 elsif ($key == kb::Up() or $key == kb::Left() or $key == kg::PgUp()) {
341 $prev_button->notify('Click');
342 }
343 else {
344 $code_eval->notify('KeyUp', $code, $key, $mod);
345 }
346 }
347  
348  
349 #############################################################
350 # Function that transitions between paragraphs and sections #
351 #############################################################
352  
353 sub setup_slide {
354 my $number = shift;
355 if ($number == 0) {
356 $prev_button->enabled(0);
357 }
358 else {
359 $prev_button->enabled(1);
360 }
361 if ($number == @demo/3 - 1) {
362 $next_button->enabled(1);
363 $next_button->text('Finish');
364 }
365 elsif ($number == @demo/3) {
366 # Close the window
367 $window->notify('Destroy');
368 return;
369 }
370 else {
371 $next_button->enabled(1);
372 $next_button->text('Next');
373 }
374
375 $number *= 3;
376 # Set the section title and code
377 $section_title_label->text($demo[$number]);
378 $code_eval->text($demo[$number+2]);
379
380 # Load the pod
381 $text_pod->open_read;
382 $text_pod->read("=pod\n\n$demo[$number+1]\n\n=cut");
383 $text_pod->close_read;
384
385 # Run the demo
386 $run_button->notify('Click');
387 }
388  
389 # This way, it can be invoked as "perl -MPDL::Demos::Prima" or as
390 # "perl path/to/Prima.pm"
391 if ($0 eq '-' or $0 eq __FILE__) {
392 run;
393 exit;
394 }
395  
396 1;
397  
398 __DATA__