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

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