-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.py
More file actions
156 lines (133 loc) · 6.08 KB
/
Copy pathcontroller.py
File metadata and controls
156 lines (133 loc) · 6.08 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
from domain.generator import Generator
from domain.calculator import Calculator
SOLUTION_STEPS = 4
# Welche Widget-Felder (Index) welchem Event-Attribut entsprechen – je Lösungsschritt
SOLUTION_FIELDS = {
1: [(0, "faz"), (1, "fez"), (4, "duration")],
2: [(0, "faz"), (1, "fez"), (2, "saz"), (3, "sez"), (4, "duration")],
3: [(0, "faz"), (1, "fez"), (2, "saz"), (3, "sez"), (4, "duration"), (5, "gp"), (6, "fp")],
}
STEP_NAMES = {
1: "Vorwärtsterminierung",
2: "Rückwärtsterminierung",
3: "Pufferberechnung",
4: "Kritischer Pfad",
}
MAX_EVENT_COUNT = 26
MAX_SUCCESSOR_COUNT = 6
class NetzplanController:
"""
Verbindet Domain und GUI.
Kennt beide Seiten, aber hält sie voneinander getrennt.
"""
def __init__(self, gui=None) -> None:
self.gui = gui
self.generator = Generator()
self.calculator = Calculator()
self.event_count = 9
self.successor_count = 3
self.output_index = 0
self.events = {}
self.hierarchie_count = 0
self.solutions = []
self.widget_height = 20
self.widget_width = 30
# ------------------------------------------------------------------
# Eingabe-Validierung
# ------------------------------------------------------------------
def validate_input(self, attr_name: str, widget, inc_dec: int, is_entered: bool) -> None:
limits = {"event_count": MAX_EVENT_COUNT, "successor_count": MAX_SUCCESSOR_COUNT}
hi = limits[attr_name]
if is_entered:
text = widget.text()
if text.isdigit() and 0 < int(text) <= hi:
setattr(self, attr_name, int(text))
widget.setStyleSheet("background-color: lightblue;")
else:
widget.setStyleSheet("background-color: lightcoral;")
else:
new_value = max(1, min(hi, getattr(self, attr_name) + inc_dec))
setattr(self, attr_name, new_value)
widget.setText(str(new_value))
# ------------------------------------------------------------------
# Übungs-Steuerung
# ------------------------------------------------------------------
def start_exercise(self) -> None:
self.gui.values = {}
self.events, self.hierarchie_count = self.generator.create_events(
self.event_count, self.successor_count
)
self.solutions = self.calculator.create_solutions(self.events, self.event_count)
self.gui.fill_table(self.events)
self.gui.show_input_events(self.events, self.hierarchie_count, self.widget_width, self.widget_height)
self.gui.draw_connections(self.gui.input_canvas, self.events, self.widget_width, self.widget_height)
self.output_index = 0
self._render_output()
self._update_check_button_label()
def process_button(self, index: int) -> None:
match index:
case 0: # Vorheriger Schritt
if self.output_index > 0:
self.output_index -= 1
self._render_output()
case 1: # Nächster Schritt
if self.output_index < SOLUTION_STEPS:
self.output_index += 1
self._render_output()
case 2:
self.gui.show_info()
case 3:
if self.output_index != 0:
self._check_input()
case 4:
if self.output_index != 0:
self._broadcast_output()
self._update_check_button_label()
def _render_output(self) -> None:
"""Übergibt den aktuellen Lösungsschritt an die GUI zum Rendern."""
step = SOLUTION_STEPS - 1 if self.output_index == SOLUTION_STEPS else self.output_index
self.gui.show_output_events(
self.solutions[step], self.events, self.hierarchie_count,
self.widget_width, self.widget_height
)
self.gui.draw_connections(self.gui.output_canvas, self.events, self.widget_width, self.widget_height)
if self.output_index == SOLUTION_STEPS:
self.gui.draw_critical(self.events, self.hierarchie_count, self.widget_width, self.widget_height)
# ------------------------------------------------------------------
# Eingabe-Prüfung & Übertragung
# ------------------------------------------------------------------
def _get_active_step(self) -> int:
return SOLUTION_STEPS - 1 if self.output_index == SOLUTION_STEPS else self.output_index
def _find_event(self, name: str):
for level_events in self.events.values():
for event in level_events:
if event.name == name:
return event
return None
def _check_input(self) -> None:
"""Vergleicht Nutzereingaben mit der Musterlösung – grün = richtig, rot = falsch."""
step = self._get_active_step()
fields = SOLUTION_FIELDS[step]
for event_name, widgets in self.gui.values.items():
event = self._find_event(event_name)
if event is None:
continue
for widget_idx, attr_name in fields:
is_correct = widgets[widget_idx].text() == str(getattr(event, attr_name))
color = "lightgreen" if is_correct else "lightcoral"
widgets[widget_idx].setStyleSheet(f"background-color: {color};")
def _broadcast_output(self) -> None:
"""Überträgt die Musterlösung in die Eingabefelder."""
step = self._get_active_step()
fields = SOLUTION_FIELDS[step]
for event_name, widgets in self.gui.values.items():
event = self._find_event(event_name)
if event is None:
continue
for widget_idx, attr_name in fields:
widgets[widget_idx].setText(str(getattr(event, attr_name)))
def _update_check_button_label(self) -> None:
"""Aktualisiert den 'Prüfe Eingabe'-Button mit dem aktuellen Schrittnamen."""
step_name = STEP_NAMES.get(self.output_index, "")
label = f"Prüfe Eingabe: {step_name}" if step_name else "Prüfe Eingabe"
self.gui.buttons[3].setText(label)