-
Notifications
You must be signed in to change notification settings - Fork 0
/
hole_map_gui.py
executable file
·405 lines (317 loc) · 15.1 KB
/
hole_map_gui.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#!/usr/bin/env python2.7
import Tkinter
import BetterCanvas
import ImageCanvas
import Plate
import tkMessageBox
import os
#mainloop
#meat: plate.loadHoles then self.show
#self.plate.regionify(active_setup=self.getActiveSetup())
class HoleInfoDialog:
def __init__(self, parent, canvas, plate, setup, holeIDs):
self.canvas=canvas
self.parent=parent
self.setup=setup
self.getHoleInfo=lambda a:plate.getHoleInfo(a)
self.getFiberForHole=lambda a:plate.getFiberForHole(a, setup)
self.getChannelForHole=lambda a:plate.getChannelForHole(a, setup)
if len(holeIDs) > 1:
self.initializeSelection(holeIDs)
else:
self.initializeSingle(holeIDs[0])
def initializeSelection(self, holeIDs):
self.dialog=Tkinter.Toplevel(self.parent)
self.dialog.bind("<FocusOut>", self.defocusCallback)
self.dialog.bind("<Destroy>", self.destroyCallback)
self.holeID=holeIDs
for i,id in enumerate(holeIDs):
self.add_callback_for_id(id)
self.canvas.itemconfigure('.'+id, state=Tkinter.DISABLED)
info=self.getHoleInfo(id)
lbl_str=' '.join(['ID:',id,'Type:',info['TYPE'],'Targ ID:',
info['ID'],'other info'])
Tkinter.Label(self.dialog, text=lbl_str).grid(row=i,column=0)
Tkinter.Button(self.dialog,text='Select',command=getattr(self,'cb'+id)).grid(row=i,column=1)
def initializeSingle(self, holeID):
self.canvas.itemconfigure('.'+holeID,state=Tkinter.DISABLED)
self.dialog=Tkinter.Toplevel(self.parent)
self.dialog.bind("<FocusOut>", self.defocusCallback)
self.dialog.bind("<Destroy>", self.destroyCallback)
self.holeID=holeID
info=self.getHoleInfo(holeID)
print info
#types: TROSFGA
Tkinter.Label(self.dialog, text='Type: '+info['TYPE']).pack(anchor='w')
if info['TYPE'] in 'OSGA':
Tkinter.Label(self.dialog, text='ID: '+info['ID']).pack(anchor='w')
Tkinter.Label(self.dialog, text='RA: '+info['RA']).pack(anchor='w')
Tkinter.Label(self.dialog, text='Dec: '+info['DEC']).pack(anchor='w')
if info['TYPE'] not in 'S':
Tkinter.Label(self.dialog, text='Mag: %f'%info['MAGNITUDE']).pack(anchor='w')
Tkinter.Label(self.dialog, text='Color: %f'%info['COLOR']).pack(anchor='w')
Tkinter.Label(self.dialog, text='Setup Specific Information').pack()
Tkinter.Label(self.dialog, text="Channel: "+self.getChannelForHole(holeID)).pack(anchor='w')
Tkinter.Label(self.dialog, text="Assigned Fiber: "+self.getFiberForHole(holeID)).pack(anchor='w')
Tkinter.Button(self.dialog,text='Done',command=self.ok).pack()
def add_callback_for_id(self, holeID):
def innercb():
self.close()
self.initializeSingle(holeID)
innercb.__name__ = "cb"+holeID
setattr(self,innercb.__name__,innercb)
def defocusCallback(self, event):
pass
#self.ok()
def ok(self):
self.save()
self.close()
def destroyCallback(self, event):
self.resetHoles()
def save(self):
pass
def close(self):
self.resetHoles()
self.dialog.destroy()
def resetHoles(self):
if isinstance(self.holeID, str):
self.canvas.itemconfig('.'+self.holeID,state=Tkinter.NORMAL)
else:
for id in self.holeID:
self.canvas.itemconfig('.'+id,state=Tkinter.NORMAL)
class App(Tkinter.Tk):
def __init__(self, parent):
Tkinter.Tk.__init__(self, parent)
self.parent = parent
self.initialize()
def initialize(self):
self.plate=Plate.Plate()
self.file_str=Tkinter.StringVar(value='No File Loaded')
#Basic window stuff
swid=120
bhei=55
whei=735
chei=whei-bhei
wwid=chei+swid
self.geometry("%ix%i"%(wwid,whei))
self.title("Hole App")
#The sidebar
frame = Tkinter.Frame(self, width=swid, bd=0, bg=None)#None)
frame.place(x=0,y=0)
#Info display
frame2 = Tkinter.Frame(self, height=bhei, bd=0, bg=None)#None)
frame2.place(x=0,y=whei-45-1)
#The canvas for drawing the plate
self.canvas=BetterCanvas.BetterCanvas(self, chei, chei, 1.01, 1.01, bg='White')
self.canvas.place(x=swid,y=0)
self.canvas.bind("<Button-1>",self.canvasclick)
#Buttons
Tkinter.Button(frame, text="Show All", command=self.show).pack()
Tkinter.Button(frame, text="Show Red",
command=lambda:self.show(channel='armR')).pack()
Tkinter.Button(frame, text="Show Blue",
command=lambda:self.show(channel='armB')).pack()
Tkinter.Button(frame, text="Make Image", command=self.makeImage).pack()
Tkinter.Button(frame, text="Make R Image",
command=lambda:self.makeImage(channel='armR')).pack()
Tkinter.Button(frame, text="Make B Image",
command=lambda:self.makeImage(channel='armB')).pack()
Tkinter.Button(frame, text="Load Holes", command=self.load).pack()
Tkinter.Button(frame, text="Regionify", command=self.makeRegions).pack()
Tkinter.Button(frame, text="Gen .plate", command=self.genPlate).pack()
self.coordshft_str=Tkinter.StringVar(value='CShift On')
Tkinter.Button(frame, textvariable=self.coordshft_str, command=self.toggleCoord).pack()
#Input
#Setup input
self.setup_str=Tkinter.StringVar(value='1')
lframe=Tkinter.Frame(frame)
lframe.pack()
Tkinter.Label(lframe, text='Setup #:').grid(row=0,column=0)
entry=Tkinter.Entry(lframe, validate='focusout', width=2,
invcmd=lambda:tkMessageBox.showerror('Bad Setup','Not a valid setup.'),
vcmd=lambda:self.plate.isValidSetup(self.setup_str.get()),
textvariable=self.setup_str)
entry.grid(row=0,column=1)
#Assignwith input
self.assignwith_str=Tkinter.StringVar(value='')
lframe=Tkinter.Frame(frame)
lframe.pack()
Tkinter.Label(lframe, text='Setup #s:').grid(row=0,column=0)
entry=Tkinter.Entry(lframe, validate='focusout', width=2,
invcmd=lambda:tkMessageBox.showerror(
'Much Bad.','No regionwith. Mix Improper.'),
vcmd=lambda:self.plate.isValidAssignwith(
self.get_assign_with_list()),
textvariable=self.assignwith_str)
entry.grid(row=0,column=1)
#entry.bind("<Return>",self.show)
#Coordinate shift input
self.Dparam_str=Tkinter.StringVar(value='64')
self.rmparam_str=Tkinter.StringVar(value='13.21875')
self.aparam_str=Tkinter.StringVar(value='0.03')
self.Rparam_str=Tkinter.StringVar(value='50.68')
paramw=4
pframe=Tkinter.LabelFrame(frame,text='Coord. Params',relief='flat')
pframe.pack()
Dframe=Tkinter.LabelFrame(pframe,text='D',relief='flat')
Dframe.grid(row=0,column=0)
entry=Tkinter.Entry(Dframe, validate='focusout', width=paramw,
invcmd=lambda:tkMessageBox.showerror('Bad D','Not a value for D.'),
vcmd=lambda:self.plate.isValidCoordParam_D(self.Dparam_str.get()),
textvariable=self.Dparam_str)
entry.pack()
#entry.bind("<FocusOut>",self.setCoordShiftD)
entry.bind("<Return>",self.setCoordShiftD)
rmframe=Tkinter.LabelFrame(pframe,text='rm',relief='flat')
rmframe.grid(row=0,column=1)
entry=Tkinter.Entry(rmframe, validate='focusout', width=paramw,
invcmd=lambda:tkMessageBox.showerror('Bad rm','Not a value for rm.'),
vcmd=lambda:self.plate.isValidCoordParam_rm(self.rmparam_str.get()),
textvariable=self.rmparam_str)
entry.pack()
#entry.bind("<FocusOut>",self.setCoordShiftrm)
entry.bind("<Return>",self.setCoordShiftrm)
Rframe=Tkinter.LabelFrame(pframe,text='R',relief='flat')
Rframe.grid(row=1,column=0)
entry=Tkinter.Entry(Rframe, validate='focusout', width=paramw,
invcmd=lambda:tkMessageBox.showerror('Bad R','Not a value for R.'),
vcmd=lambda:self.plate.isValidCoordParam_R(self.Rparam_str.get()),
textvariable=self.Rparam_str)
entry.pack()
#entry.bind("<FocusOut>",self.setCoordShiftR)
entry.bind("<Return>",self.setCoordShiftR)
aframe=Tkinter.LabelFrame(pframe,text='a',relief='flat')
aframe.grid(row=1,column=1)
entry=Tkinter.Entry(aframe, validate='focusout', width=paramw,
invcmd=lambda:tkMessageBox.showerror('Bad a','Not a value for a.'),
vcmd=lambda:self.plate.isValidCoordParam_a(self.aparam_str.get()),
textvariable=self.aparam_str)
entry.pack()
#entry.bind("<FocusOut>",self.setCoordShifta)
entry.bind("<Return>",self.setCoordShifta)
#Info output
self.info_str=Tkinter.StringVar(value='Red: 000 Blue: 000 Total: 0000')
Tkinter.Label(frame2, textvariable=self.info_str).pack(anchor='w')
Tkinter.Label(frame2, textvariable=self.file_str).pack(anchor='w')
self.testinit()
def get_assign_with_list(self):
if self.assignwith_str.get():
return self.assignwith_str.get().replace(' ','').split(',')
else:
return []
def toggleCoord(self):
self.plate.toggleCoordShift()
if self.plate.doCoordShift:
self.coordshft_str.set('CShift On')
else:
self.coordshft_str.set('CShift Off')
self.show()
def testinit(self):
# create a second window and make it cover the entire projector screen
self.proj_win=Tkinter.Toplevel(self.parent)
self.proj_win.overrideredirect(1)
self.proj_win.geometry("768x768+1494+0")
self.moving={'stat':False}
self.proj_win.bind("<Button-1>",self.startMove)
self.proj_win.bind("<ButtonRelease-1>",self.stopMove)
self.proj_win.bind("<B1-Motion>", self.Move)
#print '.'+self.proj_win.winfo_screen()+'.'
self.proj_can=BetterCanvas.BetterCanvas(self.proj_win, 768,768, 1.00, 1.00, bg='Black')
self.proj_can.place(x=-3,y=-3)
self.show()
def Move(self,event):
if self.moving['stat']:
dx=event.x_root-self.moving['xs']
dy=event.y_root-self.moving['ys']
xnew=self.moving['xi']+dx
ynew=self.moving['yi']+dy
self.proj_win.geometry("768x768+%i+%i"%(xnew,ynew))
def startMove(self,event):
#print '.'+self.proj_win.winfo_screen()+'.'
self.moving={'stat':True,'xs':event.x_root,'ys':event.y_root,
'xi':self.proj_win.winfo_rootx(),
'yi':self.proj_win.winfo_rooty()}
def stopMove(self,event):
#print '.'+self.proj_win.winfo_screen()+'.'
self.moving['stat']=False
def canvasclick(self, event):
#Get holes that are within a few pixels of the mouse position
items=self.canvas.find_overlapping(event.x - 2, event.y-2, event.x+2, event.y+2)
items=filter(lambda a: 'hole' in self.canvas.gettags(a), items)
if items:
holeIDs=tuple([tag[1:] for i in items for tag in self.canvas.gettags(i) if tag[-1].isdigit()])
HoleInfoDialog(self.parent, self.canvas, self.plate, self.getActiveSetup(), holeIDs)
def setCoordShiftD(self,*args):
if self.plate.isValidCoordParam_D(self.Dparam_str.get()):
self.plate.setCoordShiftD(self.Dparam_str.get())
else:
self.Dparam_str.set(str(self.plate.coordShift_D))
if self.plate.doCoordShift:
self.show()
def setCoordShiftR(self,*args):
if self.plate.isValidCoordParam_R(self.Rparam_str.get()):
self.plate.setCoordShiftR(self.Rparam_str.get())
else:
self.Rparam_str.set(str(self.plate.coordShift_R))
if self.plate.doCoordShift:
self.show()
def setCoordShiftrm(self,*args):
if self.plate.isValidCoordParam_rm(self.rmparam_str.get()):
self.plate.setCoordShiftrm(self.rmparam_str.get())
else:
self.rmparam_str.set(str(self.plate.coordShift_rm))
if self.plate.doCoordShift:
self.show()
def setCoordShifta(self,*args):
if self.plate.isValidCoordParam_a(self.aparam_str.get()):
self.plate.setCoordShifta(self.aparam_str.get())
else:
self.aparam_str.set(str(self.plate.coordShift_a))
if self.plate.doCoordShift:
self.show()
def getActiveSetup(self):
return "Setup "+self.setup_str.get()
def show(self, channel='all'):
self.canvas.clear()
self.proj_can.clear()
self.info_str.set(self.plate.getSetupInfo(self.getActiveSetup()))
self.plate.draw(self.canvas, channel=channel, active_setup=self.getActiveSetup())
self.plate.drawImage(self.proj_can, channel=channel,radmult=1.1, active_setup=self.getActiveSetup())
def makeRegions(self):
self.plate.regionify(setup_number=self.setup_str.get(),
awith=self.get_assign_with_list())
self.show()
def genPlate(self):
self.plate.plateHoleInfo.write_platefile()
@staticmethod
def getPath(sequence):
dir=os.path.os.path.join(*sequence)
if os.name is 'nt':
dir='C:\\'+dir+os.path.sep
else:
dir=os.path.expanduser('~/')+dir+os.path.sep
return dir
def load(self):
from tkFileDialog import askopenfilename
dir=App.getPath(('hole_mapper','plates'))
file=askopenfilename(initialdir=dir,
filetypes=[('asc files', '.asc'),
('plate files', '.plate')])
file=os.path.normpath(file)
print file
if file:
self.plate.load(file)
self.file_str.set(os.path.basename(file))
self.show()
def makeImage(self,channel='all'):
#The image canvas for drawing the plate to a file
dir=App.getPath(('hole_mapper',self.plate.plate_name))
if not os.path.exists(dir):
os.makedirs(dir)
imgcanvas=ImageCanvas.ImageCanvas(1280, 1280, 1.0, 1.0)
self.plate.drawImage(imgcanvas, channel=channel, active_setup=self.getActiveSetup())
imgcanvas.save(dir+self.file_str.get()+'_'+self.getActiveSetup()+'_'+channel+'.png')
if __name__ == "__main__":
app = App(None)
app.title('Hole Mapper')
app.mainloop()