| 1 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 2 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | ############################################################################ | ||||||||||
| 5 | package PDL::Graphics::Prima; | ||||||||||
| 6 | ############################################################################ | ||||||||||
| 7 | our $VERSION = 0.17; # do not delete these spaces; run update-version.pl | ||||||||||
| 8 | # if you change this | ||||||||||
| 9 | |||||||||||
| 10 | # Add automatic support for PDL terminal interactivity | ||||||||||
| 11 | 1 | ▶ | use PDL::Graphics::Prima::ReadLine; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | sub import { | ||||||||||
| 13 | 0 | ▶ | my $class = shift; | ||||||||
|
Subroutine
import: not called CC=2 SCAR=17.9
| |||||||||||
| 14 | |||||||||||
| 15 | # Set up the interactivity, if possible with this terminal | ||||||||||
| 16 | 0 | ▶ | PDL::Graphics::Prima::ReadLine->setup($PERLDL::TERM) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | if PDL::Graphics::Prima::ReadLine->is_happy_with($PERLDL::TERM); | ||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | ############################################################################ | ||||||||||
| 21 | package Prima::Plot; | ||||||||||
| 22 | ############################################################################ | ||||||||||
| 23 | |||||||||||
| 24 | # Prima | ||||||||||
| 25 | 0 | use Prima qw(noX11 Application ImageDialog MsgBox Utils Buttons InputLine | |||||||||
| 26 | 1 | ▶ | Label); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=1.2
| |||||||||||
| 27 | use base 'Prima::Widget'; | ||||||||||
| 28 | |||||||||||
| 29 | # Error reporting | ||||||||||
| 30 | use Carp; | ||||||||||
| 31 | |||||||||||
| 32 | # PDL | ||||||||||
| 33 | use PDL::Lite; | ||||||||||
| 34 | use PDL::NiceSlice; | ||||||||||
| 35 | use PDL::Drawing::Prima; | ||||||||||
| 36 | |||||||||||
| 37 | # library-specific modules whose functionality I need | ||||||||||
| 38 | use PDL::Graphics::Prima::Axis; | ||||||||||
| 39 | use PDL::Graphics::Prima::DataSet; | ||||||||||
| 40 | |||||||||||
| 41 | # Next: use block-comments to describe the purpose of each method. | ||||||||||
| 42 | |||||||||||
| 43 | ###################################### | ||||||||||
| 44 | # Usage : ???? | ||||||||||
| 45 | # Purpose : ???? | ||||||||||
| 46 | # Arguments : ???? | ||||||||||
| 47 | # Returns : ???? | ||||||||||
| 48 | # Side Effects : none | ||||||||||
| 49 | # Throws : no exceptions | ||||||||||
| 50 | # Comments : none | ||||||||||
| 51 | # See Also : n/a | ||||||||||
| 52 | |||||||||||
| 53 | ###################################### | ||||||||||
| 54 | # Usage : Not used directly; this is invoked by Prima's inherited | ||||||||||
| 55 | # : constructor. | ||||||||||
| 56 | # Purpose : Sets up a default profile for a graph widget | ||||||||||
| 57 | # Arguments : The (completely uninitialized) object | ||||||||||
| 58 | # Returns : a hashref | ||||||||||
| 59 | # Side Effects : none | ||||||||||
| 60 | # Throws : never | ||||||||||
| 61 | # Comments : none | ||||||||||
| 62 | # See Also : init | ||||||||||
| 63 | |||||||||||
| 64 | sub profile_default { | ||||||||||
| 65 | my %def = %{$_[ 0]-> SUPER::profile_default}; | ||||||||||
| 66 | |||||||||||
| 67 | return { | ||||||||||
| 68 | %def, | ||||||||||
| 69 | # default properties follow | ||||||||||
| 70 | |||||||||||
| 71 | # Title basics | ||||||||||
| 72 | title => '', | ||||||||||
| 73 | titleSpace => '1line', | ||||||||||
| 74 | titleFont => { height => '10%height' }, | ||||||||||
| 75 | |||||||||||
| 76 | backColor => cl::White, | ||||||||||
| 77 | |||||||||||
| 78 | # replot duration in milliseconds | ||||||||||
| 79 | replotDuration => 30, | ||||||||||
| 80 | # Blank profiles for the axes: | ||||||||||
| 81 | x => {}, | ||||||||||
| 82 | y => {}, | ||||||||||
| 83 | |||||||||||
| 84 | # Other important and basic settings | ||||||||||
| 85 | selectable => 1, | ||||||||||
| 86 | buffered => 1, | ||||||||||
| 87 | }; | ||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | ###################################### | ||||||||||
| 91 | # Usage : Not used directly; this is invoked by Prima's inherited | ||||||||||
| 92 | # : constructor. | ||||||||||
| 93 | # Purpose : Initializes self's data from the profile. | ||||||||||
| 94 | # Arguments : $self, as yet uninitialized | ||||||||||
| 95 | # : a list of key => value pairs corresponding to the list of | ||||||||||
| 96 | # : arguments provided to the constructor, merged with the default | ||||||||||
| 97 | # : profile. | ||||||||||
| 98 | # Returns : A list of key => value pairs suitable for a profile hash. | ||||||||||
| 99 | # Side Effects : none | ||||||||||
| 100 | # Throws : if unable to initialize the x/y axes from their associated | ||||||||||
| 101 | # : constructor hashrefs | ||||||||||
| 102 | # Comments : This has a lot of logic for setting defaults. It might not be | ||||||||||
| 103 | # : a bad idea to refactor some of this out, so that a Plot widget | ||||||||||
| 104 | # : can be reset to a default state. | ||||||||||
| 105 | # See Also : profile_default | ||||||||||
| 106 | |||||||||||
| 107 | sub init { | ||||||||||
| 108 | my $self = shift; | ||||||||||
| 109 | my %profile = $self->SUPER::init(@_); | ||||||||||
| 110 | |||||||||||
| 111 | # Set the title properties | ||||||||||
| 112 | $self->_title($profile{title}); | ||||||||||
| 113 | $self->_titleSpace($profile{titleSpace}); | ||||||||||
| 114 | $self->_titleFont(%{$profile{titleFont}}); | ||||||||||
| 115 | |||||||||||
| 116 | # Create the x- and y-axis objects, overriding the owner and axis name | ||||||||||
| 117 | # properties if they are set in the profile. | ||||||||||
| 118 | for ('x', 'y') { | ||||||||||
| 119 | if (eval{$profile{$_}->isa('PDL::Graphics::Prima::Axis')}) { | ||||||||||
| 120 | $self->{$_} = $profile{$_}; | ||||||||||
| 121 | $self->{$_}->owner($self); | ||||||||||
| 122 | $self->{$_}->name($_); | ||||||||||
| 123 | } | ||||||||||
| 124 | elsif (ref ($profile{$_}) eq 'HASH') { | ||||||||||
| 125 | $self->{$_} = PDL::Graphics::Prima::Axis->create( | ||||||||||
| 126 | %{$profile{$_}} | ||||||||||
| 127 | , owner => $self | ||||||||||
| 128 | , name => $_ | ||||||||||
| 129 | ); | ||||||||||
| 130 | } | ||||||||||
| 131 | elsif (not ref($profile{$_})) { | ||||||||||
| 132 | # No ref means scalar; assume it's a label name | ||||||||||
| 133 | $self->{$_} = PDL::Graphics::Prima::Axis->create( | ||||||||||
| 134 | label => $profile{$_} | ||||||||||
| 135 | , owner => $self | ||||||||||
| 136 | , name => $_ | ||||||||||
| 137 | ); | ||||||||||
| 138 | } | ||||||||||
| 139 | else { | ||||||||||
| 140 | croak("Unable to create $_-axis from $profile{$_}"); | ||||||||||
| 141 | } | ||||||||||
| 142 | } | ||||||||||
| 143 | |||||||||||
| 144 | $self->{timer} = Prima::Timer->create( | ||||||||||
| 145 | timeout => $profile{replotDuration}, | ||||||||||
| 146 | onTick => sub { | ||||||||||
| 147 | $_[0]->stop; | ||||||||||
| 148 | $self->repaint; | ||||||||||
| 149 | } | ||||||||||
| 150 | ); | ||||||||||
| 151 | |||||||||||
| 152 | # Create an empty dataset array and tie it to the DataSetHash class: | ||||||||||
| 153 | my %datasets; | ||||||||||
| 154 | tie %datasets, 'PDL::Graphics::Prima::DataSet::Collection', $self; | ||||||||||
| 155 | $self->{dataSets} = \%datasets; | ||||||||||
| 156 | |||||||||||
| 157 | # Turn off the axis autoscaling until after we've added the data | ||||||||||
| 158 | $self->{x}->{initializing} = 1; | ||||||||||
| 159 | $self->{y}->{initializing} = 1; | ||||||||||
| 160 | |||||||||||
| 161 | # Add datasets. All of the datasets are validated when added as key/value | ||||||||||
| 162 | # pairs to the tied hash: | ||||||||||
| 163 | while (my ($key, $value) = each %profile) { | ||||||||||
| 164 | next unless $key =~ /^-(.+)/; | ||||||||||
| 165 | # $1 contains the name of the dataset; $value is the dataset itself: | ||||||||||
| 166 | # working here - catch errors? | ||||||||||
| 167 | $self->dataSets->{$1} = $value; | ||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | # Turn the axis autoscaling back on: | ||||||||||
| 171 | $self->{x}->{initializing} = 0; | ||||||||||
| 172 | $self->{y}->{initializing} = 0; | ||||||||||
| 173 | |||||||||||
| 174 | return %profile; | ||||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | sub on_destroy { | ||||||||||
| 178 | my $self = shift; | ||||||||||
| 179 | $self->{prop_window}->destroy if exists $self->{prop_window}; | ||||||||||
| 180 | } | ||||||||||
| 181 | |||||||||||
| 182 | # This is key: *this* is what triggers autoscaling for the first time | ||||||||||
| 183 | # working here, consider setting sizeMin, sizeMax | ||||||||||
| 184 | |||||||||||
| 185 | sub on_size { | ||||||||||
| 186 | my $self = shift; | ||||||||||
| 187 | # Note: order here is important as x needs to access information about y | ||||||||||
| 188 | # in its calculations | ||||||||||
| 189 | $self->y->update_edges; | ||||||||||
| 190 | $self->x->update_edges; | ||||||||||
| 191 | } | ||||||||||
| 192 | |||||||||||
| 193 | my $inf = -PDL->new(0)->log->at(0); | ||||||||||
| 194 | |||||||||||
| 195 | |||||||||||
| 196 | sub collect_collated_min_max_for; | ||||||||||
| 197 | sub pair_down_collation; | ||||||||||
| 198 | |||||||||||
| 199 | sub compute_min_max_for { | ||||||||||
| 200 | my ($self, $axis_name) = @_; | ||||||||||
| 201 | |||||||||||
| 202 | # Special handling for x-axis stuff: | ||||||||||
| 203 | my (undef, $y_max_is_auto) = $self->y->max; | ||||||||||
| 204 | if ($axis_name =~ /x/ and $y_max_is_auto) { | ||||||||||
| 205 | # First perform all of this on y so that the edge requirements are | ||||||||||
| 206 | # correctly computed for x-calculations later. | ||||||||||
| 207 | |||||||||||
| 208 | # Perform the collation: | ||||||||||
| 209 | my ($trimmed_minima, $trimmed_maxima) | ||||||||||
| 210 | = $self->collect_collated_min_max_for('y'); | ||||||||||
| 211 | return if not defined $trimmed_maxima; | ||||||||||
| 212 | |||||||||||
| 213 | # Perform a round of minimazation: | ||||||||||
| 214 | my ($min, $max) | ||||||||||
| 215 | = $self->pair_down_collation('y', $trimmed_minima, $trimmed_maxima); | ||||||||||
| 216 | |||||||||||
| 217 | # Update the axis min/max and redo the pair-down: | ||||||||||
| 218 | (undef, my $min_auto) = $self->y->min; | ||||||||||
| 219 | (undef, my $max_auto) = $self->y->max; | ||||||||||
| 220 | $self->y->_min($min); | ||||||||||
| 221 | $self->y->_max($max); | ||||||||||
| 222 | $self->y->recalculate_edge_requirements($self); | ||||||||||
| 223 | $self->y->_min(lm::Auto) if $min_auto; | ||||||||||
| 224 | $self->y->_max(lm::Auto) if $max_auto; | ||||||||||
| 225 | } | ||||||||||
| 226 | |||||||||||
| 227 | # Perform the collation: | ||||||||||
| 228 | my ($trimmed_minima, $trimmed_maxima) | ||||||||||
| 229 | = $self->collect_collated_min_max_for($axis_name); | ||||||||||
| 230 | return if not defined $trimmed_maxima; | ||||||||||
| 231 | |||||||||||
| 232 | return $self->pair_down_collation($axis_name, $trimmed_minima, $trimmed_maxima); | ||||||||||
| 233 | } | ||||||||||
| 234 | |||||||||||
| 235 | sub get_pixel_extent_for; | ||||||||||
| 236 | |||||||||||
| 237 | sub collect_collated_min_max_for { | ||||||||||
| 238 | my ($self, $axis_name) = @_; | ||||||||||
| 239 | |||||||||||
| 240 | # Get plotting pixel extent, which I'll need to send to the dataSets in | ||||||||||
| 241 | # order for them to compute their pyramids. | ||||||||||
| 242 | my $pixel_extent = $self->get_pixel_extent_for($axis_name); | ||||||||||
| 243 | return if $pixel_extent <= 0; | ||||||||||
| 244 | |||||||||||
| 245 | my $datasets = $self->{dataSets}; | ||||||||||
| 246 | my (@min_collection, @max_collection); | ||||||||||
| 247 | while (my ($key, $dataset) = each %$datasets) { | ||||||||||
| 248 | next if $key eq 'widget'; | ||||||||||
| 249 | |||||||||||
| 250 | # Accumulate all the collated results | ||||||||||
| 251 | my ($min, $max) = $dataset->compute_collated_min_max_for($axis_name, $pixel_extent); | ||||||||||
| 252 | # The collated results should be one dimensional, so no reduction | ||||||||||
| 253 | # necessary (as opposed to the DataSet code): | ||||||||||
| 254 | push @min_collection, $min; | ||||||||||
| 255 | push @max_collection, $max; | ||||||||||
| 256 | } | ||||||||||
| 257 | |||||||||||
| 258 | return if @min_collection == 0; | ||||||||||
| 259 | |||||||||||
| 260 | # Merge all the data: | ||||||||||
| 261 | my $collated_min = PDL::cat(@min_collection)->mv(-1,0)->minimum; | ||||||||||
| 262 | my $collated_max = PDL::cat(@max_collection)->mv(-1,0)->maximum; | ||||||||||
| 263 | |||||||||||
| 264 | # It could be the case that all the values are bad. In that case, insert | ||||||||||
| 265 | # an entry at zero-pixels corresponding to the current view limits: | ||||||||||
| 266 | $collated_min(0) .= $self->{$axis_name}->min if $collated_min->isbad->all; | ||||||||||
| 267 | $collated_max(0) .= $self->{$axis_name}->max if $collated_max->isbad->all; | ||||||||||
| 268 | |||||||||||
| 269 | # Iterativelye pair down the set until we've found the minmax. At this | ||||||||||
| 270 | # point, we have two arrays with $pixel_extent elements each. Cat an | ||||||||||
| 271 | # index and what will eventually be a computed value onto the original | ||||||||||
| 272 | # lists so our slicing keeps track of the padding and original values. | ||||||||||
| 273 | my $minima = $collated_min->cat($collated_min->sequence, $collated_min); | ||||||||||
| 274 | my $maxima = $collated_max->cat($collated_max->sequence, $collated_max); | ||||||||||
| 275 | |||||||||||
| 276 | # Get rid of all the bad values. We know that at least one good value will | ||||||||||
| 277 | # remain due to the min/max insertion a few lines up. | ||||||||||
| 278 | my $trimmed_minima = $minima->whereND($minima(:,0;-)->isgood); | ||||||||||
| 279 | my $trimmed_maxima = $maxima->whereND($maxima(:,0;-)->isgood); | ||||||||||
| 280 | |||||||||||
| 281 | return ($trimmed_minima, $trimmed_maxima); | ||||||||||
| 282 | } | ||||||||||
| 283 | |||||||||||
| 284 | # Given a collated set of mininam and maxima (from collect_collated_min_max_for) | ||||||||||
| 285 | # this function determines that minimum and maximum necessary for viewing *all* | ||||||||||
| 286 | # the data. | ||||||||||
| 287 | |||||||||||
| 288 | sub pair_down_collation { | ||||||||||
| 289 | my ($self, $axis_name, $trimmed_minima, $trimmed_maxima) = @_; | ||||||||||
| 290 | |||||||||||
| 291 | my $min_mask = $trimmed_minima->trim_collated_min; | ||||||||||
| 292 | my $max_mask = $trimmed_maxima->trim_collated_max; | ||||||||||
| 293 | $trimmed_minima = $trimmed_minima->whereND($min_mask); | ||||||||||
| 294 | $trimmed_maxima = $trimmed_maxima->whereND($max_mask); | ||||||||||
| 295 | |||||||||||
| 296 | # Compute properly scaled extrema. | ||||||||||
| 297 | # min_pix and max_pix are the plain pixel paddings needed by the lowest | ||||||||||
| 298 | # element in the pyramid: | ||||||||||
| 299 | my ($min_pix, $max_pix) = ($trimmed_minima->at(0,1), $trimmed_maxima->at(0,1)); | ||||||||||
| 300 | # virtual_pixel_extent is the room available to plot the data when the | ||||||||||
| 301 | # real pixel extent is reduced by the requested pixel padding: | ||||||||||
| 302 | my $pixel_extent = $self->get_pixel_extent_for($axis_name); | ||||||||||
| 303 | my $virtual_pixel_extent = $pixel_extent - $min_pix - $max_pix; | ||||||||||
| 304 | # working here - come up with something better than just croaking. | ||||||||||
| 305 | die "Internal error: virtual pixel extent is non-positive" | ||||||||||
| 306 | if $virtual_pixel_extent <= 0; | ||||||||||
| 307 | # min_data and max_data are the actual min and max values of the data | ||||||||||
| 308 | # that are supposed to fit within the virtual pixel extent. | ||||||||||
| 309 | my ($min_data, $max_data) | ||||||||||
| 310 | = ($trimmed_minima(:,(2))->min, $trimmed_maxima(:,(2))->max); | ||||||||||
| 311 | |||||||||||
| 312 | # It is possible that all the x-data or all the y-data are identical. | ||||||||||
| 313 | # Check if the current min/max values are identical and return the | ||||||||||
| 314 | # scaling's response in such a situation: | ||||||||||
| 315 | return $self->{$axis_name}->scaling->min_max_for_degenerate($min_data) | ||||||||||
| 316 | if $min_data == $max_data; | ||||||||||
| 317 | |||||||||||
| 318 | # min and max are the final minimal and maximal values that we use for | ||||||||||
| 319 | # the axis so that all the data can be drawn within the plot. | ||||||||||
| 320 | my $min = $self->{$axis_name}->scaling->inv_transform($min_data, $max_data | ||||||||||
| 321 | , -$min_pix/$virtual_pixel_extent); | ||||||||||
| 322 | my $max = $self->{$axis_name}->scaling->inv_transform($min_data, $max_data | ||||||||||
| 323 | , 1 + $max_pix/$virtual_pixel_extent); | ||||||||||
| 324 | # working here - come up with something better than just croaking. | ||||||||||
| 325 | die "Internal error: min ($min) is greater than max ($max)" if $min > $max; | ||||||||||
| 326 | |||||||||||
| 327 | # We will iterate until we stop removing rows, at which point the lowest | ||||||||||
| 328 | # level of the pyramid is what we want. | ||||||||||
| 329 | my $N_rows; | ||||||||||
| 330 | do { | ||||||||||
| 331 | $N_rows = $trimmed_minima->dim(0); | ||||||||||
| 332 | |||||||||||
| 333 | # Assuming that the current min/max are roughly correct, compute the | ||||||||||
| 334 | # updated min/max calculated values: | ||||||||||
| 335 | $trimmed_minima(:,2) | ||||||||||
| 336 | .= $self->{$axis_name}->pixels_to_reals( | ||||||||||
| 337 | $self->{$axis_name}->reals_to_pixels($trimmed_minima(:,0), 1, $min, $max) | ||||||||||
| 338 | - $trimmed_minima(:,1), 1, $min, $max); | ||||||||||
| 339 | $trimmed_maxima(:,2) | ||||||||||
| 340 | .= $self->{$axis_name}->pixels_to_reals( | ||||||||||
| 341 | $self->{$axis_name}->reals_to_pixels($trimmed_maxima(:,0), 1, $min, $max) | ||||||||||
| 342 | + $trimmed_maxima(:,1), 1, $min, $max); | ||||||||||
| 343 | |||||||||||
| 344 | # Trim again: | ||||||||||
| 345 | $min_mask = $trimmed_minima->trim_collated_min; | ||||||||||
| 346 | $max_mask = $trimmed_maxima->trim_collated_max; | ||||||||||
| 347 | |||||||||||
| 348 | $trimmed_minima = $trimmed_minima->whereND($min_mask); | ||||||||||
| 349 | $trimmed_maxima = $trimmed_maxima->whereND($max_mask); | ||||||||||
| 350 | |||||||||||
| 351 | # Recompute the properly scaled extrema: | ||||||||||
| 352 | ($min_pix, $max_pix) = ($trimmed_minima->at(0,1), $trimmed_maxima->at(0,1)); | ||||||||||
| 353 | $virtual_pixel_extent = $pixel_extent - $min_pix - $max_pix; | ||||||||||
| 354 | ($min_data, $max_data) = ($trimmed_minima->at(0,0), $trimmed_maxima->at(0,0)); | ||||||||||
| 355 | $min = $self->{$axis_name}->scaling->inv_transform($min_data, $max_data | ||||||||||
| 356 | , -$min_pix/$virtual_pixel_extent); | ||||||||||
| 357 | $max = $self->{$axis_name}->scaling->inv_transform($min_data, $max_data | ||||||||||
| 358 | , 1 + $max_pix/$virtual_pixel_extent); | ||||||||||
| 359 | } while($N_rows != $trimmed_minima->dim(0)); | ||||||||||
| 360 | |||||||||||
| 361 | return ($min, $max); | ||||||||||
| 362 | } | ||||||||||
| 363 | |||||||||||
| 364 | sub get_pixel_extent_for { | ||||||||||
| 365 | my ($self, $axis_name) = @_; | ||||||||||
| 366 | |||||||||||
| 367 | my ($left, $bottom, $right, $top) = $self->get_edge_requirements; | ||||||||||
| 368 | my $pixel_extent; | ||||||||||
| 369 | if ($axis_name eq 'x') { | ||||||||||
| 370 | $pixel_extent = $self->width - $left - $right; | ||||||||||
| 371 | } | ||||||||||
| 372 | else { | ||||||||||
| 373 | $pixel_extent = $self->height - $top - $bottom; | ||||||||||
| 374 | } | ||||||||||
| 375 | return $pixel_extent; | ||||||||||
| 376 | } | ||||||||||
| 377 | |||||||||||
| 378 | # This should distinguish between shared space and exclusive space. For | ||||||||||
| 379 | # example, the title requests exclusive space (a height of titleSpace), | ||||||||||
| 380 | # which is added to that shared max shared space requested by both of the | ||||||||||
| 381 | # axes. The return values should be left, bottom, right, top | ||||||||||
| 382 | sub get_edge_requirements { | ||||||||||
| 383 | my $self = shift; | ||||||||||
| 384 | my @x_req = $self->x->get_edge_requirements; | ||||||||||
| 385 | my @y_req = $self->y->get_edge_requirements; | ||||||||||
| 386 | |||||||||||
| 387 | # Merge the two: | ||||||||||
| 388 | my @requirement = (0, 0, 0, 0); | ||||||||||
| 389 | my $i = 0; | ||||||||||
| 390 | foreach my $req (@x_req, @y_req) { | ||||||||||
| 391 | $requirement[$i] = $req if $requirement[$i] < $req; | ||||||||||
| 392 | $i++; | ||||||||||
| 393 | $i %= 4; | ||||||||||
| 394 | } | ||||||||||
| 395 | |||||||||||
| 396 | $requirement[3] += $self->titleSpace | ||||||||||
| 397 | if defined $self->{title} and $self->{title} ne ''; | ||||||||||
| 398 | |||||||||||
| 399 | return @requirement; | ||||||||||
| 400 | } | ||||||||||
| 401 | |||||||||||
| 402 | ###################################### | ||||||||||
| 403 | # Usage : my $title_string = $plot->title; # get | ||||||||||
| 404 | # : $plot->title('new title'); # set | ||||||||||
| 405 | # : $plot->title(undef); # clear | ||||||||||
| 406 | # : $plot->title(''); # clear | ||||||||||
| 407 | # Purpose : Gets, sets, or clears the plot's title. | ||||||||||
| 408 | # Arguments : $self | ||||||||||
| 409 | # : an optional title string, or an optional undef | ||||||||||
| 410 | # Returns : In get mode, the title; in set mode, the newly set title. | ||||||||||
| 411 | # Side Effects : Issues a ChangeTitle notification, which will cause the | ||||||||||
| 412 | # : y-extent of the graph to change when the title goes from unset | ||||||||||
| 413 | # : to set, or set to unset. | ||||||||||
| 414 | # Throws : never | ||||||||||
| 415 | # Comments : None | ||||||||||
| 416 | # See Also : titleSpace, on_changetitle, draw_plot, ChangeTitle | ||||||||||
| 417 | |||||||||||
| 418 | sub _title { | ||||||||||
| 419 | $_[0]->{title} = $_[1]; | ||||||||||
| 420 | } | ||||||||||
| 421 | |||||||||||
| 422 | sub title { | ||||||||||
| 423 | return $_[0]->{title} unless $#_; | ||||||||||
| 424 | $_[0]->_title($_[1]); | ||||||||||
| 425 | $_[0]->notify('ChangeTitle'); | ||||||||||
| 426 | return $_[0]->{title}; | ||||||||||
| 427 | } | ||||||||||
| 428 | |||||||||||
| 429 | sub _titleFont { | ||||||||||
| 430 | my $self = shift; | ||||||||||
| 431 | |||||||||||
| 432 | # Return a copy of the current font hash if this is a getter | ||||||||||
| 433 | return %{$self->{titleFont}} if @_ == 0; | ||||||||||
| 434 | |||||||||||
| 435 | # Otherwise store the provided font hash, overwriting the previous one | ||||||||||
| 436 | $self->{titleFont} = { @_ }; | ||||||||||
| 437 | } | ||||||||||
| 438 | |||||||||||
| 439 | sub titleFont { | ||||||||||
| 440 | $_[0]->notify('ChangeTitle') if @_ > 1; | ||||||||||
| 441 | goto \&_titleFont; | ||||||||||
| 442 | } | ||||||||||
| 443 | |||||||||||
| 444 | # Assumes that begin_paint or begin_paint_info have already been called. | ||||||||||
| 445 | sub _set_title_font { | ||||||||||
| 446 | my ($self, $canvas) = (shift, shift); | ||||||||||
| 447 | |||||||||||
| 448 | # Get a copy of the title font hash and process any relative sizes | ||||||||||
| 449 | my %title_font_hash = $self->_titleFont; | ||||||||||
| 450 | SETTING: for my $setting ( qw(height width size) ) { | ||||||||||
| 451 | next SETTING unless exists $title_font_hash{$setting}; | ||||||||||
| 452 | if ($title_font_hash{$setting} =~ /(.*)\%width/) { | ||||||||||
| 453 | $title_font_hash{$setting} = $canvas->width * $1 / 100; | ||||||||||
| 454 | } | ||||||||||
| 455 | elsif ($title_font_hash{$setting} =~ /(.*)\%height/) { | ||||||||||
| 456 | $title_font_hash{$setting} = $canvas->height * $1 / 100; | ||||||||||
| 457 | } | ||||||||||
| 458 | elsif ($title_font_hash{$setting} =~ /(.*)x/) { | ||||||||||
| 459 | $title_font_hash{$setting} = $canvas->font->{$setting} * $1; | ||||||||||
| 460 | } | ||||||||||
| 461 | } | ||||||||||
| 462 | |||||||||||
| 463 | # Set it | ||||||||||
| 464 | $canvas->font(%title_font_hash); | ||||||||||
| 465 | } | ||||||||||
| 466 | |||||||||||
| 467 | sub _title_font_height { | ||||||||||
| 468 | my ($self, $canvas) = (shift, shift); | ||||||||||
| 469 | # Set up the paint info state. This will fail if we're already in paint | ||||||||||
| 470 | # info state, in which case we will not want to clear it at the end: | ||||||||||
| 471 | my $will_clear_paint_info = $canvas->begin_paint_info; | ||||||||||
| 472 | |||||||||||
| 473 | # Backup the font, change to the title font, and compute | ||||||||||
| 474 | my $font = $canvas->font; | ||||||||||
| 475 | $self->_set_title_font($canvas); | ||||||||||
| 476 | my $height = $canvas->font->height; | ||||||||||
| 477 | |||||||||||
| 478 | # Restore the widget to the previous state | ||||||||||
| 479 | $canvas->font($font); | ||||||||||
| 480 | $canvas->end_paint_info if $will_clear_paint_info; | ||||||||||
| 481 | |||||||||||
| 482 | # All done | ||||||||||
| 483 | return $height; | ||||||||||
| 484 | } | ||||||||||
| 485 | |||||||||||
| 486 | ###################################### | ||||||||||
| 487 | # Usage : my $title_space = $plot->titleSpace; # get | ||||||||||
| 488 | # : $plot->titleSpace(40); # set | ||||||||||
| 489 | # : $plot->titleSpace('10% - 5px'); # set | ||||||||||
| 490 | # : $plot->title(0); # hide | ||||||||||
| 491 | # : $plot->title(undef); # hide | ||||||||||
| 492 | # Purpose : Gets or sets the plot's titleSpace. | ||||||||||
| 493 | # Arguments : $self | ||||||||||
| 494 | # : an optional title height in pixels | ||||||||||
| 495 | # Returns : In get mode, the titleSpace; in set mode, the new height | ||||||||||
| 496 | # Side Effects : Issues a ChangeTitle notification, which will cause the | ||||||||||
| 497 | # : y-extent of the graph to change. | ||||||||||
| 498 | # Throws : when the new titleSpace is not a nonnegative integer | ||||||||||
| 499 | # Comments : An argument of undef is treated as zero, and stored as such. | ||||||||||
| 500 | # : Note that this mechanism is not very powerful at the moment. | ||||||||||
| 501 | # : It is likely to be extended to allow for specification in terms | ||||||||||
| 502 | # : of canvas height or width, em-width, and other parameters. See | ||||||||||
| 503 | # : PDL::Graphics::Prima::PlotType::Annotation's spec_string | ||||||||||
| 504 | # : handling to get an idea for what I have in mind. | ||||||||||
| 505 | # See Also : title, on_changetitle, draw_plot, ChangeTitle | ||||||||||
| 506 | |||||||||||
| 507 | my %allowed_entries = map {$_ => 1} qw(pixels canvas_pct lines); | ||||||||||
| 508 | my $float_point_regex = qr/[-+]?([0-9]*\.?[0-9]+|[0-9]+\.[0-9]*)([eE][-+]?[0-9]+)?/; | ||||||||||
| 509 | sub _titleSpace { | ||||||||||
| 510 | my ($self, $new_space) = @_; | ||||||||||
| 511 | $new_space = 0 if not defined $new_space; | ||||||||||
| 512 | |||||||||||
| 513 | # special-cases for a subref or nonnegative integer: just set it | ||||||||||
| 514 | if (ref($new_space) eq ref(sub{}) | ||||||||||
| 515 | or ref($new_space) eq ref('scalar') and $new_space =~ /^\d+$/ | ||||||||||
| 516 | ) { | ||||||||||
| 517 | $self->{titleSpace} = $new_space; | ||||||||||
| 518 | return; | ||||||||||
| 519 | } | ||||||||||
| 520 | # Special case for hashref: check and set if it passes | ||||||||||
| 521 | if (ref($new_space) eq 'HASH') { | ||||||||||
| 522 | # Make sure the entries are valid | ||||||||||
| 523 | for my $key (keys %$new_space) { | ||||||||||
| 524 | croak("titleSpace hash has invalid key $key") | ||||||||||
| 525 | unless $allowed_entries{$key}; | ||||||||||
| 526 | } | ||||||||||
| 527 | # Good to go; set it | ||||||||||
| 528 | $self->{titleSpace} = $new_space; | ||||||||||
| 529 | return; | ||||||||||
| 530 | } | ||||||||||
| 531 | |||||||||||
| 532 | # Otherwise we have a height spec to parse | ||||||||||
| 533 | my $spec = {}; | ||||||||||
| 534 | $new_space =~ s/\s+//g; | ||||||||||
| 535 | $new_space = lc $new_space; | ||||||||||
| 536 | |||||||||||
| 537 | if ($new_space =~ s/($float_point_regex)lines?//) { | ||||||||||
| 538 | $spec->{lines} = $1; | ||||||||||
| 539 | } | ||||||||||
| 540 | if ($new_space =~ s/($float_point_regex)\%//) { | ||||||||||
| 541 | $spec->{canvas_pct} = $1 / 100; | ||||||||||
| 542 | } | ||||||||||
| 543 | if ($new_space =~ s/($float_point_regex)px//) { | ||||||||||
| 544 | $spec->{pixels} = $1; | ||||||||||
| 545 | } | ||||||||||
| 546 | if (length($new_space) > 0) { | ||||||||||
| 547 | croak("Unknown fragment in titleSpace specification: $new_space"); | ||||||||||
| 548 | } | ||||||||||
| 549 | |||||||||||
| 550 | # All done parsing. | ||||||||||
| 551 | $self->{titleSpace} = $spec; | ||||||||||
| 552 | } | ||||||||||
| 553 | |||||||||||
| 554 | sub titleSpace { | ||||||||||
| 555 | my $self = shift; | ||||||||||
| 556 | |||||||||||
| 557 | # Handle the setter case | ||||||||||
| 558 | if (@_) { | ||||||||||
| 559 | $self->_titleSpace(@_); | ||||||||||
| 560 | $self->notify('ChangeTitle'); | ||||||||||
| 561 | return $self->titleSpace; | ||||||||||
| 562 | } | ||||||||||
| 563 | |||||||||||
| 564 | # OK, the rest of this is for the getter code | ||||||||||
| 565 | |||||||||||
| 566 | # First the simple one: a plain number | ||||||||||
| 567 | return $self->{titleSpace} unless ref($self->{titleSpace}); | ||||||||||
| 568 | |||||||||||
| 569 | # Next the subref, which requires an invocation: | ||||||||||
| 570 | return $self->titleSpace->($self) | ||||||||||
| 571 | if ref($self->{titleSpace}) eq ref(sub{}); | ||||||||||
| 572 | |||||||||||
| 573 | # We can only reach here if we have a hashref, which requires calculation | ||||||||||
| 574 | my $spec_hash = $self->{titleSpace}; | ||||||||||
| 575 | my $titleSpace = 0; | ||||||||||
| 576 | $titleSpace += $spec_hash->{pixels} if exists $spec_hash->{pixels}; | ||||||||||
| 577 | $titleSpace += $spec_hash->{canvas_pct} * $self->height | ||||||||||
| 578 | if exists $spec_hash->{canvas_pct}; | ||||||||||
| 579 | $titleSpace += $self->_title_font_height($self) * $spec_hash->{lines} | ||||||||||
| 580 | if exists $spec_hash->{lines}; | ||||||||||
| 581 | |||||||||||
| 582 | return $titleSpace; | ||||||||||
| 583 | } | ||||||||||
| 584 | |||||||||||
| 585 | sub dataSets { | ||||||||||
| 586 | # Return the (tied) hash ref if called as a getter: | ||||||||||
| 587 | return $_[0]->{dataSets} unless $#_; | ||||||||||
| 588 | # If called as a setter, copy all the values. This may seem inefficient, | ||||||||||
| 589 | # but it automatically performs all the data validation for me: | ||||||||||
| 590 | my ($self, %new_data) = @_; | ||||||||||
| 591 | |||||||||||
| 592 | # Clear the dataset (except the widget key, whick never disappears): | ||||||||||
| 593 | %{$self->{dataSets}} = (); | ||||||||||
| 594 | |||||||||||
| 595 | # Add each dataset individually, to ensure the validation does what it's | ||||||||||
| 596 | # supposed to do: | ||||||||||
| 597 | while(my ($key, $dataset) = each %new_data) { | ||||||||||
| 598 | $self->{dataSets}->{$key} = $dataset; | ||||||||||
| 599 | } | ||||||||||
| 600 | |||||||||||
| 601 | # Don't need to issue a notification because that already happened with | ||||||||||
| 602 | # the above assignments. | ||||||||||
| 603 | #$self->notify('ChangeData'); | ||||||||||
| 604 | } | ||||||||||
| 605 | |||||||||||
| 606 | sub get_image { | ||||||||||
| 607 | my $self = shift; | ||||||||||
| 608 | |||||||||||
| 609 | # Build a prima image canvas and draw to it: | ||||||||||
| 610 | my $image = Prima::Image->create( | ||||||||||
| 611 | height => $self->height, | ||||||||||
| 612 | width => $self->width, | ||||||||||
| 613 | font => $self->font, | ||||||||||
| 614 | backColor => $self->backColor, | ||||||||||
| 615 | ) or die "Can't create an image!\n"; | ||||||||||
| 616 | $image->begin_paint or die "Can't draw on image"; | ||||||||||
| 617 | $image->clear; | ||||||||||
| 618 | $self->paint_with_widgets($image); | ||||||||||
| 619 | $image->end_paint; | ||||||||||
| 620 | return $image; | ||||||||||
| 621 | } | ||||||||||
| 622 | |||||||||||
| 623 | use Prima::PS::Drawable; | ||||||||||
| 624 | use Prima::FileDialog; | ||||||||||
| 625 | use Prima::Drawable::Subcanvas; | ||||||||||
| 626 | |||||||||||
| 627 | sub save_to_postscript { | ||||||||||
| 628 | # Get the filename as an argument, or from the save-as dialog. | ||||||||||
| 629 | my ($self, $filename) = @_; | ||||||||||
| 630 | |||||||||||
| 631 | unless ($filename) { | ||||||||||
| 632 | my $save_dialog = Prima::SaveDialog-> new( | ||||||||||
| 633 | defaultExt => 'eps', | ||||||||||
| 634 | filter => [ | ||||||||||
| 635 | ['Encapsulated Postscript files' => '*.eps'], | ||||||||||
| 636 | ['All files' => '*'], | ||||||||||
| 637 | ], | ||||||||||
| 638 | ); | ||||||||||
| 639 | # Return if they cancel out: | ||||||||||
| 640 | return unless $save_dialog->execute; | ||||||||||
| 641 | # Otherwise get the filename: | ||||||||||
| 642 | $filename = $save_dialog->fileName; | ||||||||||
| 643 | # Provide a default extension | ||||||||||
| 644 | $filename .= '.eps' unless $filename =~ /\.eps$/; | ||||||||||
| 645 | } | ||||||||||
| 646 | unlink $filename if -f $filename; | ||||||||||
| 647 | |||||||||||
| 648 | # Calculate width and height using the (hopefully standard) rule that | ||||||||||
| 649 | # 100px = 72pt = 1in. This doesn't quite work right, still. Compare the | ||||||||||
| 650 | # output of the pathological-sizing.pl script to the original raster window. | ||||||||||
| 651 | my $scaling_ratio = 72.27 / 100; | ||||||||||
| 652 | my $width = $self->width * $scaling_ratio; | ||||||||||
| 653 | my $height = $self->height * $scaling_ratio; | ||||||||||
| 654 | # Create the postscript canvas and plot to it: | ||||||||||
| 655 | my $ps = Prima::PS::Drawable-> create( onSpool => sub { | ||||||||||
| 656 | open my $fh, ">>", $filename; | ||||||||||
| 657 | print $fh $_[1]; | ||||||||||
| 658 | close $fh; | ||||||||||
| 659 | }, | ||||||||||
| 660 | pageSize => [$width, $height], | ||||||||||
| 661 | pageMargins => [0, 0, 0, 0], | ||||||||||
| 662 | isEPS => 1, | ||||||||||
| 663 | useDeviceFontsOnly => 1, | ||||||||||
| 664 | ); | ||||||||||
| 665 | $ps->resolution($self->resolution); | ||||||||||
| 666 | $ps->font(height => $self->font->height); | ||||||||||
| 667 | |||||||||||
| 668 | $ps->begin_doc | ||||||||||
| 669 | or do { | ||||||||||
| 670 | my $message = "Error generating Postscript output: $@"; | ||||||||||
| 671 | if (defined $::application) { | ||||||||||
| 672 | Prima::MsgBox::message($message, mb::Ok); | ||||||||||
| 673 | carp($message); | ||||||||||
| 674 | } | ||||||||||
| 675 | else { | ||||||||||
| 676 | croak($message); | ||||||||||
| 677 | } | ||||||||||
| 678 | }; | ||||||||||
| 679 | |||||||||||
| 680 | $self->paint_with_widgets($ps); | ||||||||||
| 681 | $ps->end_doc; | ||||||||||
| 682 | } | ||||||||||
| 683 | |||||||||||
| 684 | # A routine to save the current plot to a rasterized file: | ||||||||||
| 685 | sub save_to_file { | ||||||||||
| 686 | # Get the filename as an argument or from a save-as dialog. | ||||||||||
| 687 | my ($self, $filename) = @_; | ||||||||||
| 688 | |||||||||||
| 689 | # Get the image | ||||||||||
| 690 | my $image = $self->get_image; | ||||||||||
| 691 | |||||||||||
| 692 | # If they didn't specify a filename, run a dialog to get it: | ||||||||||
| 693 | unless ($filename) { | ||||||||||
| 694 | my $dlg = Prima::ImageSaveDialog-> create; | ||||||||||
| 695 | |||||||||||
| 696 | $dlg->save($image); | ||||||||||
| 697 | return; | ||||||||||
| 698 | } | ||||||||||
| 699 | |||||||||||
| 700 | # If they specified a filename, simply save it: | ||||||||||
| 701 | $image-> save($filename) | ||||||||||
| 702 | or do { | ||||||||||
| 703 | my $message = "Error generating figure output: $@"; | ||||||||||
| 704 | if (defined $::application) { | ||||||||||
| 705 | Prima::MsgBox::message($message, mb::Ok); | ||||||||||
| 706 | carp($message); | ||||||||||
| 707 | } | ||||||||||
| 708 | else { | ||||||||||
| 709 | croak($message); | ||||||||||
| 710 | } | ||||||||||
| 711 | }; | ||||||||||
| 712 | } | ||||||||||
| 713 | |||||||||||
| 714 | sub copy_to_clipboard { | ||||||||||
| 715 | my $self = shift; | ||||||||||
| 716 | my $image = $self->get_image; | ||||||||||
| 717 | |||||||||||
| 718 | my $clipboard = $::application->Clipboard; | ||||||||||
| 719 | $clipboard->open; | ||||||||||
| 720 | $clipboard->clear; | ||||||||||
| 721 | $clipboard->image($image); | ||||||||||
| 722 | $clipboard->close; | ||||||||||
| 723 | } | ||||||||||
| 724 | |||||||||||
| 725 | # For a change in title, recompute the autoscaling and issue an immediate | ||||||||||
| 726 | # repaint. Replotting is not appropriate here as replotting issues a timer | ||||||||||
| 727 | # event that may not get triggered if the event loop isn't running (i.e. | ||||||||||
| 728 | # we're in the PDL shell without ReadLine integration). | ||||||||||
| 729 | sub on_changetitle { | ||||||||||
| 730 | my $self = shift; | ||||||||||
| 731 | $self->x->update_edges; | ||||||||||
| 732 | $self->y->update_edges; | ||||||||||
| 733 | $self->notify('Paint'); | ||||||||||
| 734 | # If running in the PDL shell, clear the event queue so this hits | ||||||||||
| 735 | # immediately | ||||||||||
| 736 | $::application->yield if defined $PERLDL::TERM; | ||||||||||
| 737 | } | ||||||||||
| 738 | |||||||||||
| 739 | # Sets up a timer in self that eventually calls the paint notification: | ||||||||||
| 740 | sub on_replot { | ||||||||||
| 741 | my ($self) = @_; | ||||||||||
| 742 | return if $self->{timer}->get_active; | ||||||||||
| 743 | $self->{timer}->start; | ||||||||||
| 744 | } | ||||||||||
| 745 | |||||||||||
| 746 | # for now, this is a replica of the above: | ||||||||||
| 747 | *on_changedata = \&on_changetitle; | ||||||||||
| 748 | |||||||||||
| 749 | ################# | ||||||||||
| 750 | # Notifications # | ||||||||||
| 751 | ################# | ||||||||||
| 752 | # Add a new notification_type for each of the notifications just defined. | ||||||||||
| 753 | { | ||||||||||
| 754 | # Keep the notifications hash in its own lexically scoped block so that | ||||||||||
| 755 | # other's can't mess with it. | ||||||||||
| 756 | my %notifications = ( | ||||||||||
| 757 | %{Prima::Widget-> notification_types()}, | ||||||||||
| 758 | # working here - choose a better signal type? | ||||||||||
| 759 | 'Replot' => nt::Default, | ||||||||||
| 760 | map { ("Change$_" => nt::Default) } qw(Title Data), | ||||||||||
| 761 | ); | ||||||||||
| 762 | |||||||||||
| 763 | sub notification_types { return \%notifications } | ||||||||||
| 764 | } | ||||||||||
| 765 | |||||||||||
| 766 | sub on_paint { | ||||||||||
| 767 | my ($self, $canvas) = @_; | ||||||||||
| 768 | |||||||||||
| 769 | # We need to handle the case of this canvas not being this widget. | ||||||||||
| 770 | if (defined $canvas and $canvas != $self) { | ||||||||||
| 771 | my $setup_paint = ($canvas->get_paint_state != ps::Enabled); | ||||||||||
| 772 | $canvas->begin_paint if $setup_paint; | ||||||||||
| 773 | $canvas->clear; | ||||||||||
| 774 | $self->draw_plot($canvas); | ||||||||||
| 775 | $canvas->end_paint if $setup_paint; | ||||||||||
| 776 | return; | ||||||||||
| 777 | } | ||||||||||
| 778 | |||||||||||
| 779 | # If the paint state is not enabled, issue a repaint, which will ultimately | ||||||||||
| 780 | # re-invoke this method, but in a paint-enabled state. This achieves the | ||||||||||
| 781 | # same purpose as something like this: | ||||||||||
| 782 | # $self->begin_paint; | ||||||||||
| 783 | # $self->on_paint; | ||||||||||
| 784 | # $self->end_paint | ||||||||||
| 785 | # but it does not flicker the way that the above code does. | ||||||||||
| 786 | return $self->repaint if $self->get_paint_state != 1; | ||||||||||
| 787 | |||||||||||
| 788 | # Otherwise, clear the canvas and invoke our plot drawing routine on ourself | ||||||||||
| 789 | $self->clear; | ||||||||||
| 790 | $self->draw_plot($self); | ||||||||||
| 791 | } | ||||||||||
| 792 | |||||||||||
| 793 | # This is the actual functionality for drawing on the canvas. This was once part | ||||||||||
| 794 | # of on_paint, but was pulled out so that it can be overridden by subclasses | ||||||||||
| 795 | # without having to deal with the vagaries of getting the paint state right. | ||||||||||
| 796 | # Again, this is *meant* to be overridden. :-) | ||||||||||
| 797 | sub draw_plot { | ||||||||||
| 798 | my ($self, $canvas) = @_; | ||||||||||
| 799 | |||||||||||
| 800 | # Get the clipping rectangle for the actual drawing space: | ||||||||||
| 801 | my ($clip_left, $clip_bottom, $right_edge, $top_edge) | ||||||||||
| 802 | = $self->get_edge_requirements; | ||||||||||
| 803 | |||||||||||
| 804 | my $ratio = $canvas->height / $self->height; | ||||||||||
| 805 | |||||||||||
| 806 | # The right and top edge values should be subtracted from the width and | ||||||||||
| 807 | # height, respectively: | ||||||||||
| 808 | my $clip_right = $canvas->width - $ratio * $right_edge; | ||||||||||
| 809 | my $clip_top = $canvas->height - $ratio * $top_edge; | ||||||||||
| 810 | |||||||||||
| 811 | # Correct the left and bottom clipping for the canvas ratio | ||||||||||
| 812 | $clip_left *= $ratio; | ||||||||||
| 813 | $clip_bottom *= $ratio; | ||||||||||
| 814 | |||||||||||
| 815 | # Clip the widget before we begin drawing | ||||||||||
| 816 | $canvas->clipRect($clip_left, $clip_bottom, $clip_right, $clip_top); | ||||||||||
| 817 | |||||||||||
| 818 | # Draw the data, sorted by key name: | ||||||||||
| 819 | foreach my $key (sort keys %{$self->{dataSets}}) { | ||||||||||
| 820 | next if $key eq 'widget'; | ||||||||||
| 821 | $self->{dataSets}->{$key}->draw($canvas, $ratio); | ||||||||||
| 822 | } | ||||||||||
| 823 | |||||||||||
| 824 | # Draw the zoom-rectangle, if there is one | ||||||||||
| 825 | if (exists $self->{mouse_down_rel}->{mb::Right}) { | ||||||||||
| 826 | my ($x, $y) = $self->pointerPos; | ||||||||||
| 827 | my ($x_start_rel, $y_start_rel) = @{$self->{mouse_down_rel}->{mb::Right}}; | ||||||||||
| 828 | my $x_start_pixel = $self->x->relatives_to_pixels($x_start_rel, $ratio); | ||||||||||
| 829 | my $y_start_pixel = $self->y->relatives_to_pixels($y_start_rel, $ratio); | ||||||||||
| 830 | $canvas->rectangle($x_start_pixel, $y_start_pixel, $x, $y); | ||||||||||
| 831 | } | ||||||||||
| 832 | |||||||||||
| 833 | # Draw the axes | ||||||||||
| 834 | $canvas->clipRect(0, 0, $self->size); | ||||||||||
| 835 | $self->x->draw($canvas, $clip_left, $clip_bottom, $clip_right, $clip_top, $ratio); | ||||||||||
| 836 | $self->y->draw($canvas, $clip_left, $clip_bottom, $clip_right, $clip_top, $ratio); | ||||||||||
| 837 | |||||||||||
| 838 | # Draw the title: | ||||||||||
| 839 | if (defined $self->{title} and $self->{title} ne '') { | ||||||||||
| 840 | my $backup_font = $canvas->font; | ||||||||||
| 841 | my ($width, $height) = $canvas->size; | ||||||||||
| 842 | # Compute the titleSpace before changing anything | ||||||||||
| 843 | my $titleSpace = $self->titleSpace; | ||||||||||
| 844 | # Set up the title font | ||||||||||
| 845 | $self->_set_title_font($canvas); | ||||||||||
| 846 | |||||||||||
| 847 | # Draw the title: | ||||||||||
| 848 | $canvas->draw_text($self->{title}, 0, $height - $titleSpace * $ratio | ||||||||||
| 849 | , $width, $height | ||||||||||
| 850 | , dt::Center | dt::VCenter | dt::NewLineBreak | dt::NoWordWrap | ||||||||||
| 851 | | dt::UseExternalLeading); | ||||||||||
| 852 | |||||||||||
| 853 | # Reset the font characteristics: | ||||||||||
| 854 | $canvas->font($backup_font); | ||||||||||
| 855 | } | ||||||||||
| 856 | } | ||||||||||
| 857 | |||||||||||
| 858 | # For mousewheel events, we zoom in or out. However, if they're over the axes, | ||||||||||
| 859 | # only zoom in or out for that axis. | ||||||||||
| 860 | sub on_mousewheel { | ||||||||||
| 861 | return unless $_[0]->enabled; | ||||||||||
| 862 | my ($self, $mods, $x, $y, $dir) = @_; | ||||||||||
| 863 | my ($width, $height) = $self->size; | ||||||||||
| 864 | |||||||||||
| 865 | # Get the relative x and y positions. When the mouse is over the plot itself | ||||||||||
| 866 | # this is a number between 0 and 1 for both x and y. When the house is, for | ||||||||||
| 867 | # example, over the x-axis tick labels, the y value will be less than 0. | ||||||||||
| 868 | my $rel_x = $self->x->pixels_to_relatives($x); | ||||||||||
| 869 | my $rel_y = $self->y->pixels_to_relatives($y); | ||||||||||
| 870 | |||||||||||
| 871 | # if the mouse is over the data or the x-axis, zoom in the x-direction, | ||||||||||
| 872 | # preserving the position of the mouse's x-value: | ||||||||||
| 873 | if ($rel_x > 0 and $rel_x < 1) { | ||||||||||
| 874 | # Dir > 0 means zooming in: | ||||||||||
| 875 | my ($rel_min, $rel_max) = (0, 1); | ||||||||||
| 876 | if ($dir > 0) { | ||||||||||
| 877 | $rel_min += $rel_x/5; | ||||||||||
| 878 | $rel_max -= (1 - $rel_x) / 5; | ||||||||||
| 879 | } | ||||||||||
| 880 | else { | ||||||||||
| 881 | $rel_min -= $rel_x/5; | ||||||||||
| 882 | $rel_max += (1 - $rel_x) / 5; | ||||||||||
| 883 | } | ||||||||||
| 884 | |||||||||||
| 885 | # Compute the new min/max values from the axis scaling: | ||||||||||
| 886 | my $real_min = $self->x->relatives_to_reals($rel_min); | ||||||||||
| 887 | my $real_max = $self->x->relatives_to_reals($rel_max); | ||||||||||
| 888 | $self->x->minmax($real_min, $real_max); | ||||||||||
| 889 | } | ||||||||||
| 890 | # if the mouse is over the data or the y-axis, zoom in the y-direction: | ||||||||||
| 891 | if ($rel_y > 0 and $rel_y < 1) { | ||||||||||
| 892 | # Dir > 0 means zooming in: | ||||||||||
| 893 | my ($rel_min, $rel_max) = (0, 1); | ||||||||||
| 894 | if ($dir > 0) { | ||||||||||
| 895 | $rel_min += $rel_y/5; | ||||||||||
| 896 | $rel_max -= (1 - $rel_y) / 5; | ||||||||||
| 897 | } | ||||||||||
| 898 | else { | ||||||||||
| 899 | $rel_min -= $rel_y/5; | ||||||||||
| 900 | $rel_max += (1 - $rel_y) / 5; | ||||||||||
| 901 | } | ||||||||||
| 902 | |||||||||||
| 903 | # Compute the new min/max values from the axis scaling: | ||||||||||
| 904 | my $real_min = $self->y->relatives_to_reals($rel_min); | ||||||||||
| 905 | my $real_max = $self->y->relatives_to_reals($rel_max); | ||||||||||
| 906 | $self->y->minmax($real_min, $real_max); | ||||||||||
| 907 | } | ||||||||||
| 908 | |||||||||||
| 909 | $self->notify('Replot'); | ||||||||||
| 910 | } | ||||||||||
| 911 | |||||||||||
| 912 | sub get_min_max_for { | ||||||||||
| 913 | my ($first, $second) = @_; | ||||||||||
| 914 | return ($first, $second) if $first < $second; | ||||||||||
| 915 | return ($second, $first); | ||||||||||
| 916 | } | ||||||||||
| 917 | |||||||||||
| 918 | sub on_mousedown { | ||||||||||
| 919 | return unless $_[0]->enabled; | ||||||||||
| 920 | my ($self, $down_button, undef, $x, $y) = @_; | ||||||||||
| 921 | # Store the relative click locations: | ||||||||||
| 922 | $x = $self->x->pixels_to_relatives($x); | ||||||||||
| 923 | $y = $self->y->pixels_to_relatives($y); | ||||||||||
| 924 | foreach (mb::Left, mb::Right, mb::Middle) { | ||||||||||
| 925 | $self->{mouse_down_rel}->{$_} = [$x, $y] if $down_button & $_; | ||||||||||
| 926 | } | ||||||||||
| 927 | $self->{mouse_move_rel} = [$x, $y]; | ||||||||||
| 928 | } | ||||||||||
| 929 | |||||||||||
| 930 | sub on_mousemove { | ||||||||||
| 931 | return unless $_[0]->enabled; | ||||||||||
| 932 | my ($self, $drag_button, $x_stop_pixel, $y_stop_pixel) = @_; | ||||||||||
| 933 | |||||||||||
| 934 | # Compute the relative and real final mouse locations | ||||||||||
| 935 | my $x_stop_rel = $self->x->pixels_to_relatives($x_stop_pixel); | ||||||||||
| 936 | my $y_stop_rel = $self->y->pixels_to_relatives($y_stop_pixel); | ||||||||||
| 937 | |||||||||||
| 938 | # On windows, the drag button is not properly reported. However, it is | ||||||||||
| 939 | # there is never any issue with failure to report a mouse-down event (like | ||||||||||
| 940 | # we get with X systems), so if I got here, it is easy enough to adapt. | ||||||||||
| 941 | if ($^O =~ /MS/) { | ||||||||||
| 942 | $drag_button = mb::Left if defined $self->{mouse_down_rel}->{mb::Left}; | ||||||||||
| 943 | $drag_button = mb::Right if defined $self->{mouse_down_rel}->{mb::Right}; | ||||||||||
| 944 | } | ||||||||||
| 945 | |||||||||||
| 946 | if ($drag_button & mb::Left) { | ||||||||||
| 947 | # A left mouse drag actually moves the graph around. Determine the | ||||||||||
| 948 | # change in relative values, then change the min/max accordingly. | ||||||||||
| 949 | |||||||||||
| 950 | # It sometimes happens that the mouse move event gets triggered | ||||||||||
| 951 | # without a corresponding mouse down event, such as when I click | ||||||||||
| 952 | # on the window from *another* application and move my mouse around. | ||||||||||
| 953 | # If the mouse down coordinates are not known, store the current | ||||||||||
| 954 | # ones and simply return. | ||||||||||
| 955 | if (not defined $self->{mouse_down_rel}->{mb::Left}) { | ||||||||||
| 956 | $self->{mouse_down_rel}->{mb::Left} = [$x_stop_rel, $y_stop_rel]; | ||||||||||
| 957 | return 1; | ||||||||||
| 958 | } | ||||||||||
| 959 | |||||||||||
| 960 | # working here - per-button mouse click tracking? I ask because sometimes | ||||||||||
| 961 | # I accidentially click the left mouse button when I'm selecting a zoom | ||||||||||
| 962 | # rectangle and it messed things up. | ||||||||||
| 963 | |||||||||||
| 964 | # The behavior depends upon where they initially clicked and where the | ||||||||||
| 965 | # mouse was last, so retrieve both of those values: | ||||||||||
| 966 | my ($x_down_rel, $y_down_rel) = @{$self->{mouse_down_rel}->{mb::Left}}; | ||||||||||
| 967 | my ($x_start_rel, $y_start_rel) = @{$self->{mouse_move_rel}}; | ||||||||||
| 968 | |||||||||||
| 969 | # If the initial click was within the x-boundaries, then the x-values | ||||||||||
| 970 | # should be adjusted: | ||||||||||
| 971 | if ($x_down_rel > 0 and $x_down_rel < 1) { | ||||||||||
| 972 | # Determine the relative change and the adjusted min/max: | ||||||||||
| 973 | my $dx = $x_stop_rel - $x_start_rel; | ||||||||||
| 974 | my $new_min = $self->x->relatives_to_reals(-$dx); | ||||||||||
| 975 | my $new_max = $self->x->relatives_to_reals(1 - $dx); | ||||||||||
| 976 | |||||||||||
| 977 | # Call the non-notifying version. The notifying version causes an | ||||||||||
| 978 | # immediate redraw that causes the plot to accelerate away in | ||||||||||
| 979 | # perldl. | ||||||||||
| 980 | $self->x->_min($new_min); | ||||||||||
| 981 | $self->x->_max($new_max); | ||||||||||
| 982 | } | ||||||||||
| 983 | # If the initial click was within the y-boundaries, then the y-values | ||||||||||
| 984 | # should be adjusted: | ||||||||||
| 985 | if ($y_down_rel > 0 and $y_down_rel < 1) { | ||||||||||
| 986 | # Determine the relative change and the adjusted min/max: | ||||||||||
| 987 | my $dy = $y_stop_rel - $y_start_rel; | ||||||||||
| 988 | my $new_min = $self->y->relatives_to_reals(-$dy); | ||||||||||
| 989 | my $new_max = $self->y->relatives_to_reals(1 - $dy); | ||||||||||
| 990 | |||||||||||
| 991 | # Call the non-notifying version. The notifying version causes an | ||||||||||
| 992 | # immediate redraw that causes the plot to accelerate away in | ||||||||||
| 993 | # perldl. | ||||||||||
| 994 | $self->y->_min($new_min); | ||||||||||
| 995 | $self->y->_max($new_max); | ||||||||||
| 996 | } | ||||||||||
| 997 | } | ||||||||||
| 998 | |||||||||||
| 999 | # Repaint if they're dragging the mouse | ||||||||||
| 1000 | $self->notify('Replot') if $drag_button; | ||||||||||
| 1001 | |||||||||||
| 1002 | # Store the intermediate locations: | ||||||||||
| 1003 | $self->{mouse_move_rel} = [$x_stop_rel, $y_stop_rel]; | ||||||||||
| 1004 | } | ||||||||||
| 1005 | |||||||||||
| 1006 | sub on_mouseup { | ||||||||||
| 1007 | return unless $_[0]->enabled; | ||||||||||
| 1008 | my ($self, $up_button, $up_mods, $x_stop_pixel, $y_stop_pixel) = @_; | ||||||||||
| 1009 | |||||||||||
| 1010 | # Remove the previous button record for left and middle buttons: | ||||||||||
| 1011 | if ($up_button & mb::Left) { | ||||||||||
| 1012 | delete $self->{mouse_down_rel}->{mb::Left}; | ||||||||||
| 1013 | } | ||||||||||
| 1014 | elsif ($up_button & mb::Middle) { | ||||||||||
| 1015 | delete $self->{mouse_down_rel}->{mb::Middle}; | ||||||||||
| 1016 | } | ||||||||||
| 1017 | elsif ($up_button & mb::Right and defined $self->{mouse_down_rel}->{mb::Right}) { | ||||||||||
| 1018 | # Zoom in to the requested rectangle: | ||||||||||
| 1019 | my ($x_start_rel, $y_start_rel) = @{$self->{mouse_down_rel}->{mb::Right}}; | ||||||||||
| 1020 | my $x_stop_rel = $self->x->pixels_to_relatives($x_stop_pixel); | ||||||||||
| 1021 | my $y_stop_rel = $self->y->pixels_to_relatives($y_stop_pixel); | ||||||||||
| 1022 | |||||||||||
| 1023 | # Only rescale if there is a legitimate x- and y- box: | ||||||||||
| 1024 | if ($x_stop_rel != $x_start_rel and $y_stop_rel != $y_start_rel) { | ||||||||||
| 1025 | # Reset the x min/max | ||||||||||
| 1026 | my ($min_rel, $max_rel) = get_min_max_for($x_start_rel, $x_stop_rel); | ||||||||||
| 1027 | # Compute the new min/max values from the axis scaling: | ||||||||||
| 1028 | my $min_real = $self->x->relatives_to_reals($min_rel); | ||||||||||
| 1029 | my $max_real = $self->x->relatives_to_reals($max_rel); | ||||||||||
| 1030 | # Set the new min/max values: | ||||||||||
| 1031 | $self->x->minmax($min_real, $max_real); | ||||||||||
| 1032 | |||||||||||
| 1033 | # Reset the y min/max | ||||||||||
| 1034 | ($min_rel, $max_rel) = get_min_max_for($y_start_rel, $y_stop_rel); | ||||||||||
| 1035 | # Compute the new min/max values from the axis scaling: | ||||||||||
| 1036 | $min_real = $self->y->relatives_to_reals($min_rel); | ||||||||||
| 1037 | $max_real = $self->y->relatives_to_reals($max_rel); | ||||||||||
| 1038 | # Set the new min/max values: | ||||||||||
| 1039 | $self->y->minmax($min_real, $max_real); | ||||||||||
| 1040 | } | ||||||||||
| 1041 | # Call the popup menu if it 'looks' like a right-click: | ||||||||||
| 1042 | elsif ($x_stop_rel == $x_start_rel and $y_stop_rel == $y_start_rel) { | ||||||||||
| 1043 | $self->popup(Prima::Popup->new( | ||||||||||
| 1044 | items => [ | ||||||||||
| 1045 | ['~Copy' => sub { | ||||||||||
| 1046 | Prima::Timer->create( | ||||||||||
| 1047 | timeout => 250, | ||||||||||
| 1048 | onTick => sub { | ||||||||||
| 1049 | $_[0]->stop; | ||||||||||
| 1050 | $self->copy_to_clipboard; | ||||||||||
| 1051 | }, | ||||||||||
| 1052 | )->start; | ||||||||||
| 1053 | }], | ||||||||||
| 1054 | ['Save As ~Postscript...' => sub { | ||||||||||
| 1055 | $self->save_to_postscript; | ||||||||||
| 1056 | }], | ||||||||||
| 1057 | ['~Save As...' => sub { | ||||||||||
| 1058 | Prima::Timer->create( | ||||||||||
| 1059 | timeout => 250, | ||||||||||
| 1060 | onTick => sub { | ||||||||||
| 1061 | $_[0]->stop; | ||||||||||
| 1062 | $self->save_to_file; | ||||||||||
| 1063 | }, | ||||||||||
| 1064 | )->start; | ||||||||||
| 1065 | }], | ||||||||||
| 1066 | ['~Autoscale' => sub { | ||||||||||
| 1067 | $self->x->minmax(lm::Auto, lm::Auto); | ||||||||||
| 1068 | $self->y->minmax(lm::Auto, lm::Auto); | ||||||||||
| 1069 | }], | ||||||||||
| 1070 | ['~Properties' => sub { | ||||||||||
| 1071 | $self->set_properties_dialog; | ||||||||||
| 1072 | }], | ||||||||||
| 1073 | ], | ||||||||||
| 1074 | )); | ||||||||||
| 1075 | } | ||||||||||
| 1076 | # Remove the previous button record, so a zoom rectangle is not drawn: | ||||||||||
| 1077 | delete $self->{mouse_down_rel}->{mb::Right}; | ||||||||||
| 1078 | } | ||||||||||
| 1079 | } | ||||||||||
| 1080 | |||||||||||
| 1081 | use Scalar::Util qw(looks_like_number); | ||||||||||
| 1082 | |||||||||||
| 1083 | sub insert_minmax_input { | ||||||||||
| 1084 | my ($group_box, $method, $axis, $y_pos) = @_; | ||||||||||
| 1085 | $group_box->insert(Label => | ||||||||||
| 1086 | place => { x => 45, y => $y_pos, height => 25, width => 60, anchor => 'sw' }, | ||||||||||
| 1087 | height => 30, | ||||||||||
| 1088 | text => ucfirst($method) . ':', | ||||||||||
| 1089 | ); | ||||||||||
| 1090 | # the widgets we are about to add | ||||||||||
| 1091 | my ($auto_button, $inline); | ||||||||||
| 1092 | # lexical state variable to bypass updates if the input line triggered | ||||||||||
| 1093 | # the update | ||||||||||
| 1094 | my $update_inline = 1; | ||||||||||
| 1095 | # initial value and autoscaling state of the axis | ||||||||||
| 1096 | my ($init_val, $is_auto) = $axis->$method; | ||||||||||
| 1097 | |||||||||||
| 1098 | # Attach an event listener to the axis min/max methods to keep is_auto | ||||||||||
| 1099 | # up-to-date, and ensure that the input line is accurate | ||||||||||
| 1100 | my $notification_idx = $axis->add_notification(ChangeBounds => sub { | ||||||||||
| 1101 | # get the new min or max | ||||||||||
| 1102 | (my $curr_val, $is_auto) = $axis->$method; | ||||||||||
| 1103 | |||||||||||
| 1104 | $inline->text($curr_val . ($is_auto ? ' (Auto)' : '')) | ||||||||||
| 1105 | if $update_inline; | ||||||||||
| 1106 | $auto_button->enabled(!$is_auto); | ||||||||||
| 1107 | }); | ||||||||||
| 1108 | |||||||||||
| 1109 | no PDL::NiceSlice; | ||||||||||
| 1110 | my $val_is_good = $method eq 'min' ? sub { $_[0] < $axis->max } | ||||||||||
| 1111 | : sub { $_[0] > $axis->min }; | ||||||||||
| 1112 | $inline = $group_box->insert(InputLine => | ||||||||||
| 1113 | place => { x => 110, y => $y_pos, height => 30, width => 280, anchor => 'sw' }, | ||||||||||
| 1114 | height => 30, | ||||||||||
| 1115 | text => ($is_auto ? "$init_val (Auto)" : $init_val), | ||||||||||
| 1116 | onEnter => sub { | ||||||||||
| 1117 | if ($is_auto) { | ||||||||||
| 1118 | $_[0]->text(scalar($axis->$method)); | ||||||||||
| 1119 | } | ||||||||||
| 1120 | }, | ||||||||||
| 1121 | onLeave => sub { | ||||||||||
| 1122 | if ($is_auto) { | ||||||||||
| 1123 | my $value = $axis->$method; | ||||||||||
| 1124 | $_[0]->text("$value (Auto)"); | ||||||||||
| 1125 | } | ||||||||||
| 1126 | }, | ||||||||||
| 1127 | onKeyDown => sub { | ||||||||||
| 1128 | my ($self, $code, $key) = @_; | ||||||||||
| 1129 | # Only check typed codes, in which case code >= 32 | ||||||||||
| 1130 | return if $code < 32; | ||||||||||
| 1131 | # Screen what they typed for being a correct numerical entry | ||||||||||
| 1132 | $self->clear_event() unless chr($code) =~ /[\d.+\-e]/i; | ||||||||||
| 1133 | }, | ||||||||||
| 1134 | onKeyUp => sub { | ||||||||||
| 1135 | my ($self, $code, $key) = @_; | ||||||||||
| 1136 | return if $code < 32 && $key != kb::Backspace && $key != kb::Delete; | ||||||||||
| 1137 | my $new_val = $self->text; | ||||||||||
| 1138 | if (looks_like_number($new_val) and $val_is_good->($new_val) | ||||||||||
| 1139 | and $axis->scaling->is_valid_extremum($new_val) | ||||||||||
| 1140 | ) { | ||||||||||
| 1141 | # Change the actual axis value | ||||||||||
| 1142 | $update_inline = 0; | ||||||||||
| 1143 | $axis->$method($new_val) if defined $new_val; | ||||||||||
| 1144 | $update_inline = 1; | ||||||||||
| 1145 | |||||||||||
| 1146 | # Update the color to notify a good entry value | ||||||||||
| 1147 | $self->backColor(cl::White); | ||||||||||
| 1148 | } | ||||||||||
| 1149 | else { | ||||||||||
| 1150 | $self->backColor(0xffdcdc); | ||||||||||
| 1151 | undef($new_val); | ||||||||||
| 1152 | } | ||||||||||
| 1153 | |||||||||||
| 1154 | }, | ||||||||||
| 1155 | ); | ||||||||||
| 1156 | use PDL::NiceSlice; | ||||||||||
| 1157 | |||||||||||
| 1158 | $auto_button = $group_box->insert(Button => | ||||||||||
| 1159 | text => 'Autoscale', | ||||||||||
| 1160 | place => { x => 395, y => $y_pos, height => 30, width => 100, anchor => 'sw' }, | ||||||||||
| 1161 | height => 30, | ||||||||||
| 1162 | onClick => sub { $axis->$method(lm::Auto) }, | ||||||||||
| 1163 | ); | ||||||||||
| 1164 | $auto_button->enabled(!$is_auto); | ||||||||||
| 1165 | |||||||||||
| 1166 | return $notification_idx; | ||||||||||
| 1167 | } | ||||||||||
| 1168 | |||||||||||
| 1169 | sub insert_label_input { | ||||||||||
| 1170 | my ($group_box, $axis) = @_; | ||||||||||
| 1171 | $group_box->insert(Label => | ||||||||||
| 1172 | place => { x => 45, y => 40, height => 25, width => 60, anchor => 'sw' }, | ||||||||||
| 1173 | height => 30, | ||||||||||
| 1174 | text => 'Label:', | ||||||||||
| 1175 | ); | ||||||||||
| 1176 | my $label_text = $axis->label || ''; | ||||||||||
| 1177 | $group_box->insert(InputLine => | ||||||||||
| 1178 | text => $label_text, | ||||||||||
| 1179 | place => { x => 110, y => 40, height => 30, width => 380, anchor => 'sw' }, | ||||||||||
| 1180 | height => 30, | ||||||||||
| 1181 | onKeyUp => sub { | ||||||||||
| 1182 | my $new_label = shift->text; | ||||||||||
| 1183 | if ($new_label ne $label_text) { | ||||||||||
| 1184 | $label_text = $new_label; | ||||||||||
| 1185 | $axis->label($new_label); | ||||||||||
| 1186 | } | ||||||||||
| 1187 | }, | ||||||||||
| 1188 | ); | ||||||||||
| 1189 | } | ||||||||||
| 1190 | |||||||||||
| 1191 | sub insert_scaling_radios { | ||||||||||
| 1192 | my ($group_box, $axis) = @_; | ||||||||||
| 1193 | my $update_radios = 1; | ||||||||||
| 1194 | my ($init_min, $init_max) = $axis->minmax; | ||||||||||
| 1195 | my $linear_radio = $group_box->insert(Radio => | ||||||||||
| 1196 | place => { x => 80, y => 5, height => 30, width => 30, anchor => 'sw' }, | ||||||||||
| 1197 | height => 30, | ||||||||||
| 1198 | onCheck => sub { | ||||||||||
| 1199 | return unless $update_radios; | ||||||||||
| 1200 | $update_radios = 0; | ||||||||||
| 1201 | $axis->scaling(sc::Linear); | ||||||||||
| 1202 | $update_radios = 1; | ||||||||||
| 1203 | }, | ||||||||||
| 1204 | text => 'Linear Scaling', | ||||||||||
| 1205 | checked => $axis->scaling eq sc::Linear ? 1 : 0, | ||||||||||
| 1206 | ); | ||||||||||
| 1207 | |||||||||||
| 1208 | my $log_radio = $group_box->insert(Radio => | ||||||||||
| 1209 | place => { x => 275, y => 5, height => 30, width => 30, anchor => 'sw' }, | ||||||||||
| 1210 | height => 30, | ||||||||||
| 1211 | onCheck => sub { | ||||||||||
| 1212 | return unless $update_radios; | ||||||||||
| 1213 | $update_radios = 0; | ||||||||||
| 1214 | $axis->scaling(sc::Log); | ||||||||||
| 1215 | $update_radios = 1; | ||||||||||
| 1216 | }, | ||||||||||
| 1217 | text => 'Logarithmic Scaling', | ||||||||||
| 1218 | checked => $axis->scaling eq sc::Log ? 1 : 0, | ||||||||||
| 1219 | ); | ||||||||||
| 1220 | $log_radio->enabled(0) unless $init_max > 0 && $init_min > 0; | ||||||||||
| 1221 | |||||||||||
| 1222 | my $bounds_notification = $axis->add_notification(ChangeBounds => sub { | ||||||||||
| 1223 | # Can't go negative if log scaling is enabled, so negative means | ||||||||||
| 1224 | # we must have linear scaling. As such, only enable/disable the | ||||||||||
| 1225 | # log radio based on negative signs, don't change the radios | ||||||||||
| 1226 | my ($min, $max) = $axis->minmax; | ||||||||||
| 1227 | if ($min <= 0 or $max <= 0) { | ||||||||||
| 1228 | $update_radios = 0; | ||||||||||
| 1229 | $log_radio->enabled(0); | ||||||||||
| 1230 | $update_radios = 1; | ||||||||||
| 1231 | } | ||||||||||
| 1232 | else { | ||||||||||
| 1233 | $update_radios = 0; | ||||||||||
| 1234 | $log_radio->enabled(1); | ||||||||||
| 1235 | $update_radios = 1; | ||||||||||
| 1236 | } | ||||||||||
| 1237 | }); | ||||||||||
| 1238 | my $scaling_notification = $axis->add_notification(ChangeScaling => sub { | ||||||||||
| 1239 | return unless $update_radios; | ||||||||||
| 1240 | if ($axis->scaling eq sc::Linear) { | ||||||||||
| 1241 | $update_radios = 0; | ||||||||||
| 1242 | $linear_radio->check; | ||||||||||
| 1243 | $update_radios = 1; | ||||||||||
| 1244 | } | ||||||||||
| 1245 | else { | ||||||||||
| 1246 | $update_radios = 0; | ||||||||||
| 1247 | $log_radio->check; | ||||||||||
| 1248 | $update_radios = 1; | ||||||||||
| 1249 | } | ||||||||||
| 1250 | }); | ||||||||||
| 1251 | |||||||||||
| 1252 | return ($bounds_notification, $scaling_notification); | ||||||||||
| 1253 | } | ||||||||||
| 1254 | |||||||||||
| 1255 | # Builds a modal window to set plotting properties | ||||||||||
| 1256 | sub set_properties_dialog { | ||||||||||
| 1257 | my $self = shift; | ||||||||||
| 1258 | |||||||||||
| 1259 | # If one already exists, bring it back to the front | ||||||||||
| 1260 | if (exists $self->{prop_window}) { | ||||||||||
| 1261 | $self->{prop_window}->select; | ||||||||||
| 1262 | $self->{prop_window}->bring_to_front; | ||||||||||
| 1263 | return; | ||||||||||
| 1264 | } | ||||||||||
| 1265 | |||||||||||
| 1266 | my $total_height = 0; | ||||||||||
| 1267 | $self->{prop_window} = my $prop_win = Prima::Window->new( | ||||||||||
| 1268 | text => 'Plot Properties', width => 500, height => 380, | ||||||||||
| 1269 | visible => 0, | ||||||||||
| 1270 | ); | ||||||||||
| 1271 | $prop_win->insert(Widget => | ||||||||||
| 1272 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1273 | height => 10, | ||||||||||
| 1274 | ); | ||||||||||
| 1275 | |||||||||||
| 1276 | # Title input | ||||||||||
| 1277 | my $title_box = $prop_win->insert(GroupBox => | ||||||||||
| 1278 | pack => { side => 'top', fill => 'x', padx => 10 }, | ||||||||||
| 1279 | height => 50, | ||||||||||
| 1280 | text => 'Title', | ||||||||||
| 1281 | ); | ||||||||||
| 1282 | my $title_text = $self->title || ''; | ||||||||||
| 1283 | $title_box->insert(InputLine => | ||||||||||
| 1284 | text => $title_text, | ||||||||||
| 1285 | place => { | ||||||||||
| 1286 | x => 5, y => 5, | ||||||||||
| 1287 | relwidth => 1, | ||||||||||
| 1288 | width => -10, | ||||||||||
| 1289 | anchor => 'sw', | ||||||||||
| 1290 | }, | ||||||||||
| 1291 | onKeyUp => sub { | ||||||||||
| 1292 | my $new_title = shift->text; | ||||||||||
| 1293 | if ($new_title ne $title_text) { | ||||||||||
| 1294 | $title_text = $new_title; | ||||||||||
| 1295 | $self->title($new_title); | ||||||||||
| 1296 | } | ||||||||||
| 1297 | }, | ||||||||||
| 1298 | ); | ||||||||||
| 1299 | |||||||||||
| 1300 | my (@x_notifications, @y_notifications); | ||||||||||
| 1301 | |||||||||||
| 1302 | # x axis input | ||||||||||
| 1303 | $prop_win->insert(Widget => | ||||||||||
| 1304 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1305 | height => 10, | ||||||||||
| 1306 | ); | ||||||||||
| 1307 | my $x_box = $prop_win->insert(GroupBox => | ||||||||||
| 1308 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1309 | height => 160, | ||||||||||
| 1310 | text => 'X Axis', | ||||||||||
| 1311 | ); | ||||||||||
| 1312 | push @x_notifications, insert_minmax_input($x_box, 'min', $self->x, 110); | ||||||||||
| 1313 | push @x_notifications, insert_minmax_input($x_box, 'max', $self->x, 75); | ||||||||||
| 1314 | insert_label_input($x_box, $self->x); | ||||||||||
| 1315 | push @x_notifications, insert_scaling_radios($x_box, $self->x); | ||||||||||
| 1316 | |||||||||||
| 1317 | # y axis input | ||||||||||
| 1318 | $prop_win->insert(Widget => | ||||||||||
| 1319 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1320 | height => 10, | ||||||||||
| 1321 | ); | ||||||||||
| 1322 | my $y_box = $prop_win->insert(GroupBox => | ||||||||||
| 1323 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1324 | height => 160, | ||||||||||
| 1325 | text => 'Y Axis', | ||||||||||
| 1326 | ); | ||||||||||
| 1327 | push @y_notifications, insert_minmax_input($y_box, 'min', $self->y, 110); | ||||||||||
| 1328 | push @y_notifications, insert_minmax_input($y_box, 'max', $self->y, 75); | ||||||||||
| 1329 | insert_label_input($y_box, $self->y); | ||||||||||
| 1330 | push @y_notifications, insert_scaling_radios($y_box, $self->y); | ||||||||||
| 1331 | |||||||||||
| 1332 | # Close button | ||||||||||
| 1333 | $prop_win->insert(Widget => | ||||||||||
| 1334 | pack => { side => 'top', fill => 'x' }, | ||||||||||
| 1335 | height => 10, | ||||||||||
| 1336 | ); | ||||||||||
| 1337 | my $close_button = $prop_win->insert(Button => | ||||||||||
| 1338 | text => 'Close', | ||||||||||
| 1339 | onClick => sub { $prop_win->close }, | ||||||||||
| 1340 | pack => { side => 'right' } | ||||||||||
| 1341 | ); | ||||||||||
| 1342 | |||||||||||
| 1343 | $prop_win->height(10 + 50 + 10 + 160 + 10 + 160 + 10 + 30); | ||||||||||
| 1344 | |||||||||||
| 1345 | $prop_win->onClose(sub { | ||||||||||
| 1346 | $self->x->remove_notification($_) foreach (@x_notifications); | ||||||||||
| 1347 | $self->y->remove_notification($_) foreach (@y_notifications); | ||||||||||
| 1348 | delete $self->{prop_window}; | ||||||||||
| 1349 | # Bring the figure back to the foreground | ||||||||||
| 1350 | $self->select; | ||||||||||
| 1351 | $self->bring_to_front; | ||||||||||
| 1352 | }); | ||||||||||
| 1353 | |||||||||||
| 1354 | # Having finished building it, show the window | ||||||||||
| 1355 | $prop_win->visible(1); | ||||||||||
| 1356 | } | ||||||||||
| 1357 | |||||||||||
| 1358 | 1; | ||||||||||
| 1359 | |||||||||||
| 1360 | __END__ | ||||||||||