source: branches/blt4/gui/scripts/sequenceresult.tcl @ 1644

Last change on this file since 1644 was 1644, checked in by gah, 13 years ago
File size: 19.3 KB
Line 
1# ----------------------------------------------------------------------
2#  COMPONENT: sequenceresult - series of results forming an animation
3#
4#  This widget displays a series of results of the same type that are
5#  grouped together and displayed as an animation.  The user can play
6#  through the results, or single step through individual values.
7# ======================================================================
8#  AUTHOR:  Michael McLennan, Purdue University
9#  Copyright (c) 2004-2005  Purdue Research Foundation
10#
11#  See the file "license.terms" for information on usage and
12#  redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13# ======================================================================
14package require Itk
15package require BLT
16
17option add *SequenceResult.width 3i widgetDefault
18option add *SequenceResult.height 3i widgetDefault
19option add *SequenceResult.controlBackground gray widgetDefault
20option add *SequenceResult.dialProgressColor #ccccff widgetDefault
21option add *SequenceResult.font \
22    -*-helvetica-medium-r-normal-*-12-* widgetDefault
23option add *SequenceResult.boldFont \
24    -*-helvetica-bold-r-normal-*-12-* widgetDefault
25
26itcl::class Rappture::SequenceResult {
27    inherit itk::Widget
28
29    constructor {args} { # defined below }
30    destructor { # defined below }
31
32    public method add {dataobj {settings ""}}
33    public method get {}
34    public method delete {args}
35    public method scale {args}
36    public method parameters {title args} { # do nothing }
37    public method download {option args}
38
39    public method play {}
40    public method pause {}
41    public method goto {{newval ""}}
42
43    protected method _rebuild {args}
44    protected method _playFrame {}
45    protected method _fixValue {}
46
47    private variable _dispatcher "" ;# dispatcher for !events
48    private variable _dlist ""      ;# list of data objects
49    private variable _topmost ""    ;# topmost data object in _dlist
50    private variable _indices ""    ;# list of active indices
51    private variable _pos 0         ;# current position in the animation
52    private variable _afterId ""    ;# current "after" event for play op
53
54    private common _play            ;# options for "play" operation
55    set _play(speed) 60
56    set _play(loop) 0
57}
58
59itk::usual SequenceResult {
60    keep -background -foreground -cursor -font
61}
62
63# ----------------------------------------------------------------------
64# CONSTRUCTOR
65# ----------------------------------------------------------------------
66itcl::body Rappture::SequenceResult::constructor {args} {
67    Rappture::dispatcher _dispatcher
68    $_dispatcher register !rebuild
69    $_dispatcher dispatch $this !rebuild [itcl::code $this _rebuild]
70
71    option add hull.width hull.height
72    pack propagate $itk_component(hull) no
73
74    itk_component add player {
75        frame $itk_interior.player
76    }
77    pack $itk_component(player) -side bottom -fill x
78    grid columnconfigure $itk_component(player) 1 -weight 1
79
80    itk_component add play {
81        button $itk_component(player).play \
82            -bitmap [Rappture::icon play] \
83            -command [itcl::code $this play]
84    }
85    grid $itk_component(play) -row 0 -rowspan 2 -column 0 \
86        -ipadx 2 -padx {0 4} -pady 4 -sticky nsew
87
88    itk_component add dial {
89        Rappture::Radiodial $itk_component(player).dial \
90            -length 10 -valuewidth 0 -valuepadding 0 -padding 6 \
91            -linecolor "" -activelinecolor "" \
92            -knobimage [Rappture::icon knob2] -knobposition center@middle
93    } {
94        usual
95        keep -dialprogresscolor
96    }
97    grid $itk_component(dial) -row 1 -column 1 -sticky ew
98    bind $itk_component(dial) <<Value>> [itcl::code $this _fixValue]
99
100    itk_component add info {
101        frame $itk_component(player).info
102    }
103    grid $itk_component(info) -row 0 -column 1 -columnspan 2 -sticky ew
104
105    itk_component add indexLabel {
106        label $itk_component(info).ilabel
107    } {
108        usual
109        rename -font -boldfont boldFont Font
110    }
111    pack $itk_component(indexLabel) -side left
112
113    itk_component add indexValue {
114        label $itk_component(info).ivalue -padx 0
115    }
116    pack $itk_component(indexValue) -side left
117
118    # add an element.about.label stanza
119    itk_component add eleLabel {
120        label $itk_component(info).elabel -padx 10
121    }
122    pack $itk_component(eleLabel) -side left
123
124    itk_component add options {
125        button $itk_component(player).options -text "Options..." \
126            -padx 1 -pady 0 -relief flat -overrelief raised
127    }
128    grid $itk_component(options) -row 1 -column 2 -sticky sw
129
130    #
131    # Popup option panel
132    #
133    set fn [option get $itk_component(hull) font Font]
134    set bfn [option get $itk_component(hull) boldFont Font]
135
136    Rappture::Balloon $itk_component(hull).popup \
137        -title "Player Settings" -padx 4 -pady 4
138    set inner [$itk_component(hull).popup component inner]
139
140    label $inner.loopl -text "Loop:" -font $bfn
141    grid $inner.loopl -row 0 -column 0 -sticky e
142    radiobutton $inner.loopOn -text "Play once and stop" -font $fn \
143        -variable ::Rappture::SequenceResult::_play(loop) -value 0
144    grid $inner.loopOn -row 0 -column 1 -sticky w
145    radiobutton $inner.loopOff -text "Play continuously" -font $fn \
146        -variable ::Rappture::SequenceResult::_play(loop) -value 1
147    grid $inner.loopOff -row 1 -column 1 -sticky w
148    grid rowconfigure $inner 2 -minsize 8
149
150    label $inner.speedl -text "Speed:" -font $bfn
151    grid $inner.speedl -row 3 -column 0 -sticky e
152    frame $inner.speed
153    grid $inner.speed -row 3 -column 1 -sticky ew
154    label $inner.speed.slowl -text "Slower" -font $fn
155    pack $inner.speed.slowl -side left
156    ::scale $inner.speed.value -from 100 -to 1 \
157        -showvalue 0 -orient horizontal \
158        -variable ::Rappture::SequenceResult::_play(speed)
159    pack $inner.speed.value -side left
160    label $inner.speed.fastl -text "Faster" -font $fn
161    pack $inner.speed.fastl -side left
162
163    $itk_component(options) configure -command \
164        [list $itk_component(hull).popup activate $itk_component(options) above]
165
166    #
167    # Main viewer
168    #
169    itk_component add area {
170        frame $itk_interior.area
171    }
172    pack $itk_component(area) -expand yes -fill both
173
174    eval itk_initialize $args
175}
176
177# ----------------------------------------------------------------------
178# DESTRUCTOR
179# ----------------------------------------------------------------------
180itcl::body Rappture::SequenceResult::destructor {} {
181    pause  ;# stop any animation that might be playing
182}
183
184# ----------------------------------------------------------------------
185# USAGE: add <sequence> ?<settings>?
186#
187# Clients use this to add a data sequence to the viewer.  The optional
188# <settings> are used to configure the display of the data.  Allowed
189# settings are -color, -brightness, -width, -linestyle and -raise.
190# The only setting used here is -raise, which indicates the current
191# object.
192# ----------------------------------------------------------------------
193itcl::body Rappture::SequenceResult::add {dataobj {settings ""}} {
194    array set params {
195        -color auto
196        -brightness 0
197        -width 1
198        -raise 0
199        -linestyle solid
200        -description ""
201        -param ""
202    }
203    foreach {opt val} $settings {
204        if {![info exists params($opt)]} {
205            error "bad setting \"$opt\": should be [join [lsort [array names params]] {, }]"
206        }
207        set params($opt) $val
208    }
209
210    if {$params(-raise) && "" == $_topmost} {
211        set _topmost $dataobj
212    }
213    lappend _dlist $dataobj
214    $_dispatcher event -idle !rebuild
215}
216
217# ----------------------------------------------------------------------
218# USAGE: get
219#
220# Clients use this to query the list of data objects being displayed,
221# in order from bottom to top of this result.
222# ----------------------------------------------------------------------
223itcl::body Rappture::SequenceResult::get {} {
224    # put the dataobj list in order according to -raise options
225    set dlist $_dlist
226
227    set i [lsearch $_dlist $_topmost]
228    if {$i >= 0} {
229        set dlist [lreplace $dlist $i $i]
230        set dlist [linsert $dlist 0 $_topmost]
231    }
232    return $dlist
233}
234
235# ----------------------------------------------------------------------
236# USAGE: delete ?<dataobj1> <dataobj2> ...?
237#
238# Clients use this to delete a data object from the viewer.  If no
239# data objects are specified, then all data objects are deleted.
240# ----------------------------------------------------------------------
241itcl::body Rappture::SequenceResult::delete {args} {
242    if {[llength $args] == 0} {
243        set args $_dlist
244    }
245    pause
246
247    # delete all specified curves
248    set changed 0
249    foreach dataobj $args {
250        set pos [lsearch -exact $_dlist $dataobj]
251        if {$pos >= 0} {
252            set _dlist [lreplace $_dlist $pos $pos]
253            set changed 1
254
255            if {$dataobj == $_topmost} {
256                set _topmost ""
257            }
258        }
259    }
260
261    # if anything changed, then rebuild the plot
262    if {$changed} {
263        $_dispatcher event -idle !rebuild
264    }
265}
266
267# ----------------------------------------------------------------------
268# USAGE: scale ?<dataobj1> <dataobj2> ...?
269#
270# Sets the default limits for the overall plot according to the
271# limits of the data for all of the given <dataobj> objects.  This
272# accounts for all data objects--even those not showing on the screen.
273# Because of this, the limits are appropriate for all data objects as
274# the user scans through data in the ResultSet viewer.
275# ----------------------------------------------------------------------
276itcl::body Rappture::SequenceResult::scale {args} {
277    # do nothing
278}
279
280# ----------------------------------------------------------------------
281# USAGE: download coming
282# USAGE: download controls <downloadCommand>
283# USAGE: download now
284#
285# Clients use this method to create a downloadable representation
286# of the plot.  Returns a list of the form {ext string}, where
287# "ext" is the file extension (indicating the type of data) and
288# "string" is the data itself.
289# ----------------------------------------------------------------------
290itcl::body Rappture::SequenceResult::download {option args} {
291    if { ![winfo exists $itk_component(area).viewer] } {
292        return "";      # No data, no viewer, no download.
293    }
294    switch $option {
295        coming {
296            return [$itk_component(area).viewer download coming]
297        }
298        controls {
299            return [eval $itk_component(area).viewer download controls $args]
300        }
301        now {
302            if {0} {
303                # produce a movie of results
304                set rval ""
305                if {"" != $_topmost} {
306                    set max [$_topmost size]
307                    set all ""
308                    for {set i 0} {$i < $max} {incr i} {
309                        set dataobj [lindex [$_topmost value $i] 0]
310                        if {[catch {$dataobj tkimage} imh] == 0} {
311                            lappend all $imh
312                        }
313                    }
314                    if {[llength $all] > 0} {
315                        set delay [expr {int(ceil(pow($_play(speed)/10.0+2,2.0)*15))}]
316                        set rval [eval Rappture::icon::gif_animate $delay $all]
317                    }
318                }
319                if {[string length $rval] > 0} {
320                    return [list .gif $rval]
321                }
322                return ""
323            }
324            # otherwise, return download of single frame
325            return [eval $itk_component(area).viewer download now $args]
326        }
327        default {
328            error "bad option \"$option\": should be coming, controls, now"
329        }
330    }
331}
332
333# ----------------------------------------------------------------------
334# USAGE: play
335#
336# Invoked when the user hits the "play" button to play the current
337# sequence of frames as a movie.
338# ----------------------------------------------------------------------
339itcl::body Rappture::SequenceResult::play {} {
340    if { [llength $_indices] == 0 } {
341        return;                         # No frames (i.e. no data).
342    }
343    # cancel any existing animation
344    pause
345
346    # at the end? then restart fresh
347    if {$_pos >= [llength $_indices]-1} {
348        goto 0
349    }
350
351    # toggle the button to "pause" mode
352    $itk_component(play) configure \
353        -bitmap [Rappture::icon pause] \
354        -command [itcl::code $this pause]
355
356    # schedule the first frame
357    set delay [expr {int(ceil(pow($_play(speed)/10.0+2,2.0)*15))}]
358    set _afterId [after $delay [itcl::code $this _playFrame]]
359}
360
361# ----------------------------------------------------------------------
362# USAGE: pause
363#
364# Invoked when the user hits the "pause" button to stop playing the
365# current sequence of frames as a movie.
366# ----------------------------------------------------------------------
367itcl::body Rappture::SequenceResult::pause {} {
368    if {"" != $_afterId} {
369        catch {after cancel $_afterId}
370        set _afterId ""
371    }
372
373    # toggle the button to "play" mode
374    $itk_component(play) configure \
375        -bitmap [Rappture::icon play] \
376        -command [itcl::code $this play]
377}
378
379# ----------------------------------------------------------------------
380# USAGE: goto ?<index>?
381#
382# Used internally to move the current position of the animation to
383# the frame at a particular <index>.  If the <index> is not specified,
384# then it returns the current position.
385# ----------------------------------------------------------------------
386itcl::body Rappture::SequenceResult::goto {{newval ""}} {
387    if {"" == $newval} {
388        return $_pos
389    }
390    set _pos $newval
391    set val [$itk_component(dial) get -format label @$_pos]
392    $itk_component(dial) current $val
393}
394
395# ----------------------------------------------------------------------
396# USAGE: _rebuild
397#
398# Invoked automatically whenever the data displayed in this viewer
399# changes.  Loads the data from the topmost (current) value into
400# the viewer.
401# ----------------------------------------------------------------------
402itcl::body Rappture::SequenceResult::_rebuild {args} {
403    if {"" == $_topmost && [llength $_dlist] > 0} {
404        set _topmost [lindex $_dlist 0]
405    }
406
407    #
408    # If we have any data, then show the viewer.
409    # Otherwise, hide it.
410    #
411    set viewer $itk_component(area).viewer
412    if {[winfo exists $viewer]} {
413        if {"" == $_topmost} {
414            pack forget $viewer
415            pack forget $itk_component(player)
416            return
417        } else {
418            pack $viewer -expand yes -fill both
419            pack $itk_component(player) -side bottom -fill x
420        }
421    } else {
422        if {"" == $_topmost} {
423            return
424        }
425
426        set type ""
427        if {[$_topmost size] > 0} {
428            set dataobj [lindex [$_topmost value 0] 0]
429            set type [$dataobj info class]
430        }
431        switch -- $type {
432            ::Rappture::Curve {
433                Rappture::XyResult $viewer
434                pack $viewer -expand yes -fill both
435            }
436            ::Rappture::Image {
437                Rappture::ImageResult $viewer
438                pack $viewer -expand yes -fill both
439            }
440            ::Rappture::Field {
441                set dimensions ""
442                set dim ""
443                foreach dim [$dataobj components -dimensions] {
444                    # check to see if the dimensions are the same
445                    # for all elements of the field. i dont think
446                    # we can display fields of differing dimensions
447                    # within the same field object.
448                    if {"" != $dimensions} {
449                        if {$dimensions != $dim} {
450                            error "don't know how to view sequences of $type\
451                                with dimensions as follows:\
452                                [$dataobj components -dimensions]"
453                        }
454                    } else {
455                        set dimensions $dim
456                    }
457                }
458                switch -- $dimensions {
459                    2D {
460                        if { [$dataobj isunirect2d] } {
461                            set mode "heightmap"
462                        } else {
463                            set mode "vtk"
464                        }
465                        Rappture::Field2DResult $viewer -mode $mode
466                    }
467                    3D {
468                        Rappture::Field3DResult $viewer
469                    }
470                    default {
471                        error "don't know how to view sequences of $type\
472                            with $dimensions dimensions"
473                    }
474                }
475                pack $viewer -expand yes -fill both
476            }
477            ::Rappture::LibraryObj {
478                switch -- [$dataobj element -as type] {
479                    structure {
480                        Rappture::DeviceResult $viewer
481                        pack $viewer -expand yes -fill both
482                    }
483                    default {
484                        error "don't know how to view sequences of [$dataobj element -as type]"
485                    }
486                }
487            }
488            default {
489                puts stderr "don't know how to view sequences of type \"$type\""
490                puts stderr "Is the sequence empty?"
491                return
492            }
493        }
494    }
495
496    #
497    # Load the current sequence info the viewer.
498    #
499    $itk_component(indexLabel) configure -text [$_topmost hints indexlabel]
500
501    $viewer delete
502    $itk_component(dial) clear
503
504    set max [$_topmost size]
505    set all ""
506    for {set i 0} {$i < $max} {incr i} {
507        eval lappend all [$_topmost value $i]
508    }
509    eval $viewer scale $all
510
511    set _indices ""
512    for {set i 0} {$i < $max} {incr i} {
513        set index [$_topmost index $i]
514        eval $itk_component(dial) add $index
515        lappend _indices [lindex $index 0]
516    }
517    _fixValue
518}
519
520# ----------------------------------------------------------------------
521# USAGE: _playFrame
522#
523# Used internally to advance each frame in the animation.  Advances
524# the frame and displays it.  When we reach the end of the animation,
525# we either loop back or stop.
526# ----------------------------------------------------------------------
527itcl::body Rappture::SequenceResult::_playFrame {} {
528    set _pos [expr {$_pos+1}]
529    set last [expr {[llength $_indices]-1}]
530
531    if {$_pos > $last} {
532        if {$_play(loop)} {
533            set _pos 0
534        } else {
535            set _pos $last
536            pause
537            return
538        }
539    }
540    goto $_pos
541
542    set delay [expr {int(ceil(pow($_play(speed)/10.0+2,2.0)*15))}]
543    set _afterId [after $delay [itcl::code $this _playFrame]]
544}
545
546# ----------------------------------------------------------------------
547# USAGE: _fixValue
548#
549# Invoked automatically whenever the value on the dial changes.
550# Updates the viewer to display the value for the selected result.
551# ----------------------------------------------------------------------
552itcl::body Rappture::SequenceResult::_fixValue {} {
553    set viewer $itk_component(area).viewer
554    if {![winfo exists $viewer]} {
555        return
556    }
557    $viewer delete
558    if { $_topmost == "" } {
559        return
560    }
561    if { "" != [$_topmost hints indexlabel] } {
562        set val [$itk_component(dial) get -format label current]
563        $itk_component(indexValue) configure -text "= $val"
564        set _pos [lsearch -glob $_indices $val*]
565    }
566    # populate the label for this element
567    $itk_component(eleLabel) configure -text "[$_topmost label $_pos]"
568    foreach dataobj [$_topmost value $_pos] {
569        set settings "-color autoreset -width 2"
570        if {[catch {$dataobj hints style} style] == 0} {
571            eval lappend settings $style
572        }
573        $viewer add $dataobj $settings
574    }
575}
Note: See TracBrowser for help on using the repository browser.