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 $VERSION = '0.13';
11  
12 1 ▶use List::Util 1.33;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use PGPLOT::Device;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use PGPLOT ();
Subroutine BEGIN: called CC=1 SCAR=2.6
16 use PDL::Graphics::PGPLOT::Window ();
17  
18  
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 sub new {
46 my ( $class, $opt ) = @_;
47  
48 my %opt = List::Util::pairmap { lc( $a ) => $b } winopts => {},
49 defined $opt ? %$opt : ();
50  
51 my $self = {};
52 $self->{device} = PGPLOT::Device->new(
53 defined $opt{device} ? $opt{device} : (),
54 defined $opt{devopts} ? $opt{devopts} : (),
55 );
56 $self->{not_first} = 0;
57 $self->{win} = undef;
58 $self->{winopts} = { %{ $opt{winopts} } };
59  
60 bless $self, $class;
61  
62 $self;
63 }
64  
65  
66  
67  
68  
69  
70  
71  
72 sub winopts {
73 return { %{ $_[0]->{winopts} } };
74 }
75  
76 sub _update_winopts {
77 my ( $self, $new ) = @_;
78  
79 my $orig = $self->{winopts};
80 $self->{winopts} = { %{$new} };
81  
82 # keys of the elements that aren't the same between the
83 # new and old winopts.
84 my %lc_orig = map { lc $_ => $orig->{$_} } keys %$orig;
85 my %lc_new = map { lc $_ => $new->{$_} } keys %$new;
86  
87 my @diffs = grep {
88 !(
89 ( exists $lc_new{$_} && exists $lc_orig{$_} ) && ( ( !defined $lc_new{$_} && !defined $lc_orig{$_} )
90 || ( $lc_new{$_} eq $lc_orig{$_} ) ) )
91 } List::Util::uniq( keys %lc_new, keys %lc_orig );
92  
93  
94 return unless @diffs && defined $self->{win};
95 my $win = $self->{win};
96  
97 my %diffs;
98 @diffs{@diffs} = ();
99  
100 if ( exists $diffs{justify} ) {
101 for my $options ( 'PlotOptions', 'Options' ) {
102 my $defaults = $win->{$options}->defaults;
103 $defaults->{Justify} = $lc_new{justify};
104 $win->{$options}->defaults( $defaults );
105 }
106 }
107  
108 # try to do something if just the number of panels are being changed
109 if ( exists $diffs{nxpanel} || exists $diffs{nypanel} ) {
110 my ( $NX, $NY )
111 = ( $lc_new{nxpanel} // 1, $lc_new{nypanel} // 1 );
112  
113 # don't look! PDL::Graphics::PGPLOT::Window doesn't understand
114 # how to change the number of panels while a device is open,
115 # so need to do it manually and then poke at the object's innards
116 # so that it will know the new panel grid and clear the device
117 # in advance of the next plot
118 PGPLOT::pgsubp( $NX, $NY );
119 $win->{NX} = $NX;
120 $win->{NY} = $NY;
121 $win->{CurrentPanel} = $NX * $NY;
122 }
123 }
124  
125  
126  
127  
128  
129  
130  
131  
132  
133 sub device { $_[0]->{device} }
134  
135  
136  
137  
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 sub next { ## no critic ( BuiltinHomonyms )
171 my $self = shift;
172 my $winopts = 'HASH' eq ref $_[0] ? shift : undef;
173  
174 $self->override( @_ ) if @_;
175  
176 # prompt user before displaying second and subsequent plots if
177 # a new plot will erase the previous one
178 PGPLOT::pgask( $self->{device}->ask )
179 if $self->{not_first}++;
180  
181 if ( $self->{device}->would_change ) {
182 $self->finish;
183 $self->{winopts} = {%$winopts} if defined $winopts;
184 $self->{win} = PDL::Graphics::PGPLOT::Window->new( {
185 Device => $self->{device}->next,
186 %{ $self->{winopts} } } );
187 }
188 else {
189 $self->_update_winopts( $winopts ) if defined $winopts;
190 }
191  
192 $self->{win};
193 }
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204 sub override {
205 my $self = shift;
206 $self->{device}->override( @_ );
207 }
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224 sub finish {
225 my ( $self ) = @_;
226 # make sure that the plot stays up until the user is done with it
227 if ( defined $self->{win} ) {
228 PGPLOT::pgask( 1 ) if $self->{device}->is_ephemeral;
229 $self->{win}->close;
230 }
231 }
232  
233 1;
234  
235 __END__
« blib/lib/PGPLOT/Device.pm ↑ Summary Build »