-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol_view.py
More file actions
138 lines (116 loc) · 5.99 KB
/
control_view.py
File metadata and controls
138 lines (116 loc) · 5.99 KB
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
import pygame
from pygame.locals import *
import icons
import events
import gui_layout
import link_gui_backend
import logging
DARK_GREY = (75, 75, 75)
def select_course_button(clicked_button, course_buttons):
for button in course_buttons:
button.color_unclicked = button.color_default
clicked_button.color_unclicked = clicked_button.color_selected
def create_class_list_buttons(students, height, file):
student_buttons = []
y_pos = 15
i = 0
for student in students:
student_buttons.append(
icons.TextButtonLinkedToFile((320, y_pos), (350, height), student, file, i)
)
y_pos += height + 1
i += 1
while i < 24:
student_buttons.append(
icons.TextButtonLinkedToFile((320, y_pos), (350, height), "", file, i)
)
y_pos += height + 1
i += 1
for button in student_buttons:
button.linked_buttons = student_buttons
return student_buttons
def class_list_from_file(config_file, selected_course, student_button_height):
students, file = link_gui_backend.load_students(config_file, selected_course)
student_buttons = create_class_list_buttons(students, student_button_height, file)
return student_buttons
def flatten(list_of_lists):
return [b for sublist in list_of_lists for b in sublist]
def run(config_file, selected_course, screen, clock, constants):
courses = link_gui_backend.courses(config_file)
background_heights = constants.WIDTH_HEIGHT[1] - 20 * 2
classes_rect = pygame.Rect(15, 15, 280, background_heights)
student_button_height = background_heights // 24
student_buttons = create_class_list_buttons(["StudentID, StudentName"], student_button_height, None)
if selected_course:
new_buttons = class_list_from_file(config_file, selected_course, student_button_height)
for (i, student_button) in enumerate(new_buttons):
student_buttons[i] = student_button
add_course_button = icons.Button((25, 25), (125, 50), "Add Course")
delete_course_button = icons.Button((160, 25), (125, 50), "Delete Course")
course_buttons = []
course_y_pos = 85
for course in courses:
course_button = icons.Button((25, course_y_pos), (260, 25), course)
course_buttons.append(course_button)
if course == selected_course:
select_course_button(course_button, course_buttons)
course_y_pos += 30
class_view_button = icons.Button(*gui_layout.right_action_button(0), "Go to class view")
seating_plan_view_button = icons.Button(*gui_layout.right_action_button(1), "Go to seating plan")
control_buttons = [
icons.ButtonWhichCallsFunction(*gui_layout.right_action_button(2), "Build reports",
link_gui_backend.build_reports, config_file),
icons.ButtonWhichCallsFunction(*gui_layout.right_action_button(3), "Calculate moyennes",
link_gui_backend.calculate_averages, config_file),
]
quit_button = icons.Button(*gui_layout.right_action_button(4), "Quit")
buttons = [[add_course_button, delete_course_button, quit_button, class_view_button, seating_plan_view_button],
course_buttons, control_buttons, student_buttons]
button = control_buttons[0] # TODO create a proper holding button
while True:
screen.fill(constants.BACKGROUND)
pygame.draw.rect(screen, DARK_GREY, classes_rect)
flat_buttons = flatten(buttons)
for event in pygame.event.get():
if event.type == pygame.QUIT:
logging.info("control_view received pygame.QUIT selected_course=%s", selected_course)
return "quit", selected_course
if event.type == KEYDOWN and button.text_editor_active:
button.handle_keydown(event, [button])
if event.type == MOUSEBUTTONDOWN:
button, _ = events.handle_mouse_button_down(
*event.pos, flat_buttons, []
)
events.turn_off_editors(flat_buttons, button)
if button == quit_button:
logging.info("control_view quit button clicked")
return "quit", None
if button == class_view_button and selected_course:
logging.info("control_view switching to class_view course=%s", selected_course)
return "class_view", selected_course
if button == seating_plan_view_button and selected_course:
logging.info("control_view switching to seating_plan_view course=%s", selected_course)
return "seating_plan_view", selected_course
if button == delete_course_button and selected_course:
logging.info("control_view deleting course=%s", selected_course)
link_gui_backend.delete_course_in_files(config_file, selected_course)
return "control_view", None
if button == add_course_button:
new_course = events.handle_add_course_button_click(add_course_button)
if new_course:
add_course_button.set_text("Add Course")
course_button = icons.Button((25, course_y_pos), (260, 25), new_course)
course_y_pos += 30
course_buttons.append(course_button)
courses = link_gui_backend.add_courses_in_files(config_file, new_course, courses)
button = course_button
if button in course_buttons:
selected_course = button.text
select_course_button(button, course_buttons)
new_buttons = class_list_from_file(config_file, selected_course, student_button_height)
for (i, student_button) in enumerate(new_buttons):
student_buttons[i] = student_button
for b in flat_buttons:
b.update(screen)
pygame.display.flip()
clock.tick(60)