-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.py
More file actions
181 lines (134 loc) · 5.71 KB
/
canvas.py
File metadata and controls
181 lines (134 loc) · 5.71 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
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
import tkinter as tk
class Canvas(tk.Canvas):
"""
Main display of application, shows machines executing tasks
"""
parameters = {"width": 800,
"height": 600,
"bg": "black", }
machine_size = 50
# margin with borders, x, y, and next machine
machine_margin = (75, 50, 50)
machine_color = "lightblue"
task_size = 25
task_margin = (50, 25) # margin with machine, with next task
task_colors = {"working": "yellow",
"paused": "red",
"finished": "green", }
time_size = 8
time_margin = 13
time_color = "white"
time_font = "Trebuchet MS"
name_size = 8
name_color = "black"
name_font = "Trebuchet MS"
scroll_sensitivity = (10, 25)
view_margin = (25, 25)
def __init__(self, master, main_application, machines=[]):
super().__init__(master, **Canvas.parameters)
self.master = master
self.main_application = main_application
self.object_to_id = {}
self.id_to_object = {}
self.task_to_text_length = {}
self.machine_to_text_id = {}
self.machines = []
self.machine_to_display = {}
self.tasks = []
for m in machines:
self.add_machine(m)
self.tasks += list(m.allTasks.values())
self.view_offset_x, self.view_offset_y = 0, 0
self.draw_machines()
self.bind("<Left>", lambda x: self.scroll_view(x, (1, 0)))
self.bind("<Right>", lambda x: self.scroll_view(x, (-1, 0)))
self.bind("<Up>", lambda x: self.scroll_view(x, (0, 1)))
self.bind("<Button-4>", lambda x: self.scroll_view(x, (0, 1)))
self.bind("<Down>", lambda x: self.scroll_view(x, (0, -1)))
self.bind("<Button-5>", lambda x: self.scroll_view(x, (0, -1)))
self.focus_set()
def add_machine(self, machine):
self.machines.append(machine)
self.machine_to_display[machine] = []
def clear_display(self):
self.delete('all')
def draw_machines(self):
"""
Draws all machines
"""
# TO-DO : Need to discriminate with machine's type for display
self.clear_display()
x1, y1, _ = Canvas.machine_margin
x2, y2 = x1 + Canvas.machine_size, y1 + Canvas.machine_size
x1 += self.view_offset_x
x2 += self.view_offset_x
y1 += self.view_offset_y
y2 += self.view_offset_y
for k in range(len(self.machines)):
machine = self.machines[k]
machine_id = self.create_rectangle(x1, y1, x2, y2,
fill=Canvas.machine_color)
text_id = self.create_text(round(.5*(x1+x2)), round(.5*(y1+y2)),
text="{} {}".format(machine.name, machine.id), anchor=tk.CENTER,
font=(Canvas.name_font, Canvas.name_size))
y1 = y2 + Canvas.machine_margin[2]
y2 = y1 + Canvas.machine_size
self.object_to_id[machine] = machine_id
self.id_to_object[machine_id] = machine
self.machine_to_text_id[machine] = text_id
self.draw_tasks(machine_id)
def draw_tasks(self, machine_id):
"""
Draws all tasks of a machine
"""
x1, y1, x2, y2 = self.coords(machine_id)
x1 += Canvas.machine_size + Canvas.task_margin[0]
y1 += round((Canvas.machine_size - Canvas.task_size)/2)
x2, y2 = x1 + Canvas.task_size, y1 + Canvas.task_size
machine = self.id_to_object[machine_id]
for task in machine.allTasks.values():
if task.status != "unavailable" and task not in self.machine_to_display[machine]:
self.machine_to_display[machine].append(task)
for task in machine.finishedTasks.values():
try:
self.machine_to_display[machine].remove(task)
except ValueError:
pass
for task in self.machine_to_display[machine]:
task_id = self.create_oval(
x1, y1, x2, y2, fill=Canvas.task_colors[task.status])
if task.status != "finished" and task.currentStep > 0:
extent = int(360 * task.currentStep / task.realLength)
fill = Canvas.task_colors["finished"] if task.status == "working" else Canvas.task_colors["paused"]
pogress_id = self.create_arc(
x1, y1, x2, y2, fill=fill, start=90, extent=extent)
x1 += Canvas.task_size + Canvas.task_margin[1]
x2 = x1 + Canvas.task_size
self.object_to_id[task] = task_id
self.id_to_object[task_id] = task
self.draw_length(task)
def draw_length(self, task):
"""
Draws the lengths of a task, [real, predicted]
"""
x1, y1, x2, y2 = self.coords(self.object_to_id[task])
text = "[{}, {}]".format(task.realLength, task.predLength)
x, y = round((x1 + x2)/2), y2 + Canvas.time_margin
text_id = self.create_text(x, y, anchor=tk.CENTER, text=text, fill=Canvas.time_color, font=(
Canvas.time_font, Canvas.time_size))
self.task_to_text_length[task] = text_id
def _make_frame(self):
self.clear_display()
self.draw_machines()
def scroll_view(self, event, direction=(0, 0)):
self.view_offset_x += Canvas.scroll_sensitivity[0] * direction[0]
self.view_offset_y += Canvas.scroll_sensitivity[1] * direction[1]
if self.view_offset_x > 0:
self.view_offset_x = 0
if self.view_offset_y > 0:
self.view_offset_y = 0
self._make_frame()
def run(self, event):
for m in self.machines:
m.run(1)
self._make_frame()