-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path__main__.py
392 lines (285 loc) · 18.6 KB
/
__main__.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 03/10/2020
@author: Wilfried - IRAP
Software to manipulate projections for the sphere orb.
"""
import sys
import multiprocessing
import matplotlib
matplotlib.use('TkAgg')
import tkinter as tk
from tkinter import ttk
from signal import signal, SIGINT
from threading import Thread
import os.path as opath
# Program imports
import setup
from icons import iconload
from sigint import sigintHandler
from widgets import PlotWindow, ConfigWindow, Validate, Tab
class mainApplication:
'''
Main application where all the different layouts are defined.
'''
def __init__(self, parent):
'''
Inputs
------
parent : tk.Tk instance
root propagated throughout the different Frames and Canvas
'''
self.parent = parent
self.color = 'white smoke'
self.parent.config(cursor='arrow', bg=self.color)
# Initial program setup
self.settings, errCode = setup.init()
self.font = self.settings['font']
self.loadPath = self.settings['path']
# Check that given paths exist
if not opath.exists(self.loadPath) or not opath.exists(self.settings['iconPath']):
raise IOError('One of the path in the yaml configuration file does not exist.')
icons = iconload(self.settings['iconPath'])
projects = self.settings['projects']
if errCode != 0:
print('YAML configuration could not be read correctly. A new one with default values has been created instead.')
# Miscellaneous parameters
self.cpuCount = multiprocessing.cpu_count()
######################################################
# Check for old projects #
######################################################
# If project file still exists, add it to the list
self.projects = []
for proj in projects:
if opath.isfile(proj):
self.projects.append(proj)
# It at least one project exist, show validation window at startup
if len(self.projects) > 0:
# Validation window
window = Validate(self, self, parent,
mainText='You have old projects saved in the setting file. Select the files you want to open.',
listNames=self.projects,
textProperties={'highlightthickness':0, 'bd':0, 'bg':self.color, 'font':(self.font, 10)},
buttonsProperties={'bg':'floral white', 'activebackground':'linen'},
winProperties={'bg':self.color},
acceptFunction=lambda *args, **kwargs: self.loadProjects(*args, **kwargs))
self.parent.lift()
# For MAC it seems we must set False and then True
window.overrideredirect(False)
window.overrideredirect(True)
window.focus_force()
window.grab_set()
window.geometry('+%d+%d' %(self.parent.winfo_screenwidth()//2-2*window.winfo_reqwidth(), self.parent.winfo_screenheight()//2-window.winfo_reqheight()))
window.state('normal')
###########################################################
# Custom ttk styles #
###########################################################
# Custom style for Notebook
self.style = ttk.Style()
self.style.configure('main.TNotebook', background=self.color)
self.style.configure('main.TNotebook.Tab', padding=[5, 2], font=('fixed', 10))
self.style.map('main.TNotebook.Tab',
background=[("selected", 'lavender'), ('!selected', 'white smoke')],
foreground=[('selected', 'RoyalBlue2')])
#############################################################
# Load icons into Image objects #
#############################################################
self.iconDict = {}
self.iconDict['FOLDER'] = tk.BitmapImage(data=icons['FOLDER'], maskdata=icons['FOLDER_MASK'], background='goldenrod')
self.iconDict['FOLDER_256'] = tk.BitmapImage(data=icons['FOLDER_256'], maskdata=icons['FOLDER_256_MASK'], background='goldenrod')
self.iconDict['FOLDER_17'] = tk.BitmapImage(data=icons['FOLDER_17'], maskdata=icons['FOLDER_17_MASK'], background='goldenrod')
self.iconDict['DELETE'] = tk.BitmapImage(data=icons['DELETE'], maskdata=icons['DELETE_MASK'], background='light cyan', foreground='black')
self.iconDict['DUPPLICATE'] = tk.BitmapImage(data=icons['DUPPLICATE'], maskdata=icons['DUPPLICATE_MASK'], background='black', foreground='black')
self.iconDict['CONFIG'] = tk.BitmapImage(data=icons['CONFIG'], maskdata=icons['CONFIG_MASK'], background=self.color, foreground='black')
self.iconDict['RUN'] = tk.BitmapImage(data=icons['RUN'], maskdata=icons['RUN_MASK'], background='SpringGreen4', foreground='white')
self.iconDict['CHECK'] = tk.BitmapImage(data=icons['CHECK'], maskdata=icons['CHECK_MASK'], background=self.color, foreground='firebrick1')
#############################################
# Buttons #
#############################################
# Top frame with buttons and sliders
self.topframe = tk.Frame( self.parent, bg=self.color, bd=0, relief=tk.GROOVE)
self.confButton = tk.Button(self.topframe, image=self.iconDict['CONFIG'],
bd=0, bg=self.color, highlightbackground=self.color, relief=tk.FLAT, activebackground=self.color)
self.loadButton = tk.Button(self.topframe, image=self.iconDict['FOLDER'],
bd=0, bg=self.color, highlightbackground=self.color, relief=tk.FLAT, activebackground='black')
self.delButton = tk.Button(self.topframe, image=self.iconDict['DELETE'],
bd=0, bg=self.color, highlightbackground=self.color, relief=tk.FLAT, activebackground=self.color)
self.duppButton = tk.Button(self.topframe, image=self.iconDict['DUPPLICATE'],
bd=0, bg=self.color, highlightbackground=self.color, relief=tk.FLAT, activebackground=self.color)
##############################################
# Scales #
##############################################
self.scaleframe = tk.Frame( self.topframe, bg=self.color, bd=0, highlightthickness=0)
self.latframe = tk.Frame( self.scaleframe, bg=self.color, bd=0, highlightthickness=0)
self.longframe = tk.Frame( self.scaleframe, bg=self.color, bd=0, highlightthickness=0)
self.latLabel = tk.Label( self.latframe, text='Latitude', bg=self.color)
self.longLabel = tk.Label( self.longframe, text='Longitude', bg=self.color)
self.latScale = tk.Scale( self.latframe, length=200, width=12, orient='horizontal', from_=-90, to=90,
cursor='hand1', showvalue=False,
bg=self.color, bd=1, highlightthickness=1, highlightbackground=self.color, troughcolor='lavender', activebackground='black', font=('fixed', 11),
command=lambda value: self.updateScale('latitude', value))
self.longScale = tk.Scale( self.longframe, length=200, width=12, orient='horizontal', from_=-180, to=180,
cursor='hand1', showvalue=False,
bg=self.color, bd=1, highlightthickness=1, highlightbackground=self.color, troughcolor='lavender', activebackground='black', font=('fixed', 11),
command=lambda value: self.updateScale('longitude', value))
###########################################################
# Bottom notebook #
###########################################################
self.notebook = ttk.Notebook(self.parent, style='main.TNotebook')
# Keep track of notebook tabs
self.tabs = {}
self.addTab()
#######################################################################
# Bindings #
#######################################################################
self.parent.bind( '<Control-o>', lambda *args, **kwargs: self.tabs[self.notebook.select()].askLoad())
self.parent.bind( '<Control-p>', lambda *args, **kwargs: self.showConfigWindow(*args, **kwargs))
self.confButton.bind('<Enter>', lambda *args, **kwargs: self.iconDict['CONFIG'].configure(foreground='RoyalBlue2'))
self.confButton.bind('<Leave>', lambda *args, **kwargs: self.iconDict['CONFIG'].configure(foreground='black'))
self.confButton.bind('<Button-1>', lambda *args, **kwargs: self.showConfigWindow(*args, **kwargs))
self.loadButton.bind('<Enter>', lambda *args, **kwargs: self.tabs[self.notebook.select()].loadButton.configure(bg='black') if not self.tabs[self.notebook.select()].loaded else None)
self.loadButton.bind('<Leave>', lambda *args, **kwargs: self.tabs[self.notebook.select()].loadButton.configure(bg='lavender') if not self.tabs[self.notebook.select()].loaded else None)
self.loadButton.bind('<Button-1>', lambda *args, **kwargs: self.tabs[self.notebook.select()].askLoad())
self.delButton.bind( '<Enter>', lambda *args, **kwargs: self.iconDict['DELETE'].configure(foreground='red', background=self.color))
self.delButton.bind( '<Leave>', lambda *args, **kwargs: self.iconDict['DELETE'].configure(foreground='black', background='light cyan'))
self.delButton.bind( '<Button-1>', lambda *args, **kwargs: self.delTab(*args, **kwargs))
self.plotWindow = None
self.duppButton.bind('<Enter>', lambda *args, **kwargs: self.iconDict['DUPPLICATE'].configure(background='RoyalBlue2'))
self.duppButton.bind('<Leave>', lambda *args, **kwargs: self.iconDict['DUPPLICATE'].configure(background='black'))
self.duppButton.bind('<Button-1>', lambda *args, **kwargs: self.showPlotWindow())
self.latScale.bind( '<Enter>', lambda *args, **kwargs: self.latScale.configure(highlightbackground='RoyalBlue2'))
self.latScale.bind( '<Leave>', lambda *args, **kwargs: self.latScale.configure(highlightbackground=self.color))
self.longScale.bind( '<Enter>', lambda *args, **kwargs: self.longScale.configure(highlightbackground='RoyalBlue2'))
self.longScale.bind( '<Leave>', lambda *args, **kwargs: self.longScale.configure(highlightbackground=self.color))
self.notebook.bind( '<<NotebookTabChanged>>', lambda *args, **kwargs: self.tabCghanged(*args, **kwargs))
##########################################################
# Drawing frames #
##########################################################
self.confButton.pack(side=tk.LEFT, pady=10)
self.loadButton.pack(side=tk.LEFT, pady=10)
self.delButton.pack( side=tk.LEFT, pady=10)
self.latLabel.grid( row=0, stick=tk.W)
self.longLabel.grid( row=0, stick=tk.W)
self.latScale.grid( row=1)
self.longScale.grid( row=1)
self.latframe.pack( side=tk.LEFT, padx=10)
self.longframe.pack( side=tk.LEFT, padx=10)
self.scaleframe.pack(side=tk.LEFT, padx=10, fill='x', expand=True)
self.duppButton.pack(side=tk.RIGHT)
self.topframe.pack(fill='x', padx=10)
self.notebook.pack(fill='both', expand=True)
##############################################
# Creating new windows #
##############################################
def showConfigWindow(self, *args, **kwargs):
'''Create or show back a configuration window to generate projections.'''
if self.confButton['state'] != 'disabled':
winProperties = {'bg':'white smoke'}
entryProperties = {'bg':'lavender', 'fg':'SpringGreen4'}
self.configWindow = ConfigWindow(self, self, self.parent, title='Projection facility',
winProperties=winProperties, entryProperties=entryProperties)
self.confButton.configure(state=tk.DISABLED)
return
def showPlotWindow(self, *args, **kwargs):
'''Create or show back the separate window for the plot.'''
self.duppButton.pack_forget()
self.tabs[self.notebook.select()].showExplanation()
data = self.tabs[self.notebook.select()].data
title = self.tabs[self.notebook.select()].name
if self.plotWindow is not None:
self.plotWindow.setTitle(title)
self.plotWindow.state('normal')
self.plotWindow.update(data)
else:
winProperties = {'bg':'lavender'}
self.plotWindow = PlotWindow(self, self, self.parent, data=data, winProperties=winProperties, title=title)
return
##########################################################
# Tab interactions #
##########################################################
def addTab(self, *args, **kwargs):
'''Add a new tab in the tab list.'''
tab = Tab(self.notebook, self, self.notebook, properties={'bg':'lavender', 'bd':1, 'highlightthickness':0})
self.tabs[self.notebook.tabs()[-1]] = tab
if len(self.tabs) > 1:
self.notebook.tab(self.notebook.tabs()[-1], state='disabled')
return
def delTab(self, *args, **kwargs):
'''Delete a tab if some conditions are met'''
# Remove the tab from the notebook AND the tab dictionary
idd = self.notebook.select()
if self.tabs[idd].loaded:
self.plotWindow.update([[0, 0], [0, 0]])
self.notebook.forget(idd)
self.tabs.pop(idd)
return
def loadProjects(self, files, *args, **kwargs):
'''
Load the projects listed in files at startup only.
Parameters
----------
files : list of str
projects yaml configuration files to load
'''
for f in files:
idd = self.notebook.select()
self.tabs[idd].load(f)
self.notebook.select(self.notebook.tabs()[-1])
return
def tabCghanged(self, event, *args, **kwargs):
'''Actions taken when a tab is changed.'''
tab = self.tabs[event.widget.select()]
tab.updateSliders()
if not tab.loaded:
self.duppButton.pack_forget()
else:
self.duppButton.pack(side=tk.RIGHT, padx=10)
return
########################################
# Scale commands #
########################################
def updateScale(self, which, value, *args, **kwargs):
'''Action taken when the latitude or longitude scales are updated.'''
idd = self.notebook.select()
if self.tabs[idd].loaded:
if which == 'longitude':
self.tabs[idd].updateGraph(longitude=value, latitude=self.latScale.get())
elif which == 'latitude':
self.tabs[idd].updateGraph(latitude=value, longitude=self.longScale.get())
# Because it is a callback called at the very end of the load data process, we update back to normal the last tab here to avoid doing it before the scale values are updated
if self.notebook.tab(self.notebook.tabs()[-1])['state'] != 'normal':
self.notebook.tab(self.notebook.tabs()[-1], state='normal')
return
class runMainloop(Thread):
'''Class inheriting from threading.Thread. Defined this way to ensure that SIGINT signal from the shell can be caught despite the mainloop.'''
def run(self):
'''Run method from Thread called when using start()'''
self.root = tk.Tk()
self.root.title('Sphere - projections at hand')
self.root.geometry('800x800+%d+%d' %(self.root.winfo_screenwidth()//2-400, self.root.winfo_screenheight()//2-400))
app = mainApplication(self.root)
self.root.protocol("WM_DELETE_WINDOW", lambda signal=SIGINT, frame=None, obj=self, skipUpdate=True: sigintHandler(signal, obj, None, skipUpdate))
self.root.mainloop()
def runMainLoopMac(*args, **kwargs):
'''A temporary function to disable the Ctrl+C function from another thread working on linux platforms but not on MAC OS.'''
root = tk.Tk()
root.title('Sphere - projections at hand')
root.geometry('800x800+%d+%d' %(root.winfo_screenwidth()//2-400, root.winfo_screenheight()//2-400))
app = mainApplication(root)
root.protocol("WM_DELETE_WINDOW", lambda signal=SIGINT, frame=None, root=root, obj=None, skipUpdate=True: sigintHandler(signal, obj, root, skipUpdate))
root.mainloop()
return root
def main():
platform = sys.platform
if platform == 'darwin':
root = runMainLoopMac()
# Link Ctrl+C keystroke in shell to terminating window (window focus needs to be given on mac for this to work)
signal(SIGINT, lambda signal, frame, obj=None, root=root, skipUpdate=False: sigintHandler(signal, obj, root, skipUpdate))
else:
mainLoop = runMainloop()
# Link Ctrl+C keystroke in shell to terminating window
signal(SIGINT, lambda signal, frame, obj=mainLoop, root=None, skipUpdate=False: sigintHandler(signal, obj, root, skipUpdate))
mainLoop.start()
if __name__ == '__main__':
main()