source: branches/blt4/gui/scripts/resultviewer.tcl @ 1970

Last change on this file since 1970 was 1923, checked in by gah, 14 years ago
File size: 18.2 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  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 Itk
16
17itcl::class Rappture::ResultViewer {
18    inherit itk::Widget
19
20    itk_option define -width width Width 4i
21    itk_option define -height height Height 4i
22    itk_option define -colors colors Colors ""
23    itk_option define -clearcommand clearCommand ClearCommand ""
24    itk_option define -simulatecommand simulateCommand SimulateCommand ""
25
26    constructor {args} { # defined below }
27    destructor { # defined below }
28
29    public method add {index xmlobj path}
30    public method clear {{index ""}}
31    public method value {xmlobj}
32
33    public method plot {option args}
34    public method download {option args}
35
36    protected method _plotAdd {xmlobj {settings ""}}
37    protected method _fixScale {args}
38    protected proc _xml2data {xmlobj path}
39
40    private variable _dispatcher ""  ;# dispatchers for !events
41    private variable _mode ""        ;# current plotting mode (xy, etc.)
42    private variable _mode2widget    ;# maps plotting mode => widget
43    private variable _dataslots ""   ;# list of all data objects in this widget
44}
45
46itk::usual ResultViewer {
47    keep -background -foreground -cursor -font
48}
49
50# ----------------------------------------------------------------------
51# CONSTRUCTOR
52# ----------------------------------------------------------------------
53itcl::body Rappture::ResultViewer::constructor {args} {
54    # create a dispatcher for events
55    Rappture::dispatcher _dispatcher
56    $_dispatcher register !scale
57    $_dispatcher dispatch $this !scale \
58        [itcl::code $this _fixScale]
59
60    eval itk_initialize $args
61}
62
63# ----------------------------------------------------------------------
64# DESTRUCTOR
65# ----------------------------------------------------------------------
66itcl::body Rappture::ResultViewer::destructor {} {
67    foreach slot $_dataslots {
68        foreach obj $slot {
69            itcl::delete object $obj
70        }
71    }
72}
73
74# ----------------------------------------------------------------------
75# USAGE: add <index> <xmlobj> <path>
76#
77# Adds a new result to this result viewer at the specified <index>.
78# Data is taken from the <xmlobj> object at the <path>.
79# ----------------------------------------------------------------------
80itcl::body Rappture::ResultViewer::add {index xmlobj path} {
81    set dobj [_xml2data $xmlobj $path]
82
83    #
84    # If the index doesn't exist, then fill in empty slots and
85    # make it exist.
86    #
87    for {set i [llength $_dataslots]} {$i <= $index} {incr i} {
88        lappend _dataslots ""
89    }
90    set slot [lindex $_dataslots $index]
91    lappend slot $dobj
92    set _dataslots [lreplace $_dataslots $index $index $slot]
93
94    $_dispatcher event -idle !scale
95}
96
97# ----------------------------------------------------------------------
98# USAGE: clear ?<index>?
99#
100# Clears one or all results in this result viewer.
101# ----------------------------------------------------------------------
102itcl::body Rappture::ResultViewer::clear {{index ""}} {
103    if {"" != $index} {
104        # clear one result
105        if {$index >= 0 && $index < [llength $_dataslots]} {
106            set slot [lindex $_dataslots $index]
107            foreach dobj $slot {
108                itcl::delete object $dobj
109            }
110            set _dataslots [lreplace $_dataslots $index $index ""]
111        }
112    } else {
113        # clear all results
114        plot clear
115        foreach slot $_dataslots {
116            foreach dobj $slot {
117                itcl::delete object $dobj
118            }
119        }
120        set _dataslots ""
121    }
122}
123
124# ----------------------------------------------------------------------
125# USAGE: value <xmlobj>
126#
127# Convenience method for showing a single value.  Loads the value
128# into the widget via add/clear, then immediately plots the value.
129# This makes the widget consistent with other widgets, such as
130# the DeviceEditor, etc.
131# ----------------------------------------------------------------------
132itcl::body Rappture::ResultViewer::value {xmlobj} {
133    clear
134    if {"" != $xmlobj} {
135        add 0 $xmlobj ""
136        plot add 0 ""
137    }
138}
139
140# ----------------------------------------------------------------------
141# USAGE: plot add ?<index> <settings> <index> <settings> ...?
142# USAGE: plot clear
143#
144# Used to manipulate the contents of this viewer.  The "plot clear"
145# command clears the current viewer.  Data is still stored in the
146# widget, but the results are not shown on screen.  The "plot add"
147# command adds the data at the specified <index> to the plot.  If
148# the optional <settings> are specified, then they are applied
149# to the plot; otherwise, default settings are used.
150# ----------------------------------------------------------------------
151itcl::body Rappture::ResultViewer::plot {option args} {
152    switch -- $option {
153        add {
154            set params ""
155            foreach {index opts} $args {
156                if {$index == "params"} {
157                    set params $opts
158                    continue
159                }
160                set reset "-color autoreset"
161                set slot [lindex $_dataslots $index]
162                foreach dobj $slot {
163                    set settings ""
164                    # start with color reset, only for first object in series
165                    if {"" != $reset} {
166                        set settings $reset
167                        set reset ""
168                    }
169                    # add default settings from data object
170                    if {[catch {$dobj hints style} style] == 0} {
171                        eval lappend settings $style
172                    }
173                    if {[catch {$dobj hints type} type] == 0} {
174                        if {"" != $type} {
175                            eval lappend settings "-type $type"
176                        }
177                    }
178                    # add override settings passed in here
179                    eval lappend settings $opts
180
181                    _plotAdd $dobj $settings
182                }
183            }
184            if {"" != $params && "" != $_mode} {
185                eval $_mode2widget($_mode) parameters $params
186            }
187        }
188        clear {
189            # clear the contents of the current mode
190            if {"" != $_mode} {
191                $_mode2widget($_mode) delete
192            }
193        }
194        default {
195            error "bad option \"$option\": should be add or clear"
196        }
197    }
198}
199
200# ----------------------------------------------------------------------
201# USAGE: _plotAdd <dataobj> <settings>
202#
203# Used internally to add a <dataobj> representing some data to
204# the plot at the top of this widget.  The data is added to the
205# current plot.  Use the "clear" function to clear before adding
206# new data.
207# ----------------------------------------------------------------------
208itcl::body Rappture::ResultViewer::_plotAdd {dataobj {settings ""}} {
209    switch -- [$dataobj info class] {
210        ::Rappture::DataTable {
211            set mode "datatable"
212            if {![info exists _mode2widget($mode)]} {
213                set w $itk_interior.datatable
214                Rappture::DataTableResult $w
215                set _mode2widget($mode) $w
216            }
217        }
218        ::Rappture::Drawing3d {
219            set mode "vtkviewer"
220            if {![info exists _mode2widget($mode)]} {
221                set w $itk_interior.xy
222                Rappture::VtkViewer $w
223                set _mode2widget($mode) $w
224            }
225        }
226        ::Rappture::Histogram {
227            set mode "histogram"
228            if {![info exists _mode2widget($mode)]} {
229                set w $itk_interior.xy
230                Rappture::HistogramResult $w
231                set _mode2widget($mode) $w
232            }
233        }
234        ::Rappture::Curve {
235            set type [$dataobj hints type]
236            set mode "xy"
237            if { $type == "bars" } {
238                if {![info exists _mode2widget($mode)]} {
239                    set w $itk_interior.xy
240                    Rappture::BarResult $w
241                    set _mode2widget($mode) $w
242                }
243            } else {
244                if {![info exists _mode2widget($mode)]} {
245                    set w $itk_interior.xy
246                    Rappture::XyResult $w
247                    set _mode2widget($mode) $w
248                }
249            }
250        }
251        ::Rappture::Field {
252            set dims [lindex [lsort [$dataobj components -dimensions]] end]
253            switch -- $dims {
254                1D {
255                    set mode "xy"
256                    if {![info exists _mode2widget($mode)]} {
257                        set w $itk_interior.xy
258                        Rappture::XyResult $w
259                        set _mode2widget($mode) $w
260                    }
261                }
262                2D {
263                    set mode "contour"
264                    if {![info exists _mode2widget($mode)]} {
265                        if { [$dataobj isunirect2d] } {
266                            set resultMode "heightmap"
267                        } else {
268                            set resultMode "vtk"
269                        }
270                        set extents [$dataobj extents]
271                        if { $extents > 1 } {
272                            set resultMode "flowvis"
273                        }
274                        set w $itk_interior.contour
275                        if { ![winfo exists $w] } {
276                            Rappture::Field2DResult $w -mode $resultMode
277                        }
278                        set _mode2widget($mode) $w
279                    }
280                }
281                3D {
282                    set mode "field3D"
283                    if {![info exists _mode2widget($mode)]} {
284                        set mesh [$dataobj mesh]
285                        set fmt [expr {("" != $mesh) ? "vtk" : "nanovis"}]
286                        set extents [$dataobj extents]
287                        if { $extents > 1 } {
288                            set fmt "flowvis"
289                        }
290                        set w $itk_interior.field3D
291                        Rappture::Field3DResult $w -mode $fmt
292                        set _mode2widget($mode) $w
293                    }
294                }
295                default {
296                    error "can't handle [$dataobj components -dimensions] field"
297                }
298            }
299        }
300        ::Rappture::Mesh {
301            switch -- [$dataobj dimensions] {
302                2 {
303                    set mode "mesh"
304                    if {![info exists _mode2widget($mode)]} {
305                        set w $itk_interior.mesh
306                        Rappture::MeshResult $w
307                        set _mode2widget($mode) $w
308                    }
309                }
310                default {
311                    error "can't handle [$dataobj dimensions]D field"
312                }
313            }
314        }
315        ::Rappture::Table {
316            set cols [Rappture::EnergyLevels::columns $dataobj]
317            if {"" != $cols} {
318                set mode "energies"
319                if {![info exists _mode2widget($mode)]} {
320                    set w $itk_interior.energies
321                    Rappture::EnergyLevels $w
322                    set _mode2widget($mode) $w
323                }
324            }
325        }
326        ::Rappture::LibraryObj {
327            switch -- [$dataobj element -as type] {
328                string - log {
329                    set mode "log"
330                    if {![info exists _mode2widget($mode)]} {
331                        set w $itk_interior.log
332                        Rappture::TextResult $w
333                        set _mode2widget($mode) $w
334                    }
335                }
336                structure {
337                    set mode "structure"
338                    if {![info exists _mode2widget($mode)]} {
339                        set w $itk_interior.struct
340                        Rappture::DeviceResult $w
341                        set _mode2widget($mode) $w
342                    }
343                }
344                number - integer {
345                    set mode "number"
346                    if {![info exists _mode2widget($mode)]} {
347                        set w $itk_interior.number
348                        Rappture::NumberResult $w
349                        set _mode2widget($mode) $w
350                    }
351                }
352                boolean - choice {
353                    set mode "value"
354                    if {![info exists _mode2widget($mode)]} {
355                        set w $itk_interior.value
356                        Rappture::ValueResult $w
357                        set _mode2widget($mode) $w
358                    }
359                }
360            }
361        }
362        ::Rappture::Image {
363            set mode "image"
364            if {![info exists _mode2widget($mode)]} {
365                set w $itk_interior.image
366                Rappture::ImageResult $w
367                set _mode2widget($mode) $w
368            }
369        }
370        ::Rappture::Sequence {
371            set mode "sequence"
372            if {![info exists _mode2widget($mode)]} {
373                set w $itk_interior.image
374                Rappture::SequenceResult $w
375                set _mode2widget($mode) $w
376            }
377        }
378        default {
379            error "don't know how to plot <$type> data"
380        }
381    }
382
383    if {$mode != $_mode && $_mode != ""} {
384        set nactive [llength [$_mode2widget($_mode) get]]
385        if {$nactive > 0} {
386            return  ;# mixing data that doesn't mix -- ignore it!
387        }
388    }
389
390    # are we plotting in a new mode? then change widgets
391    if {$_mode2widget($mode) != [pack slaves $itk_interior]} {
392        # remove any current window
393        foreach w [pack slaves $itk_interior] {
394            pack forget $w
395        }
396        pack $_mode2widget($mode) -expand yes -fill both
397
398        set _mode $mode
399        $_dispatcher event -idle !scale
400    }
401    $_mode2widget($mode) add $dataobj $settings
402}
403
404# ----------------------------------------------------------------------
405# USAGE: _fixScale ?<eventArgs>...?
406#
407# Invoked automatically whenever a new dataset is added to fix the
408# overall scales of the viewer.  This makes the visualizer consistent
409# across all <dataobj> in this widget, so that it can plot all
410# available data.
411# ----------------------------------------------------------------------
412itcl::body Rappture::ResultViewer::_fixScale {args} {
413    if {"" != $_mode} {
414        set dlist ""
415        foreach slot $_dataslots {
416            foreach dobj $slot {
417                lappend dlist $dobj
418            }
419        }
420        eval $_mode2widget($_mode) scale $dlist
421    }
422}
423
424# ----------------------------------------------------------------------
425# USAGE: download coming
426# USAGE: download controls <downloadCommand>
427# USAGE: download now
428#
429# Clients use this method to create a downloadable representation
430# of the plot.  Returns a list of the form {ext string}, where
431# "ext" is the file extension (indicating the type of data) and
432# "string" is the data itself.
433# ----------------------------------------------------------------------
434itcl::body Rappture::ResultViewer::download {option args} {
435    if {"" == $_mode} {
436        return ""
437    }
438    return [eval $_mode2widget($_mode) download $option $args]
439}
440
441# ----------------------------------------------------------------------
442# USAGE: _xml2data <xmlobj> <path>
443#
444# Used internally to create a data object for the data at the
445# specified <path> in the <xmlobj>.
446# ----------------------------------------------------------------------
447itcl::body Rappture::ResultViewer::_xml2data {xmlobj path} {
448    set type [$xmlobj element -as type $path]
449    switch -- $type {
450        curve {
451            return [Rappture::Curve ::#auto $xmlobj $path]
452        }
453        datatable {
454            return [Rappture::DataTable ::#auto $xmlobj $path]
455        }
456        histogram {
457            return [Rappture::Histogram ::#auto $xmlobj $path]
458        }
459        field {
460            return [Rappture::Field ::#auto $xmlobj $path]
461        }
462        mesh {
463            return [Rappture::Mesh ::#auto $xmlobj $path]
464        }
465        table {
466            return [Rappture::Table ::#auto $xmlobj $path]
467        }
468        image {
469            return [Rappture::Image ::#auto $xmlobj $path]
470        }
471        sequence {
472            return [Rappture::Sequence ::#auto $xmlobj $path]
473        }
474        string - log {
475            return [$xmlobj element -as object $path]
476        }
477        structure {
478            return [$xmlobj element -as object $path]
479        }
480        number - integer - boolean - choice {
481            return [$xmlobj element -as object $path]
482        }
483        drawing3d {
484            return [Rappture::Drawing3d ::#auto $xmlobj $path]
485        }
486        time - status {
487            return ""
488        }
489    }
490    error "don't know how to plot <$type> data"
491}
492
493# ----------------------------------------------------------------------
494# CONFIGURATION OPTION: -width
495# ----------------------------------------------------------------------
496itcl::configbody Rappture::ResultViewer::width {
497    set w [winfo pixels $itk_component(hull) $itk_option(-width)]
498    set h [winfo pixels $itk_component(hull) $itk_option(-height)]
499    if {$w == 0 || $h == 0} {
500        pack propagate $itk_component(hull) yes
501    } else {
502        component hull configure -width $w -height $h
503        pack propagate $itk_component(hull) no
504    }
505}
506
507# ----------------------------------------------------------------------
508# CONFIGURATION OPTION: -height
509# ----------------------------------------------------------------------
510itcl::configbody Rappture::ResultViewer::height {
511    set h [winfo pixels $itk_component(hull) $itk_option(-height)]
512    set w [winfo pixels $itk_component(hull) $itk_option(-width)]
513    if {$w == 0 || $h == 0} {
514        pack propagate $itk_component(hull) yes
515    } else {
516        component hull configure -width $w -height $h
517        pack propagate $itk_component(hull) no
518    }
519}
Note: See TracBrowser for help on using the repository browser.