source: trunk/gui/scripts/deviceEditor.tcl @ 872

Last change on this file since 872 was 770, checked in by nkissebe, 17 years ago

rename resource variable to look for to molvis_server instead of hubvis_server

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