-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgui.py
244 lines (198 loc) · 8.77 KB
/
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
from config import config
import myNotebook as nb
from ttkHyperlinkLabel import HyperlinkLabel
import types
from neutron import Neutron
from riches import Riches
from blank import Blank
try:
# Python 2
import Tkinter as tk
import ttk
except ModuleNotFoundError:
# Python 3
import tkinter as tk
import tkinter.ttk as ttk
class MainGUI(tk.Frame):
"""
For new page:
Set new ID for that page
Create setup function to create menu and page
You can see how its done in setup_neutron for example
"""
def __init__(self, master, global_data, **kw):
self.BLANK_ID = 0
self.NEUTRON_ID = 1
self.RICHES_ID = 2
tk.Frame.__init__(self, master, **kw)
self.main_frame = None
self.menu_frame = None
self.globals = global_data
self.pages = dict()
self.menu_buttons = dict()
self.page_active = 0
self.setup()
self.update()
def setup(self):
"""
Creates main frame and menu frame
:return:
"""
# Plugin's frame
self.globals.logger.debug("Creating GUI's main frame...")
self.main_frame = tk.Frame(self, borderwidth=2, relief="flat")
self.main_frame.pack(side="top", fill="both", expand=True, padx=0, pady=0)
self.globals.logger.debug("GUI's main frame created")
# Plugin's menu frame
self.globals.logger.debug("Creating GUI's menu frame..")
self.menu_frame = tk.Frame(self.main_frame, borderwidth=0, highlightthickness=0)
self.setup_riches()
self.setup_neutron()
self.setup_blank()
self.menu_frame.pack(side="top", fill="both", expand=True, anchor="w")
self.globals.logger.debug("GUI's menu frame created.")
def setup_blank(self):
"""
Creates blank GUI page
"""
self.globals.logger.debug("Creating GUI blank frame..")
ID = self.BLANK_ID
# MENU
# None
# PAGE
self.pages[ID] = Blank(self.main_frame, self.globals)
self.globals.logger.debug("GUI blank frame created.")
def setup_neutron(self):
"""
Creates Neutron Plotter GUI
"""
self.globals.logger.debug("Creating neutron plotter frame GUI...")
# MENU
ID = self.NEUTRON_ID
self.menu_buttons[ID] = tk.Button(self.menu_frame, text="Neutron", relief=tk.FLAT,
command=lambda page=ID: self.page(page))
# PAGE
self.pages[ID] = Neutron(self.main_frame, self.globals)
self.globals.logger.debug("Neutron plotter frame GUI created.")
def setup_riches(self):
"""
Creates Road to Riches GUI
:return:
"""
self.globals.logger.debug("Creating road to riches frame GUI...")
# MENU
ID = self.RICHES_ID
self.menu_buttons[ID] = tk.Button(self.menu_frame, text="Riches", relief=tk.FLAT,
command=lambda page=ID: self.page(page))
# PAGE
self.pages[ID] = Riches(self.main_frame, self.globals)
self.globals.logger.debug("Neutron road to riches GUI created.")
def update(self):
self.globals.logger.debug("Updating GUI")
# Are pages enabled
neutron = config.getint("autopath_neutron") # page 1
riches = config.getint("autopath_riches") # page 2
# Menu column
column = 0
button_len = 0
# Hide all buttons
self.globals.logger.debug("Hiding all menu buttons")
for button in self.menu_buttons.values():
button.grid_forget()
self.globals.logger.debug("Menu buttons hidden.")
# Neutron button shown
if neutron:
button_len = button_len + 1
self.globals.logger.debug("Neutron plotter menu button shown.")
self.menu_buttons[self.NEUTRON_ID].grid(row=0, column=column)
column = column + 1
if self.page_active == 0:
self.page_active = self.NEUTRON_ID
else:
if self.page_active == self.NEUTRON_ID:
self.page_active = 0
# Road to riches button shown
# Todo: Road to riches unimplememted so hardcode disabled
if riches and False:
button_len = button_len + 1
self.globals.logger.debug("Road to riches menu button shown")
self.menu_buttons[self.RICHES_ID].grid(row=0, column=column)
column = column + 1
if self.page_active == 0:
self.page_active = self.RICHES_ID
else:
if self.page_active == self.RICHES_ID:
self.page_active = 0
self.menu_frame.pack_forget()
if button_len > 2:
self.menu_frame.pack(side="top", fill="both", expand=True, anchor="w")
# Hide all pages
self.globals.logger.debug("Hide all pages")
for page in self.pages.values():
page.pack_forget()
self.globals.logger.debug("All pages hidden.")
# Show page
self.globals.logger.debug("Trying to show page number " + str(self.page_active))
if self.page_active in self.pages.keys():
self.globals.logger.debug("Page " + str(self.page_active) + " exist, enabled")
self.pages[self.page_active].pack(side="top", fill="both", expand=True, anchor="w")
def page(self, page):
self.globals.logger.debug("Changing GUI page to " + str(page))
self.page_active = page
self.update()
class PrefGUI:
# Settings for pref layout
PADX = 10
BUTTONX = 12
# Storage for checkboxes
neutron = tk.IntVar(value=config.getint('autopath_neutron'))
riches = tk.IntVar(value=config.getint('autopath_riches'))
@staticmethod
def create_gui(parent, update_func, globals):
"""
Create plugin's preference GUI
:param parent: parent frame
:param gui:
:return: plugin's preference GUI
"""
# Settings frame
frame = nb.Frame(parent)
frame.columnconfigure(1, weight=1)
# Headline label
globals.logger.debug("Creating settings hyperling label..")
HyperlinkLabel(frame, text='EDMC: Autopath', background=nb.Label().cget('background'),
url='https://github.com/Sognus/EDMC-Autopath', underline=True).grid(columnspan=2,
padx=PrefGUI.PADX,
sticky=tk.W)
globals.logger.debug("Settings hyperlink label created.")
# Checkbox for Road To Riches
globals.logger.debug("Creating settings neutron plotter checkbox...")
neutron_button = nb.Checkbutton(frame, text=_('Enable neutron plotter feature'), variable=PrefGUI.neutron,
command=lambda update_func=update_func: PrefGUI.prefs_changed(update_func, globals))
neutron_button.grid(columnspan=2, padx=PrefGUI.BUTTONX, pady=(5, 0), sticky=tk.W)
globals.logger.debug("Settings neutron plotter created.")
# Checkbox for Road To Riches
globals.logger.debug("Creating settings road to riches checkbox")
riches_button = nb.Checkbutton(frame, text=_('Enable road to riches feature'), variable=PrefGUI.riches,
command=lambda update_func=update_func: PrefGUI.prefs_changed(update_func, globals))
riches_button.grid(columnspan=2, padx=PrefGUI.BUTTONX, pady=(5, 0), sticky=tk.W)
globals.logger.debug("Settings neutron plotter created")
return frame
@staticmethod
def prefs_changed(update_func=None, globals=None):
"""
Save changes to config storage (whatever is used - windows registers or file)
If update func is set, func will be called
:return: None
"""
globals.logger.debug("Saving settings..")
config.set("autopath_neutron", PrefGUI.neutron.get())
config.set("autopath_riches", PrefGUI.riches.get())
globals.logger.debug("Settings saved.")
globals.logger.debug("Trying to invoke GUI update function.")
if update_func is not None and isinstance(update_func, types.FunctionType):
globals.logger.debug("Calling GUI update function...")
update_func()
globals.logger.debug("GUI update function completed")
else:
globals.logger.debug("GUI update function cannot be invoked")