source: trunk/gui/scripts/sequence.tcl @ 428

Last change on this file since 428 was 428, checked in by mmc, 18 years ago
  • Added <sequence> for playing movie outputs and other sequences of related results.
  • Added <resize> option to <image> elements. This can be used to resize input items to a smaller size, so they don't take up so much real estate on the form.
  • Fixed a bug in right/below cases for popup balloons.
  • Reduced the tooltip delay time to 750ms to interact better with Rick's attention span.
  • Fixed the sash between grips to light up when you touch it, so it's easier to see.
File size: 6.4 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: sequence - represents a sequence of output results
3#
4#  This object represents a sequence of other output results.  Each
5#  element in the sequence has an index and a value.  All values in
6#  the sequence must have the same type, but they can all be curves,
7#  images, or other results.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005  Purdue Research Foundation
11#
12#  See the file "license.terms" for information on usage and
13#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
14# ======================================================================
15package require Itcl
16package require BLT
17
18namespace eval Rappture { # forward declaration }
19
20itcl::class Rappture::Sequence {
21    constructor {xmlobj path} { # defined below }
22    destructor { # defined below }
23
24    public method value {pos}
25    public method index {pos}
26    public method size {}
27    public method hints {{keyword ""}}
28
29    private variable _xmlobj ""  ;# ref to lib obj with sequence data
30    private variable _dataobjs   ;# maps index => data object
31    private variable _indices    ;# list of sorted index values
32    private variable _hints      ;# cache of hints stored in XML
33}
34
35# ----------------------------------------------------------------------
36# CONSTRUCTOR
37# ----------------------------------------------------------------------
38itcl::body Rappture::Sequence::constructor {xmlobj path} {
39    if {![Rappture::library isvalid $xmlobj]} {
40        error "bad value \"$xmlobj\": should be LibraryObj"
41    }
42    set _xmlobj [$xmlobj element -as object $path]
43
44    #
45    # Extract data values from the element definitions.
46    #
47    foreach name [$_xmlobj children -type element] {
48        set index [$xmlobj get $path.$name.index]
49        if {"" == $index} {
50            continue
51        }
52
53        foreach cname [$_xmlobj children $name] {
54            set type [$xmlobj element -as type $path.$name.$cname]
55            switch -- $type {
56                index {
57                    # ignore this
58                }
59                curve {
60                    set obj [Rappture::Curve ::#auto $xmlobj $path.$name.$cname]
61                    break
62                }
63                field {
64                    set obj [Rappture::Field ::#auto $xmlobj $path.$name.$cname]
65                    break
66                }
67                image {
68                    set obj [Rappture::Image ::#auto $xmlobj $path.$name.$cname]
69                    break
70                }
71                default {
72                    error "don't know how to handle sequences of $type"
73                }
74            }
75        }
76        set _dataobjs($index) $obj
77    }
78
79    #
80    # Generate a list of sorted index values.
81    #
82    set units [$xmlobj get path.index.units]
83    if {"" != $units} {
84        # build up a list:  {10m 10} {10cm 0.1} ...
85        set vals ""
86        foreach key [array names _dataobjs] {
87            lappend vals [list $key [Rappture::Units::convert $key \
88                -context $units -to $units -units off]]
89        }
90
91        # sort according to raw values; store both values
92        set _indices [lsort -real -index 1 $vals]
93
94    } else {
95        # are the indices integers, reals, or strings?
96        set how -integer
97        foreach key [array names _dataobjs] {
98            if {[regexp {^[0-9]+[eE][-+]?[0-9]+|([0-9]+)?\.[0-9]+([eE][-+]?[0-9]+)$} $key]} {
99                set how -real
100                break
101            } elseif {![regexp {^[0-9]+$} $key]} {
102                set how -dictionary
103                break
104            }
105        }
106
107        # keep a list of indices sorted in order
108        set _indices ""
109        if {[string equal $how -dictionary]} {
110            set n 0
111            foreach val [lsort $how [array names _dataobjs]] {
112                lappend _indices [list $val $n]
113            }
114        } else {
115            foreach val [lsort $how [array names _dataobjs]] {
116                lappend _indices [list $val $val]
117            }
118        }
119    }
120}
121
122# ----------------------------------------------------------------------
123# DESTRUCTOR
124# ----------------------------------------------------------------------
125itcl::body Rappture::Sequence::destructor {} {
126    foreach key [array names _dataobjs] {
127        itcl::delete object $_dataobjs($key)
128    }
129    itcl::delete object $_xmlobj
130}
131
132# ----------------------------------------------------------------------
133# USAGE: value <pos>
134#
135# Returns the value for the element as position <pos> in the
136# list of all elements.  Here, <pos> runs from 0 to size-1.
137# ----------------------------------------------------------------------
138itcl::body Rappture::Sequence::value {pos} {
139    set i [lindex [lindex $_indices $pos] 0]
140    return $_dataobjs($i)
141}
142
143# ----------------------------------------------------------------------
144# USAGE: index <pos>
145#
146# Returns information about the index value for the element at
147# position <pos> in the list of all elements.  The return value is
148# a list of two elements:  {string rawNumberValue}.  Here, <pos>
149# runs from 0 to size-1.
150# ----------------------------------------------------------------------
151itcl::body Rappture::Sequence::index {pos} {
152    return [lindex $_indices $pos]
153}
154
155# ----------------------------------------------------------------------
156# USAGE: size
157#
158# Returns the number of elements in this sequence.
159# ----------------------------------------------------------------------
160itcl::body Rappture::Sequence::size {} {
161    return [llength $_indices]
162}
163
164# ----------------------------------------------------------------------
165# USAGE: hints ?<keyword>?
166#
167# Returns a list of key/value pairs for various hints about showing
168# this image.  If a particular <keyword> is specified, then it returns
169# the hint for that <keyword>, if it exists.
170# ----------------------------------------------------------------------
171itcl::body Rappture::Sequence::hints {{keyword ""}} {
172    if {![info exists _hints]} {
173        foreach {key path} {
174            label        about.label
175            indexlabel   index.label
176            indexdesc    index.description
177        } {
178            set str [$_xmlobj get $path]
179            if {"" != $str} {
180                set _hints($key) $str
181            }
182        }
183    }
184
185    if {$keyword != ""} {
186        if {[info exists _hints($keyword)]} {
187            return $_hints($keyword)
188        }
189        return ""
190    }
191    return [array get _hints]
192}
Note: See TracBrowser for help on using the repository browser.