File: Style.pm

package info (click to toggle)
libdemeter-perl 0.9.27%2Bds6-9
  • links: PTS, VCS
  • area: contrib
  • in suites: forky, sid, trixie
  • size: 74,028 kB
  • sloc: perl: 73,233; python: 2,196; makefile: 1,999; ansic: 1,368; lisp: 454; sh: 74
file content (209 lines) | stat: -rw-r--r-- 6,634 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package Demeter::UI::Athena::Plot::Style;

use strict;
use warnings;

use Wx qw( :everything );
use base 'Wx::Panel';
use Wx::Event qw(EVT_BUTTON EVT_LISTBOX EVT_LISTBOX_DCLICK);
use Wx::Perl::TextValidator;

use File::Basename;
use File::Copy;
use File::Spec;
use YAML::Tiny;

#use Demeter::UI::Wx::SpecialCharacters qw(:all);

sub new {
  my ($class, $parent, $app) = @_;
  my $this = $class->SUPER::new($parent, -1, wxDefaultPosition, wxDefaultSize, wxMAXIMIZE_BOX );

  my $box = Wx::BoxSizer->new( wxVERTICAL );

  $this->{make} = Wx::Button->new($this, -1, "Save current");
  $box->Add($this->{make}, 0, wxALL|wxGROW, 1);
  EVT_BUTTON($this, $this->{make}, sub{make_style(@_, $app)});

  $this->{list} = Wx::ListBox->new($this, -1, wxDefaultPosition, wxDefaultSize, [], wxLB_SINGLE|wxLB_NEEDED_SB);
  $box->Add($this->{list}, 1, wxALL|wxGROW, 1);
  EVT_LISTBOX($this, $this->{list}, sub{restore_style(@_, $app)});
  EVT_LISTBOX_DCLICK($this, $this->{list}, sub{discard_style(@_, $app)});

  $this->SetSizerAndFit($box);

  my $yaml = File::Spec->catfile(Demeter->dot_folder, 'athena.styles');
  if (not -e $yaml) {
    copy(File::Spec->catfile(dirname($INC{'Demeter.pm'}), 'Demeter', 'share', 'ini', 'athena.styles.ini'), $yaml);
  };
  my @list;
  eval {local $SIG{__DIE__} = sub {}; @list = YAML::Tiny::LoadFile($yaml)};
  foreach my $sty (@list) {
    my $style = Demeter::Plot::Style->new();
    foreach my $att (keys %$sty) {
      next if ($att eq 'group');
      $style->$att($sty->{$att});
    };
    $this->{list}->Append($style->name, $style);
  };
  return $this;
};

sub label {
  return 'Plotting styles';
};


sub make_style {
  my ($this, $event, $app) = @_;

  my $ted = Wx::TextEntryDialog->new($app->{main}, "Enter a name for this style", "Name this style", q{}, wxOK|wxCANCEL, Wx::GetMousePosition);
  $app->set_text_buffer($ted, "style");
  if ($ted->ShowModal == wxID_CANCEL) {
    $app->{main}->status("Making new style canceled.");
    $app->{style_pointer} = $#{$app->{style_buffer}}+1;
    return;
  };
  my $name = $ted->GetValue;
  $app->update_text_buffer("style", $name, 1);
  if ($name =~ m{\A\s*\z}) {
    $app->{main}->status("No name provided.  Making new style canceled.");
    $app->{style_pointer} = $#{$app->{style_buffer}}+1;
    return;
  };

  my $style = Demeter::Plot::Style->new(name=>$name);

  $style -> set(emin => $app->{main}->{PlotE}->{emin}->GetValue,
		emax => $app->{main}->{PlotE}->{emax}->GetValue,
	       );
  $style -> set(kmin => $app->{main}->{PlotK}->{kmin}->GetValue,
		kmax => $app->{main}->{PlotK}->{kmax}->GetValue,
	       );
  # my $val = ($app->{main}->{PlotR}->{mmag} -> GetValue) ? 'm'
  #         : ($app->{main}->{PlotR}->{mre}  -> GetValue) ? 'r'
  #         : ($app->{main}->{PlotR}->{mim}  -> GetValue) ? 'i'
  #         : ($app->{main}->{PlotR}->{mpha} -> GetValue) ? 'p'
  # 	  :                                              'm';
  $style -> set(rmin => $app->{main}->{PlotR}->{rmin}->GetValue,
		rmax => $app->{main}->{PlotR}->{rmax}->GetValue,
		##r_pl => $val,
	       );
  # $val = ($app->{main}->{PlotQ}->{mmag} -> GetValue) ? 'm'
  #      : ($app->{main}->{PlotQ}->{mre}  -> GetValue) ? 'r'
  #      : ($app->{main}->{PlotQ}->{mim}  -> GetValue) ? 'i'
  #      : ($app->{main}->{PlotQ}->{mpha} -> GetValue) ? 'p'
  #      :                                               'm';
  $style -> set(qmin => $app->{main}->{PlotQ}->{qmin}->GetValue,
		qmax => $app->{main}->{PlotQ}->{qmax}->GetValue,
		##q_pl => $val,
	       );

  $this->{list}->Append($style->name, $style);
  # local $|=1;
  # foreach my $i (0 .. $this->{list}->GetCount-1) {
  #   print $this->{list}->GetClientData($i)->serialization;
  # };
  $this->persist;
  $app->{main}->status("Saved plotting style ".$style->name);
};

