source: trunk/gui/scripts/valueresult.tcl @ 1837

Last change on this file since 1837 was 1837, checked in by gah, 14 years ago

add -tkwait flag to molvisviewer addmethod

File size: 8.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: ValueResult - Log output for ResultSet
3#
4#  This widget is used to show text output in a ResultSet.  The log
5#  output from a tool, for example, is rendered as a ValueResult.
6# ======================================================================
7#  AUTHOR:  Michael McLennan, Purdue University
8#  Copyright (c) 2004-2005  Purdue Research Foundation
9#
10#  See the file "license.terms" for information on usage and
11#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12# ======================================================================
13package require Itk
14package require BLT
15
16option add *ValueResult.font \
17    -*-helvetica-medium-r-normal-*-12-* widgetDefault
18
19itcl::class Rappture::ValueResult {
20    inherit itk::Widget
21
22    constructor {args} { # defined below }
23
24    public method add {dataobj {settings ""}}
25    public method get {}
26    public method delete {args}
27    public method scale {args}
28    public method parameters {title args} { # do nothing }
29    public method download {option args}
30
31    protected method _rebuild {}
32
33    private variable _dispatcher "" ;# dispatcher for !events
34
35    private variable _dlist ""    ;# list of data objects being displayed
36    private variable _dobj2color  ;# maps data object => color
37    private variable _dobj2raise  ;# maps data object => raise flag 0/1
38    private variable _dobj2desc   ;# maps data object => description
39}
40                                                                               
41itk::usual ValueResult {
42    keep -background -foreground -cursor -font
43}
44
45# ----------------------------------------------------------------------
46# CONSTRUCTOR
47# ----------------------------------------------------------------------
48itcl::body Rappture::ValueResult::constructor {args} {
49    Rappture::dispatcher _dispatcher
50    $_dispatcher register !rebuild
51    $_dispatcher dispatch $this !rebuild "[itcl::code $this _rebuild]; list"
52
53    itk_component add scroller {
54        Rappture::Scroller $itk_interior.scroller \
55            -xscrollmode auto -yscrollmode auto
56    }
57    pack $itk_component(scroller) -expand yes -fill both
58
59    itk_component add html {
60        Rappture::HTMLviewer $itk_component(scroller).html
61    }
62    $itk_component(scroller) contents $itk_component(html)
63
64    eval itk_initialize $args
65}
66
67# ----------------------------------------------------------------------
68# USAGE: add <dataobj> ?<settings>?
69#
70# Clients use this to add a data object to the plot.  If the optional
71# <settings> are specified, then the are applied to the data.  Allowed
72# settings are -color and -brightness, -width, -linestyle and -raise.
73# (Many of these are ignored.)
74# ----------------------------------------------------------------------
75itcl::body Rappture::ValueResult::add {dataobj {settings ""}} {
76    array set params {
77        -color ""
78        -brightness 0
79        -width ""
80        -linestyle ""
81        -raise 0
82        -description ""
83        -param ""
84        -tkwait no
85    }
86    foreach {opt val} $settings {
87        if {![info exists params($opt)]} {
88            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
89        }
90        set params($opt) $val
91    }
92    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
93        # can't handle -autocolors yet
94        set params(-color) black
95    }
96
97    if {"" != $dataobj} {
98        # find the value and assign it with the proper coloring
99        if {"" != $params(-color) && "" != $params(-brightness)
100              && $params(-brightness) != 0} {
101            set params(-color) [Rappture::color::brightness \
102                $params(-color) $params(-brightness)]
103        }
104
105        set pos [lsearch -exact $dataobj $_dlist]
106        if {$pos < 0} {
107            lappend _dlist $dataobj
108            set _dobj2color($dataobj) $params(-color)
109            set _dobj2raise($dataobj) $params(-raise)
110            set _dobj2desc($dataobj) $params(-description)
111            $_dispatcher event -idle !rebuild
112        }
113    }
114}
115
116# ----------------------------------------------------------------------
117# USAGE: get
118#
119# Clients use this to query the list of objects being plotted, in
120# order from bottom to top of this result.
121# ----------------------------------------------------------------------
122itcl::body Rappture::ValueResult::get {} {
123    # put the dataobj list in order according to -raise options
124    set dlist $_dlist
125    foreach obj $dlist {
126        if {[info exists _dobj2raise($obj)] && $_dobj2raise($obj)} {
127            set i [lsearch -exact $dlist $obj]
128            if {$i >= 0} {
129                set dlist [lreplace $dlist $i $i]
130                lappend dlist $obj
131            }
132        }
133    }
134    return $dlist
135}
136
137# ----------------------------------------------------------------------
138# USAGE: delete ?<curve1> <curve2> ...?
139#
140# Clients use this to delete a curve from the plot.  If no curves
141# are specified, then all curves are deleted.
142# ----------------------------------------------------------------------
143itcl::body Rappture::ValueResult::delete {args} {
144    if {[llength $args] == 0} {
145        set args $_dlist
146    }
147
148    # delete all specified objects
149    set changed 0
150    foreach obj $args {
151        set pos [lsearch -exact $_dlist $obj]
152        if {$pos >= 0} {
153            set _dlist [lreplace $_dlist $pos $pos]
154            catch {unset _dobj2color($obj)}
155            catch {unset _dobj2raise($obj)}
156            catch {unset _dobj2desc($obj)}
157            set changed 1
158        }
159    }
160
161    # if anything changed, then rebuild the plot
162    if {$changed} {
163        $_dispatcher event -idle !rebuild
164    }
165}
166
167# ----------------------------------------------------------------------
168# USAGE: scale ?<curve1> <curve2> ...?
169#
170# Sets the default limits for the overall plot according to the
171# limits of the data for all of the given <curve> objects.  This
172# accounts for all curves--even those not showing on the screen.
173# Because of this, the limits are appropriate for all curves as
174# the user scans through data in the ResultSet viewer.
175# ----------------------------------------------------------------------
176itcl::body Rappture::ValueResult::scale {args} {
177    # nothing to do for values
178}
179
180# ----------------------------------------------------------------------
181# USAGE: download coming
182# USAGE: download controls <downloadCommand>
183# USAGE: download now
184#
185# Clients use this method to create a downloadable representation
186# of the plot.  Returns a list of the form {ext string}, where
187# "ext" is the file extension (indicating the type of data) and
188# "string" is the data itself.
189# ----------------------------------------------------------------------
190itcl::body Rappture::ValueResult::download {option args} {
191    switch $option {
192        coming {
193            # nothing to do
194        }
195        controls {
196            # no controls for this download yet
197            return ""
198        }
199        now {
200            if {[llength $_dlist] == 1} {
201                set lstr [$_dlist get about.label]
202                set mesg "$lstr [$_dlist get current]"
203            } else {
204                set mesg ""
205                foreach obj $_dlist {
206                    set lstr [$obj get about.label]
207                    append mesg "$lstr [$obj get current]\n"
208                    if {[string length $_dobj2desc($obj)] > 0} {
209                        foreach line [split $_dobj2desc($obj) \n] {
210                            append mesg " * $line\n"
211                        }
212                        append mesg "\n"
213                    }
214                }
215            }
216            return [list .txt $mesg]
217        }
218        default {
219            error "bad option \"$option\": should be coming, controls, now"
220        }
221    }
222}
223
224# ----------------------------------------------------------------------
225# USAGE: _rebuild
226#
227# Used internally to rebuild the contents of this widget
228# whenever the data within it changes.  Shows the value
229# for the topmost data object in its associated color.
230# ----------------------------------------------------------------------
231itcl::body Rappture::ValueResult::_rebuild {} {
232    set html "<html><body>"
233
234    set obj [lindex $_dlist 0]
235    if {"" != $obj} {
236        set label [$obj get about.label]
237        if {"" != $label && [string index $label end] != ":"} {
238            append label ":"
239        }
240        append html "<h3>$label</h3>\n"
241    }
242
243    foreach obj $_dlist {
244        if {$_dobj2raise($obj)} {
245            set bold0 "<b>"
246            set bold1 "</b>"
247            set bg "background:#ffffcc; border:1px solid #cccccc;"
248        } else {
249            set bold0 ""
250            set bold1 ""
251            set bg ""
252        }
253        if {$_dobj2color($obj) != ""} {
254            set color0 "<font style=\"color: $_dobj2color($obj)\">"
255            set color1 "</font>"
256        } else {
257            set color0 ""
258            set color1 ""
259        }
260
261        append html "<div style=\"margin:8px; padding:4px; $bg\">${bold0}${color0}[$obj get current]${color1}${bold1}"
262        if {$_dobj2raise($obj) && [string length $_dobj2desc($obj)] > 0} {
263            foreach line [split $_dobj2desc($obj) \n] {
264                append html "<li style=\"margin-left:12px;\">$line</li>\n"
265            }
266        }
267        append html "</div>"
268    }
269    append html "</body></html>"
270    $itk_component(html) load $html
271}
Note: See TracBrowser for help on using the repository browser.