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

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