source: branches/uq/gui/scripts/resultviewer.tcl @ 5884

Last change on this file since 5884 was 5884, checked in by mmh, 9 years ago

remove code causing missing output with a non-uq run followed by a uq run

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