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

Last change on this file since 1003 was 1003, checked in by dkearney, 16 years ago

definging _mkdir in tool.tcl and removing it from analyzer because the code that uses it has moved

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