Bsoft  2.1.0
Bernard's software package
bshow_file.tcl
Go to the documentation of this file.
1 ##
2 # @file bshow_file.tcl
3 #
4 # @brief Procedures to manage file I/O
5 #
6 # @author Bernard Heymann
7 # @date Created: 20010516
8 # @date Modified: 20191112
9 
10 set img_load_phase 0
11 set img_filter "*"
12 
13 ## @brief Menu for opening and closing image files and quiting
14 #
15 
16 proc setupFileMenu {} {
17  global filename
18 
19  .menuBar add cascade -menu .menuBar.file -label "File" -underline 0
20  menu .menuBar.file -tearoff 0
21  .menuBar.file add command -label "Open ..." -command { fileDialog "open" } \
22  -underline 0 -accelerator "Ctrl-o"
23  .menuBar.file add command -label "Save" -command { fileDialog "save" } \
24  -underline 0
25  .menuBar.file add command -label "Save as ..." -command { fileDialog "saveAs" } \
26  -underline 0 -accelerator "Ctrl-S"
27  .menuBar.file add command -label "Save region ..." -command { extractImage } \
28  -underline 0
29  .menuBar.file add command -label "File browser" -command { fileBrowserWindow .fb } \
30  -underline 0
31  .menuBar.file add command -label "Revert" -command { loadImage $filename -1 } \
32  -underline 0 -accelerator "Ctrl-u"
33  .menuBar.file add command -label "Get info" -command { getInfo $filename } \
34  -underline 0 -accelerator "Ctrl-i"
35  .menuBar.file add command -label "Write PostScript" -command { writePostScript } \
36  -underline 0
37  .menuBar.file add command -label "Quit" -command { Quit } -underline 0 \
38  -accelerator "Ctrl-q"
39 }
40 
41 # Type names Extension(s) Mac File Type(s)
42 #
43 #---------------------------------------------------------
44 set filetypes {
45  {"All files" *}
46  {"CCP4" {.map .MAP} }
47  {"MRC" {.mrc .MRC .stk .STK .st .ST .ali .ALI .rec .REC} }
48  {"PIF" {.pif .PIF} }
49  {"EM" {.em .EM } }
50  {"IMAGIC" {.img .IMG .hed .HED} }
51  {"SPIDER" {.spi .SPI} }
52  {"SUPRIM" {.sup .SUP .spm .SPM} }
53  {"DM3" {.dm3 .DM3} }
54  {"DM4" {.dm4 .DM4} }
55  {"SER" {.ser .SER} }
56  {"TIFF" {.tif .tiff .TIF .TIFF} }
57  {"PNG" {.png .PNG} }
58  {"PNM" {.pbm .pgm .ppm .PBM .PGM .PPM} }
59  {"JPEG" {.jpg .jpeg .JPG .JPEG} }
60  {"ASCII" {.txt .asc .ascii .TXT .ASC .ASCII} }
61 }
62 
63 ## @brief Selects a file for reading or writing using a standard dialog
64 #
65 # @param operation "open", "save" or "saveAs".
66 
67 proc fileDialog { operation } {
68  global theimg
69  global filename imgdir
70  global filetypes
71  global paramfile
72  set currdir $imgdir
73  if { [string length $currdir] < 2 } { set currdir [pwd] }
74 # puts "Image directory: $imgdir"
75  if { [string compare $operation "open"] == 0 } {
76  set new_filename [tk_getOpenFile -title "Open image file" \
77  -filetypes $filetypes -initialdir $currdir]
78  if { [string length $new_filename] < 1 } { return }
79  set filename [relativePath [pwd] $new_filename]
80  set imgdir [file dirname $filename]
81 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
82 # "imgdir = $imgdir\nfilename = $filename"
83  loadImage $filename -1
84  } else {
85  if { [string compare $operation "saveAs"] == 0 } {
86  set new_filename [tk_getSaveFile -filetypes $filetypes \
87  -initialfile [file tail $filename] -initialdir $currdir \
88  -defaultextension .mrc]
89  if { [string length $new_filename] < 1 } { return }
90  set filename [relativePath [pwd] $new_filename]
91  }
92  Bimage save $theimg $filename
93  writeSettingsFile
94  }
95 # puts "Image directory: $imgdir"
96 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
97 # "$imgdir $filename"
98  if [Bmg exists] { setMgParam }
99  wm title . [list $filename ":" $paramfile]
100 }
101 
102 
103 ## @brief Populates a file browser tree node
104 #
105 # @param tree File browser tree.
106 # @param node File browser node.
107 
108 proc openNode {tree node} {
109  global img_filter
110 
111  if {[$tree set $node type] ne "directory"} {
112  return
113  }
114 
115  set path [$tree set $node fullpath]
116  $tree delete [$tree children $node]
117 
118  foreach f [lsort -dictionary [glob -nocomplain -dir $path *]] {
119 # set r [string match "./$img_filter" $f]
120 # puts "$f $r"
121  if [string match "./$img_filter" $f] {
122  set type [file type $f]
123  set id [$tree insert $node end -text [file tail $f] \
124  -values [list $f $type]]
125 
126  if {$type eq "directory"} {
127  ## Make it so that this node is openable
128  $tree insert $id 0 -text dummy ;# a dummy
129  $tree item $id -text [file tail $f]/
130 
131  } elseif {$type eq "file"} {
132  set size [file size $f]
133  ## Format the file size nicely
134  if {$size >= 1024*1024*1024} {
135  set size [format %.1f\ GB [expr {$size/1024/1024/1024.}]]
136  } elseif {$size >= 1024*1024} {
137  set size [format %.1f\ MB [expr {$size/1024/1024.}]]
138  } elseif {$size >= 1024} {
139  set size [format %.1f\ kB [expr {$size/1024.}]]
140  } else {
141  append size " bytes"
142  }
143  $tree set $id size $size
144  }
145  }
146  }
147 
148  # Stop this code from rerunning on the current node
149  $tree set $node type processedDirectory
150 }
151 
152 ## @brief Points the file browser to the current directory
153 #
154 # @param w File browser tree.
155 
156 proc setupFileBrowser { w } {
157  global img_filter
158 
159  $w.tree set {} fullpath "./"
160  $w.tree set {} type "directory"
161 
162  set img_filter [$w.filter.e get]
163  if { $img_filter eq "" } {
164  set img_filter "*"
165  $w.filter.e insert 0 $img_filter
166  }
167 
168  openNode $w.tree {}
169 }
170 
171 ## @brief Opens a file indicated by a double-click in the file browser
172 #
173 # @param wtree File browser tree.
174 
175 proc openFileFromTree { wtree } {
176  global filename imgdir
177  set type [$wtree set [$wtree focus] type]
178  if { $type eq "file" || $type eq "link" } {
179  set fn [$wtree set [$wtree focus] fullpath]
180  set tbt [exec file $fn]
181  if [string match "*text*" $tbt] {
182  editTextFile $fn
183  } else {
184  set ft [fileType $fn]
185  if { $ft == 1 } {
186  set filename [relativePath [pwd] $fn]
187  set imgdir [file dirname $filename]
188  loadImage $fn -1
189  } else {
190  tk_messageBox -icon info -type ok \
191  -title "Error" -message "File type not supported!"
192  }
193  }
194  }
195 }
196 
197 
198 
199 ## @brief Sets up the file browser window
200 #
201 # @param w Window name.
202 
203 proc fileBrowserWindow { w } {
204  global img_filter
205 
206  catch {destroy $w}
207  toplevel $w
208  wm title $w "File browser"
209  wm iconname $w "Browser"
210  wm geometry $w 500x800+500+50
211 
212  ## Buttons
213  ttk::frame $w.buttons
214  ttk::button $w.buttons.close -text "Close" \
215  -command [list destroy [winfo toplevel $w]]
216  ttk::button $w.buttons.code -text "Refresh" \
217  -command [list setupFileBrowser $w]
218  pack $w.buttons.code $w.buttons.close -side left
219  pack $w.buttons -side bottom -padx 2 -pady 2
220 
221  ## Filter
222  labelframe $w.filter -text "File filter" -labelanchor nw
223  ttk::entry $w.filter.e
224  $w.filter.e insert 0 $img_filter
225  pack $w.filter.e -side left -expand yes -fill x
226  pack $w.filter -side bottom -fill x -padx 2 -pady 2
227 
228  ## Create the tree and set it up
229  ttk::treeview $w.tree -columns {fullpath type size} -displaycolumns {size} \
230  -yscroll "$w.vsb set" -xscroll "$w.hsb set"
231  if {[tk windowingsystem] ne "aqua"} {
232  ttk::scrollbar $w.vsb -orient vertical -command "$w.tree yview"
233  ttk::scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
234  } else {
235  scrollbar $w.vsb -orient vertical -command "$w.tree yview"
236  scrollbar $w.hsb -orient horizontal -command "$w.tree xview"
237  }
238  $w.tree heading \#0 -text "File"
239  $w.tree heading size -text "Size"
240  $w.tree column size -stretch 0 -width 70
241 
242  setupFileBrowser $w
243 
244  ## Arrange the tree and its scrollbars in the toplevel
245  lower [ttk::frame $w.gframe]
246  pack $w.gframe -fill both -expand 1
247  grid $w.tree $w.vsb -sticky nsew -in $w.gframe
248  grid $w.hsb -sticky nsew -in $w.gframe
249  grid columnconfigure $w.gframe 0 -weight 1
250  grid rowconfigure $w.gframe 0 -weight 1
251 
252  bind $w.tree <<TreeviewOpen>> {openNode %W [%W focus]}
253  bind $w.tree <Double-Button-1> {openFileFromTree %W }
254  bind $w <Return> [list setupFileBrowser $w]
255 }
256 
257 ## @brief Loads the selected file into a photo image for display.
258 #
259 # @param filename Image file name.
260 # @param img_num Sub-image number.
261 
262 proc loadImage { filename img_num } {
263  global theimg
264  global imgdir
265  global paramfile modelfile
266  global mode
267  global window_width window_height window_scale rem_scale
268  global img_load_phase
269  global avg_check kernel
270  if { [string length $filename] < 1 } { return }
271  if { ![winfo exists .] } { return }
272  set imgdir [file dirname $filename]
273  set filename [file tail $filename]
274 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
275 # "$imgdir $filename"
276  if { [Bmg exists] } {
277  wm title . [list $filename ":" $paramfile]
278  } elseif { [Bmodel exists] } {
279  wm title . [list $filename ":" $modelfile]
280  } else {
281  wm title . $filename
282  }
283 # puts "Opening image $imgdir/$filename ($img_num)"
284 
285  if { [catch { set nimages [Bimage open $theimg $imgdir/$filename $img_num] } err] } {
286 # bgerror $err
287  fileDialog "open"
288  return
289  }
290 # puts "Image opened with $nimages sub-images"
291  if { $nimages < 1 } { return }
292  set nslices [Bimage get $theimg nslices]
293 # puts "Image opened with $nslices slices"
294  if { $nslices < 1 } { return }
295 
296  if { $avg_check } {
297 # puts "Average filtering kernel: $kernel"
298  Bimage average $theimg $kernel $kernel $kernel
299  }
300 
301  set c [getImageCanvas $theimg]
302  set wc [getControlWindow $theimg]
303 # puts "Loading: $window_width $window_height $window_scale"
304  set img_load_phase 1
305  set width [Bimage get $theimg width]
306  set height [Bimage get $theimg height]
307  set origin [Bimage get $theimg origin]
308 # puts $origin
309  if { $img_num < 0 } { set img_num 0 }
310 # puts "Image opened with origin $origin"
311  set oz [lindex $origin 2]
312  if { $oz < 0 } { set oz 0 }
313  if { $oz >= $nslices } { set oz 0 }
314  set scale $window_scale
315 # $wc.scale.scale set $window_scale
316  if { $window_width < 2 || $rem_scale == 0 } {
317  set scale 1.0
318  set window_width 512
319  set window_height 512
320  if { $width > $window_width } { set scale [expr 0.1*(($window_width*10)/$width) ] }
321  }
322  if { $scale < 0.1 } { set scale 0.1 }
323  set window_scale $scale
324 # puts "Image scale = $scale mode = $mode"
325  set width [expr $width * $scale ]
326  set height [expr $height * $scale ]
327  set size "0 0 $width $height"
328  if { $width < 200 } { set width 200 }
329  if { $height < 200 } { set height 200 }
330  if { $width > $window_width } { set width $window_width }
331  if { $height > $window_height } { set height $window_height }
332  set window_width $width
333  set window_height $height
334 # puts "Width $width height $height scrollregion $size"
335  $c configure -width $width -height $height -scrollregion $size
336 # $c configure -scrollregion $size
337 # puts "Loading2: $window_width $window_height $window_scale"
338  reconfigureRangeStep
339 # puts "Range step reconfigured"
340  reconfigureScales
341 # puts "Scales reconfigured"
342  if { ![ regexp {^([0-9.]+)$} $oz ] } { set oz 0 }
343  $wc.image.scale set $img_num
344  $wc.slice.scale set $oz
345  $wc.scale.scale set $scale
346  updatePixelsize
347 # puts "Pixel size updated"
348  update
349  set img_load_phase 0
350  Update 1
351 }
352 
353 ## @brief Derives the relative path from an absolute one relative to a given directory.
354 #
355 # @param cdir Current directory.
356 # @param path Absolute path to convert.
357 
358 proc relativePath { cdir path } {
359  if { $cdir == "/" } { return $path }
360  set dlist [file split $cdir]
361  set plist [file split $path]
362  set len [llength $dlist]
363 # tk_messageBox -icon info -type ok -title "Current directory and path in relativePath" -message \
364 # "Directory = $cdir ($len) Path = $path [lindex $dlist 0]"
365 # puts "Current directory: $cdir Path: $path"
366  if { [lindex $dlist 0] == [lindex $plist 0] } {
367  for { set i 0 } { ( $i < $len ) && ( [lindex $dlist $i] == [lindex $plist $i] ) } { incr i 1 } {
368  }
369  set path ""
370  for { set j $i } { $j < $len } { incr j 1 } {
371  append path "../"
372  }
373  set len [llength $plist]
374  for { set j $i } { $j < $len } { incr j 1 } {
375  set path [file join $path [lindex $plist $j]]
376  }
377  } elseif { [lindex $plist 0] == ".." } {
378  }
379 # puts "Relative path: $path"
380 # tk_messageBox -icon info -type ok -title "Modified path in relativePath" -message "$path"
381  return $path
382 }
383 
384 ## @brief Loads the information for the current image file into a text window for display.
385 #
386 # @param filename Image file name.
387 
388 proc getInfo { filename } {
389  global Bsoft
390  global imgdir
391 # puts "$Bsoft $imgdir $filename"
392  if ![winfo exists .info] {
393  toplevel .info
394  frame .info.buttons
395  pack .info.buttons -side bottom -fill x
396  button .info.buttons.dismiss -text Dismiss \
397  -default active -command "destroy .info"
398  pack .info.buttons.dismiss -side left -expand 1 -pady 2
399  frame .info.frame
400  pack .info.frame -expand yes -fill both -padx 1 -pady 1
401  text .info.text -height 40 -width 100 -wrap word -background white\
402  -xscrollcommand ".info.xscroll set" \
403  -yscrollcommand ".info.yscroll set" \
404  -setgrid 1 -highlightthickness 0 -pady 2 -padx 3
405  scrollbar .info.xscroll -command ".info.text xview" \
406  -highlightthickness 0 -orient horizontal
407  scrollbar .info.yscroll -command ".info.text yview" \
408  -highlightthickness 0 -orient vertical
409 
410  grid .info.text -in .info.frame -padx 1 -pady 1 \
411  -row 0 -column 0 -rowspan 1 -columnspan 1 -sticky news
412  grid .info.yscroll -in .info.frame -padx 1 -pady 1 \
413  -row 0 -column 1 -rowspan 1 -columnspan 1 -sticky news
414  grid rowconfig .info.frame 0 -weight 1 -minsize 0
415  grid columnconfig .info.frame 0 -weight 1 -minsize 0
416  } else {
417  wm deiconify .info
418  raise .info
419  }
420  set filename [file tail $filename]
421  wm title .info $filename
422  wm iconname .info $filename
423 # puts "$Bsoft/bin/bhead -verbose 7 -info $imgdir/$filename"
424  catch { exec $Bsoft/bin/bhead -verbose 7 -info $imgdir/$filename } theinfo
425  .info.text delete 1.0 end
426  .info.text insert 1.0 $theinfo
427  .info.text mark set insert 1.0
428 }
429 
430 ## @brief Writes the current canvas to a postscript file.
431 #
432 
433 proc writePostScript { } {
434  global theimg
435  global imgdir
436  global filename
437  set currdir $imgdir
438  if { [string length $currdir] < 2 } { set currdir [pwd] }
439 # puts "Image directory: $imgdir"
440  set init_filename [file rootname $filename]
441  append init_filename .ps
442  set ps_filename [tk_getSaveFile -filetypes {{"PostScript" {.ps .PS} }} \
443  -initialfile $init_filename -initialdir $currdir -defaultextension .ps]
444  if { [string length $ps_filename] < 1 } { return }
445  set c [getImageCanvas $theimg]
446  set wc [getControlWindow $theimg]
447  set scale [$wc.scale.scale get]
448  set w [expr [Bimage get $theimg width] * $scale]
449  set h [expr [Bimage get $theimg height] * $scale]
450  $c postscript -file $ps_filename -pageanchor sw -x 0 -y 0 -width $w -height $h
451 # tk_messageBox -icon info -type ok -title "Directory and file name" -message \
452 # "$imgdir $filename"
453 }
454 
455