-
Notifications
You must be signed in to change notification settings - Fork 0
/
SourceEditor.py
287 lines (199 loc) · 8.08 KB
/
SourceEditor.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
# $Id: SourceEditor.py,v 1.1.1.1.6.2 2017/02/01 04:22:16 jasercio Exp $
#******************************************************************************
# Import external modules.
# Standard modules
import tkinter
# Third-party modules
import Pmw
# Project modules
from ElementEditor import ElementEditor
from Source import Source
from SpectrumEditor import SpectrumEditor
from SpatialModelEditor import SpatialModelEditor
#******************************************************************************
class SourceEditor(ElementEditor):
"""Class to edit ModelEditor Source objects.
Python implementation of the SourceEditor class which allows the
user to edit the fields of a <source> element. This compound
widget is designed to be embedded in other widgets.
Attributes:
_nameEntryField: (Pmw.EntryField) Controls editing of the Source
name.
_typeLabel: (tkinter.Label) Displays the Source type. Note that
this value cannot be changed by the user.
_spectrumEditor: (SpectrumEditor) For editing the Spectrum.
_spatialModelEditor: (SpatialModelEditor) For editing the
SpatialModel.
"""
#--------------------------------------------------------------------------
def __init__(self,
parent = None,
source = None,
*args, **kwargs):
"""Initialize this object.
Parameters:
self: This object.
parent: (tkinter.Frame) Parent object for this widget.
source: (Source) Source to initialize fields.
Return value:
None.
Description:
Initialize this SourceEditor.
"""
# Initialize the parent class (which calls _makeWidgets and
# set for this class).
if source is None:
source = Source()
ElementEditor.__init__(self, parent, source, *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 an EntryField for the Source name.
entryField = Pmw.EntryField(self, label_text = 'Source Name:',
labelpos = 'w')
entryField.grid(row = 0, column = 0, sticky = 'w')
self._nameEntryField = entryField
self._balloon.bind(self._nameEntryField, 'Enter the source name.')
# Create and grid a Label for the Source type.
label = tkinter.Label(self, text = 'Source Type:')
label.grid(row = 0, column = 1, sticky = 'e')
self._typeLabel = label
# Create and grid a SpectrumEditor.
spectrumEditor = SpectrumEditor(self, borderwidth = 2,
relief = 'ridge')
spectrumEditor.grid(row = 1, column = 0, columnspan = 2, sticky = 'ew')
self._spectrumEditor = spectrumEditor
# Create and grid a SpatialModel editor.
spatialModelEditor = SpatialModelEditor(self, borderwidth = 2,
relief = 'ridge')
spatialModelEditor.grid(row = 2, column = 0, columnspan = 2,
sticky = 'ew')
self._spatialModelEditor = spatialModelEditor
#--------------------------------------------------------------------------
def get(self):
"""Return the contents of the editor.
Parameters:
self: This object.
Return value:
Copy of Source being edited.
Description:
Return a new Source containing the current state of the
editor.
"""
# Get the name EntryField value.
name = self._nameEntryField.getvalue()
# Fetch the type Label and strip out header text.
type = self._typeLabel['text']
type = type.replace('Source Type:', '')
# Get the contents of the SpectrumEditor.
spectrum = self._spectrumEditor.get()
# Get the contents of the SpatialModelEditor.
spatialModel = self._spatialModelEditor.get()
# Make a new Source from the current field values.
source = Source(name, type, spectrum, spatialModel)
# Return the new Source.
return source
def set(self, source):
"""Set the editor state.
Parameters:
self: This object.
source: (Source): Object to set in this editor.
Return value:
None.
Description:
Set the fields using a Source.
"""
# Fill in the name EntryField.
name = source.getName()
self._nameEntryField.setvalue(name)
# Fill in the type Label.
type = source.getType()
self._typeLabel.configure(text = 'Source Type:' + type)
# Fill out the SpectrumEditor.
spectrum = source.getSpectrum()
self._spectrumEditor.set(spectrum)
# Fill out the SpatialModel editor.
spatialModel = source.getSpatialModel()
self._spatialModelEditor.set(spatialModel)
#--------------------------------------------------------------------------
def enable(self):
"""Activate all child widgets.
Parameters:
self: This object.
Return value:
None:
Description:
Activate all of the child widgets in this SourceEditor.
"""
self._nameEntryField.configure(entry_state = 'normal')
self._typeLabel.configure(state = 'normal')
self._spectrumEditor.enable()
self._spatialModelEditor.enable()
def disable(self):
"""Deactivate all child widgets.
Parameters:
self: This object.
Return value:
None:
Description:
Deactivate all of the child widgets in this SourceEditor.
"""
self._nameEntryField.configure(entry_state = 'disabled')
self._typeLabel.configure(state = 'disabled')
self._spectrumEditor.disable()
self._spatialModelEditor.disable()
#--------------------------------------------------------------------------
def setDS9Connector(self,connector):
self._ds9Connector = connector
#we and finally down to the SpatialModelEditor down to the source editor
self._spatialModelEditor.setDS9Connector(connector)
#******************************************************************************
# 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('SourceEditor test')
# Create and grid the editor, a button to commit changes, and a
# button to dump its contents.
sourceEditor = SourceEditor(root)
sourceEditor.grid(row = 0, column = 0)
commitButton = tkinter.Button(root, text = 'Commit',
command = sourceEditor.commit)
commitButton.grid(row = 0, column = 1)
resetButton = tkinter.Button(root, text = 'Reset',
command = sourceEditor.reset)
resetButton.grid(row = 0, column = 2)
printButton = tkinter.Button(root, text = 'Print',
command = \
lambda: _print(sourceEditor))
printButton.grid(row = 0, column = 3)
printXMLButton = tkinter.Button(root, text = 'Print XML',
command = \
lambda: _printXML(sourceEditor))
printXMLButton.grid(row = 0, column = 4)
# Enter the main program loop.
root.mainloop()