use strict;
use warnings;
############################################################################
package PDL::Demos::Prima;
############################################################################
use PDL;
=head1 NAME
PDL::Demos::Prima - PDL demo for PDL::Graphics::Prima
=head1 SYNOPSIS
You can enjoy this demo in any number of ways. First, you can invoke the
demo from the command line by saying
perl -MPDL::Demos::Prima
Second, you can invoke the demo from with the pdl shell by saying
pdl> demo prima
Finally, all of the content is in the pod documentation, so you can simply
read this, though it won't be quite so interactive. :-)
perldoc PDL::Demos::Prima
podview PDL::Demos::Prima
=head1 DESCRIPTION
The documentation in this module is meant to give a short, hands-on
introduction to L, a plotting
library written on top of the L GUI toolkit.
=cut
##############################
# Check load status of Prima #
##############################
my $min_version = 0.13;
my $loaded_prima = eval {
require PDL::Graphics::Prima;
return 0 if $PDL::Graphics::Prima::VERSION < $min_version;
require PDL::Graphics::Prima::Simple;
PDL::Graphics::Prima::Simple->import();
require Prima::Application;
Prima::Application->import();
1;
};
###########################################
# Pull the demo pod into a data structure #
###########################################
# Pull the pod apart into the following sort of array structure
# @demo = (
# 'Introduction' => $first_paragraph => $first_code,
# 'Introduction' => $second_paragraph => $second_code,
# ...
# 'First steps' => $first_paragraph => $first_code,
# ...
# );
my (@demo, $curr_section, $curr_par, $curr_code);
my $curr_state = 'section_title';
while(my $line = ) {
# Only =head2s in this documentation
last if $line =~ /=head1/;
if ($line =~ /^=head2 (.*)/) {
# Add the current section's name and an empty arrayref
$curr_section = $1;
}
elsif ($line =~ /^\n/) {
if (defined $curr_par and defined $curr_code) {
push @demo, $curr_section, $curr_par, $curr_code;
$curr_par = $curr_code = undef;
}
}
elsif (not defined $curr_par) {
$curr_par = $line;
}
elsif (not defined $curr_code and $line !~ /^\s/) {
$curr_par .= $line;
}
elsif ($line =~ /^\s/) {
# Accumulate code lines, stripping off the leading space
$line =~ s/^\s//;
$curr_code .= $line;
}
}
# Add some extra content for Prima viewing only
if ($loaded_prima) {
unshift @demo, 'Introduction',
'This is the demo for L. Explanatory
text will appear here; code samples will appear below. Tip: you can modify and
re-run the code samples. When you are done, simply close the window.',
'### HEY, EDIT ME! ###
use Prima::MsgBox;
Prima::MsgBox::message( "Hello, this is the PDL::Graphics::Prima demo.", mb::Ok);'
}
##################################
# The command that runs the demo #
##################################
# These are widgts I will need across multiple functions, so they are globals.
my ($section_title_label, $text_pod, $code_eval, $prev_button, $next_button,
$run_button, $help_window, $window, $is_evaling);
sub run {
# Make sure they have it. Otherwise, bail out.
if (not $loaded_prima) {
my $reason =
"I couldn't load the library, either because it's not installed on your
machine or it's broken.";
$reason =
"your version of PDL::Graphics::Prima (v$PDL::Graphics::Prima::VERSION) is out of date. This demo
requires at least v$min_version." if defined $loaded_prima;
print <create(
place => {
relx => 0.15, relwidth => 0.7, relheight => 0.7, rely => 0.15,
anchor => 'sw',
},
sizeMax => [600, 800],
sizeMin => [600, 800],
text => 'PDL::Graphics::Prima Demo',
onDestroy => sub {
require Prima::Utils;
# Throw an exception after destruction is complete so that we
# break out of the $::application->go loop.
Prima::Utils::post(sub { die 'time to exit the event loop' });
},
onKeyUp => \&keypress_handler,
);
$window->font->size(12);
# Title
# ---( Build list of windows that we don't want to close )---
my @dont_touch = $::application->get_widgets;
my $title_height = 50;
$section_title_label = $window->insert(Label =>
place => {
x => 0, relwidth => 1, anchor => 'sw',
y => -$title_height, rely => 1, height => $title_height,
},
text => '',
height => $title_height,
alignment => ta::Center(),
valignment => ta::Center(),
backColor => cl::White(),
font => {
size => 24,
},
onKeyUp => \&keypress_handler,
);
# Buttons
my $button_height = 35;
$prev_button = $window->insert(Button =>
place => {
x => 0, relwidth => 0.333, anchor => 'sw',
y => 0, height => $button_height,
},
height => $button_height,
text => 'Previous',
enabled => 0,
onClick => sub {
$current_slide-- unless $current_slide == 0;
setup_slide($current_slide);
},
);
$run_button = $window->insert(Button =>
place => {
relx => 0.333, relwidth => 0.333, anchor => 'sw',
y => 0, height => $button_height,
},
height => $button_height,
text => 'Run',
onClick => sub {
# Clear out old windows
for my $curr_window ($::application->get_widgets) {
next if grep { $curr_window == $_ } @dont_touch
or defined $help_window and $curr_window == $help_window;
$curr_window->destroy;
}
# Disable the buttons
my $prev_state = $prev_button->enabled;
$prev_button->enabled(0);
$run_button->enabled(0);
my $next_state = $next_button->enabled;
$next_button->enabled(0);
# Run the eval
eval 'no strict; no warnings; ' . $code_eval->text;
if ($@ and $@ !~ /time to exit the event loop/ ) {
warn $@;
Prima::MsgBox::message($@);
}
$prev_button->enabled($prev_state);
$run_button->enabled(1);
$next_button->enabled($next_state);
},
);
$next_button = $window->insert(Button =>
place => {
relx => 0.666, relwidth => 0.333, anchor => 'sw',
y => 0, height => $button_height,
},
height => $button_height,
text => 'Next',
onClick => sub {
$current_slide++ unless $current_slide == @demo/3;
setup_slide($current_slide);
},
);
# Text
my $par_container = $window->insert(Widget =>
place => {
x => 0, relwidth => 1, anchor => 'sw',
rely => 0.6, relheight => 0.4, height => -$title_height-1,
},
backColor => cl::White(),
);
my $padding = 10;
$text_pod = $par_container->insert(PodView =>
place => {
x => $padding, relwidth => 1, width => -2*$padding,
y => $padding, relheight => 1, height => -2*$padding - 15,
anchor => 'sw',
},
# This Event does not appear to be documented!!! Beware!!!
# Modify link clicking so that it opens the help window instead
# of following the link.
onLink => sub {
my ($self, $link) = @_;
# $link is a reference to the link that should be opened; deref
$::application->open_help($$link);
# Store the help window so we can close it on exit later
$help_window = $::application->get_active_window;
# Bring the help window to the fore
$::application->get_active_window->bring_to_front
if $::application->get_active_window;
# Clear the event so that it doesn't follow the link in this
# renderer
$self->clear_event;
},
backColor => cl::White(),
borderWidth => 0,
autoVScroll => 1,
onKeyUp => \&keypress_handler,
);
# Code
my $code_container = $window->insert(Widget =>
place => {
x => 0, relwidth => 1, anchor => 'sw',
y => $button_height+1, relheight => 0.6, height => -$button_height-2,
},
backColor => cl::White(),
);
$code_eval = $code_container->insert(Edit =>
place => {
x => $padding, relwidth => 1, width => -2*$padding,
y => $padding, relheight => 1, height => -2*$padding,
anchor => 'sw',
},
borderWidth => 0,
backColor => cl::White(),
tabIndent => 4,
syntaxHilite => 1,
wantTabs => 1,
wantReturns => 1,
wordWrap => 0,
autoIndent => 1,
cursorWrap => 1,
font => { name => 'monospace', size => 12 },
);
$window->bring_to_front;
setup_slide(0);
# Run this sucker
local $@;
eval { $::application->go };
$help_window->close if defined $help_window and $help_window->alive;
}
sub keypress_handler {
my ($self, $code, $key, $mod) = @_;
if ($key == kb::Down() or $key == kb::Right() or $key == kb::PgDn()) {
$next_button->notify('Click');
}
elsif ($key == kb::Up() or $key == kb::Left() or $key == kg::PgUp()) {
$prev_button->notify('Click');
}
else {
$code_eval->notify('KeyUp', $code, $key, $mod);
}
}
#############################################################
# Function that transitions between paragraphs and sections #
#############################################################
sub setup_slide {
my $number = shift;
if ($number == 0) {
$prev_button->enabled(0);
}
else {
$prev_button->enabled(1);
}
if ($number == @demo/3 - 1) {
$next_button->enabled(1);
$next_button->text('Finish');
}
elsif ($number == @demo/3) {
# Close the window
$window->notify('Destroy');
return;
}
else {
$next_button->enabled(1);
$next_button->text('Next');
}
$number *= 3;
# Set the section title and code
$section_title_label->text($demo[$number]);
$code_eval->text($demo[$number+2]);
# Load the pod
$text_pod->open_read;
$text_pod->read("=pod\n\n$demo[$number+1]\n\n=cut");
$text_pod->close_read;
# Run the demo
$run_button->notify('Click');
}
# This way, it can be invoked as "perl -MPDL::Demos::Prima" or as
# "perl path/to/Prima.pm"
if ($0 eq '-' or $0 eq __FILE__) {
run;
exit;
}
1;
__DATA__
=head2 use PDL::Graphics::Prima::Simple
To get started, you will want to use
L. This
module provides a set of friendly wrappers for simple, first-cut data
visualization. L, the underlying
library, is a general-purpose 2D plotting library built as a widget in the
L, but we don't need the full functionality for
the purposes of this demo.
use PDL::Graphics::Prima::Simple;
my $x = sequence(100)/10;
line_plot($x, $x->sin);
=head2 More than just lines!
In addition to numerous ways to plot x/y data, you can also plot
distributions and images. The best run-down of the simple plotting routines
can be found in
L.
$distribution = grandom(100);
hist_plot($distribution);
$x = sequence(100)/10;
cross_plot($x, $x->sin);
$image = rvals(100, 100);
matrix_plot($image);
=head2 Mouse Interaction
Plots allow for
L,
herein referred to as twiddling. You can resize the window, zoom with the
scroll wheel, or click and drag the canvas around. There is also a
right-click zoom-rectangle, and a right-click context menu.
hist_plot(grandom(100));
# Run this, then try using your mouse
In your Perl scripts, and in the PDL shell for some operating systems and
some versions of L, twiddling will cause your script to pause
when you create a new plot. To resume your script or return execution to the
shell, either close the window or press 'q'.
# If your PDL shell supports simultaneous
# input and plot interaction, running this
# should display both plots simultaneously:
$x = sequence(100)/10;
cross_plot($x, $x->sin);
line_plot($x, $x->cos);
=head2 Multiple plots without blocking
The blocking behavior just discussed is due to what is called autotwiddling.
To turn this off, simply send a boolean false value to auto_twiddle. Then,
be sure to invoke twiddling when you're done creating your plots.
auto_twiddle(0);
hist_plot(grandom(100));
matrix_plot(rvals(100, 100));
twiddle();
Once turned off, autotwiddling will remain off until you turn it back on.
# autotwiddling still off
hist_plot(grandom(100));
matrix_plot(rvals(100, 100));
twiddle();
=head2 Adding a title and axis labels
Functions like
L,
L, and
L actually create and
return plot objects which you can subsequently modify. For example,
adding a title and axis labels are pretty easy. For titles, you call the
L. For axis
labels, you call the
L