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 |
---|
11 | # Purdue Research Foundation, West Lafayette, IN |
---|
12 | # ====================================================================== |
---|
13 | package require Itk |
---|
14 | |
---|
15 | option add *DeviceEditor.width 5i widgetDefault |
---|
16 | option add *DeviceEditor.height 5i widgetDefault |
---|
17 | |
---|
18 | itcl::class Rappture::DeviceEditor { |
---|
19 | inherit itk::Widget |
---|
20 | |
---|
21 | constructor {tool args} { # defined below } |
---|
22 | |
---|
23 | public method value {args} |
---|
24 | |
---|
25 | protected method _redraw {} |
---|
26 | protected method _type {xmlobj} |
---|
27 | |
---|
28 | private variable _tool "" ;# tool containing this editor |
---|
29 | private variable _xmlobj "" ;# XML <structure> object |
---|
30 | } |
---|
31 | |
---|
32 | itk::usual DeviceEditor { |
---|
33 | } |
---|
34 | |
---|
35 | # ---------------------------------------------------------------------- |
---|
36 | # CONSTRUCTOR |
---|
37 | # ---------------------------------------------------------------------- |
---|
38 | itcl::body Rappture::DeviceEditor::constructor {tool args} { |
---|
39 | set _tool $tool |
---|
40 | |
---|
41 | itk_option add hull.width hull.height |
---|
42 | pack propagate $itk_component(hull) no |
---|
43 | |
---|
44 | itk_component add top { |
---|
45 | frame $itk_interior.top |
---|
46 | } |
---|
47 | pack $itk_component(top) -fill x |
---|
48 | |
---|
49 | itk_component add editors { |
---|
50 | Rappture::Notebook $itk_interior.editors |
---|
51 | } |
---|
52 | pack $itk_component(editors) -expand yes -fill both |
---|
53 | |
---|
54 | eval itk_initialize $args |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | # ---------------------------------------------------------------------- |
---|
59 | # USAGE: value ?-check? ?<newval>? |
---|
60 | # |
---|
61 | # Clients use this to query/set the value for this widget. With |
---|
62 | # no args, it returns the current value for the widget. If the |
---|
63 | # <newval> is specified, it sets the value of the widget and |
---|
64 | # sends a <<Value>> event. If the -check flag is included, the |
---|
65 | # new value is not actually applied, but just checked for correctness. |
---|
66 | # ---------------------------------------------------------------------- |
---|
67 | itcl::body Rappture::DeviceEditor::value {args} { |
---|
68 | set onlycheck 0 |
---|
69 | set i [lsearch -exact $args -check] |
---|
70 | if {$i >= 0} { |
---|
71 | set onlycheck 1 |
---|
72 | set args [lreplace $args $i $i] |
---|
73 | } |
---|
74 | |
---|
75 | if {[llength $args] == 1} { |
---|
76 | # delete any existing object |
---|
77 | if {$_xmlobj != ""} { |
---|
78 | itcl::delete object $_xmlobj |
---|
79 | set _xmlobj "" |
---|
80 | } |
---|
81 | set newval [lindex $args 0] |
---|
82 | if {$newval != ""} { |
---|
83 | if {$onlycheck} { |
---|
84 | return |
---|
85 | } |
---|
86 | if {![Rappture::library isvalid $newval]} { |
---|
87 | error "bad value \"$newval\": should be Rappture::Library" |
---|
88 | } |
---|
89 | set _xmlobj $newval |
---|
90 | } |
---|
91 | _redraw |
---|
92 | event generate $itk_component(hull) <<Value>> |
---|
93 | |
---|
94 | } elseif {[llength $args] != 0} { |
---|
95 | error "wrong # args: should be \"value ?-check? ?newval?\"" |
---|
96 | } |
---|
97 | return $_xmlobj |
---|
98 | } |
---|
99 | |
---|
100 | # ---------------------------------------------------------------------- |
---|
101 | # USAGE: _redraw |
---|
102 | # |
---|
103 | # Used internally to load new device data into the appropriate |
---|
104 | # editor. If the editor needs to be created, it is created and |
---|
105 | # activated within this widget. Then, the data is loaded into |
---|
106 | # the editor. |
---|
107 | # ---------------------------------------------------------------------- |
---|
108 | itcl::body Rappture::DeviceEditor::_redraw {} { |
---|
109 | switch -- [_type $_xmlobj] { |
---|
110 | molecule { |
---|
111 | if {[catch {$itk_component(editors) page molecule} p]} { |
---|
112 | set p [$itk_component(editors) insert end molecule] |
---|
113 | Rappture::MoleculeViewer $p.mol $_tool |
---|
114 | pack $p.mol -expand yes -fill both |
---|
115 | } |
---|
116 | $p.mol configure -device $_xmlobj |
---|
117 | $itk_component(editors) current molecule |
---|
118 | } |
---|
119 | device1D { |
---|
120 | if {[catch {$itk_component(editors) page device1D} p]} { |
---|
121 | set p [$itk_component(editors) insert end device1D] |
---|
122 | Rappture::DeviceViewer1D $p.dev $_tool |
---|
123 | pack $p.dev -expand yes -fill both |
---|
124 | } |
---|
125 | $p.dev configure -device $_xmlobj |
---|
126 | $itk_component(editors) current device1D |
---|
127 | } |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | # ---------------------------------------------------------------------- |
---|
132 | # USAGE: _type <xmlobj> |
---|
133 | # |
---|
134 | # Used internally to determine the type of the device data stored |
---|
135 | # within the <xmlobj>. Returns a name such as "molecule" or |
---|
136 | # "device1D" indicating the type of editor that should be used to |
---|
137 | # display the data. |
---|
138 | # ---------------------------------------------------------------------- |
---|
139 | itcl::body Rappture::DeviceEditor::_type {xmlobj} { |
---|
140 | if {$xmlobj == ""} { |
---|
141 | return "" |
---|
142 | } |
---|
143 | if {[llength [$xmlobj children -type molecule components]] > 0} { |
---|
144 | return "molecule" |
---|
145 | } |
---|
146 | return "device1D" |
---|
147 | } |
---|