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 | # ====================================================================== |
---|
15 | package require Itk |
---|
16 | |
---|
17 | option add *DeviceEditor.autoCleanUp yes widgetDefault |
---|
18 | |
---|
19 | itcl::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 | |
---|
33 | protected method _redraw {} |
---|
34 | protected method _type {xmlobj} |
---|
35 | |
---|
36 | private variable _current "" ;# active device editor |
---|
37 | |
---|
38 | public common _hubvisHosts "" |
---|
39 | |
---|
40 | public proc setHubvisServer {namelist} { |
---|
41 | if {[regexp {^[a-zA-Z0-9\.]+:[0-9]+(,[a-zA-Z0-9\.]+:[0-9]+)*$} $namelist match]} { |
---|
42 | set _hubvisHosts $namelist |
---|
43 | } else { |
---|
44 | error "bad visualization server address \"$namelist\": should be host:port,host:port,..." |
---|
45 | } |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | itk::usual DeviceEditor { |
---|
50 | } |
---|
51 | |
---|
52 | # must use this name -- plugs into Rappture::resources::load |
---|
53 | proc deviceEditor_init_resources {} { |
---|
54 | Rappture::resources::register \ |
---|
55 | hubvis_server Rappture::DeviceEditor::setHubvisServer |
---|
56 | } |
---|
57 | |
---|
58 | # ---------------------------------------------------------------------- |
---|
59 | # CONSTRUCTOR |
---|
60 | # ---------------------------------------------------------------------- |
---|
61 | itcl::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 | # ---------------------------------------------------------------------- |
---|
80 | itcl::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 | # ---------------------------------------------------------------------- |
---|
127 | itcl::body Rappture::DeviceEditor::add {dataobj {settings ""}} { |
---|
128 | |
---|
129 | set _xmlobj $dataobj |
---|
130 | |
---|
131 | if {"" == $_current} { # Make sure viewer instance exists |
---|
132 | _redraw |
---|
133 | } |
---|
134 | |
---|
135 | eval $_current add $dataobj [list $settings] |
---|
136 | } |
---|
137 | |
---|
138 | # ---------------------------------------------------------------------- |
---|
139 | # USAGE: delete ?<dataobj> <dataobj> ...? |
---|
140 | # |
---|
141 | # Clients use this to delete a dataobj from the plot. If no dataobjs |
---|
142 | # are specified, then all dataobjs are deleted. |
---|
143 | # ---------------------------------------------------------------------- |
---|
144 | itcl::body Rappture::DeviceEditor::delete {args} { |
---|
145 | |
---|
146 | if {"" != $_current} { |
---|
147 | eval $_current delete $args |
---|
148 | set _xmlobj [lindex [$_current get] end] |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | # ---------------------------------------------------------------------- |
---|
153 | # USAGE: download coming |
---|
154 | # USAGE: download controls <downloadCommand> |
---|
155 | # USAGE: download now |
---|
156 | # |
---|
157 | # Clients use this method to create a downloadable representation |
---|
158 | # of the plot. Returns a list of the form {ext string}, where |
---|
159 | # "ext" is the file extension (indicating the type of data) and |
---|
160 | # "string" is the data itself. |
---|
161 | # ---------------------------------------------------------------------- |
---|
162 | itcl::body Rappture::DeviceEditor::download {option args} { |
---|
163 | if {"" != $_current} { |
---|
164 | return [eval $_current download $option $args] |
---|
165 | } |
---|
166 | return "" |
---|
167 | } |
---|
168 | |
---|
169 | # ---------------------------------------------------------------------- |
---|
170 | # USAGE: _redraw |
---|
171 | # |
---|
172 | # Used internally to load new device data into the appropriate |
---|
173 | # editor. If the editor needs to be created, it is created and |
---|
174 | # activated within this widget. Then, the data is loaded into |
---|
175 | # the editor. |
---|
176 | # ---------------------------------------------------------------------- |
---|
177 | itcl::body Rappture::DeviceEditor::_redraw {} { |
---|
178 | if {$_current != ""} { |
---|
179 | $_current configure -device "" |
---|
180 | set _current "" |
---|
181 | } |
---|
182 | switch -- [_type $_xmlobj] { |
---|
183 | molecule { |
---|
184 | if {![winfo exists $itk_component(hull).mol]} { |
---|
185 | catch {destroy $itk_component(hull).dev} |
---|
186 | if {"" != $_hubvisHosts} { |
---|
187 | Rappture::MolvisViewer $itk_component(hull).mol $_hubvisHosts |
---|
188 | } else { |
---|
189 | Rappture::MoleculeViewer $itk_component(hull).mol $this |
---|
190 | } |
---|
191 | pack $itk_component(hull).mol -expand yes -fill both |
---|
192 | } |
---|
193 | |
---|
194 | set _current $itk_component(hull).mol |
---|
195 | } |
---|
196 | device1D { |
---|
197 | if {![winfo exists $itk_component(hull).dev]} { |
---|
198 | catch {destroy $itk_component(hull).mol} |
---|
199 | Rappture::DeviceViewer1D $itk_component(hull).dev $this |
---|
200 | pack $itk_component(hull).dev -expand yes -fill both |
---|
201 | } |
---|
202 | |
---|
203 | set _current $itk_component(hull).dev |
---|
204 | } |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | # ---------------------------------------------------------------------- |
---|
209 | # USAGE: _type <xmlobj> |
---|
210 | # |
---|
211 | # Used internally to determine the type of the device data stored |
---|
212 | # within the <xmlobj>. Returns a name such as "molecule" or |
---|
213 | # "device1D" indicating the type of editor that should be used to |
---|
214 | # display the data. |
---|
215 | # ---------------------------------------------------------------------- |
---|
216 | itcl::body Rappture::DeviceEditor::_type {xmlobj} { |
---|
217 | if {$xmlobj == ""} { |
---|
218 | return "" |
---|
219 | } |
---|
220 | if {[llength [$xmlobj children -type molecule components]] > 0} { |
---|
221 | return "molecule" |
---|
222 | } |
---|
223 | return "device1D" |
---|
224 | } |
---|