-
Notifications
You must be signed in to change notification settings - Fork 0
/
topbar.py
451 lines (413 loc) · 17.8 KB
/
topbar.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.graphics import *
from kivy.uix.dropdown import DropDown
from kivy.uix.boxlayout import BoxLayout
from popups import Popups
from customwidgets import HoverButton
from constants import *
import manager
import database
import fileIO
##
# Class: TopBar
# -------------
# This class constructs and provides an interface for interaction with the top
# bar of the menu screen. Should be created from within menu.py
##
class TopBar(FloatLayout):
##
# Class Constructor: __init__
# ---------------------------
# This method is called during creation of the TopBar object.
#
# @params
# (TopBar) self This instance of TopBar
# (Various) **kwargs Arguments for construction of internal
# FloatLayout object
##
def __init__(self, **kwargs):
super(TopBar, self).__init__(size_hint=(1,None),
size=(1000, 60),
**kwargs)
with self.canvas.before:
self.rect = Image(source=BKGD_LCHRC,
keep_ratio=False,
allow_stretch=True,
size_hint=(1,1))
self.bind(pos=manager.update_rect, size=manager.update_rect)
self.button_panel = None
self.search_bar = None
self.settings = None
self.search_mode = None
self.search_dropdown = None
self.popups = Popups()
self.mode = "Users"
def add_logo(self):
self.add_widget(Image(source='images/logo.jpg',
keep_ratio=True,
allow_stretch=True,
size_hint=(None,None),
size=(90,60),
pos_hint={'top':1,'left':1}))
##
# Class Method: draw_top_bar
# --------------------------
# This method clears and redraws the top bar, ommiting the create new group
# and user buttons if the logged-in user is not an admin.
#
# @params
# (TopBar) self This instance of TopBar
##
def draw_top_bar(self):
self.clear_widgets()
self.create_button_panel()
self.add_logo()
if manager.ADMIN:
self.create_add_user_button()
self.create_add_group_button()
self.create_search_bar()
self.create_search_filter()
self.create_settings_dropdown()
##
# Class Method: get_search_term
# -----------------------------
# This method returns the text that is currently entered into the search bar.
#
# @params
# (TopBar) self This instance of TopBar
#
# (str) return Text in searchbar
##
def get_search_term(self):
return self.search_bar.text
##
# Class Method: create_button_panel
# ---------------------------------
# This method creates the layout for all TopBar buttons, sizing itself dependant
# on whether the logged-in user is an admin.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_button_panel(self):
x_size = 680
if not manager.ADMIN:
x_size = 410
self.button_panel = BoxLayout(size=(x_size,60),size_hint=(None, None), pos_hint={"right":1, "top":1},
orientation="horizontal", spacing=10)
self.add_widget(self.button_panel)
##
# Class Method: create_add_user_button
# ---------------------------------
# This method creates and adds to this instance of TopBar the "Add New User"
# button.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_add_user_button(self):
self.button_panel.add_widget(HoverButton(text="Add New User",
button_up=BTN_LCHRC[0],
button_down=BTN_LCHRC[1],
font_size=14,
size=(125,40),
size_hint=(None,None),
pos_hint={'center_x':.5, 'center_y':.5},
on_press=self.popups.open_add_user_popup))
##
# Class Method: create_add_group_button
# ---------------------------------
# This method creates and adds to this instance of TopBar the "Add New Group"
# button.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_add_group_button(self):
self.button_panel.add_widget(HoverButton(text="Add New Group",
button_up=BTN_LCHRC[0],
button_down=BTN_LCHRC[1],
font_size=14,
size=(125,40),
size_hint=(None,None),
pos_hint={'center_x':.5, 'center_y':.5},
on_press=self.popups.open_new_group_popup))
##
# Class Method: on_text
# ---------------------
# This method is called by the search bar any time the text within it is
# modified. This method communicates with menu.py, instructing it to update the
# list of displayed user/group names to ones that match the search term.
#
# @params
# (TopBar) self This instance of TopBar
# (TextInput) instance The searchbar
# (str) value Text in the searchbar
##
def on_text(self, instance, value):
manager.menu.show_users(value)
manager.menu.show_groups(value)
if 'attr' in self.search_mode.text.lower():
manager.menu.recolor_info_panel()
##
# Class Method: create_search_filter
# ----------------------------------
# This method creates the dropdown on the topbar which allows
# selection of a search filter between 'name', 'attr name',
# and 'attr value.'
#
# @params
# (TopBar) self This instance of TopBar
##
def create_search_filter(self):
self.search_dropdown = DropDown(auto_width=False,
max_height=180,
on_dismiss=self.reactivate_buttons)
filter_layout = FloatLayout(size=(80, 60))
search_label = Label(text="Search Mode",
font_size=12,
pos_hint={'center_x':.5, 'top':1.35})
self.search_mode = HoverButton(text="Username",
font_size=12,
size_hint=(None, None),
size=(80,30),
pos_hint={'right':1, 'center_y':.4},
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
on_release=lambda instance: self.open_dropdown(instance,
self.search_dropdown))
self.search_mode.name_button = HoverButton(text="Username",
font_size=12,
button_up=DD_LCHRC[0],
pos_hint={"right":1},
button_down=DD_LCHRC[1],
size_hint=(None,None),
size=(100,30),
on_release=self.switch_search_mode)
self.search_mode.attr_button = HoverButton(text="Attr Name",
font_size=12,
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
size_hint=(None,None),
size=(100,30),
on_release=self.switch_search_mode)
self.search_mode.val_button = HoverButton(text="Attr Value",
font_size=12,
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
size_hint=(None,None),
size=(100,30),
on_release=self.switch_search_mode)
self.search_dropdown.add_widget(self.search_mode.name_button)
self.search_dropdown.add_widget(self.search_mode.attr_button)
self.search_dropdown.add_widget(self.search_mode.val_button)
self.button_panel.add_widget(filter_layout)
filter_layout.add_widget(self.search_mode)
filter_layout.add_widget(search_label)
##
# Class Method: get_search_mode
# -----------------------------
# This method returns the mode in which the searchbar is
# filtering which users/groups are displayed.
#
# @params
# (TopBar) self This instance of TopBar
##
def get_search_mode(self):
if "attr" not in self.search_mode.text.lower():
return "name"
else:
if "Name" in self.search_mode.text:
return "attr name"
else:
return "attr val"
##
# Class Method: switch_search_mode
# --------------------------------
# This method switches the search mode and displays the currently searched
# users/groups on the tabbed menu.
#
# (TopBar) self This instance of TopBar
# (HoverButton) instance The button pressed to switch search modes
##
def switch_search_mode(self, instance):
self.search_mode.text = instance.text
self.search_dropdown.dismiss()
if self.mode == 'Users':
manager.menu.show_searched_users()
else:
manager.menu.show_searched_groups()
manager.menu.recolor_info_panel()
##
# Class Method: toggle_group_user
# -------------------------------
# This method toggles the topbar mode between 'Users' mode and 'Groups' mode.
#
# @params
# (TopBar) self This instance of TopBar
# (str) mode Mode to switch to. 'Users' or 'Groups'
##
def toggle_group_user(self, mode):
if not self.search_mode:
return
if mode == 'Users':
self.mode = 'Users'
self.search_mode.name_button.text = 'Username'
if self.search_mode.text == 'Groupname':
self.search_mode.text = 'Username'
else:
self.mode = 'Groups'
self.search_mode.name_button.text = 'Groupname'
if self.search_mode.text == 'Username':
self.search_mode.text = 'Groupname'
##
# Class Method: create_search_bar
# -------------------------------
# This method creates a searchbar and adds it to this instance of TopBar.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_search_bar(self):
self.search_bar = TextInput(size_hint=(None, None),
size=(250, 30),
background_normal=SRCH_BKGD[0],
padding_x=[33,0],
background_active=SRCH_BKGD[1],
pos_hint={'right':.995, 'center_y':.5},
hint_text='Search',
multiline=False)
self.search_bar.bind(text=self.on_text)
self.button_panel.add_widget(self.search_bar)
##
# Class Method: create_presets_menu_button
# ----------------------------------------
# This method creates a button that switches the page to the presets menu page
# and adds the button to the settings dropdown.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_presets_menu_button(self):
#Used by presets menu button to transition screens
def to_preset_manager(instance):
manager.sm.current = "presetMenu"
self.settings.dismiss()
self.settings.add_widget(HoverButton(text="User Presets Manager",
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
font_size=12,
size=(180,40),
size_hint=(None,None),
pos_hint={'right':1, 'right':1},
on_press=to_preset_manager))
##
# Class Method: create_upload_file_button
# ---------------------------------------
# This method creates a button that allows uploading a user .txt file to load
# into the database and adds the button to the settings dropdown.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_upload_file_button(self):
def to_file_chooser(instance):
self.settings.dismiss()
self.popups.open_file_chooser(instance)
self.settings.add_widget(HoverButton(text="Upload User File",
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
font_size=12,
size=(180,40),
size_hint=(None,None),
pos_hint={'right':1, 'right':1},
on_press=to_file_chooser))
##
# Class Method: create_logout_button
# ----------------------------------
# This method creates the logout button and adds it to the settings dropdown.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_logout_button(self):
self.settings.add_widget(HoverButton(text="Logout",
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
font_size=12,
size=(180,40),
size_hint=(None,None),
pos_hint={'right':1, 'right':1},
on_press=lambda instance: manager.logout(self.settings)))
##
# Class Method: create_admin_pw_button
# ------------------------------------
# This method creates the change admin password button and adds it to the settings dropdown.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_admin_pw_button(self):
self.settings.add_widget(HoverButton(text="Change Admin Password",
button_up=DD_LCHRC[0],
button_down=DD_LCHRC[1],
font_size=12,
size=(180,40),
size_hint=(None,None),
pos_hint={'right':1, 'right':1},
on_press=self.popups.change_adminpw_popup))
##
# Class Method: open_dropdown
# ---------------------------
# This method opens a dropdown and makes the group of buttons
# underneath the dropdown (buttons on the options panel)
# inactive.
#
# @params
# (TopBar) self This instance of TopBar
# (HoverButton) instance Mainbutton of dropdown
##
def open_dropdown(self, instance, dropdown):
dropdown.open(instance)
HoverButton.inactive_group = 'options'
##
# Class Method: reactivate_buttons
# --------------------------------
# This method is called when the settings dropdown is dismissed, and
# it simply reactivates the buttons below where the dropdown was.
# (reactivates buttons on the options panel).
#
# @params
# (TopBar) self This instance of TopBar
# (DropDown) instance Dropdown which was dismissed
##
def reactivate_buttons(self, instance):
HoverButton.inactive_group = None
##
# Class Method: create_settings_dropdown
# --------------------------------------
# This method creates the settings dropdown, and adds it to this
# instance of topbar.
#
# @params
# (TopBar) self This instance of TopBar
##
def create_settings_dropdown(self):
self.settings = DropDown(auto_width=False,
width=180,
pos_hint={"right":1},
on_dismiss=self.reactivate_buttons)
mainbutton = HoverButton(size=(60,60),size_hint=(None,None),
button_down=BTN_OPTS[1],
button_up=BTN_OPTS[0],
pos_hint={"center_y":.5},
on_release=lambda instance: self.open_dropdown(instance, self.settings))
self.button_panel.add_widget(mainbutton)
if manager.ADMIN:
self.create_presets_menu_button()
self.create_upload_file_button()
self.create_admin_pw_button()
self.create_logout_button()