source: branches/1.5/gui/scripts/deviceEditor.tcl @ 6127

Last change on this file since 6127 was 6127, checked in by ldelgass, 9 years ago

Merge r6052:6053,r6066:6069,r6080 from trunk

File size: 7.5 KB
Line 
1# -*- mode: tcl; indent-tabs-mode: nil -*-
2# ----------------------------------------------------------------------
3#  COMPONENT: deviceEditor - general-purpose device editor
4#
5#  This widget takes a <structure> description and chooses a specific
6#  editor appropriate for the contents of the structure.  If the
7#  <structure> contains a molecule, then it chooses a MoleculeViewer.
8#  If it contains a 1D device, then it chooses a deviceViewer1D.
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
18option add *DeviceEditor.autoCleanUp yes widgetDefault
19
20itcl::class Rappture::DeviceEditor {
21    inherit itk::Widget Rappture::ControlOwner
22
23    itk_option define -autocleanup autoCleanUp AutoCleanUp 1
24
25    constructor {owner args} {
26        Rappture::ControlOwner::constructor $owner
27    } {
28        # defined below
29    }
30    public method value {args}
31    public method download {option args}
32    public method add {dataobj {settings ""}}
33    public method delete {args}
34    public method parameters {title args} {
35        # do nothing
36    }
37    public method snap {w h}
38
39    protected method _redraw {}
40    protected method _type {xmlobj}
41
42    private variable _current ""  ;# active device editor
43
44    public common _molvisHosts ""
45
46    public proc setMolvisServer {namelist} {
47        if {[regexp {^[a-zA-Z0-9\.]+:[0-9]+(,[a-zA-Z0-9\.]+:[0-9]+)*$} $namelist match]} {
48            set _molvisHosts $namelist
49        } else {
50            error "bad visualization server address \"$namelist\": should be host:port,host:port,..."
51        }
52    }
53}
54
55itk::usual DeviceEditor {
56}
57
58# ----------------------------------------------------------------------
59# CONSTRUCTOR
60# ----------------------------------------------------------------------
61itcl::body Rappture::DeviceEditor::constructor {owner args} {
62    itk_component add top {
63        frame $itk_interior.top
64    }
65    pack $itk_component(top) -fill x
66
67    eval itk_initialize $args
68}
69
70
71# ----------------------------------------------------------------------
72# USAGE: value ?-check? ?<newval>?
73#
74# Clients use this to query/set the value for this widget.  With
75# no args, it returns the current value for the widget.  If the
76# <newval> is specified, it sets the value of the widget and
77# sends a <<Value>> event.  If the -check flag is included, the
78# new value is not actually applied, but just checked for correctness.
79# ----------------------------------------------------------------------
80itcl::body Rappture::DeviceEditor::value {args} {
81    set onlycheck 0
82    set i [lsearch -exact $args -check]
83    if {$i >= 0} {
84        set onlycheck 1
85        set args [lreplace $args $i $i]
86    }
87
88    if {[llength $args] == 1} {
89        if {$_xmlobj != ""} {
90            if {$itk_option(-autocleanup)} {
91                # delete any existing object
92                itcl::delete object $_xmlobj
93            }
94            set _xmlobj ""
95        }
96
97        set newval [lindex $args 0]
98        if {$newval != ""} {
99            if {$onlycheck} {
100                return
101            }
102            if {![Rappture::library isvalid $newval]} {
103                error "bad value \"$newval\": should be Rappture::Library"
104            }
105            set _xmlobj $newval
106        }
107        _redraw
108        $_current configure -device $_xmlobj
109        event generate $itk_component(hull) <<Value>>
110
111    } elseif {[llength $args] == 0} {
112        sync  ;# querying -- must sync controls with the value
113    } else {
114        error "wrong # args: should be \"value ?-check? ?newval?\""
115    }
116    return $_xmlobj
117}
118
119# ----------------------------------------------------------------------
120# USAGE: add <dataobj> ?<settings>?
121#
122# Clients use this to add a data object to the plot.  The optional
123# <settings> are used to configure the plot. Allowed settings are
124# -color, -brightness, -width, -linestyle, and -raise. Only
125# -brightness and -raise do anything.
126# ----------------------------------------------------------------------
127itcl::body Rappture::DeviceEditor::add {dataobj {settings ""}} {
128    set _xmlobj $dataobj
129    if {"" == $_current} {    # Make sure viewer instance exists
130        _redraw
131    }
132    eval $_current add $dataobj [list $settings]
133}
134
135# ----------------------------------------------------------------------
136# USAGE: delete ?<dataobj> <dataobj> ...?
137#
138# Clients use this to delete a dataobj from the plot. If no dataobjs
139# are specified, then all dataobjs are deleted.
140# ----------------------------------------------------------------------
141itcl::body Rappture::DeviceEditor::delete {args} {
142    if {"" != $_current} {
143        eval $_current delete $args
144        set _xmlobj [lindex [$_current get] end]
145    }
146}
147
148# ----------------------------------------------------------------------
149# USAGE: download coming
150# USAGE: download controls <downloadCommand>
151# USAGE: download now
152#
153# Clients use this method to create a downloadable representation
154# of the plot.  Returns a list of the form {ext string}, where
155# "ext" is the file extension (indicating the type of data) and
156# "string" is the data itself.
157# ----------------------------------------------------------------------
158itcl::body Rappture::DeviceEditor::download {option args} {
159    if {"" != $_current} {
160        return [eval $_current download $option $args]
161    }
162    return ""
163}
164
165# ----------------------------------------------------------------------
166# USAGE: _redraw
167#
168# Used internally to load new device data into the appropriate
169# editor.  If the editor needs to be created, it is created and
170# activated within this widget.  Then, the data is loaded into
171# the editor.
172# ----------------------------------------------------------------------
173itcl::body Rappture::DeviceEditor::_redraw {} {
174    if {$_current != ""} {
175        $_current configure -device ""
176        set _current ""
177    }
178    switch -- [_type $_xmlobj] {
179        molecule {
180            if { ![winfo exists $itk_component(hull).mol] } {
181                catch {
182                    destroy $itk_component(hull).dev
183                }
184                Rappture::MolvisViewer $itk_component(hull).mol
185                pack $itk_component(hull).mol -expand yes -fill both
186            }
187            set _current $itk_component(hull).mol
188        }
189        device1D {
190            if { ![winfo exists $itk_component(hull).dev] } {
191                catch {
192                    destroy $itk_component(hull).mol
193                }
194                Rappture::DeviceViewer1D $itk_component(hull).dev $this
195                pack $itk_component(hull).dev -expand yes -fill both
196            }
197            set _current $itk_component(hull).dev
198        }
199    }
200}
201
202# ----------------------------------------------------------------------
203# USAGE: _type <xmlobj>
204#
205# Used internally to determine the type of the device data stored
206# within the <xmlobj>.  Returns a name such as "molecule" or
207# "device1D" indicating the type of editor that should be used to
208# display the data.
209# ----------------------------------------------------------------------
210itcl::body Rappture::DeviceEditor::_type {xmlobj} {
211    if {$xmlobj == ""} {
212        return ""
213    }
214    if {[llength [$xmlobj children -type molecule components]] > 0} {
215        return "molecule"
216    }
217    return "device1D"
218}
219
220itcl::body Rappture::DeviceEditor::snap { w h } {
221    return [$_current snap $w $h]
222}
Note: See TracBrowser for help on using the repository browser.