source: branches/blt4/gui/scripts/valueresult.tcl @ 1676

Last change on this file since 1676 was 1651, checked in by gah, 15 years ago
File size: 8.4 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        blt::scrollset $itk_interior.scroller \
55            -xscrollbar $itk_interior.scroller.xs \
56            -yscrollbar $itk_interior.scroller.ys \
57            -window $itk_interior.scroller.html
58    }
59    blt::tk::scrollbar $itk_interior.scroller.xs
60    blt::tk::scrollbar $itk_interior.scroller.ys
61    pack $itk_component(scroller) -expand yes -fill both
62
63    itk_component add html {
64        Rappture::HTMLviewer $itk_component(scroller).html
65    }
66
67    eval itk_initialize $args
68}
69
70# ----------------------------------------------------------------------
71# USAGE: add <dataobj> ?<settings>?
72#
73# Clients use this to add a data object to the plot.  If the optional
74# <settings> are specified, then the are applied to the data.  Allowed
75# settings are -color and -brightness, -width, -linestyle and -raise.
76# (Many of these are ignored.)
77# ----------------------------------------------------------------------
78itcl::body Rappture::ValueResult::add {dataobj {settings ""}} {
79    array set params {
80        -color ""
81        -brightness 0
82        -width ""
83        -linestyle ""
84        -raise 0
85        -description ""
86        -param ""
87    }
88    foreach {opt val} $settings {
89        if {![info exists params($opt)]} {
90            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
91        }
92        set params($opt) $val
93    }
94    if {$params(-color) == "auto" || $params(-color) == "autoreset"} {
95        # can't handle -autocolors yet
96        set params(-color) black
97    }
98
99    if {"" != $dataobj} {
100        # find the value and assign it with the proper coloring
101        if {"" != $params(-color) && "" != $params(-brightness)
102              && $params(-brightness) != 0} {
103            set params(-color) [Rappture::color::brightness \
104                $params(-color) $params(-brightness)]
105        }
106
107        set pos [lsearch -exact $dataobj $_dlist]
108        if {$pos < 0} {
109            lappend _dlist $dataobj
110            set _dobj2color($dataobj) $params(-color)
111            set _dobj2raise($dataobj) $params(-raise)
112            set _dobj2desc($dataobj) $params(-description)
113            $_dispatcher event -idle !rebuild
114        }
115    }
116}
117
118# ----------------------------------------------------------------------
119# USAGE: get
120#
121# Clients use this to query the list of objects being plotted, in
122# order from bottom to top of this result.
123# ----------------------------------------------------------------------
124itcl::body Rappture::ValueResult::get {} {
125    # put the dataobj list in order according to -raise options
126    set dlist $_dlist
127    foreach obj $dlist {
128        if {[info exists _dobj2raise($obj)] && $_dobj2raise($obj)} {
129            set i [lsearch -exact $dlist $obj]
130            if {$i >= 0} {
131                set dlist [lreplace $dlist $i $i]
132                lappend dlist $obj
133            }
134        }
135    }
136    return $dlist
137}
138
139# ----------------------------------------------------------------------
140# USAGE: delete ?<curve1> <curve2> ...?
141#
142# Clients use this to delete a curve from the plot.  If no curves
143# are specified, then all curves are deleted.
144# ----------------------------------------------------------------------
145itcl::body Rappture::ValueResult::delete {args} {
146    if {[llength $args] == 0} {
147        set args $_dlist
148    }
149
150    # delete all specified objects
151    set changed 0
152    foreach obj $args {
153        set pos [lsearch -exact $_dlist $obj]
154        if {$pos >= 0} {
155            set _dlist [lreplace $_dlist $pos $pos]
156            catch {unset _dobj2color($obj)}
157            catch {unset _dobj2raise($obj)}
158            catch {unset _dobj2desc($obj)}
159            set changed 1
160        }
161    }
162
163    # if anything changed, then rebuild the plot
164    if {$changed} {
165        $_dispatcher event -idle !rebuild
166    }
167}
168
169# ----------------------------------------------------------------------
170# USAGE: scale ?<curve1> <curve2> ...?
171#
172# Sets the default limits for the overall plot according to the
173# limits of the data for all of the given <curve> objects.  This
174# accounts for all curves--even those not showing on the screen.
175# Because of this, the limits are appropriate for all curves as
176# the user scans through data in the ResultSet viewer.
177# ----------------------------------------------------------------------
178itcl::body Rappture::ValueResult::scale {args} {
179    # nothing to do for values
180}
181
182# ----------------------------------------------------------------------
183# USAGE: download coming
184# USAGE: download controls <downloadCommand>
185# USAGE: download now
186#
187# Clients use this method to create a downloadable representation
188# of the plot.  Returns a list of the form {ext string}, where
189# "ext" is the file extension (indicating the type of data) and
190# "string" is the data itself.
191# ----------------------------------------------------------------------
192itcl::body Rappture::ValueResult::download {option args} {
193    switch $option {
194        coming {
195            # nothing to do
196        }
197        controls {
198            # no controls for this download yet
199            return ""
200        }
201        now {
202            if {[llength $_dlist] == 1} {
203                set lstr [$_dlist get about.label]
204                set mesg "$lstr [$_dlist get current]"
205            } else {
206                set mesg ""
207                foreach obj $_dlist {
208                    set lstr [$obj get about.label]
209                    append mesg "$lstr [$obj get current]\n"
210                    if {[string length $_dobj2desc($obj)] > 0} {
211                        foreach line [split $_dobj2desc($obj) \n] {
212                            append mesg " * $line\n"
213                        }
214                        append mesg "\n"
215                    }
216                }
217            }
218            return [list .txt $mesg]
219        }
220        default {
221            error "bad option \"$option\": should be coming, controls, now"
222        }
223    }
224}
225
226# ----------------------------------------------------------------------
227# USAGE: _rebuild
228#
229# Used internally to rebuild the contents of this widget
230# whenever the data within it changes.  Shows the value
231# for the topmost data object in its associated color.
232# ----------------------------------------------------------------------
233itcl::body Rappture::ValueResult::_rebuild {} {
234    set html "<html><body>"
235
236    set obj [lindex $_dlist 0]
237    if {"" != $obj} {
238        set label [$obj get about.label]
239        if {"" != $label && [string index $label end] != ":"} {
240            append label ":"
241        }
242        append html "<h3>$label</h3>\n"
243    }
244
245    foreach obj $_dlist {
246        if {$_dobj2raise($obj)} {
247            set bold0 "<b>"
248            set bold1 "</b>"
249            set bg "background:#ffffcc; border:1px solid #cccccc;"
250        } else {
251            set bold0 ""
252            set bold1 ""
253            set bg ""
254        }
255        if {$_dobj2color($obj) != ""} {
256            set color0 "<font style=\"color: $_dobj2color($obj)\">"
257            set color1 "</font>"
258        } else {
259            set color0 ""
260            set color1 ""
261        }
262
263        append html "<div style=\"margin:8px; padding:4px; $bg\">${bold0}${color0}[$obj get current]${color1}${bold1}"
264        if {$_dobj2raise($obj) && [string length $_dobj2desc($obj)] > 0} {
265            foreach line [split $_dobj2desc($obj) \n] {
266                append html "<li style=\"margin-left:12px;\">$line</li>\n"
267            }
268        }
269        append html "</div>"
270    }
271    append html "</body></html>"
272    $itk_component(html) load $html
273}
Note: See TracBrowser for help on using the repository browser.