Debian Codegraph @

attic/scripts/Tk/Plotter.pm

raw
1 #!/usr/bin/perl -w
2 ######################################################################
3 ## Plotter notecard module for Atoms 3.0beta9 using Ifeffit
4 ## copyright (c) 1999 Bruce Ravel
5 ## [email protected]
6 ## http://feff.phys.washington.edu/~ravel/
7 ##
8 ## The latest version of Atoms can always be found at
9 ## http://feff.phys.washington.edu/~ravel/atoms/
10 ##
11 ## -------------------------------------------------------------------
12 ## All rights reserved. This program is free software; you can
13 ## redistribute it and/or modify it under the same terms as Perl
14 ## itself.
15 ##
16 ## This program is distributed in the hope that it will be useful,
17 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ## Artistic License for more details.
20 ## -------------------------------------------------------------------
21 ######################################################################
22
23 package Xray::Tk::Plotter;
24
25 use strict;
26 use vars qw($VERSION $cvs_info @ISA @EXPORT @EXPORT_OK);
27
28 require Exporter;
29
30 @ISA = qw(Exporter AutoLoader);
31 @EXPORT_OK = qw(plotter);
32 $cvs_info = '$Id: Plotter.pm,v 1.2 2001/09/20 17:55:28 bruce Exp $ ';
33 $VERSION = (split(' ', $cvs_info))[2] || 'pre_release';
34
35 require Tk;
36 require Xray::Atoms;
37 use Ifeffit qw(ifeffit put_array get_scalar put_scalar get_echo);
38 my $is_windows = (($^O eq 'MSWin32') or ($^O eq 'cygwin'));
39
40 ifeffit('set &screen_echo = 0');
41
42 my ($plot_frame, $menubar);
43 my $this;
44 my (%plotter_values, @plotter_history, $plotter_pointer);
45
46 $plotter_values{command} = '';
47 push @plotter_history, $plotter_values{command};
48 $plotter_pointer = $#plotter_history;
49
50
51 my %function_refs = ('cursor' => \&cursor,
52 'zoom' => \&zoom,
53 'unzoom' => \&unzoom,
54 'read' => \&read_data,
55 'previous' => [\&history, -1],
56 'next' => [\&history, 1],
57 );
58
59
60
61 sub plotter {
62 $plot_frame = $::pages{Plotter} -> Frame() -> pack(-fill=>'x');
63 ##$plot_frame -> Tk::bind('<Up>', sub{&history(-1)});
64 ##$plot_frame -> Tk::bind('<Down>', sub{\&history(1)});
65
66 ## ---- menubar -----------
67 $menubar = $plot_frame -> Frame(-borderwidth=>2, -relief=>'ridge',)
68 -> pack(-anchor=>'nw', -fill=>'x', -pady=>4, -padx=>4);
69
70 my $file_menu = $menubar ->
71 Menubutton(@::file_menubutton) -> pack(-side=>'left');
72 &::manage($file_menu, "menu");
73 my $this = $file_menu
74 -> command(-label=>$$::labels{load_input}, @::menu_args,
75 -command=>\&::load_input,
76 -accelerator=>'Control+o', );
77 &::manage($this, "menu");
78 ($::LWP_simple_exists) and do {
79 $this = $file_menu
80 -> command(-label=>$$::labels{load_adb}, @::menu_args,
81 -command=>\&::load_adb, );
82 &::manage($this, "menu");
83 $this = $file_menu
84 -> command(-label=>$$::labels{download_adb}, @::menu_args,
85 -command=>\&::download_adb, );
86 &::manage($this, "menu");
87 };
88 my $recent = $file_menu ->
89 cascade(-label=>"Recent files", @::menu_args, -tearoff=>0,);
90 &::manage($recent, "menu");
91 push @::recent_registry, $recent;
92 foreach my $f (@::recent_files) {
93 $recent -> command(-label=>$f, -command=>sub{&::load_input($f, 1)});
94 };
95
96 my $sep = $file_menu -> separator();
97 &::manage($sep, "separator");
98 $this = $file_menu
99 -> command(-label=>$$::labels{save_input}, @::menu_args,
100 -command=>\&::save_input,
101 -accelerator=>'Control+s', );
102 &::manage($this, "menu");
103 ## $sep = $file_menu -> separator();
104 ## &::manage($sep, "separator");
105 ## $this = $file_menu -> command(@::apt_args);
106 ## &::manage($this, "menu");
107 $sep = $file_menu -> separator();
108 &::manage($sep, "separator");
109 $this = $file_menu
110 -> command(-label=>$$::labels{quit}, @::menu_args,
111 -command=>\&::quit_tkatoms,
112 -accelerator=>'Control+q', );
113 &::manage($this, "menu");
114
115 my $show_menu = $menubar ->
116 Menubutton(-text=>'Show', @::menu_args) ->
117 pack(-side=>'left');
118 &::manage($show_menu, "menu");
119 foreach my $l (qw(@scalars @arrays @groups @strings @commands @macros)) {
120 my $this = $show_menu ->
121 command(-label=>$l, @::menu_args,
122 -command=>sub{&write_command("show $l"); &send_command()});
123 &::manage($this, "menu");
124 };
125
126
127 my $help_menu = $menubar ->
128 Menubutton(@::help_menubutton) -> pack(-side=>'right');
129 &::set_help_menu($help_menu);
130
131 my $pref_menu = $menubar ->
132 Menubutton(@::pref_menubutton) -> pack(-side=>'right');
133 &::set_pref_menu($pref_menu);
134
135 &::manage($help_menu, "menu");
136 &::manage($pref_menu, "menu");
137
138 ## Descripton
139 my $top_frame = $plot_frame -> Frame(-relief=>'groove', -borderwidth=>2)
140 -> pack(-pady=>1, -side=>'top');
141 my $plot_label = $top_frame ->
142 Label(-text=>"Plotter: ".$$::labels{'plotter_description'}, @::header_args, ) ->
143 pack();
144 &::manage($plot_label, "header");
145
146
147 ## ----- frame containing the command line
148 my $command_frame = $plot_frame -> Frame(-borderwidth=>1)
149 -> pack(-pady=>1);
150 my $command_label = $command_frame -> Label(-text=>$$::labels{plotter_command},
151 @::label_args)
152 -> pack(-side=>'left', -padx=>2);
153 &::manage($command_label, "label");
154 my $command_button = $command_frame ->
155 Button(-text=>$$::labels{'plotter_enter'},
156 @::button_args, -command=>\&send_command)
157 -> pack(-side=>'right', -padx=>2);
158 &::manage($command_button, "button");
159 my $command_entry = $command_frame ->
160 Entry(-textvariable=>\$plotter_values{command},
161 @::entry_args, -width=>60, -justify=>'left', -relief=>'sunken')
162 -> pack(-side=>'right', -padx=>2);
163 &::manage($command_entry, "entry");
164 $command_entry -> Tk::bind('<Key-Return>', \&send_command);
165 $command_entry -> Tk::bind('<Up>', sub{&history(-1)});
166 $command_entry -> Tk::bind('<Down>', sub{\&history(1)});
167
168 $::balloon->attach($command_label, -msg=>$$::help{plotter_command},);
169 $::balloon->attach($command_button, -msg=>$$::help{plotter_command},);
170
171
172 ## ----- frame containing various utility buttons
173 my $button_frame = $plot_frame -> Frame(-borderwidth=>1)
174 -> pack(-pady=>1);
175 foreach (qw(cursor zoom unzoom read previous next)) {
176 my $button = $button_frame -> Button(-text=>$$::labels{'plotter_'.$_},
177 @::button_args,
178 -command=>$function_refs{$_})
179 -> pack(-side=>'left', -padx=>2);
180 &::manage($button, "button");
181 $::balloon->attach($button, -msg=>$$::help{"plotter_".$_},);
182 };
183
184 ## ----- frame containing the scrolling output buffer
185 my $output_frame = $plot_frame -> Frame(-borderwidth=>0)
186 -> pack(-pady=>4, -padx=>4, -expand=>1, -fill=>'both');
187 $plotter_values{output_box} = $output_frame
188 -> Scrolled('Text', qw/-scrollbars e -height 7 -wrap word/);
189 $plotter_values{output_box}->Subwidget("yscrollbar")->configure(-background=>$::colors{background});
190 $plotter_values{output_box}->pack(qw/-side left -expand 1 -fill both/);
191 $plotter_values{output_box}->configure(qw/-state disabled/);
192 };
193
194 sub command {
195 &write_command($_[0]);
196 &send_command;
197 };
198
199 sub write_command {
200 $plotter_values{command} = $_[0];
201 };
202
203 sub send_command {
204 return if ($plotter_values{command} =~ /^\s*$/);
205 ifeffit($plotter_values{command});
206 $plotter_history[$#plotter_history] = $plotter_values{command};
207 push @plotter_history, '';
208 $plotter_pointer = $#plotter_history;
209 $plotter_values{command} = '';
210 my $string;
211 map {$string .= Ifeffit::get_echo().$/} (0..Ifeffit::get_scalar('&echo_lines'));
212 return if ($string =~ /^\s*$/);
213 $plotter_values{output_box} -> configure(qw/-state normal/);
214 $plotter_values{output_box} -> insert('end', $string);
215 $plotter_values{output_box} -> yview('end');
216 $plotter_values{output_box} -> configure(qw/-state disabled/);
217 };
218
219 sub history {
220 $plotter_pointer += $_[0];
221 ($plotter_pointer < 0) and ($plotter_pointer = 0);
222 ($plotter_pointer > $#plotter_history) and
223 ($plotter_pointer = $#plotter_history);
224 write_command($plotter_history[$plotter_pointer]);
225 };
226
227 sub read_data {
228 require Cwd;
229 local $Tk::FBox::a;
230 local $Tk::FBox::b;
231 my $path = $::default_filepath || Cwd::cwd();
232 my $types = [['Data files', '.dat' ],
233 ['All Files', '*', ],];
234 my $fname = $::top -> getOpenFile(-filetypes=>$types,
235 #(not $is_windows) ?
236 # (-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
237 -initialdir=>$path,
238 -title => $$::labels{'save_dialog'});
239 return 0 unless $fname;
240 ($path !~ /\/$/) and $path .= '/';
241 $fname =~ s/$path//;
242 write_command("read_data(file=$fname, type=raw, group=my)");
243 };
244
245 sub cursor {
246 &command('cursor');
247 &command('echo (X ,Y) of the selected point:');
248 &command('print cursor_x cursor_y');
249 };
250
251 ## need to build a zoom stack which gets emptied with every newplot
252 sub zoom {
253 &command('zoom');
254 };
255
256 sub unzoom {
257 my $string = "Unzooming is not yet available.$/";
258 $plotter_values{output_box} -> configure(qw/-state normal/);
259 $plotter_values{output_box} -> insert('end', $string);
260 $plotter_values{output_box} -> yview('end');
261 $plotter_values{output_box} -> configure(qw/-state disabled/);
262 };
263
264
265 sub plot_with_Ifeffit {
266 my ($x, $y, $newplot, $id, $group, $key) = @_;
267 unless (@$x and @$y) {
268 $::top -> messageBox(-icon => 'error',
269 -message => "You have not made a calculation yet!",
270 -title => 'Atoms: Error',
271 -type => 'OK');
272 return;
273 };
274 ifeffit('set &screen_echo = 0');
275 ifeffit('macro; cursor; print cursor_x corsor_y; end macro');
276 put_scalar("foo", 5);
277 $group = $$group || "my";
278 put_array($group.'.x', $x);
279 put_array($group.'.y', $y);
280 my $args = '';
281 SWITCH: {
282 ((lc($id) eq 'powder') and ($newplot)) and do {
283 $args = "xlabel=2theta, ylabel=intensity, title=\"Powder simulation\", key=$key";
284 last SWITCH;
285 };
286 ((lc($id) eq 'powder') and (not $newplot)) and do {
287 $args = "key=$key";
288 last SWITCH;
289 };
290 ((lc($id) eq 'dafs') and ($newplot)) and do {
291 $args = "xlabel=energy, ylabel=intensity, title=\"DAFS simulation\", key=$key";
292 last SWITCH;
293 };
294 ((lc($id) eq 'dafs') and (not $newplot)) and do {
295 $args = "key=$key";
296 last SWITCH;
297 };
298 };
299 my $string = ($newplot) ? "newplot($group.x, $group.y, $args)" :
300 "plot($group.x, $group.y, $args)";
301 &write_command($string);
302 &send_command();
303 };
304
305 sub saveplot {
306 my ($format, $fname) = @_;
307 my $dev;
308 SWITCH: {
309 (lc($format) eq 'gif') and do {
310 $dev = '/gif';
311 last SWITCH;
312 };
313 (lc($format) eq 'ps') and do {
314 $dev = '/cps';
315 last SWITCH;
316 };
317 $dev = '/gif';
318 };
319
320 my $path = $::default_filepath || Cwd::cwd();
321 my $types = [[uc($format), ".".lc($format) ],
322 ['All Files', '*', ],];
323 my $file = $::top -> getSaveFile(-defaultextension=>'inp',
324 -filetypes=>$types,
325 #(not $is_windows) ?
326 #(-sortcmd=>sub{$Tk::FBox::a cmp $Tk::FBox::b}) : () ,
327 -initialdir=>$path,
328 -initialfile=>$fname,
329 -title => $$::labels{'save_dialog'});
330 if ($file) {
331 ifeffit("plot(device=\"$dev\", file=\"$file\")\n");
332 ifeffit("plot(device=\"/xserve\", file=\"\")\n");
333 };
334
335 };
336
337 1;
338 __END__