source: branches/1.3/gui/scripts/resultviewer.tcl @ 3800

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

add -simulation to plotadd calls

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