source: branches/1.7/gui/scripts/main.tcl @ 6561

Last change on this file since 6561 was 6561, checked in by ldelgass, 6 years ago

Merge r6560 from trunk (loader fix)

File size: 14.6 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2#!/bin/sh
3# ----------------------------------------------------------------------
4#  USER INTERFACE DRIVER
5#
6#  This driver program loads a tool description from a tool.xml file,
7#  and produces a user interface automatically to drive an application.
8#  The user can set up input, click and button to launch a tool, and
9#  browse through output.
10#
11#  RUN AS FOLLOWS:
12#    wish main.tcl ?-tool <toolfile>?
13#
14#  If the <toolfile> is not specified, it defaults to "tool.xml" in
15#  the current working directory.
16#
17# ======================================================================
18#  AUTHOR:  Michael McLennan, Purdue University
19#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
20#
21#  See the file "license.terms" for information on usage and
22#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
23# ======================================================================
24
25# take the main window down for now, so we can avoid a flash on the screen
26wm withdraw .
27
28package require Itcl
29package require Img
30package require Rappture
31package require RapptureGUI
32
33option add *MainWin.mode desktop startupFile
34option add *MainWin.borderWidth 0 startupFile
35option add *MainWin.anchor fill startupFile
36
37# "web site" look
38option add *MainWin.bgScript {
39    rectangle 0 0 200 <h> -outline "" -fill #5880BB
40    rectangle 200 0 300 <h> -outline "" -fill #425F8B
41    rectangle 300 0 <w> <h> -outline "" -fill #324565
42} startupFile
43
44# "clean" look
45option add *MainWin.bgScript "" startupFile
46option add *MainWin.bgColor white startupFile
47option add *Tooltip.background white
48option add *Editor.background white
49option add *Gauge.textBackground white
50option add *TemperatureGauge.textBackground white
51option add *Switch.textBackground white
52option add *Progress.barColor #ffffcc
53option add *Balloon.titleBackground #6666cc
54option add *Balloon.titleForeground white
55option add *Balloon*Label.font -*-helvetica-medium-r-normal-*-12-*
56option add *Balloon*Radiobutton.font -*-helvetica-medium-r-normal-*-12-*
57option add *Balloon*Checkbutton.font -*-helvetica-medium-r-normal-*-12-*
58option add *ResultSelector.controlbarBackground #6666cc
59option add *ResultSelector.controlbarForeground white
60option add *ResultSelector.activeControlBackground #ccccff
61option add *ResultSelector.activeControlForeground black
62option add *Radiodial.length 3i
63option add *BugReport*banner*foreground white
64option add *BugReport*banner*background #a9a9a9
65option add *BugReport*banner*highlightBackground #a9a9a9
66option add *BugReport*banner*font -*-helvetica-bold-r-normal-*-18-*
67option add *hubcntls*Button.padX 0 widgetDefault
68option add *hubcntls*Button.padY 0 widgetDefault
69option add *hubcntls*Button.relief flat widgetDefault
70option add *hubcntls*Button.overRelief raised widgetDefault
71option add *hubcntls*Button.borderWidth 1 widgetDefault
72option add *hubcntls*Button.font \
73    -*-helvetica-medium-r-normal-*-8-* widgetDefault
74
75switch $tcl_platform(platform) {
76    unix - windows {
77        event add <<PopupMenu>> <ButtonPress-3>
78    }
79    macintosh {
80        event add <<PopupMenu>> <Control-ButtonPress-1>
81    }
82}
83
84# install a better bug handler
85Rappture::bugreport::install
86# fix the "grab" command to support a stack of grab windows
87Rappture::grab::init
88
89#
90# Process command line args to get the names of files to load...
91#
92Rappture::getopts argv params {
93    value -tool tool.xml
94    list  -load ""
95    value -input ""
96    value -simset ""
97    value -nosim 0
98}
99
100proc ReadToolParameters { numTries } {
101    incr numTries -1
102    if { $numTries < 0 } {
103        return
104    }
105    global env
106    set paramsFile $env(TOOL_PARAMETERS)
107    if { ![file readable $paramsFile] } {
108        after 500 ReadToolParmeters $numTries
109        return
110    }
111    catch {
112        set f [open $paramsFile "r"]
113        set contents [read $f]
114        close $f
115        set pattern {^file\((.*)\):(.*)$}
116        foreach line [split $contents "\n"] {
117            if { [regexp $pattern $line match path rest] } {
118                set ::Rappture::parameters($path) $rest
119            }
120        }
121    }
122}
123
124if { [info exists env(TOOL_PARAMETERS)] } {
125    ReadToolParameters 10
126}
127
128set loadobjs {}
129foreach runfile $params(-load) {
130    if {![file exists $runfile]} {
131        puts stderr "can't find run: \"$runfile\""
132        exit 1
133    }
134    set status [catch {Rappture::library $runfile} result]
135    lappend loadobjs $result
136}
137
138set inputobj {}
139if {$params(-input) ne ""} {
140    if {![file exists $params(-input)]} {
141        puts stderr "can't find input file: \"$params(-input)\""
142        exit 1
143    }
144    if {[catch {Rappture::library $params(-input)} result] == 0} {
145        set inputobj $result
146    }
147}
148
149# open the XML file containing the tool parameters
150if {![file exists $params(-tool)]} {
151    # check to see if the user specified any run.xml files to load.
152    # if so, we can use that as the tool.xml. if we can find where
153    # the original application was installed using the xml tag
154    # tool.version.application.directory(top), the user can
155    # run new simulations, otherwise they can only revisualize the
156    # run.xml files they are loading.
157    set pseudotool ""
158    if {[llength $loadobjs] == 0 && $inputobj eq ""} {
159        puts stderr "can't find tool \"$params(-tool)\""
160        exit 1
161    }
162    # search the loadfiles for the install location
163    # we could just use run.xml files as tool.xml, but
164    # if there are loaders or notes, they will still need
165    # examples/ and docs/ dirs from the install location
166    set check [concat $loadobjs $inputobj]
167    foreach runobj $check {
168        set tdir \
169            [string trim [$runobj get tool.version.application.directory(tool)]]
170        if {[file isdirectory $tdir] && \
171            [file exists $tdir/tool.xml]} {
172            set pseudotool $tdir/tool.xml
173            break
174        }
175    }
176    if {![file exists $pseudotool]} {
177        # we didn't find a tool.xml file,
178        # use info from a runfile to build gui
179        # disable simulation, because no tool.xml
180        set pseudotool [lindex $params(-load) 0]
181        array set params [list -nosim true]
182    }
183    if {![file exists $pseudotool]} {
184        puts stderr "can't find tool \"$params(-tool)\""
185        exit 1
186    }
187    array set params [list -tool $pseudotool]
188}
189
190set xmlobj [Rappture::library $params(-tool)]
191
192set installdir [file normalize [file dirname $params(-tool)]]
193$xmlobj put tool.version.application.directory(tool) $installdir
194set tool [Rappture::Tool ::#auto $xmlobj $installdir]
195
196# ----------------------------------------------------------------------
197# CHECK JOB FAILURE REPORTING
198#
199# If this tool might fail when it launches jobs (i.e., Rappture
200# can't check some inputs, such as strings), then disable the
201# automatic ticket submission for job failures
202# ----------------------------------------------------------------------
203set val [string trim [$tool xml get tool.reportJobFailures]]
204if { "" != $val} {
205    if {[catch {Rappture::bugreport::shouldReport jobfailures $val} result]} {
206        puts stderr "WARNING for reportJobFailures: $result"
207    }
208}
209
210# ----------------------------------------------------------------------
211# LOAD RESOURCE SETTINGS
212#
213# Try to load the $SESSIONDIR/resources file, which contains
214# middleware settings, such as the application name and the
215# filexfer settings.
216# ----------------------------------------------------------------------
217Rappture::resources::load
218
219# ----------------------------------------------------------------------
220# START LOGGING
221#
222# If the $RAPPTURE_LOG directory is set to a directory used for
223# logging, then open a log file and start logging.
224# ----------------------------------------------------------------------
225Rappture::Logger::init
226
227# ----------------------------------------------------------------------
228# INITIALIZE THE DESKTOP CONNECTION
229#
230# If there's a SESSION ID, then this must be running within the
231# nanoHUB.  Try to initialize the server handling the desktop
232# connection.
233# ----------------------------------------------------------------------
234Rappture::filexfer::init
235
236# ----------------------------------------------------------------------
237# MAIN WINDOW
238# ----------------------------------------------------------------------
239Rappture::MainWin .main -borderwidth 0
240.main configure -title [string trim [$tool xml get tool.title]]
241wm withdraw .main
242wm protocol .main WM_DELETE_WINDOW {Rappture::Logger::cleanup; exit}
243
244# if the FULLSCREEN variable is set, then nanoHUB wants us to go full screen
245if {[info exists env(FULLSCREEN)]} {
246    .main configure -mode web
247}
248
249#
250# The main window has a pager that acts as a notebook for the
251# various parts.  This notebook as at least two pages--an input
252# page and an output (analysis) page.  If there are <phase>'s
253# for input, then there are more pages in the notebook.
254#
255set win [.main component app]
256Rappture::Postern $win.postern
257pack $win.postern -side bottom -fill x
258
259Rappture::Pager $win.pager
260pack $win.pager -expand yes -fill both
261
262#
263# Add a place for menu button in the breadcrumbs area of this pager.
264#
265set app [string trim [$tool xml get tool.id]]
266set url [Rappture::Tool::resources -huburl]
267set crumbs [$win.pager component breadcrumbarea]
268frame $crumbs.hubcntls
269pack $crumbs.hubcntls -side right -padx 4
270
271set m $crumbs.hubcntls.help.menu
272menubutton $crumbs.hubcntls.help \
273    -image [Rappture::icon hamburger_menu] \
274    -highlightthickness 0 \
275    -menu $m
276pack $crumbs.hubcntls.help -side top -anchor w
277
278#
279# Load up the components in the various phases of input.
280#
281set phases [$tool xml children -type phase input]
282if {[llength $phases] > 0} {
283    set plist ""
284    foreach name $phases {
285        lappend plist input.$name
286    }
287    set phases $plist
288} else {
289    set phases input
290}
291
292foreach comp $phases {
293    set title [string trim [$tool xml get $comp.about.label]]
294    if {$title == ""} {
295        set title "Input #auto"
296    }
297    $win.pager insert end -name $comp -title $title
298
299    #
300    # Build the page of input controls for this phase.
301    #
302    set f [$win.pager page $comp]
303    Rappture::Page $f.cntls $tool $comp
304    pack $f.cntls -expand yes -fill both
305}
306
307# let components (loaders) in the newly created pages settle
308update
309
310# ----------------------------------------------------------------------
311# OUTPUT AREA
312# ----------------------------------------------------------------------
313
314# adjust the title of the page here.
315# to adjust the button text, look in analyzer.tcl
316set simtxt [string trim [$xmlobj get tool.action.label]]
317if {"" == $simtxt} {
318    set simtxt "Simulate"
319}
320$win.pager insert end -name analyzer -title $simtxt
321set f [$win.pager page analyzer]
322$win.pager page analyzer -command [subst {
323    if { !$params(-nosim) } {
324        $win.pager busy yes
325        update
326        $f.analyze simulate -ifneeded
327        $win.pager busy no
328    }
329}]
330
331Rappture::Analyzer $f.analyze $tool -simcontrol auto -notebookpage about
332pack $f.analyze -expand yes -fill both
333
334$tool notify add analyzer * [list $f.analyze reset]
335$f.analyze buildMenu $m $url $app
336
337# ----------------------------------------------------------------------
338# Finalize the arrangement
339# ----------------------------------------------------------------------
340if {[llength [$win.pager page]] > 2} {
341    # We have phases, so we shouldn't allow the "Simulate" button.
342    # If it pops up, there are two ways to push simulate and duplicate
343    # links for "About" and "Questions?".
344    $f.analyze configure -simcontrol off
345} elseif {[llength [$win.pager page]] == 2} {
346    set style [string trim [$xmlobj get tool.layout]]
347    set screenw [winfo screenwidth .]
348
349    update idletasks
350    set w0 [winfo reqwidth [$win.pager page @0]]
351    set w1 [winfo reqwidth [$win.pager page @1]]
352
353    if { $style != "wizard" } {
354        # If only two windows and they're small enough, put them up
355        # side-by-side
356        if {$w0+$w1 < $screenw } {
357            $win.pager configure -arrangement side-by-side
358            $f.analyze configure -holdwindow [$win.pager page @0]
359        }
360    }
361    set type [string trim [$tool xml get tool.control]]
362    if {$type == ""} {
363        set type [string trim [$tool xml get tool.control.type]]
364    }
365    set arrangement [$win.pager cget -arrangement]
366    if { $type == "" } {
367        if { $arrangement != "side-by-side" } {
368           set type auto
369        }
370    }
371    if { $arrangement != "side-by-side" &&
372            ($type == "manual" || $type == "manual-resim" ||
373             $type == "auto" || $style == "wizard") } {
374        # in "auto" mode, we don't need a simulate button
375        $f.analyze configure -simcontrol off
376    } else {
377        # not in "auto" mode but side-by-side, we always need the button
378        $f.analyze configure -simcontrol on
379    }
380}
381
382# load previous xml runfiles
383update
384if {[llength $params(-load)] > 0} {
385    foreach runobj $loadobjs {
386        $f.analyze load $runobj
387    }
388    # load the inputs for the very last run
389    $tool load $runobj
390
391    # don't need simulate button if we cannot simulate
392    if {$params(-nosim)} {
393        $f.analyze configure -simcontrol off
394    }
395    $f.analyze configure -notebookpage analyze
396    $win.pager configure -nosim 1
397    $win.pager current analyzer
398    $win.pager configure -nosim 0
399} elseif {$params(-simset) ne ""} {
400    $f.analyze reload $params(-simset)
401} elseif {$params(-input) ne ""} {
402    $tool load $inputobj
403}
404
405# let components (loaders) settle after the newly loaded runs
406#
407# This update was put in to allow the loader to wait for all the
408# widgets to get created before updating them with whatever is the
409# default for the loader.
410#
411# This update is a source of problems for tools that are 1) loading
412# previously saved/shared simulation results and 2) talk to a visualization
413# server.
414#
415# So we're commenting out the "update".  It may need to go back
416# if this breaks loaders with a default setting.
417#update
418
419foreach path [array names ::Rappture::parameters] {
420    if { $path == "simset" } {
421        continue;                       # Don't consider file(simset)
422    }
423    set fname $::Rappture::parameters($path)
424    if { ![file exists $fname] } {
425        puts stderr "WARNING: tool parameters file \"$fname\" doesn't exist"
426        continue;                       # Can't find parameters file
427    }       
428    if { ![file readable $fname] } {
429        puts stderr "WARNING: can't read tool parameters file \"$fname\""
430        continue;                       # Can't read parameters file
431    }
432    set f [open $fname r]
433    set contents [read $f]
434    close $f
435   
436    set w [$tool widgetfor $path]
437    if { $w == "" } {
438        puts stderr "WARNING: can't find control for tool parameter: $path"
439        continue;                       # Can't find rappture control for path
440    }
441    if {[catch {$w value [string trim $info]} result]} {
442        puts stderr "WARNING: bad tool parameter value for \"$path\""
443        puts stderr "  $result"
444    }
445}
446
447wm deiconify .main
Note: See TracBrowser for help on using the repository browser.