forked from RohanBali/EGUANA_Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EGUANA.py
351 lines (250 loc) · 12.9 KB
/
EGUANA.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
ZetCode Tkinter tutorial
In this script, we use the pack manager
to position two buttons in the
bottom-right corner of the window.
Author: Jan Bodnar
Last modified: November 2015
Website: www.zetcode.com
"""
import os, os.path
from tkinter import *
from tkinter import filedialog, FLAT, PhotoImage, Menu, CENTER, S, NW,NE, SW,W,SE, E, Toplevel, Entry, messagebox, simpledialog
from tkinter import Tk, RIGHT, RAISED, ttk, Frame, Button, Label, Text, TOP,RIDGE, BOTH,BOTTOM, Y,X,W, N, LEFT, SUNKEN
from egdialogs import CoilNumDialog
from egmenu import EguanaMenu
from egpopup import FilterFunctionPopup
from egpopup import FilterTypePopup
from eguanaModel import EguanaModel
import math
from machineConfig.eguanaMachineConfig import EguanaMachineConfig
class EguanaInit(object):
@staticmethod
def get_eguana_root_dir_name():
drive_name = os.path.splitdrive(sys.executable)[0]
is_unix = drive_name == ''
is_windows = not is_unix
if is_unix:
eguana_root_dir = '/eguana'
else:
eguana_root_dir = drive_name+"\\\\eguana"
return eguana_root_dir
@staticmethod
def eguana_root_dir_exists():
eguana_dir_name = EguanaInit.get_eguana_root_dir_name()
return os.path.exists(eguana_dir_name)
# To do
@staticmethod
def create_eguana_root_dir():
eguana_dir_name = EguanaInit.get_eguana_root_dir_name()
#os.makedirs(eguana_dir_name)
#os.chown(eguana_dir_name, int(os.getenv('SUDO_UID')), int(os.getenv('SUDO_GID')))
print(os.getenv("SUDO_USER"))
class EguanaGUI(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
# if not EguanaInit.eguana_root_dir_exists():
# EguanaInit.create_eguana_root_dir()
def initUI(self):
self.parent.title("EGUANA")
self.style = ttk.Style()
self.style.theme_use("alt")
self.photoName = "eguana.gif"
self.frame = Frame(self, relief=FLAT, borderwidth=10,bg='#FADC46')
self.frame.pack(fill=BOTH, expand=True)
self.pack(fill=BOTH, expand=True)
self.setupMenuBar()
self.setupTopBar()
def setupMenuBar(self):
self.menubar = EguanaMenu(self.parent,self)
self.parent.config(menu=self.menubar)
def setupTopBar(self):
self.supportedDevices = []
for fileName in [name for name in os.listdir('./machineConfig') if os.path.isfile('./machineConfig/' + name) and not name == 'eguanaMachineConfig.py' and name.endswith('.py')]:
# try:{
components = fileName.split('.')
fileName = components[0]
className = fileName[0].upper() + fileName[1:]
try:
module = __import__("machineConfig."+fileName,fromlist=["machineConfig."])
classVar = getattr(module,className)
except:
continue
self.supportedDevices.append(classVar())
# except:
# pass
self.selectMachineFrame = Frame(self.frame,relief=FLAT,bg='#FADC46')
self.selectMachineFrame.pack(fill=BOTH,expand=True)
self.setupSelectMachineButtons()
def setupSelectMachineButtons(self):
numDevices = len(self.supportedDevices)
numColumns = 3
numRows = math.ceil((numDevices+1)/3)
self.photo = PhotoImage(file="eguana.gif")
self.photo = self.photo.subsample(2);
self.photo_label = Label(self.selectMachineFrame,image=self.photo,borderwidth=0,highlightthickness=0)
self.photo_label.configure(bg='#FADC46')
self.photo_label.grid(row=int(numRows/2),column=1, sticky=N+S+E+W,padx=2,pady =2)
self.photo_label.image = self.photo
index = 0
for i in range(numRows):
for j in range(numColumns):
if not(j == 1 and i == int(numRows/2)) and (index < numDevices):
device = self.supportedDevices[index]
b = Button(self.selectMachineFrame,text=device.name,relief=RAISED, command=lambda device=device :self.machineButtonPressed(device))
b.grid(row=i,column=j, sticky=N+S+E+W,padx=2,pady =2)
index += 1
for i in range(numRows):
self.selectMachineFrame.rowconfigure(i,weight=1)
for i in range(numColumns):
self.selectMachineFrame.columnconfigure(i,weight=1)
def machineButtonPressed(self,inputDevice):
dirStr = filedialog.askdirectory()
if len(dirStr) and inputDevice.isDirectoryValid(dirStr):
inputDevice.setDirPath(dirStr)
EguanaModel().machine = inputDevice
self.selectMachineFrame.destroy()
self.menubar.inputSelected()
self.photo_label.destroy()
dirStr = 'Input Path : '+dirStr
self.selectPlotFrame = Frame(self.frame, relief=FLAT,bg='#FADC46')
self.selectPlotFrame.pack(fill=BOTH,expand=True)
self.selectPlotFrame.rowconfigure(0,weight=1)
self.selectPlotFrame.rowconfigure(1,weight=2)
self.selectPlotFrame.columnconfigure(0,weight=1)
self.infoFrame = Frame(self.selectPlotFrame, relief=FLAT, bg='#FADC46')
self.infoFrame.grid(row=0,column=0, sticky=N+S+E+W,padx=2,pady =2)
self.machineNameLabel = Label(self.infoFrame, text=inputDevice.name,relief=FLAT)
self.machineNameLabel.grid(row=0,column=0,columnspan=2, sticky=N+S+E+W,padx=2,pady =2)
self.directoryLabel = Label(self.infoFrame, text="No project currently selected",relief=FLAT)
self.directoryLabel.grid(row=1,column=0,columnspan=2, sticky=N+S+E+W,padx=2,pady =2)
self.directoryLabel.config(text=dirStr)
self.outputDirButton = Button(self.infoFrame,text="No output directory selected. Click to select an output directory ",relief=RAISED,fg='red',command=self.askOutputDirectory)
self.outputDirButton.grid(row=2,column=0,columnspan=2, sticky=N+S+E+W,padx=2,pady =2)
self.filterButton = Button(self.infoFrame,text="No filter selected. Click to select a filter",relief=RAISED,fg='red',command=self.selectFilter)
self.filterButton.grid(row=3,column=0,columnspan=2, sticky=N+S+E+W,padx=2,pady =2)
self.trialLabel = Label(self.infoFrame,text="Trial Number",relief=FLAT,justify=RIGHT,anchor=E)
self.trialLabel.grid(row=4,column=0, sticky=N+S+E+W,padx=2,pady =2)
vcmd = (self.master.register(self.validate),'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.trialEntry = Entry(self.infoFrame,validate = 'key', validatecommand = vcmd)
self.trialEntry.grid(row=4,column=1, sticky=N+S+E+W,padx=2,pady =2)
self.infoFrame.columnconfigure(0, weight=1)
self.infoFrame.columnconfigure(1, weight=1)
self.infoFrame.rowconfigure(0, weight=1)
self.infoFrame.rowconfigure(1, weight=10)
self.infoFrame.rowconfigure(2, weight=10)
self.infoFrame.rowconfigure(3, weight=10)
self.infoFrame.rowconfigure(4, weight=10)
self.showPlotTools()
def validate(self, action, index, value_if_allowed,
prior_value, text, validation_type, trigger_type, widget_name):
if len(value_if_allowed)==0 :
return True
if text in '0123456789.-+ ':
try:
float(value_if_allowed)
return True
except ValueError:
return False
else:
return False
def askOutputDirectory(self):
dirStr = filedialog.askdirectory()
if len(dirStr):
dirStr = 'Output Path : '+dirStr
self.outputDirButton.config(text=dirStr)
def showPlotTools(self):
f2= Frame(self.selectPlotFrame, relief=FLAT,bg='#FADC46')
f2.grid(row=1,column=0,sticky=N+S+E+W,padx=10,pady =10)
b1 = Button(f2,text='3D K',relief=RAISED,command= lambda:self.plotButtonPressed(1))
b1.grid(row=0, column=0,sticky=N+S+E+W,padx=5,pady =5)
b2 = Button(f2,text='3D Dist',relief=RAISED,command=lambda:self.plotButtonPressed(2))
b2.grid(row=0, column=1,sticky=N+S+E+W,padx=5,pady =5)
b3 = Button(f2,text='3D DP',relief=RAISED,command=lambda:self.plotButtonPressed(3))
b3.grid(row=0, column=2,sticky=N+S+E+W,padx=5,pady =5)
b4 = Button(f2,text='2D K',relief=RAISED,command=lambda:self.plotButtonPressed(4))
b4.grid(row=1, column=0,sticky=N+S+E+W,padx=5,pady =5)
b5 = Button(f2,text='2D Dist',relief=RAISED,command=lambda:self.plotButtonPressed(5))
b5.grid(row=1, column=1,sticky=N+S+E+W,padx=5,pady =5)
b6 = Button(f2,text='2D DP',relief=RAISED,command=lambda:self.plotButtonPressed(6))
b6.grid(row=1, column=2,sticky=N+S+E+W,padx=5,pady =5)
b1.config(state=EguanaModel().machine.plot3DKButtonState)
b2.config(state=EguanaModel().machine.plot3DDstButtonState)
b3.config(state=EguanaModel().machine.plot3DDpButtonState)
b4.config(state=EguanaModel().machine.plot2DKButtonState)
b5.config(state=EguanaModel().machine.plot2DDstButtonState)
b6.config(state=EguanaModel().machine.plot2DDpButtonState)
f2.columnconfigure(0, weight=1)
f2.columnconfigure(1, weight=1)
f2.columnconfigure(2, weight=1)
f2.rowconfigure(0, weight=1)
f2.rowconfigure(1, weight=1)
def plotButtonPressed(self,number):
trialNum = self.trialEntry.get()
try:
trialNum = int(trialNum)
print(EguanaModel().machine.ifTrialExists(trialNum))
if EguanaModel().machine.ifTrialExists(trialNum):
self.plotFigure(number)
return True
else:
messagebox.showerror(
"Trial Number Error",
"The trial doesn't exist"
)
return False
except ValueError:
messagebox.showerror(
"Trial Number Error",
"Error with the trial number"
)
return False
def plotFigure(self,number):
m = CoilNumDialog(self.frame)
if m.isSet():
print(m.getValues())
EguanaModel().machine.getDataForTrialNumber(number)
def selectFilter(self):
ffPopup = FilterFunctionPopup(self)
if ffPopup.selectedFilter is not None:
EguanaModel().filterFunction = ffPopup.selectedFilter
ftPopup = FilterTypePopup(self,ffPopup.selectedFilter)
if (ftPopup.selectedHeadFilterType is not None) and (ftPopup.selectedHeadFilterType is not None):
self.menubar.setSelectedFilters(ffPopup.selectedFilter,ftPopup.selectedHeadFilterType,ftPopup.selectedJawFilterType)
EguanaModel().filterTypeHead = ftPopup.selectedHeadFilterType
EguanaModel().filterTypeJaw = ftPopup.selectedJawFilterType
EguanaModel().filterFunction = ffPopup.selectedFilter
buttonText = '{} - Jaw Filter : {} - Head Filter {}'.format(ffPopup.selectedFilter.name,ftPopup.selectedJawFilterType.name, ftPopup.selectedHeadFilterType.name)
self.filterButton.config(text=buttonText)
def updateSelectedFilters(self,ffName,ftHeadName,ftJawName):
buttonText = '{} - Jaw Filter : {} - Head Filter {}'.format(ffName,ftHeadName,ftJawName)
self.filterButton.config(text=buttonText)
def changeImage(self):
self.photo = PhotoImage(file=self.photoName)
self.photo = self.photo.subsample(2);
self.photo_label.image = self.photo
self.photo_label.config(image=self.photo)
def cursorPosition(self,event):
if event.widget == self.openButton3D:
if self.photoName == "eguana2.gif":
self.photoName = "eguana.gif"
self.changeImage()
elif event.widget == self.openButton2D:
if self.photoName == "eguana.gif":
self.photoName = "eguana2.gif"
self.changeImage()
def main():
global app
global root
root = Tk()
sw = root.winfo_screenwidth()
sh = root.winfo_screenheight()
root.geometry('%dx%d+%d+%d' % (sw, sh, 0, 0))
app = EguanaGUI(root)
root.mainloop()
if __name__ == '__main__':
main()