sub restore_style {
  my ($this, $event, $app) = @_;
  my $style = $this->{list}->GetClientData(scalar $this->{list}->GetSelection);
  return if not defined $style;
  $app->{main}->{PlotE}->{emin}->SetValue($style->emin);
  $app->{main}->{PlotE}->{emax}->SetValue($style->emax);
  $app->{main}->{PlotK}->{kmin}->SetValue($style->kmin);
  $app->{main}->{PlotK}->{kmax}->SetValue($style->kmax);
  $app->{main}->{PlotR}->{rmin}->SetValue($style->rmin);
  $app->{main}->{PlotR}->{rmax}->SetValue($style->rmax);
  $app->{main}->{PlotQ}->{qmin}->SetValue($style->qmin);
  $app->{main}->{PlotQ}->{qmax}->SetValue($style->qmax);
  $app->{main}->status("Restored plotting style ".$style->name);
};

sub discard_style {
  my ($this, $event, $app) = @_;
  my $i = scalar $this->{list}->GetSelection;
  my $style = $this->{list}->GetClientData($i);
  my $yesno = Demeter::UI::Wx::VerbDialog->new($this, -1,
					       "Really discard the \"".$style->name."\" plotting style?",
					       "Really discard?",
					       "Discard");
  if ($yesno->ShowModal == wxID_NO) {
    $app->{main}->status("Not discarding ".$style->name);
    return;
  };
  my $n = $this->{list}->GetCount;
  # if ($n > 1) {
  #   if ($i = $n) {
  #     $this->{list}->SetSelection($i-1);
  #     $this->restore_style(q{}, $app);
  #   } else {
  #     $this->{list}->SetSelection($i+1);
  #     $this->restore_style(q{}, $app);
  #   };
  # };
  $this->{list}->Delete($i);
  $style->DEMOLISH;
  $this->persist;
  $app->{main}->status("Discarded plotting style ".$style->name);
  $event->Skip();
};

sub persist {
  my ($this) = @_;
  my $text = q{};
  foreach my $i (0 .. $this->{list}->GetCount-1) {
    $text .= $this->{list}->GetClientData($i)->serialization;
  };
  open(my $YAML, '>', File::Spec->catfile(Demeter->dot_folder, 'athena.styles'));
  print $YAML $text;
  close $YAML;
};

1;


=head1 NAME

Demeter::UI::Athena::Plot::Style - plotting style controls

=head1 VERSION

This documentation refers to Demeter version 0.9.26.

=head1 SYNOPSIS

This module provides controls for plotting styles in Athena

=head1 DEPENDENCIES

Demeter's dependencies are in the F<Build.PL> file.

=head1 BUGS AND LIMITATIONS

Please report problems to the Ifeffit Mailing List
(L<http://cars9.uchicago.edu/mailman/listinfo/ifeffit/>)

Patches are welcome.

=head1 AUTHOR

Bruce Ravel, L<http://bruceravel.github.io/home>

L<http://bruceravel.github.io/demeter/>

=head1 LICENCE AND COPYRIGHT

Copyright (c) 2006-2019 Bruce Ravel (L<http://bruceravel.github.io/home>). All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlgpl>.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

=cut