-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectrumEditor.py
387 lines (276 loc) · 11.4 KB
/
SpectrumEditor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# $Id: SpectrumEditor.py,v 1.1.1.1 2008/04/20 15:03:22 elwinter Exp $
#******************************************************************************
# Import external modules.
# Standard modules
from tkinter.filedialog import Open
import tkinter
# Third-party modules
import Pmw
# Project modules
from ElementEditor import ElementEditor
from Parameter import Parameter
from ParameterEditor import ParameterEditor
from Spectrum import Spectrum
#******************************************************************************
class SpectrumEditor(ElementEditor):
"""Class to edit ModelEditor Spectrum objects.
Python implementation of the SpectrumEditor class which allows the
user to edit the fields of a <spectrum> element. This compound
widget is designed to be embedded in other widgets.
Attributes:
_typeOptionMenu: (Pmw.OptionMenu) For selecting the Spectrum type.
_fileEntryField: (Pmw.EntryField) For specifying a file associated
with the Spectrum.
_fileBrowseButton: (tkinter.Button) Summons a Open dialog to
select a file for the fileEntryField.
_parameterEditors: (ParameterEditor) List of ParameterEditor
objects for individual Parameters.
"""
#--------------------------------------------------------------------------
# Class constants
# Number of ParameterEditors to create
_nParameterEditors = 7
#--------------------------------------------------------------------------
def __init__(self,
parent = None,
spectrum = None,
*args, **kwargs):
"""Initialize this SpectrumEditor.
Parameters:
self: This object.
parent: (tkinter.Frame) Parent object for this widget.
spectrum: (Spectrum) Spectrum to initialize fields.
Return value:
None.
Description:
Initialize this SpectrumEditor.
"""
# Initialize the parent class (which calls _makeWidgets and
# set for this class).
if spectrum is None:
spectrum = Spectrum()
ElementEditor.__init__(self, parent, spectrum, *args, **kwargs)
#--------------------------------------------------------------------------
def _makeWidgets(self):
"""Build child widgets.
Parameters:
self: This object.
Return value:
None.
Description:
Create all of the GUI widgets required by this object.
"""
# Create any inherited widgets.
ElementEditor._makeWidgets(self)
# Create and grid a OptionMenu for the Spectrum type.
width = max([len(s) for s in Spectrum._validTypes])
optionMenu = Pmw.OptionMenu(self, items = sorted(Spectrum._validTypes),
label_text = 'Spectrum Type:',
labelpos = 'w',
menubutton_width = width,
command = self._onSelectType)
optionMenu.grid(row = 0, column = 0, sticky = 'w')
self._typeOptionMenu = optionMenu
self._balloon.bind(self._typeOptionMenu, 'Set the spectrum type.')
# Create and grid an arbitrary string EntryField for the
# Spectrum file, and an accompanying button which summons an
# Open dialog.
entryField = Pmw.EntryField(self, labelpos = 'w', label_text = 'File:')
entryField.grid(row = 0, column = 1, sticky = 'e')
self._fileEntryField = entryField
self._balloon.bind(self._fileEntryField,
'Enter the path to the file of data for this'\
' spectrum.')
button = tkinter.Button(self, text = 'Browse', command = self.onBrowse)
button.grid(row = 0, column = 2, sticky = 'e')
self._fileBrowseButton = button
self._balloon.bind(self._fileBrowseButton,
'Browse to the file of data for this spectrum.')
# Create and grid a Frame for the set of ParameterEditors.
frame = tkinter.Frame(self)
frame.grid(row = 1, column = 0, columnspan = 3, sticky = 'ew')
# Create and grid a set of ParameterEditors.
self._parameterEditors = []
for row in range(SpectrumEditor._nParameterEditors):
if row == 0:
showLabels = True
else:
showLabels = False
parameterEditor = ParameterEditor(frame, showLabels = showLabels)
parameterEditor.grid(row = row, column = 0, sticky = 'ew')
self._parameterEditors.append(parameterEditor)
#--------------------------------------------------------------------------
def get(self):
"""Return the contents of the editor.
Parameters:
self: This object.
Return value:
Copy of Spectrum being edited.
Description:
Return a new Spectrum containing the current state of the
editor.
"""
# Fetch the type.
type = self._typeOptionMenu.getvalue()
# Fetch the file (mapping empty string to None).
file = self._fileEntryField.getvalue()
if file == '':
file = None
# Get the contents of each active ParameterEditor.
parameters = [parameterEditor.get() \
for parameterEditor in \
self._parameterEditors[:self._nActiveParameterEditors]]
# Create the new Spectrum.
spectrum = Spectrum(type, file, parameters)
# Return the new Spectrum.
return spectrum
def set(self, spectrum):
"""Set the editor state.
Parameters:
self: This object.
spectrum: (Spectrum): Object to set in this editor.
Return value:
None.
Description:
Set the fields using a Spectrum.
"""
# Set the type OptionMenu.
type = spectrum.getType()
self._typeOptionMenu.setvalue(type)
# Set the file field (mapping None to empty string).
file = spectrum.getFile()
if file is None:
file = ''
self._fileEntryField.setvalue(file)
if type == 'FileFunction':
self._fileEntryField.configure(entry_state = 'normal')
self._fileBrowseButton.configure(state = 'normal')
else:
self._fileEntryField.configure(entry_state = 'disabled')
self._fileBrowseButton.configure(state = 'disabled')
# Initially clear and disable all ParameterEditors (to prevent
# shadowing of old values).
for parameterEditor in self._parameterEditors:
parameterEditor.clear()
parameterEditor.disable()
# Use the current Parameters to fill in and enable the
# required number of ParameterEditors.
parameters = spectrum.getParameters()
i = 0
for parameter in parameters:
self._parameterEditors[i].enable()
self._parameterEditors[i].set(parameter)
i += 1
self._nActiveParameterEditors = i
#--------------------------------------------------------------------------
def enable(self):
"""Activate all child widgets.
Parameters:
self: This object.
Return value:
None:
Description:
Activate all of the child widgets in this SpectrumEditor.
"""
self._typeOptionMenu.configure(menubutton_state = 'normal')
self._fileEntryField.configure(entry_state = 'normal')
self._fileBrowseButton.configure(state = 'normal')
for parameterEditor in self._parameterEditors:
parameterEditor.enable()
def disable(self):
"""Deactivate all child widgets.
Parameters:
self: This object.
Return value:
None:
Description:
Deactivate all of the child widgets in this SpectrumEditor.
"""
self._typeOptionMenu.configure(menubutton_state = 'disabled')
self._fileEntryField.configure(entry_state = 'disabled')
self._fileBrowseButton.configure(state = 'disabled')
for parameterEditor in self._parameterEditors:
parameterEditor.disable()
#--------------------------------------------------------------------------
def _onSelectType(self, type):
"""Process selection events in the type list box.
Parameters:
self: This object.
type: (string) New Spectrum type string.
Return value:
None.
Description:
Process the selection of a new Spectrum type in the type
OptionMenu. If a new Spectrum type is selected, clear the
current Spectrum and create a default Spectrum of the new
type.
"""
# If the new type is the same as the previous type, no change
# is needed.
if type == self._referenceElement.getType():
return
# A new Spectrum type has been selected, so create a new one.
spectrum = Spectrum(type = type)
# Set the editor with the new Spectrum.
self.set(spectrum)
self.commit()
#--------------------------------------------------------------------------
def onBrowse(self):
"""Present the File Open dialog.
Parameters:
self: This object.
Return value:
String containing the path to the selected file, or None if no
file was selected.
Description:
Display the File/Open dialog box and return the path to the
selected file.
"""
# Present the file/open dialog.
path = Open().show()
# If no file was selected, return.
if path == '':
return
# Save the current path in the file entry field.
self._fileEntryField.setvalue(path)
#******************************************************************************
# Self-test code.
import xml.dom.minidom
domDocument = xml.dom.minidom.Document()
# Printing routine for buttons.
def _print(editor):
print (editor.get())
if editor.getChanged():
print ("Changed by the editor.")
# XML printing routine for buttons.
def _printXML(editor):
element = editor.get()
dom = element.toDom(domDocument)
print (dom.toxml())
if editor.getChanged():
print ("Changed by the editor.")
if __name__ == '__main__':
# Create the root window.
root = tkinter.Tk()
Pmw.initialise(root)
root.title('SpectrumEditor test')
# Create and grid an editor for a default Spectrum, a button to
# commit changes, and a button to dump its contents.
spectrumEditor = SpectrumEditor()
spectrumEditor.grid(row = 0, column = 0)
commitButton = tkinter.Button(root, text = 'Commit',
command = spectrumEditor.commit)
commitButton.grid(row = 0, column = 1)
resetButton = tkinter.Button(root, text = 'Reset',
command = spectrumEditor.reset)
resetButton.grid(row = 0, column = 2)
printButton = tkinter.Button(root, text = 'Print',
command = \
lambda: _print(spectrumEditor))
printButton.grid(row = 0, column = 3)
printXMLButton = tkinter.Button(root, text = 'Print XML',
command = \
lambda: _printXML(spectrumEditor))
printXMLButton.grid(row = 0, column = 4)
# Enter the main program loop.
root.mainloop()