forked from skai2/EDAutopilot
/
settings_menu.py
147 lines (113 loc) · 4.66 KB
/
settings_menu.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
import os
from tkinter import Tk, Checkbutton, BooleanVar, Button, NORMAL, DISABLED, Label, Entry
from tkinter.messagebox import askyesnocancel, YES
import keyboard
from logger import logger
from settings_api import getOptions, setOption
#
# Simple settings menu
# Makes use of the settings file interface
# Prone to visual errors due to hardcoded widget locations
#
curr_window = None
def getKeyForBtn(btn):
key = keyboard.read_key()
if key != "esc":
btn['text'] = key
logger.debug("(settings_menu) detected key '{}' with scancode {}".format(key, keyboard.key_to_scan_codes(key)))
def create_window():
window = Tk()
window.title("EDAutopilot Settings")
window.geometry('280x300')
window.resizable(False, False)
logger.debug(os.name)
if "nt" == os.name:
from dev_autopilot import resource_path
window.iconbitmap(resource_path('src/logo.ico'))
defaults = getOptions()
def on_closing():
if unsaved():
confirm = askyesnocancel(
title="Save On Close",
message="Do you want to save before closing?",
default=YES,
icon='warning'
)
if confirm:
on_save()
elif confirm is None:
return False
global curr_window
curr_window = None
window.destroy()
window.protocol("WM_DELETE_WINDOW", on_closing)
def toggleFSS():
if dsState.get():
fssCheck['state'] = NORMAL
else:
fssCheck['state'] = DISABLED
dsState = BooleanVar()
dsState.set(defaults['DiscoveryScan'])
dsCheck = Checkbutton(window, text='Automatic Discovery Scan', var=dsState, command=toggleFSS)
dsCheck.place(relx=0.02, rely=0.02)
fssState = BooleanVar()
fssState.set(defaults['AutoFSS'])
fssCheck = Checkbutton(window, text='Automatic FSS Scan\n (Currently waits for user to operate FSS)',
var=fssState)
fssCheck.place(relx=0.05, rely=0.09)
startKeyLbl = Label(window, text='Start EDAutopilot Key:')
startKeyLbl.place(relx=0.02, rely=0.23)
def on_startKey():
getKeyForBtn(startKeyBtn)
startKeyBtn = Button(window, text=defaults['StartKey'], command=on_startKey, width=20)
startKeyBtn.place(relx=0.46, rely=0.22)
startKeyLbl = Label(window, text='End EDAutopilot Key:')
startKeyLbl.place(relx=0.02, rely=0.33)
def on_endKey():
getKeyForBtn(endKeyBtn)
endKeyBtn = Button(window, text=defaults['EndKey'], command=on_endKey, width=20)
endKeyBtn.place(relx=0.46, rely=0.32)
rtLbl = Label(window, text='Refuel threshold percentage:')
rtLbl.place(relx=0.02, rely=0.43)
def callback(strnum):
return ((str.isdigit(strnum)) and (len(strnum) <= 3) and (0 <= int(strnum) <= 100)) or strnum == ""
vcmd = (window.register(callback))
refuelThreshold = Entry(window, validate='all', validatecommand=(vcmd, '%P'), width=10, justify='center')
refuelThreshold.insert(0, defaults['RefuelThreshold'])
refuelThreshold.place(relx=0.62, rely=0.44)
def get_refuel_threshold(entry):
if not entry:
return defaults['RefuelThreshold']
return str(int(entry.get())) # Just to be triply sure it's an int in str form
def unsaved():
return str(fssState.get()) != defaults['AutoFSS'] or str(dsState.get()) != defaults['DiscoveryScan'] or \
startKeyBtn['text'] != defaults['StartKey'] or endKeyBtn['text'] != defaults['EndKey'] or \
get_refuel_threshold(refuelThreshold) != defaults['RefuelThreshold']
def on_save():
if not unsaved():
return
if str(fssState.get()) != defaults['AutoFSS']:
setOption('AutoFSS', fssState.get())
if str(dsState.get()) != defaults['DiscoveryScan']:
setOption('DiscoveryScan', dsState.get())
if startKeyBtn['text'] != defaults['StartKey']:
setOption('StartKey', startKeyBtn['text'])
if endKeyBtn['text'] != defaults['EndKey']:
setOption('EndKey', endKeyBtn['text'])
if get_refuel_threshold(refuelThreshold) != defaults['RefuelThreshold']:
setOption('RefuelThreshold', get_refuel_threshold(refuelThreshold))
saveBtn = Button(window, text="Save Settings", command=on_save)
saveBtn.place(relx=0.70, rely=0.90)
return window
def open_settings():
global curr_window
curr_window = create_window()
curr_window.focus_force()
curr_window.mainloop()
def force_close_settings():
global curr_window
if curr_window is not None:
curr_window.destroy()
curr_window = None
if __name__ == '__main__':
open_settings()