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 | |
---|
31 | protected method _redraw {} |
---|
32 | protected method _type {xmlobj} |
---|
33 | |
---|
34 | private variable _current "" ;# active device editor |
---|
35 | } |
---|
36 | |
---|
37 | itk::usual DeviceEditor { |
---|
38 | } |
---|
39 | |
---|
40 | # ---------------------------------------------------------------------- |
---|
41 | # CONSTRUCTOR |
---|
42 | # ---------------------------------------------------------------------- |
---|
43 | itcl::body Rappture::DeviceEditor::constructor {owner args} { |
---|
44 | itk_component add top { |
---|
45 | frame $itk_interior.top |
---|
46 | } |
---|
47 | pack $itk_component(top) -fill x |
---|
48 | |
---|
49 | eval itk_initialize $args |
---|
50 | } |
---|
51 | |
---|
52 | |
---|
53 | # ---------------------------------------------------------------------- |
---|
54 | # USAGE: value ?-check? ?<newval>? |
---|
55 | # |
---|
56 | # Clients use this to query/set the value for this widget. With |
---|
57 | # no args, it returns the current value for the widget. If the |
---|
58 | # <newval> is specified, it sets the value of the widget and |
---|
59 | # sends a <<Value>> event. If the -check flag is included, the |
---|
60 | # new value is not actually applied, but just checked for correctness. |
---|
61 | # ---------------------------------------------------------------------- |
---|
62 | itcl::body Rappture::DeviceEditor::value {args} { |
---|
63 | set onlycheck 0 |
---|
64 | set i [lsearch -exact $args -check] |
---|
65 | if {$i >= 0} { |
---|
66 | set onlycheck 1 |
---|
67 | set args [lreplace $args $i $i] |
---|
68 | } |
---|
69 | |
---|
70 | if {[llength $args] == 1} { |
---|
71 | if {$_xmlobj != ""} { |
---|
72 | if {$itk_option(-autocleanup)} { |
---|
73 | # delete any existing object |
---|
74 | itcl::delete object $_xmlobj |
---|
75 | } |
---|
76 | set _xmlobj "" |
---|
77 | } |
---|
78 | |
---|
79 | set newval [lindex $args 0] |
---|
80 | if {$newval != ""} { |
---|
81 | if {$onlycheck} { |
---|
82 | return |
---|
83 | } |
---|
84 | if {![Rappture::library isvalid $newval]} { |
---|
85 | error "bad value \"$newval\": should be Rappture::Library" |
---|
86 | } |
---|
87 | set _xmlobj $newval |
---|
88 | } |
---|
89 | _redraw |
---|
90 | event generate $itk_component(hull) <<Value>> |
---|
91 | |
---|
92 | } elseif {[llength $args] == 0} { |
---|
93 | sync ;# querying -- must sync controls with the value |
---|
94 | } else { |
---|
95 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
96 | } |
---|
97 | return $_xmlobj |
---|
98 | } |
---|
99 | |
---|
100 | # ---------------------------------------------------------------------- |
---|
101 | # USAGE: download coming |
---|
102 | # USAGE: download controls <downloadCommand> |
---|
103 | # USAGE: download now |
---|
104 | # |
---|
105 | # Clients use this method to create a downloadable representation |
---|
106 | # of the plot. Returns a list of the form {ext string}, where |
---|
107 | # "ext" is the file extension (indicating the type of data) and |
---|
108 | # "string" is the data itself. |
---|
109 | # ---------------------------------------------------------------------- |
---|
110 | itcl::body Rappture::DeviceEditor::download {option args} { |
---|
111 | if {"" != $_current} { |
---|
112 | return [eval $_current download $option $args] |
---|
113 | } |
---|
114 | return "" |
---|
115 | } |
---|
116 | |
---|
117 | # ---------------------------------------------------------------------- |
---|
118 | # USAGE: _redraw |
---|
119 | # |
---|
120 | # Used internally to load new device data into the appropriate |
---|
121 | # editor. If the editor needs to be created, it is created and |
---|
122 | # activated within this widget. Then, the data is loaded into |
---|
123 | # the editor. |
---|
124 | # ---------------------------------------------------------------------- |
---|
125 | itcl::body Rappture::DeviceEditor::_redraw {} { |
---|
126 | if {$_current != ""} { |
---|
127 | $_current configure -device "" |
---|
128 | set _current "" |
---|
129 | } |
---|
130 | switch -- [_type $_xmlobj] { |
---|
131 | molecule { |
---|
132 | if {![winfo exists $itk_component(hull).mol]} { |
---|
133 | catch {destroy $itk_component(hull).dev} |
---|
134 | Rappture::MoleculeViewer $itk_component(hull).mol $this |
---|
135 | pack $itk_component(hull).mol -expand yes -fill both |
---|
136 | } |
---|
137 | $itk_component(hull).mol configure -device $_xmlobj |
---|
138 | |
---|
139 | set _current $itk_component(hull).mol |
---|
140 | } |
---|
141 | device1D { |
---|
142 | if {![winfo exists $itk_component(hull).dev]} { |
---|
143 | catch {destroy $itk_component(hull).mol} |
---|
144 | Rappture::DeviceViewer1D $itk_component(hull).dev $this |
---|
145 | pack $itk_component(hull).dev -expand yes -fill both |
---|
146 | } |
---|
147 | $itk_component(hull).dev configure -device $_xmlobj |
---|
148 | |
---|
149 | set _current $itk_component(hull).dev |
---|
150 | } |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | # ---------------------------------------------------------------------- |
---|
155 | # USAGE: _type <xmlobj> |
---|
156 | # |
---|
157 | # Used internally to determine the type of the device data stored |
---|
158 | # within the <xmlobj>. Returns a name such as "molecule" or |
---|
159 | # "device1D" indicating the type of editor that should be used to |
---|
160 | # display the data. |
---|
161 | # ---------------------------------------------------------------------- |
---|
162 | itcl::body Rappture::DeviceEditor::_type {xmlobj} { |
---|
163 | if {$xmlobj == ""} { |
---|
164 | return "" |
---|
165 | } |
---|
166 | if {[llength [$xmlobj children -type molecule components]] > 0} { |
---|
167 | return "molecule" |
---|
168 | } |
---|
169 | return "device1D" |
---|
170 | } |
---|