source: trunk/gui/scripts/analyzer.tcl @ 1347

Last change on this file since 1347 was 1347, checked in by gah, 15 years ago
File size: 37.9 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: analyzer - output area for Rappture
3#
4#  This widget acts as the output side of a Rappture application.
5#  When the input has changed, it displays a Simulate button that
6#  launches the simulation.  When a simulation is running, this
7#  area shows status.  When it is finished, the results appear
8#  in place of the button, according to the specs in the <analyze>
9#  XML data.
10# ======================================================================
11#  AUTHOR:  Michael McLennan, Purdue University
12#  Copyright (c) 2004-2005  Purdue Research Foundation
13#
14#  See the file "license.terms" for information on usage and
15#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16# ======================================================================
17package require Itk
18
19option add *Analyzer.width 3.5i widgetDefault
20option add *Analyzer.height 4i widgetDefault
21option add *Analyzer.simControl "auto" widgetDefault
22option add *Analyzer.simControlBackground "" widgetDefault
23option add *Analyzer.simControlOutline gray widgetDefault
24option add *Analyzer.simControlActiveBackground #ffffcc widgetDefault
25option add *Analyzer.simControlActiveOutline black widgetDefault
26option add *Analyzer.notebookpage "about" widgetDefault
27
28option add *Analyzer.font \
29    -*-helvetica-medium-r-normal-*-12-* widgetDefault
30option add *Analyzer.codeFont \
31    -*-courier-medium-r-normal-*-12-* widgetDefault
32option add *Analyzer.textFont \
33    -*-helvetica-medium-r-normal-*-12-* widgetDefault
34option add *Analyzer.boldTextFont \
35    -*-helvetica-bold-r-normal-*-12-* widgetDefault
36
37itcl::class Rappture::Analyzer {
38    inherit itk::Widget
39
40    itk_option define -codefont codeFont Font ""
41    itk_option define -textfont textFont Font ""
42    itk_option define -boldtextfont boldTextFont Font ""
43    itk_option define -simcontrol simControl SimControl ""
44    itk_option define -simcontroloutline simControlOutline Background ""
45    itk_option define -simcontrolbackground simControlBackground Background ""
46    itk_option define -simcontrolactiveoutline simControlActiveOutline Background ""
47    itk_option define -simcontrolactivebackground simControlActiveBackground Background ""
48    itk_option define -holdwindow holdWindow HoldWindow ""
49    itk_option define -notebookpage notebookPage NotebookPage ""
50
51    constructor {tool args} { # defined below }
52    destructor { # defined below }
53
54    public method simulate {args}
55    public method reset {{when -eventually}}
56    public method load {xmlobj}
57    public method clear {}
58    public method download {option args}
59
60    protected method _plot {args}
61    protected method _reorder {comps}
62    protected method _autoLabel {xmlobj path title cntVar}
63    protected method _fixResult {}
64    protected method _fixSize {}
65    protected method _fixSimControl {}
66    protected method _fixNotebook {}
67    protected method _simState {state args}
68    protected method _simOutput {message}
69    protected method _resultTooltip {}
70
71    private variable _tool ""          ;# belongs to this tool
72    private variable _control "manual" ;# start mode
73    private variable _runs ""          ;# list of XML objects with results
74    private variable _pages 0          ;# number of pages for result sets
75    private variable _label2page       ;# maps output label => result set
76    private variable _label2desc       ;# maps output label => description
77    private variable _lastlabel ""     ;# label of last example loaded
78    private variable _plotlist ""      ;# items currently being plotted
79
80    private common job                 ;# array var used for blt::bgexec jobs
81}
82
83itk::usual Analyzer {
84    keep -background -cursor foreground -font
85}
86
87# ----------------------------------------------------------------------
88# CONSTRUCTOR
89# ----------------------------------------------------------------------
90itcl::body Rappture::Analyzer::constructor {tool args} {
91    set _tool $tool
92
93    itk_option add hull.width hull.height
94    pack propagate $itk_component(hull) no
95
96    frame $itk_interior.simol -borderwidth 1 -relief flat
97    pack $itk_interior.simol -fill x
98
99    itk_component add simbg {
100        frame $itk_interior.simol.simbg -borderwidth 0
101    } {
102        usual
103        rename -background -simcontrolcolor simControlColor Color
104    }
105    pack $itk_component(simbg) -expand yes -fill both
106
107    set simtxt [$tool xml get tool.action.label]
108    if {"" == $simtxt} {
109        set simtxt "Simulate"
110    }
111    itk_component add simulate {
112        button $itk_component(simbg).simulate -text $simtxt \
113            -command [itcl::code $this simulate]
114    } {
115        usual
116        rename -highlightbackground -simcontrolcolor simControlColor Color
117    }
118    pack $itk_component(simulate) -side left -padx 4 -pady 4
119
120    # if there's a hub url, then add "About" and "Questions" links
121    set app [$_tool xml get tool.id]
122    set url [Rappture::Tool::resources -huburl]
123    if {"" != $url && "" != $app} {
124        itk_component add hubcntls {
125            frame $itk_component(simbg).hubcntls
126        } {
127            usual
128            rename -background -simcontrolcolor simControlColor Color
129        }
130        pack $itk_component(hubcntls) -side right -padx 4
131
132        itk_component add icon {
133            label $itk_component(hubcntls).icon -image [Rappture::icon ask] \
134                -highlightthickness 0
135        } {
136            usual
137            ignore -highlightthickness
138            rename -background -simcontrolcolor simControlColor Color
139        }
140        pack $itk_component(icon) -side left
141
142        itk_component add about {
143            button $itk_component(hubcntls).about -text "About this tool" \
144                -command [list Rappture::filexfer::webpage "$url/tools/$app"]
145        } {
146            usual
147            ignore -font
148            rename -background -simcontrolcolor simControlColor Color
149            rename -highlightbackground -simcontrolcolor simControlColor Color
150        }
151        pack $itk_component(about) -side top -anchor w
152
153        itk_component add questions {
154            button $itk_component(hubcntls).questions -text Questions? \
155                -command [list Rappture::filexfer::webpage "$url/resources/$app/questions"]
156        } {
157            usual
158            ignore -font
159            rename -background -simcontrolcolor simControlColor Color
160            rename -highlightbackground -simcontrolcolor simControlColor Color
161        }
162        pack $itk_component(questions) -side top -anchor w
163    }
164
165    itk_component add simstatus {
166        text $itk_component(simbg).simstatus -borderwidth 0 \
167            -highlightthickness 0 -height 1 -width 1 -wrap none \
168            -state disabled
169    } {
170        usual
171        ignore -highlightthickness
172        rename -background -simcontrolcolor simControlColor Color
173        rename -font -textfont textFont Font
174    }
175    pack $itk_component(simstatus) -side left -expand yes -fill x
176
177    $itk_component(simstatus) tag configure popup \
178        -underline 1 -foreground blue
179
180    $itk_component(simstatus) tag bind popup \
181        <Enter> {%W configure -cursor center_ptr}
182    $itk_component(simstatus) tag bind popup \
183        <Leave> {%W configure -cursor ""}
184    $itk_component(simstatus) tag bind popup \
185        <ButtonPress> {after idle {Rappture::Tooltip::tooltip show %W}}
186
187
188    itk_component add notebook {
189        Rappture::Notebook $itk_interior.nb
190    }
191    pack $itk_interior.nb -expand yes -fill both
192
193    # ------------------------------------------------------------------
194    # ABOUT PAGE
195    # ------------------------------------------------------------------
196    set w [$itk_component(notebook) insert end about]
197
198    Rappture::Scroller $w.info -xscrollmode off -yscrollmode auto
199    pack $w.info -expand yes -fill both -padx 4 -pady 20
200    itk_component add toolinfo {
201        text $w.info.text -width 1 -height 1 -wrap word \
202            -borderwidth 0 -highlightthickness 0
203    } {
204        usual
205        ignore -borderwidth -relief
206        rename -font -textfont textFont Font
207    }
208    $w.info contents $w.info.text
209
210    # ------------------------------------------------------------------
211    # SIMULATION PAGE
212    # ------------------------------------------------------------------
213    set w [$itk_component(notebook) insert end simulate]
214    frame $w.cntls
215    pack $w.cntls -side bottom -fill x -pady 12
216    frame $w.cntls.sep -background black -height 1
217    pack $w.cntls.sep -side top -fill x
218
219    itk_component add abort {
220        button $w.cntls.abort -text "Abort" \
221            -command [itcl::code $_tool abort]
222    }
223    pack $itk_component(abort) -side left -expand yes -padx 4 -pady 4
224
225    Rappture::Scroller $w.info -xscrollmode auto -yscrollmode auto
226    pack $w.info -expand yes -fill both -padx 4 -pady 4
227    itk_component add runinfo {
228        text $w.info.text -width 1 -height 1 -wrap none \
229            -borderwidth 0 -highlightthickness 0 \
230            -state disabled
231    } {
232        usual
233        ignore -borderwidth -relief
234        rename -font -codefont codeFont Font
235    }
236    $w.info contents $w.info.text
237
238    itk_component add progress {
239        Rappture::Progress $w.progress
240    }
241
242    # ------------------------------------------------------------------
243    # ANALYZE PAGE
244    # ------------------------------------------------------------------
245    set w [$itk_component(notebook) insert end analyze]
246
247    frame $w.top
248    pack $w.top -side top -fill x -pady 8
249    label $w.top.l -text "Result:" -font $itk_option(-font)
250    pack $w.top.l -side left
251
252    itk_component add resultselector {
253        Rappture::Combobox $w.top.sel -width 10 -editable no
254    } {
255        usual
256        rename -font -textfont textFont Font
257    }
258    pack $itk_component(resultselector) -side left -expand yes -fill x
259    bind $itk_component(resultselector) <<Value>> [itcl::code $this _fixResult]
260    bind $itk_component(resultselector) <Enter> \
261        [itcl::code $this download coming]
262
263    Rappture::Tooltip::for $itk_component(resultselector) \
264        "@[itcl::code $this _resultTooltip]"
265
266    $itk_component(resultselector) choices insert end \
267        --- "---"
268
269    itk_component add download {
270        button $w.top.dl -image [Rappture::icon download] -anchor e \
271            -borderwidth 1 -relief flat -overrelief raised \
272            -command [itcl::code $this download start $w.top.dl]
273    }
274    pack $itk_component(download) -side right -padx {4 0}
275    bind $itk_component(download) <Enter> \
276        [itcl::code $this download coming]
277
278    $itk_component(resultselector) choices insert end \
279        @download [Rappture::filexfer::label download]
280
281    if {[Rappture::filexfer::enabled]} {
282        Rappture::Tooltip::for $itk_component(download) "Downloads the current result to a new web browser window on your desktop.  From there, you can easily print or save results.
283
284NOTE:  Your web browser must allow pop-ups from this site.  If your output does not appear, look for a 'pop-up blocked' message and enable pop-ups."
285    } else {
286        Rappture::Tooltip::for $itk_component(download) "Saves the current result to a file on your desktop."
287    }
288
289    itk_component add results {
290        Rappture::Panes $w.pane -sashwidth 1 -sashrelief solid -sashpadding {4 0}
291    }
292    pack $itk_component(results) -expand yes -fill both
293    set f [$itk_component(results) pane 0]
294
295    itk_component add resultpages {
296        Rappture::Notebook $f.nb
297    }
298    pack $itk_component(resultpages) -expand yes -fill both
299
300    set f [$itk_component(results) insert end -fraction 0.1]
301    itk_component add resultset {
302        Rappture::ResultSet $f.rset \
303            -clearcommand [itcl::code $this clear] \
304            -settingscommand [itcl::code $this _plot] \
305            -promptcommand [itcl::code $this _simState]
306    }
307    pack $itk_component(resultset) -expand yes -fill both
308    bind $itk_component(resultset) <<Control>> [itcl::code $this _fixSize]
309    bind $itk_component(results) <Configure> [itcl::code $this _fixSize]
310
311    eval itk_initialize $args
312
313    $itk_component(runinfo) tag configure ERROR -foreground red
314    $itk_component(runinfo) tag configure text -font $itk_option(-textfont)
315
316    #
317    # Load up tool info on the first page.
318    #
319    $itk_component(toolinfo) tag configure title \
320        -font $itk_option(-boldtextfont)
321
322    set mesg [$tool xml get tool.title]
323    if {"" != $mesg} {
324        $itk_component(toolinfo) insert end $mesg title
325        $itk_component(toolinfo) insert end "\n\n"
326    }
327
328    set mesg [$tool xml get tool.about]
329    if {"" != $mesg} {
330        $itk_component(toolinfo) insert end $mesg
331    }
332    $itk_component(toolinfo) configure -state disabled
333    $itk_component(notebook) current about
334
335    # tool can run on "manual" (default) or "auto"
336    set cntl [$tool xml get tool.control]
337    if {"" == $cntl} {
338        set cntl [$tool xml get tool.control.type]
339    }
340    if {"" != $cntl} {
341        set _control $cntl
342    }
343
344    # reset everything to a clean state
345    reset
346}
347
348# ----------------------------------------------------------------------
349# DESTRUCTOR
350# ----------------------------------------------------------------------
351itcl::body Rappture::Analyzer::destructor {} {
352    foreach obj $_runs {
353        itcl::delete object $obj
354    }
355    after cancel [itcl::code $this simulate]
356}
357
358# ----------------------------------------------------------------------
359# USAGE: simulate ?-ifneeded?
360# USAGE: simulate ?<path1> <value1> <path2> <value2> ...?
361#
362# Kicks off the simulator by executing the tool.command associated
363# with the tool.  If any arguments are specified, they are used to
364# set parameters for the simulation.  While the simulation is running,
365# it shows status.  When the simulation is finished, it switches
366# automatically to "analyze" mode and shows the results.
367# ----------------------------------------------------------------------
368itcl::body Rappture::Analyzer::simulate {args} {
369    if {$args == "-ifneeded"} {
370        # check to see if simulation is really needed
371        $_tool sync
372        if {[$itk_component(resultset) contains [$_tool xml object]]
373              && ![string equal $_control "manual-resim"]} {
374            # not needed -- show results and return
375            $itk_component(notebook) current analyze
376            return
377        }
378        set args ""
379    }
380
381    # simulation is needed -- go to simulation page
382    $itk_component(notebook) current simulate
383
384    # no progress messages yet
385    pack forget $itk_component(progress)
386    lappend args -output [itcl::code $this _simOutput]
387
388    _simState off
389    $itk_component(runinfo) configure -state normal
390    $itk_component(runinfo) delete 1.0 end
391    $itk_component(runinfo) insert end "Running simulation...\n\n" text
392    $itk_component(runinfo) configure -state disabled
393
394    # if the hold window is set, then put up a busy cursor
395    if {$itk_option(-holdwindow) != ""} {
396        blt::busy hold $itk_option(-holdwindow)
397        raise $itk_component(hull)
398        update
399    }
400
401    # execute the job
402    foreach {status result} [eval $_tool run $args] break
403
404    # if job was aborted, then allow simulation again
405    if {$result == "ABORT"} {
406        _simState on "Aborted"
407    }
408
409    # load results from run.xml into analyzer
410    if {$status == 0 && $result != "ABORT"} {
411        set status [catch {load $result} result]
412    }
413
414    # back to normal
415    if {$itk_option(-holdwindow) != ""} {
416        blt::busy release $itk_option(-holdwindow)
417    }
418    $itk_component(abort) configure -state disabled
419
420    if {$status != 0} {
421        $itk_component(runinfo) configure -state normal
422        $itk_component(runinfo) delete 1.0 end
423        $itk_component(runinfo) insert end "Problem launching job:\n\n" text
424        _simOutput $result
425        $itk_component(runinfo) configure -state disabled
426        $itk_component(runinfo) see 1.0
427
428        # Try to create a support ticket for this error.
429        # It may be a real problem.
430        if {[Rappture::bugreport::shouldReport for jobs]} {
431            Rappture::bugreport::register "Problem launching job:\n\n$result\n== RAPPTURE INPUT ==\n[$_tool xml xml]"
432        }
433    } else {
434        $itk_component(notebook) current analyze
435    }
436
437    # do this last -- after _simOutput above
438    pack forget $itk_component(progress)
439}
440
441# ----------------------------------------------------------------------
442# USAGE: reset ?-eventually|-now?
443#
444# Used to reset the analyzer whenever the input to a simulation has
445# changed.  Sets the mode back to "simulate", so the user has to
446# simulate again to see the output.  If the <start> option is set
447# to "auto", the simulation is invoked immediately.
448# ----------------------------------------------------------------------
449itcl::body Rappture::Analyzer::reset {{when -eventually}} {
450    if {$when == "-eventually"} {
451        after cancel [list catch [itcl::code $this reset -now]]
452        after idle [list catch [itcl::code $this reset -now]]
453        return
454    }
455
456    # check to see if simulation is really needed
457    $_tool sync
458    if {![$itk_component(resultset) contains [$_tool xml object]]
459          || [string equal $_control "manual-resim"]} {
460        # if control mode is "auto", then simulate right away
461        if {[string match auto* $_control]} {
462            # auto control -- don't need button
463            pack forget $itk_interior.simol
464
465            after cancel [itcl::code $this simulate]
466            after idle [itcl::code $this simulate]
467        } else {
468            _simState on "new input parameters"
469        }
470    } else {
471        _simState off
472    }
473}
474
475# ----------------------------------------------------------------------
476# USAGE: load <xmlobj>
477#
478# Loads the data from the given <xmlobj> into the appropriate results
479# sets.  If necessary, new results sets are created to store the data.
480# ----------------------------------------------------------------------
481itcl::body Rappture::Analyzer::load {xmlobj} {
482    # only show the last result? then clear first
483    if {[$_tool xml get tool.analyzer] == "last"} {
484        clear
485    }
486
487    # look for all output.load children and load them first
488    # each run.xml is loaded as a previous simulation.
489    foreach item [$xmlobj children -type run output.load] {
490        set loadfile [$xmlobj get output.load.$item]
491        set loadobj [Rappture::library $loadfile]
492        load $loadobj
493    }
494
495    foreach item [$xmlobj children -type run output.include] {
496        set id [$xmlobj element -as id output.include.$item]
497        set inclfile [$xmlobj get output.include.$item]
498        set inclobj [Rappture::library $inclfile]
499        foreach c [$inclobj children output] {
500            switch -glob -- $c {
501                # we don't want to include these tags
502                include* - time* - status* - user* {
503                    continue
504                }
505                default {
506                    set oldid [$inclobj element -as id output.$c]
507                    set oldtype [$inclobj element -as type output.$c]
508                    set newcomp "$oldtype\($id-$oldid\)"
509                    $xmlobj copy output.$newcomp from $inclobj output.$c
510                }
511            }
512        }
513    }
514
515    lappend _runs $xmlobj
516
517    # go through the analysis and find all result sets
518    set haveresults 0
519    foreach item [_reorder [$xmlobj children output]] {
520        switch -glob -- $item {
521            log* {
522                _autoLabel $xmlobj output.$item "Output Log" counters
523            }
524            number* {
525                _autoLabel $xmlobj output.$item "Number" counters
526            }
527            integer* {
528                _autoLabel $xmlobj output.$item "Integer" counters
529            }
530            string* {
531                _autoLabel $xmlobj output.$item "String" counters
532            }
533            histogram* - curve* - field* {
534                _autoLabel $xmlobj output.$item "Plot" counters
535            }
536            structure* {
537                _autoLabel $xmlobj output.$item "Structure" counters
538            }
539            table* {
540                _autoLabel $xmlobj output.$item "Energy Levels" counters
541            }
542            sequence* {
543                _autoLabel $xmlobj output.$item "Sequence" counters
544            }
545        }
546        set label [$xmlobj get output.$item.about.group]
547        if {"" == $label} {
548            set label [$xmlobj get output.$item.about.label]
549        }
550
551        set hidden [$xmlobj get output.$item.hide]
552        set hidden [expr {"" != $hidden && $hidden}]
553
554        if {"" != $label && !$hidden} {
555            set haveresults 1
556        }
557    }
558
559    # if there are any valid results, add them to the resultset
560    if {$haveresults} {
561        set index [$itk_component(resultset) add $xmlobj]
562
563        # add each result to a result viewer
564        foreach item [_reorder [$xmlobj children output]] {
565            set label [$xmlobj get output.$item.about.group]
566            if {"" == $label} {
567                set label [$xmlobj get output.$item.about.label]
568            }
569
570            set hidden [$xmlobj get output.$item.hide]
571            set hidden [expr {"" != $hidden && $hidden}]
572
573            if {"" != $label && !$hidden} {
574                if {![info exists _label2page($label)]} {
575                    set name "page[incr _pages]"
576                    set page [$itk_component(resultpages) insert end $name]
577                    set _label2page($label) $page
578                    set _label2desc($label) \
579                        [$xmlobj get output.$item.about.description]
580                    Rappture::ResultViewer $page.rviewer
581                    pack $page.rviewer -expand yes -fill both -pady 4
582
583                    set end [$itk_component(resultselector) \
584                        choices index -value ---]
585                    if {$end < 0} {
586                        set end "end"
587                    }
588                    $itk_component(resultselector) choices insert $end \
589                        $name $label
590                }
591
592                # add/replace the latest result into this viewer
593                set page $_label2page($label)
594
595                if {![info exists reset($page)]} {
596                    $page.rviewer clear $index
597                    set reset($page) 1
598                }
599                $page.rviewer add $index $xmlobj output.$item
600            }
601        }
602    }
603
604    # show the first page by default
605    set max [$itk_component(resultselector) choices size]
606    for {set i 0} {$i < $max} {incr i} {
607        set first [$itk_component(resultselector) choices get -label $i]
608        if {$first != ""} {
609            set page [$itk_component(resultselector) choices get -value $i]
610            set char [string index $page 0]
611            if {$char != "@" && $char != "-"} {
612                $itk_component(resultpages) current $page
613                $itk_component(resultselector) value $first
614                set _lastlabel $first
615                break
616            }
617        }
618    }
619}
620
621# ----------------------------------------------------------------------
622# USAGE: clear
623#
624# Discards all results previously loaded into the analyzer.
625# ----------------------------------------------------------------------
626itcl::body Rappture::Analyzer::clear {} {
627    foreach obj $_runs {
628        itcl::delete object $obj
629    }
630    set _runs ""
631
632    $itk_component(resultset) clear
633
634    # reset the size of the controls area
635    set ht [winfo height $itk_component(results)]
636    set cntlht [$itk_component(resultset) size -controlarea]
637    set frac [expr {double($cntlht)/$ht}]
638    $itk_component(results) fraction end $frac
639
640    foreach label [array names _label2page] {
641        set page $_label2page($label)
642        $page.rviewer clear
643    }
644    $itk_component(resultselector) value ""
645    $itk_component(resultselector) choices delete 0 end
646    catch {unset _label2page}
647    catch {unset _label2desc}
648    set _plotlist ""
649
650    $itk_component(resultselector) choices insert end --- "---"
651    $itk_component(resultselector) choices insert end \
652        @download [Rappture::filexfer::label download]
653    set _lastlabel ""
654
655    #
656    # HACK ALERT!!
657    # The following statement should be in place, but it causes
658    # vtk to dump core.  Leave it out until we can fix the core dump.
659    # In the mean time, we leak memory...
660    #
661    #$itk_component(resultpages) delete -all
662    #set _pages 0
663
664    _simState on
665    _fixSimControl
666    reset
667}
668
669# ----------------------------------------------------------------------
670# USAGE: download coming
671# USAGE: download controls <downloadCommand>
672# USAGE: download start ?widget?
673# USAGE: download now ?widget?
674#
675# Spools the current result so the user can download it.
676# ----------------------------------------------------------------------
677itcl::body Rappture::Analyzer::download {option args} {
678    set title [$itk_component(resultselector) value]
679    set page [$itk_component(resultselector) translate $title]
680
681    switch -- $option {
682        coming {
683            #
684            # Warn result that a download is coming, in case
685            # it needs to take a screen snap.
686            #
687            if {![regexp {^(|@download|---)$} $page]} {
688                set f [$itk_component(resultpages) page $page]
689                $f.rviewer download coming
690            }
691        }
692        controls {
693            # no controls for this download yet
694            return ""
695        }
696        start {
697            set widget $itk_component(download)
698            if {[llength $args] > 0} {
699                set widget [lindex $args 0]
700                if {[catch {winfo class $widget}]} {
701                    set widget $itk_component(download)
702                }
703            }
704            #
705            # See if this download has any controls.  If so, then
706            # post them now and let the user continue the download
707            # after selecting a file format.
708            #
709            if {$page != ""} {
710                set ext ""
711                set f [$itk_component(resultpages) page $page]
712                set popup [$f.rviewer download controls \
713                    [itcl::code $this download now $widget]]
714
715                if {"" != $popup} {
716                    $popup activate $widget below
717                } else {
718                    download now $widget
719                }
720            } else {
721                # this shouldn't happen
722                set file error.html
723                set data "<h1>Not Found</h1>There is no result selected."
724            }
725        }
726        now {
727            set widget $itk_component(download)
728            if {[llength $args] > 0} {
729                set widget [lindex $args 0]
730                if {[catch {winfo class $widget}]} {
731                    set widget $itk_component(download)
732                }
733            }
734            #
735            # Perform the actual download.
736            #
737            if {$page != ""} {
738                set ext ""
739                set f [$itk_component(resultpages) page $page]
740                set code [catch { $f.rviewer download now $widget } result]
741                if { $code == 0 }  {
742                    foreach {ext data} $result break
743                    if {"" == $ext} {
744                        if {"" != $widget} {
745                            Rappture::Tooltip::cue $widget \
746                                "Can't download this result."
747                        }
748                        return
749                    }
750                    regsub -all {[\ -\/\:-\@\{-\~]} $title {} title
751                    set file "$title$ext"
752                } else {
753                    if { $code != 3 } {
754                        if {"" != $widget} {
755                            Rappture::Tooltip::cue $widget \
756                                "Error downloading this result: $result"
757                        }
758                    }
759                    return
760                }
761            } else {
762                # this shouldn't happen
763                set file error.html
764                set data "<h1>Not Found</h1>There is no result selected."
765            }
766
767            set mesg [Rappture::filexfer::download $data $file]
768            if {[string length $mesg] > 0} {
769                Rappture::Tooltip::cue $widget $mesg
770            }
771        }
772        default {
773            error "bad option \"$option\": should be coming, controls, now, start"
774        }
775    }
776}
777
778# ----------------------------------------------------------------------
779# USAGE: _plot ?<index> <options> <index> <options>...?
780#
781# Used internally to update the plot shown in the current result
782# viewer whenever the resultset settings have changed.  Causes the
783# desired results to show up on screen.
784# ----------------------------------------------------------------------
785itcl::body Rappture::Analyzer::_plot {args} {
786    set _plotlist $args
787
788    set page [$itk_component(resultselector) value]
789    set page [$itk_component(resultselector) translate $page]
790    if {"" != $page} {
791        set f [$itk_component(resultpages) page $page]
792        $f.rviewer plot clear
793        foreach {index opts} $_plotlist {
794            $f.rviewer plot add $index $opts
795        }
796    }
797}
798
799# ----------------------------------------------------------------------
800# USAGE: _reorder <compList>
801#
802# Used internally to change the order of a series of output components
803# found in the <output> section.  Moves the <log> elements to the end
804# and returns the updated list.
805# ----------------------------------------------------------------------
806itcl::body Rappture::Analyzer::_reorder {comps} {
807    set i 0
808    set max [llength $comps]
809    while {$i < $max} {
810        set c [lindex $comps $i]
811        if {[string match log* $c]} {
812            set comps [lreplace $comps $i $i]
813            lappend comps $c
814            incr max -1
815        } else {
816            incr i
817        }
818    }
819    return $comps
820}
821
822# ----------------------------------------------------------------------
823# USAGE: _autoLabel <xmlobj> <path> <title> <cntVar>
824#
825# Used internally to check for an about.label property at the <path>
826# in <xmlobj>.  If this object doesn't have a label, then one is
827# supplied using the given <title>.  The <cntVar> is an array of
828# counters in the calling scopes for titles that have been used
829# in the past.  This is used to create titles like "Plot #2" the
830# second time it is encountered.
831#
832# The <xmlobj> is updated so that the label is inserted directly in
833# the tree.
834# ----------------------------------------------------------------------
835itcl::body Rappture::Analyzer::_autoLabel {xmlobj path title cntVar} {
836    upvar $cntVar counters
837
838    set group [$xmlobj get $path.about.group]
839    set label [$xmlobj get $path.about.label]
840    if {"" == $label} {
841        # no label -- make one up using the title specified
842        if {![info exists counters($group-$title)]} {
843            set counters($group-$title) 1
844            set label $title
845        } else {
846            set label "$title (#[incr counters($group-$title)])"
847        }
848        $xmlobj put $path.about.label $label
849    } else {
850        # handle the case of two identical labels in <output>
851        if {![info exists counters($group-$label)]} {
852            set counters($group-$label) 1
853        } else {
854            set label "$label (#[incr counters($group-$label)])"
855            $xmlobj put $path.about.label $label
856        }
857    }
858    return $label
859}
860
861# ----------------------------------------------------------------------
862# USAGE: _fixResult
863#
864# Used internally to change the result page being displayed whenever
865# the user selects a page from the results combobox.
866# ----------------------------------------------------------------------
867itcl::body Rappture::Analyzer::_fixResult {} {
868    set name [$itk_component(resultselector) value]
869    set page ""
870    if {"" != $name} {
871        set page [$itk_component(resultselector) translate $name]
872    }
873    if {$page == "@download"} {
874        # put the combobox back to its last value
875        $itk_component(resultselector) component entry configure -state normal
876        $itk_component(resultselector) component entry delete 0 end
877        $itk_component(resultselector) component entry insert end $_lastlabel
878        $itk_component(resultselector) component entry configure -state disabled
879        # perform the actual download
880        download start $itk_component(resultselector)
881    } elseif {$page == "---"} {
882        # put the combobox back to its last value
883        $itk_component(resultselector) component entry configure -state normal
884        $itk_component(resultselector) component entry delete 0 end
885        $itk_component(resultselector) component entry insert end $_lastlabel
886        $itk_component(resultselector) component entry configure -state disabled
887    } elseif {$page != ""} {
888        set _lastlabel $name
889        set win [winfo toplevel $itk_component(hull)]
890        blt::busy hold $win; update idletasks
891        $itk_component(resultpages) current $page
892
893        set f [$itk_component(resultpages) page $page]
894        $f.rviewer plot clear
895        eval $f.rviewer plot add $_plotlist
896        blt::busy release [winfo toplevel $itk_component(hull)]
897    }
898}
899
900# ----------------------------------------------------------------------
901# USAGE: _fixSize
902#
903# Used internally to change the size of the result set area whenever
904# a new control appears.  Adjusts the size available for the result
905# set up to some maximum.
906# ----------------------------------------------------------------------
907itcl::body Rappture::Analyzer::_fixSize {} {
908    set ht [winfo height $itk_component(results)]
909    if {$ht <= 1} { set ht [winfo reqheight $itk_component(results)] }
910    set cntlht [$itk_component(resultset) size -controlarea]
911    set frac [expr {double($cntlht)/$ht}]
912
913    if {$frac < 0.4} {
914        $itk_component(results) fraction end $frac
915    }
916    _fixSimControl
917}
918
919# ----------------------------------------------------------------------
920# USAGE: _simState <boolean> ?<message>? ?<settings>?
921#
922# Used internally to change the "Simulation" button on or off.
923# If the <boolean> is on, then any <message> and <settings> are
924# displayed as well.  If the <boolean> is off, then only display
925# the message. The <message> is a note to the user about
926# what will be simulated, and the <settings> are a list of
927# tool parameter settings of the form {path1 val1 path2 val2 ...}.
928# When these are in place, the next Simulate operation will use
929# these settings.  This helps fill in missing data values.
930# ----------------------------------------------------------------------
931itcl::body Rappture::Analyzer::_simState {state args} {
932    if {$state} {
933        $itk_interior.simol configure \
934            -background $itk_option(-simcontrolactiveoutline)
935        configure -simcontrolcolor $itk_option(-simcontrolactivebackground)
936
937        $itk_component(abort) configure -state disabled
938        $itk_component(simulate) configure -state normal \
939            -command [itcl::code $this simulate]
940
941        #
942        # If there's a special message, then put it up next to the button.
943        #
944        set mesg [lindex $args 0]
945        if {"" != $mesg} {
946            $itk_component(simstatus) configure -state normal
947            $itk_component(simstatus) delete 1.0 end
948            $itk_component(simstatus) insert end $mesg
949
950            #
951            # If there are any settings, then install them in the
952            # "Simulate" button.  Also, pop them up as a tooltip
953            # for the message.
954            #
955            set settings [lindex $args 1]
956            if {[llength $settings] > 0} {
957                $itk_component(simulate) configure \
958                    -command [eval itcl::code $this simulate $settings]
959
960                set details ""
961                foreach {path val} $settings {
962                    set str [$_tool xml get $path.about.label]
963                    if {"" == $str} {
964                        set str [$_tool xml element -as id $path]
965                    }
966                    append details "$str = $val\n"
967                }
968                set details [string trim $details]
969
970                Rappture::Tooltip::for $itk_component(simstatus) $details
971                $itk_component(simstatus) insert end " "
972                $itk_component(simstatus) insert end "(details...)" popup
973            }
974            $itk_component(simstatus) configure -state disabled
975        }
976    } else {
977        if {"" != $itk_option(-simcontrolbackground)} {
978            set simcbg $itk_option(-simcontrolbackground)
979        } else {
980            set simcbg $itk_option(-background)
981        }
982        $itk_interior.simol configure \
983            -background $itk_option(-simcontroloutline)
984        configure -simcontrolcolor $simcbg
985
986        $itk_component(simulate) configure -state disabled
987        $itk_component(abort) configure -state normal
988
989        $itk_component(simstatus) configure -state normal
990        $itk_component(simstatus) delete 1.0 end
991        set mesg [lindex $args 0]
992        if {"" != $mesg} {
993            $itk_component(simstatus) insert end $mesg
994        }
995        $itk_component(simstatus) configure -state disabled
996    }
997}
998
999# ----------------------------------------------------------------------
1000# USAGE: _simOutput <message>
1001#
1002# Invoked automatically whenever output comes in while running the
1003# tool.  Extracts any =RAPPTURE-???=> messages from the output and
1004# sends the output to the display.  For example, any
1005# =RAPPTURE-PROGRESS=> message pops up the progress meter and updates
1006# it to show the latest progress message.  This is useful for
1007# long-running tools, to let the user know how much longer the
1008# simulation will take.
1009# ----------------------------------------------------------------------
1010itcl::body Rappture::Analyzer::_simOutput {message} {
1011    #
1012    # Scan through and pick out any =RAPPTURE-PROGRESS=> messages first.
1013    #
1014    while {[regexp -indices \
1015               {=RAPPTURE-PROGRESS=> *([-+]?[0-9]+) +([^\n]*)(\n|$)} $message \
1016                match percent mesg]} {
1017
1018        foreach {i0 i1} $percent break
1019        set percent [string range $message $i0 $i1]
1020
1021        foreach {i0 i1} $mesg break
1022        set mesg [string range $message $i0 $i1]
1023
1024        pack $itk_component(progress) -fill x -padx 10 -pady 10
1025        $itk_component(progress) settings -percent $percent -message $mesg
1026
1027        foreach {i0 i1} $match break
1028        set message [string replace $message $i0 $i1]
1029    }
1030
1031    #
1032    # Break up the remaining lines according to =RAPPTURE-ERROR=> messages.
1033    # Show errors in a special color.
1034    #
1035    $itk_component(runinfo) configure -state normal
1036
1037    while {[regexp -indices \
1038               {=RAPPTURE-([a-zA-Z]+)=>([^\n]*)(\n|$)} $message \
1039                match type mesg]} {
1040
1041        foreach {i0 i1} $match break
1042        set first [string range $message 0 [expr {$i0-1}]]
1043        if {[string length $first] > 0} {
1044            $itk_component(runinfo) insert end $first
1045            $itk_component(runinfo) insert end \n
1046        }
1047
1048        foreach {t0 t1} $type break
1049        set type [string range $message $t0 $t1]
1050        foreach {m0 m1} $mesg break
1051        set mesg [string range $message $m0 $m1]
1052        if {[string length $mesg] > 0 && $type != "RUN"} {
1053            $itk_component(runinfo) insert end $mesg $type
1054            $itk_component(runinfo) insert end \n $type
1055        }
1056
1057        set message [string range $message [expr {$i1+1}] end]
1058    }
1059
1060    if {[string length $message] > 0} {
1061        $itk_component(runinfo) insert end $message
1062        if {[$itk_component(runinfo) get end-2char] != "\n"} {
1063            $itk_component(runinfo) insert end "\n"
1064        }
1065        $itk_component(runinfo) see end
1066    }
1067    $itk_component(runinfo) configure -state disabled
1068}
1069
1070# ----------------------------------------------------------------------
1071# USAGE: _resultTooltip
1072#
1073# Used internally to build the tooltip string displayed for the
1074# result selector.  If the current page has an associated description,
1075# then it is displayed beneath the result.
1076#
1077# Returns the string for the tooltip.
1078# ----------------------------------------------------------------------
1079itcl::body Rappture::Analyzer::_resultTooltip {} {
1080    set tip ""
1081    set name [$itk_component(resultselector) value]
1082    if {[info exists _label2desc($name)] &&
1083         [string length $_label2desc($name)] > 0} {
1084        append tip "$_label2desc($name)\n\n"
1085    }
1086    if {[array size _label2page] > 1} {
1087        append tip "Use this control to display other output results."
1088    }
1089    return $tip
1090}
1091
1092# ----------------------------------------------------------------------
1093# USAGE: _fixSimControl
1094#
1095# Used internally to add or remove the simulation control at the
1096# top of the analysis area.  This is controlled by the -simcontrol
1097# option.
1098# ----------------------------------------------------------------------
1099itcl::body Rappture::Analyzer::_fixSimControl {} {
1100    switch -- $itk_option(-simcontrol) {
1101        on {
1102            pack $itk_interior.simol -fill x -before $itk_interior.nb
1103        }
1104        off {
1105            pack forget $itk_interior.simol
1106        }
1107        auto {
1108            #
1109            # If we have two or more radiodials, then there is a
1110            # chance of encountering a combination of parameters
1111            # with no data, requiring simulation.
1112            #
1113            if {[$itk_component(resultset) size -controls] >= 2} {
1114                pack $itk_interior.simol -fill x -before $itk_interior.nb
1115            } else {
1116                pack forget $itk_interior.simol
1117            }
1118        }
1119        default {
1120            error "bad value \"$itk_option(-simcontrol)\": should be on, off, auto"
1121        }
1122    }
1123}
1124
1125# ----------------------------------------------------------------------
1126# USAGE: _fixNotebook
1127#
1128# Used internally to switch the active notebook page
1129# ----------------------------------------------------------------------
1130itcl::body Rappture::Analyzer::_fixNotebook {} {
1131    switch -- $itk_option(-notebookpage) {
1132        about {
1133            $itk_component(notebook) current about
1134        }
1135        simulate {
1136            $itk_component(notebook) current simulate
1137        }
1138        analyze {
1139            $itk_component(notebook) current analyze
1140        }
1141        default {
1142            error "bad value \"$itk_option(-notebookpage)\": should be about, simulate, analyze"
1143        }
1144    }
1145}
1146
1147# ----------------------------------------------------------------------
1148# CONFIGURATION OPTION: -simcontrol
1149#
1150# Controls whether or not the Simulate button is showing.  In some
1151# cases, it is not needed.
1152# ----------------------------------------------------------------------
1153itcl::configbody Rappture::Analyzer::simcontrol {
1154    _fixSimControl
1155}
1156
1157# ----------------------------------------------------------------------
1158# CONFIGURATION OPTION: -notebookpage
1159#
1160# Controls which page of the analyzer notebook is shown. It is
1161# particularly needed when using rerun, when you don't want to
1162# "simulate -ifneeded" because an actual simulation might be
1163# kicked off due to differences between tool.xml and run.xml
1164# ----------------------------------------------------------------------
1165itcl::configbody Rappture::Analyzer::notebookpage {
1166    _fixNotebook
1167}
Note: See TracBrowser for help on using the repository browser.