source: tags/20110826/gui/scripts/datatable.tcl @ 4643

Last change on this file since 4643 was 1929, checked in by gah, 14 years ago
File size: 5.2 KB
Line 
1 
2# ----------------------------------------------------------------------
3#  COMPONENT: datatable - extracts data from an XML description of a field
4#
5#  This object represents a datatable of data in an XML description of
6#  simulator output.  A datatable is similar to a field, but a field is
7#  a quantity versus position in device.  A datatable is any quantity
8#  versus any other quantity.  This class simplifies the process of
9#  extracting data vectors that represent the datatable.
10# ======================================================================
11#  AUTHOR:  Michael McLennan, Purdue University
12#  Copyright (c) 2004-2005  Purdue Research Foundation
13#
14#  See the file "license.terms" for information on usage and
15#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
16# ======================================================================
17package require Itcl
18package require BLT
19
20namespace eval Rappture {
21    # forward declaration
22}
23
24itcl::class Rappture::DataTable {
25    constructor {xmlobj path} {
26        # defined below
27    }
28    destructor {
29        # defined below
30    }
31
32    public method components {{pattern *}}
33    public method values {}
34    public method columns {}
35    public method hints {{key ""}}
36
37    protected method Build {}
38    private variable _xmlobj ""  ;      # ref to lib obj with datatable data
39    private variable _datatable "";     # lib obj representing this datatable
40    private variable _columns ""
41    private variable _hints      ;      # cache of hints stored in XML
42    private variable _tree ""
43}
44
45# ----------------------------------------------------------------------
46# CONSTRUCTOR
47# ----------------------------------------------------------------------
48itcl::body Rappture::DataTable::constructor {xmlobj path} {
49    if {![Rappture::library isvalid $xmlobj]} {
50        error "bad value \"$xmlobj\": should be LibraryObj"
51    }
52    set _xmlobj $xmlobj
53    set _datatable [$xmlobj element -as object $path]
54   
55    # build up vectors for various components of the datatable
56    Build
57}
58
59# ----------------------------------------------------------------------
60# DESTRUCTOR
61# ----------------------------------------------------------------------
62itcl::body Rappture::DataTable::destructor {} {
63    itcl::delete object $_datatable
64    blt::tree destroy $_tree
65}
66
67# ----------------------------------------------------------------------
68# USAGE: values
69#
70# Returns the values for the datatable as a BLT tree.
71# ----------------------------------------------------------------------
72itcl::body Rappture::DataTable::values {} {
73    return $_tree
74}
75
76# ----------------------------------------------------------------------
77# USAGE: columns
78#
79# Returns the columns of the datatable in order.
80# ----------------------------------------------------------------------
81itcl::body Rappture::DataTable::columns {} {
82    return $_columns
83}
84
85# ----------------------------------------------------------------------
86# USAGE: hints ?<keyword>?
87#
88# Returns a list of key/value pairs for various hints about plotting
89# this datatable.  If a particular <keyword> is specified, then it returns
90# the hint for that <keyword>, if it exists.
91# ----------------------------------------------------------------------
92itcl::body Rappture::DataTable::hints {{keyword ""}} {
93    if {![info exists _hints]} {
94        foreach {key path} {
95            group   about.group
96            label   about.label
97            color   about.color
98            style   about.style
99        } {
100            set str [$_datatable get $path]
101            if {"" != $str} {
102                set _hints($key) $str
103            }
104        }
105    }
106    set _hints(xmlobj) $_xmlobj
107    if { $keyword != "" } {
108        if { [info exists _hints($keyword)] } {
109            return $_hints($keyword)
110        }
111        return ""
112    }
113    return [array get _hints]
114}
115
116# ----------------------------------------------------------------------
117# USAGE: Build
118#
119# Used internally to build up the vector representation for the
120# datatable when the object is first constructed, or whenever the datatable
121# data changes.  Discards any existing vectors and builds everything
122# from scratch.
123# ----------------------------------------------------------------------
124itcl::body Rappture::DataTable::Build {} {
125    # Discard any existing data
126    if { $_tree != "" } {
127        blt::tree destroy $_tree
128        set _tree ""
129    }
130    set _columns {}
131
132    # Sniff for column information: label, descriptions, and style
133    foreach cname [$_datatable children -type "column"] {
134        set label        [$_datatable get "$cname.label"]
135        set description  [$_datatable get "$cname.description"]
136        set style        [$_datatable get "$cname.style"]
137        lappend _columns $label $description $style
138    }
139    set csvdata [$_datatable get csv]
140    set _tree [blt::tree create]
141    if {"" != $csvdata} {
142        set csvlist [blt::csv -data $csvdata]
143        foreach row $csvlist {
144            set child [$_tree insert 0]
145            set c 0
146            foreach value $row {label description style} $_columns {
147                if { $label == "" } {
148                    set label "$c"
149                }
150                $_tree set $child $label $value
151                incr c
152            }
153        }
154    }
155}
Note: See TracBrowser for help on using the repository browser.