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

Last change on this file since 3330 was 3330, checked in by gah, 11 years ago

merge (by hand) with Rappture1.2 branch

File size: 9.2 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: ValueResult - Log output for ResultSet
4#
5#  This widget is used to show text output in a ResultSet.  The log
6#  output from a tool, for example, is rendered as a ValueResult.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2012  HUBzero Foundation, LLC
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16
17option add *ValueResult.font \
18    -*-helvetica-medium-r-normal-*-12-* widgetDefault
19
20itcl::class Rappture::ValueResult {
21    inherit itk::Widget
22
23    constructor {args} { # defined below }
24
25    public method add {dataobj {settings ""}}
26    public method get {}
27    public method delete {args}
28    public method scale {args}
29    public method parameters {title args} { # do nothing }
30    public method download {option args}
31
32    protected method _rebuild {}
33
34    private variable _dispatcher "" ;# dispatcher for !events
35
36    private variable _dlist ""    ;# list of data objects being displayed
37    private variable _dobj2color  ;# maps data object => color
38    private variable _dobj2raise  ;# maps data object => raise flag 0/1
39    private variable _dobj2desc   ;# maps data object => description
40}
41                                                                               
42itk::usual ValueResult {
43    keep -background -foreground -cursor -font
44}
45
46# ----------------------------------------------------------------------
47# CONSTRUCTOR
48# ----------------------------------------------------------------------
49itcl::body Rappture::ValueResult::constructor {args} {
50    Rappture::dispatcher _dispatcher
51    $_dispatcher register !rebuild
52    $_dispatcher dispatch $this !rebuild "[itcl::code $this _rebuild]; list"
53
54    itk_component add scroller {
55        Rappture::Scroller $itk_interior.scroller \
56            -xscrollmode auto -yscrollmode auto
57    }
58    pack $itk_component(scroller) -expand yes -fill both
59
60    itk_component add html {
61        Rappture::HTMLviewer $itk_component(scroller).html
62    }
63    $itk_component(scroller) contents $itk_component(html)
64
65    eval itk_initialize $args
66}
67
68# ----------------------------------------------------------------------
69# USAGE: add <dataobj> ?<settings>?
70#
71# Clients use this to add a data object to the plot.  If the optional
72# <settings> are specified, then the are applied to the data.  Allowed
73# settings are -color and -brightness, -width, -linestyle and -raise.
74# (Many of these are ignored.)
75# ----------------------------------------------------------------------
76itcl::body Rappture::ValueResult::add {dataobj {settings ""}} {
77    array set params {
78        -color ""
79        -brightness 0
80        -width ""
81        -linestyle ""
82        -raise 0
83        -description ""
84        -param ""
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.