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

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

remove debugging puts

File size: 21.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 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    if {$mode != $_mode && $_mode != ""} {
420        set nactive [llength [$_mode2widget($_mode) get]]
421        if {$nactive > 0} {
422            return  ;# mixing data that doesn't mix -- ignore it!
423        }
424    }
425    # Are we plotting in a new mode? then change widgets
426    if {$_mode2widget($mode) != [pack slaves $itk_interior]} {
427        # remove any current window
428        foreach w [pack slaves $itk_interior] {
429            pack forget $w
430        }
431        pack $_mode2widget($mode) -expand yes -fill both
432
433        set _mode $mode
434        $_dispatcher event -idle !scale
435    }
436    $_mode2widget($mode) add $dataobj $settings
437}
438
439# ----------------------------------------------------------------------
440# USAGE: _fixScale ?<eventArgs>...?
441#
442# Invoked automatically whenever a new dataset is added to fix the
443# overall scales of the viewer.  This makes the visualizer consistent
444# across all <dataobj> in this widget, so that it can plot all
445# available data.
446# ----------------------------------------------------------------------
447itcl::body Rappture::ResultViewer::_fixScale {args} {
448    if {"" != $_mode} {
449        set dlist ""
450        set objclass ""
451        foreach slot $_dataslots {
452            foreach dobj $slot {
453                if {$objclass == ""} {
454                    set objclass [$dobj info class]
455                } else {
456                    if {$objclass != [$dobj info class]} {
457                        # If some of the objects are different classes
458                        # then we cannot use the same scale, so give up.
459                        return
460                    }
461                }
462                lappend dlist $dobj
463            }
464        }
465        eval $_mode2widget($_mode) scale $dlist
466    }
467}
468
469# ----------------------------------------------------------------------
470# USAGE: download coming
471# USAGE: download controls <downloadCommand>
472# USAGE: download now
473#
474# Clients use this method to create a downloadable representation
475# of the plot.  Returns a list of the form {ext string}, where
476# "ext" is the file extension (indicating the type of data) and
477# "string" is the data itself.
478# ----------------------------------------------------------------------
479itcl::body Rappture::ResultViewer::download {option args} {
480    if {"" == $_mode} {
481        return ""
482    }
483    return [eval $_mode2widget($_mode) download $option $args]
484}
485
486# ----------------------------------------------------------------------
487# USAGE: _xml2data <xmlobj> <path>
488#
489# Used internally to create a data object for the data at the
490# specified <path> in the <xmlobj>.
491# ----------------------------------------------------------------------
492itcl::body Rappture::ResultViewer::_xml2data {xmlobj path label {uq_part ""}} {
493    #puts "RV:_xml2data $path ([$xmlobj element -as type $path]) label=$label uq_part=$uq_part"
494
495    if {$uq_part != ""} {
496        if {[info exists _xml2data($xmlobj-$label)]} {
497            $_xml2data($xmlobj-$label) add $xmlobj $path $uq_part
498            return $_xml2data($xmlobj-$label)
499        }
500    } elseif {[info exists _xml2data($xmlobj-$path]} {
501        return $_xml2data($xmlobj-$path)
502    }
503
504    if {$uq_part != ""} {
505        set type "UQ"
506    } else {
507        set type [$xmlobj element -as type $path]
508    }
509
510    switch -- $type {
511        UQ {
512            set dobj [Rappture::UqInfo ::#auto $xmlobj $path $uq_part]
513            set path $label
514        }
515        curve {
516            set dobj [Rappture::Curve ::#auto $xmlobj $path]
517        }
518        datatable {
519            set dobj [Rappture::DataTable ::#auto $xmlobj $path]
520        }
521        histogram {
522            set dobj [Rappture::Histogram ::#auto $xmlobj $path]
523        }
524        field {
525            set dobj [Rappture::Field ::#auto $xmlobj $path]
526        }
527        mesh {
528            set dobj [Rappture::Mesh ::#auto $xmlobj $path]
529        }
530        table {
531            set dobj [Rappture::Table ::#auto $xmlobj $path]
532        }
533        image {
534            set dobj [Rappture::Image ::#auto $xmlobj $path]
535        }
536        sequence {
537            set dobj [Rappture::Sequence ::#auto $xmlobj $path]
538        }
539        string - log {
540            set dobj [$xmlobj element -as object $path]
541        }
542        structure {
543            set dobj [$xmlobj element -as object $path]
544        }
545        number - integer - boolean - choice {
546            set dobj [$xmlobj element -as object $path]
547        }
548        drawing3d - drawing {
549            set dobj [Rappture::Drawing ::#auto $xmlobj $path]
550        }
551        time - status {
552            set dobj ""
553        }
554        default {
555            error "don't know how to plot <$type> data path=$path"
556        }
557    }
558
559    # store the mapping xmlobj=>dobj so we can find this result later
560    if {$dobj ne ""} {
561        set _xml2data($xmlobj-$path) $dobj
562    }
563    return $dobj
564}
565
566# ----------------------------------------------------------------------
567# USAGE: _cleanIndex <index>
568#
569# Used internally to create a data object for the data at the
570# specified <path> in the <xmlobj>.
571# ----------------------------------------------------------------------
572itcl::body Rappture::ResultViewer::_cleanIndex {index} {
573    set index [lindex $index 0]
574    if {[regexp {^#([0-9]+)} $index match num]} {
575        return [expr {$num-1}]  ;# start from 0 instead of 1
576    } elseif {[string is integer -strict $index]} {
577        return $index
578    }
579    error "bad plot index \"$index\": should be 0,1,2,... or #1,#2,#3,..."
580}
581
582# ----------------------------------------------------------------------
583# CONFIGURATION OPTION: -width
584# ----------------------------------------------------------------------
585itcl::configbody Rappture::ResultViewer::width {
586    set w [winfo pixels $itk_component(hull) $itk_option(-width)]
587    set h [winfo pixels $itk_component(hull) $itk_option(-height)]
588    if {$w == 0 || $h == 0} {
589        pack propagate $itk_component(hull) yes
590    } else {
591        component hull configure -width $w -height $h
592        pack propagate $itk_component(hull) no
593    }
594}
595
596# ----------------------------------------------------------------------
597# CONFIGURATION OPTION: -height
598# ----------------------------------------------------------------------
599itcl::configbody Rappture::ResultViewer::height {
600    set h [winfo pixels $itk_component(hull) $itk_option(-height)]
601    set w [winfo pixels $itk_component(hull) $itk_option(-width)]
602    if {$w == 0 || $h == 0} {
603        pack propagate $itk_component(hull) yes
604    } else {
605        component hull configure -width $w -height $h
606        pack propagate $itk_component(hull) no
607    }
608}
Note: See TracBrowser for help on using the repository browser.