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

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