source: trunk/gui/scripts/resultviewer.tcl @ 11

Last change on this file since 11 was 11, checked in by mmc, 16 years ago

Major reorganization of the entire package. The config.xml file
is now irrelevant. All the action is in the tool.xml file. The
main program now organizes all input into 1) side-by-side pages,
2) input/result (wizard-style) pages, or 3) a series of wizard-
style pages. The <input> can have <phase> parts representing
the various pages.

Added a new ContourResult? widget based on Swaroop's vtk plotting
code.

Also, added easymesh and showmesh to the "tools" directory.
We need these for Eric Polizzi's code.

File size: 9.6 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: ResultViewer - plots a collection of related results
3#
4#  This widget plots a collection of results that all represent
5#  the same quantity, but for various ranges of input values.  It
6#  is normally used as part of an Analyzer, to plot the various
7#  results selected by a ResultSet.
8# ======================================================================
9#  AUTHOR:  Michael McLennan, Purdue University
10#  Copyright (c) 2004-2005
11#  Purdue Research Foundation, West Lafayette, IN
12# ======================================================================
13package require Itk
14
15option add *ResultViewer.width 4i widgetDefault
16option add *ResultViewer.height 4i widgetDefault
17
18itcl::class Rappture::ResultViewer {
19    inherit itk::Widget
20
21    itk_option define -colors colors Colors ""
22    itk_option define -clearcommand clearCommand ClearCommand ""
23    itk_option define -simulatecommand simulateCommand SimulateCommand ""
24
25    constructor {args} { # defined below }
26    destructor { # defined below }
27
28    public method add {xmlobj path}
29    public method replace {index xmlobj path}
30    public method clear {}
31
32    public method plot {option args}
33
34    protected method _plotAdd {xmlobj {settings ""}}
35    protected method _fixScale {args}
36    protected proc _xml2data {xmlobj path}
37
38    private variable _dispatcher ""  ;# dispatchers for !events
39    private variable _mode ""        ;# current plotting mode (xy, etc.)
40    private variable _mode2widget    ;# maps plotting mode => widget
41    private variable _dataobjs ""    ;# list of all data objects in this widget
42    private variable _plotlist ""    ;# list of indices plotted in _dataobjs
43}
44                                                                               
45itk::usual ResultViewer {
46    keep -background -foreground -cursor -font
47}
48
49# ----------------------------------------------------------------------
50# CONSTRUCTOR
51# ----------------------------------------------------------------------
52itcl::body Rappture::ResultViewer::constructor {args} {
53    option add hull.width hull.height
54    pack propagate $itk_component(hull) no
55
56    # create a dispatcher for events
57    Rappture::dispatcher _dispatcher
58    $_dispatcher register !scale
59    $_dispatcher dispatch $this !scale \
60        [itcl::code $this _fixScale]
61
62    eval itk_initialize $args
63}
64
65# ----------------------------------------------------------------------
66# DESTRUCTOR
67# ----------------------------------------------------------------------
68itcl::body Rappture::ResultViewer::destructor {} {
69    foreach obj $_dataobjs {
70        itcl::delete object $obj
71    }
72}
73
74# ----------------------------------------------------------------------
75# USAGE: add <xmlobj> <path>
76#
77# Adds a new result to this result viewer.  Scans through all existing
78# results to look for a difference compared to previous results.
79# ----------------------------------------------------------------------
80itcl::body Rappture::ResultViewer::add {xmlobj path} {
81    if {$path != ""} {
82        set dobj [_xml2data $xmlobj $path]
83    } else {
84        set dobj ""
85    }
86    lappend _dataobjs $dobj
87
88    $_dispatcher event -idle !scale
89}
90
91# ----------------------------------------------------------------------
92# USAGE: replace <index> <xmlobj> <path>
93#
94# Stores a new result to this result viewer, overwriting the previous
95# result at position <index>.
96# ----------------------------------------------------------------------
97itcl::body Rappture::ResultViewer::replace {index xmlobj path} {
98    set dobj [lindex $_dataobjs $index]
99    if {"" != $dobj} {
100        itcl::delete object $dobj
101    }
102
103    set dobj [_xml2data $xmlobj $path]
104    set _dataobjs [lreplace $_dataobjs $index $index $dobj]
105
106    $_dispatcher event -idle !scale
107}
108
109# ----------------------------------------------------------------------
110# USAGE: clear
111#
112# Clears all results in this result viewer.
113# ----------------------------------------------------------------------
114itcl::body Rappture::ResultViewer::clear {} {
115    plot clear
116
117    foreach obj $_dataobjs {
118        itcl::delete object $obj
119    }
120    set _dataobjs ""
121}
122
123# ----------------------------------------------------------------------
124# USAGE: plot add ?<index> <settings> <index> <settings> ...?
125# USAGE: plot clear
126#
127# Used to manipulate the contents of this viewer.  The "plot clear"
128# command clears the current viewer.  Data is still stored in the
129# widget, but the results are not shown on screen.  The "plot add"
130# command adds the data at the specified <index> to the plot.  If
131# the optional <settings> are specified, then they are applied
132# to the plot; otherwise, default settings are used.
133# ----------------------------------------------------------------------
134itcl::body Rappture::ResultViewer::plot {option args} {
135    switch -- $option {
136        add {
137            foreach {index opts} $args {
138                set dobj [lindex $_dataobjs $index]
139                if {"" != $dobj} {
140                    _plotAdd $dobj $opts
141                }
142            }
143        }
144        clear {
145            # clear the contents of the current mode
146            if {"" != $_mode} {
147                $_mode2widget($_mode) delete
148            }
149            set _plotlist ""
150        }
151        default {
152            error "bad option \"$option\": should be add or clear"
153        }
154    }
155}
156
157# ----------------------------------------------------------------------
158# USAGE: _plotAdd <dataobj> <settings>
159#
160# Used internally to add a <dataobj> representing some data to
161# the plot at the top of this widget.  The data is added to the
162# current plot.  Use the "clear" function to clear before adding
163# new data.
164# ----------------------------------------------------------------------
165itcl::body Rappture::ResultViewer::_plotAdd {dataobj {settings ""}} {
166    switch -- [$dataobj info class] {
167        ::Rappture::Curve {
168            set mode "xy"
169            if {![info exists _mode2widget($mode)]} {
170                set w $itk_interior.xy
171                Rappture::XyResult $w
172                set _mode2widget($mode) $w
173            }
174        }
175        ::Rappture::Field {
176            switch -- [$dataobj components -dimensions] {
177                1D {
178                    set mode "xy"
179                    if {![info exists _mode2widget($mode)]} {
180                        set w $itk_interior.xy
181                        Rappture::XyResult $w
182                        set _mode2widget($mode) $w
183                    }
184                }
185                2D {
186                    set mode "contour"
187                    if {![info exists _mode2widget($mode)]} {
188                        set w $itk_interior.xy
189                        Rappture::ContourResult $w
190                        set _mode2widget($mode) $w
191                    }
192                }
193                default {
194                    error "can't handle [$dataobj components -dimensions] field"
195                }
196            }
197        }
198        ::Rappture::LibraryObj {
199            switch -- [$dataobj element -as type] {
200                log {
201                    set mode "log"
202                    if {![info exists _mode2widget($mode)]} {
203                        set w $itk_interior.log
204                        Rappture::TextResult $w
205                        set _mode2widget($mode) $w
206                    }
207                }
208                table {
209                    # table for now -- should have a Table object!
210                    set mode "energies"
211                    if {![info exists _mode2widget($mode)]} {
212                        set w $itk_interior.energies
213                        Rappture::EnergyLevels $w
214                        set _mode2widget($mode) $w
215                    }
216                }
217            }
218        }
219        default {
220            error "don't know how to plot <$type> data"
221        }
222    }
223
224    if {$mode != $_mode && $_mode != ""} {
225        return  ;# mixing data that doesn't mix -- ignore it!
226    }
227
228    # are we plotting in a new mode? then change widgets
229    if {$_mode2widget($mode) != [pack slaves $itk_interior]} {
230        # remove any current window
231        foreach w [pack slaves $itk_interior] {
232            pack forget $w
233        }
234        pack $_mode2widget($mode) -expand yes -fill both
235
236        set _mode $mode
237        $_dispatcher event -idle !scale
238    }
239    $_mode2widget($mode) add $dataobj $settings
240}
241
242# ----------------------------------------------------------------------
243# USAGE: _fixScale ?<eventArgs>...?
244#
245# Invoked automatically whenever a new dataset is added to fix the
246# overall scales of the viewer.  This makes the visualizer consistent
247# across all <dataobj> in this widget, so that it can plot all
248# available data.
249# ----------------------------------------------------------------------
250itcl::body Rappture::ResultViewer::_fixScale {args} {
251    if {"" != $_mode} {
252        eval $_mode2widget($_mode) scale $_dataobjs
253    }
254}
255
256# ----------------------------------------------------------------------
257# USAGE: _xml2data <xmlobj> <path>
258#
259# Used internally to create a data object for the data at the
260# specified <path> in the <xmlobj>.
261# ----------------------------------------------------------------------
262itcl::body Rappture::ResultViewer::_xml2data {xmlobj path} {
263    set type [$xmlobj element -as type $path]
264    switch -- $type {
265        curve {
266            return [Rappture::Curve ::#auto $xmlobj $path]
267        }
268        field {
269            return [Rappture::Field ::#auto $xmlobj $path]
270        }
271        table {
272            return [$xmlobj element -as object $path]
273        }
274        log {
275            return [$xmlobj element -as object $path]
276        }
277        time - status {
278            return ""
279        }
280    }
281    error "don't know how to plot <$type> data"
282}
Note: See TracBrowser for help on using the repository browser.