blib/lib/PGPLOT/Device/PGWin.pm

filter: Statementstmt 88.2% 15 / 17 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.3% 21 / 23 CC 1 SCAR 0.5
CC
1
Cov
88%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/PGPLOT/Device.pm ↑ Summary Build »
1 package PGPLOT::Device::PGWin;
2  
3 # ABSTRACT: convenience class for PDL::Graphics::PGPLOT::Window
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 our @ISA = qw();
11  
12 our $VERSION = '0.12';
13  
14 1 ▶use List::Util 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use PGPLOT::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use PGPLOT ();
Subroutine BEGIN: called CC=1 SCAR=2.6
18 use PDL::Graphics::PGPLOT::Window ();
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47 sub new {
48 my ( $class, $opt ) = @_;
49  
50 my %opt = List::Util::pairmap { lc( $a ) => $b }
51 winopts => {},
52 defined $opt ? %$opt : ();
53  
54 my $self = {};
55 $self->{device} = PGPLOT::Device->new(
56 defined $opt{device} ? $opt{device} : (),
57 defined $opt{devopts} ? $opt{devopts} : (),
58 );
59 $self->{not_first} = 0;
60 $self->{win} = undef;
61 $self->{winopts} = { %{ $opt{winopts} } };
62  
63 bless $self, $class;
64  
65 $self;
66 }
67  
68  
69  
70  
71  
72  
73  
74  
75 sub winopts {
76 return { %{ $_[0]->{winopts} } };
77 }
78  
79 sub _update_winopts {
80 my ( $self, $new ) = @_;
81  
82 my $orig = $self->{winopts};
83 $self->{winopts} = { %{$new} };
84  
85 # keys of the elements that aren't the same between the
86 # new and old winopts.
87 my %lc_orig = map { lc $_ => $orig->{$_} } keys %$orig;
88 my %lc_new = map { lc $_ => $new->{$_} } keys %$new;
89  
90 my @diffs = grep {
91 !(
92 ( exists $lc_new{$_} && exists $lc_orig{$_} )
93 && ( ( !defined $lc_new{$_} && !defined $lc_orig{$_} )
94 || ( $lc_new{$_} eq $lc_orig{$_} ) ) )
95 } List::Util::uniq( keys %lc_new, keys %lc_orig );
96  
97  
98 return unless @diffs && defined $self->{win};
99 my $win = $self->{win};
100  
101 my %diffs;
102 @diffs{@diffs} = ();
103  
104 if ( exists $diffs{justify} ) {
105 for my $options ( 'PlotOptions', 'Options' ) {
106 my $defaults = $win->{$options}->defaults;
107 $defaults->{Justify} = $lc_new{justify};
108 $win->{$options}->defaults( $defaults );
109 }
110 }
111  
112 # try to do something if just the number of panels are being changed
113 if ( exists $diffs{nxpanel} || exists $diffs{nypanel} ) {
114 my ( $NX, $NY )
115 = ( $lc_new{nxpanel} // 1, $lc_new{nypanel} // 1 );
116  
117 # don't look! PDL::Graphics::PGPLOT::Window doesn't understand
118 # how to change the number of panels while a device is open,
119 # so need to do it manually and then poke at the object's innards
120 # so that it will know the new panel grid and clear the device
121 # in advance of the next plot
122 PGPLOT::pgsubp( $NX, $NY );
123 $win->{NX} = $NX;
124 $win->{NY} = $NY;
125 $win->{CurrentPanel} = $NX * $NY;
126 }
127 }
128  
129  
130  
131  
132  
133  
134  
135  
136  
137 sub device { $_[0]->{device} }
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174 sub next {
175 my $self = shift;
176 my $winopts = 'HASH' eq ref $_[0] ? shift : undef;
177  
178 $self->override( @_ ) if @_;
179  
180 # prompt user before displaying second and subsequent plots if
181 # a new plot will erase the previous one
182 PGPLOT::pgask( $self->{device}->ask )
183 if $self->{not_first}++;
184  
185 if ( $self->{device}->would_change ) {
186 $self->finish;
187 $self->{winopts} = { %$winopts } if defined $winopts;
188 $self->{win} = PDL::Graphics::PGPLOT::Window->new( {
189 Device => $self->{device}->next,
190 %{ $self->{winopts} } } );
191 }
192 else {
193 $self->_update_winopts( $winopts ) if defined $winopts;
194 }
195  
196 $self->{win};
197 }
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208 sub override {
209 my $self = shift;
210 $self->{device}->override( @_ );
211 }
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228 sub finish {
229 my ( $self ) = @_;
230 # make sure that the plot stays up until the user is done with it
231 if ( defined $self->{win} ) {
232 PGPLOT::pgask( 1 ) if $self->{device}->is_ephemeral;
233 $self->{win}->close;
234 }
235 }
236  
237 __END__
« blib/lib/PGPLOT/Device.pm ↑ Summary Build »