-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
177 lines (150 loc) · 6.37 KB
/
main.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
import os
import random
import sys
from PyQt6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QStatusBar,
QVBoxLayout,
QWidget,
)
from widgets.constants import APP_FOLDER, DRUGS_REGISTRY, SITUATIONS_REGISTRY
from widgets.helpers import ensure_file, read_current_drugs
from widgets.screens import (
ChooseDrugQuiz,
DrugQuiz,
RegisterDrugs,
RegisterSituation,
SituationQuiz,
StartMenu,
)
os.makedirs(APP_FOLDER, exist_ok=True)
ensure_file(DRUGS_REGISTRY, {})
ensure_file(SITUATIONS_REGISTRY, [])
class AnswerWindow(QWidget):
def __init__(self, message):
super().__init__()
self.layout = QVBoxLayout()
self.setLayout(self.layout)
self.label = QLabel(message)
self.layout.addWidget(self.label)
self.button = QPushButton("OK")
self.button.clicked.connect(self.close)
self.layout.addWidget(self.button)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.start_screen()
def _draw_screen(self, layout, buttons=None):
buttons = buttons or []
widget = QWidget()
self.setCentralWidget(widget)
widget.setLayout(layout)
statusBar = QStatusBar()
self.setStatusBar(statusBar)
for button in buttons:
statusBar.addWidget(button)
statusBar.show()
def start_screen(self):
self.start_widget = StartMenu()
self.connect_start_screen_buttons()
self._draw_screen(self.start_widget)
def connect_start_screen_buttons(self):
self.start_widget.register_drug.clicked.connect(self.draw_register_drug_screen)
self.start_widget.register_situation.clicked.connect(self.draw_register_situation_screen)
self.start_widget.situation_quiz.clicked.connect(self.draw_situation_quiz_screen)
self.start_widget.random_quiz.clicked.connect(self.random_quiz)
self.start_widget.choice_quiz.clicked.connect(self.draw_choose_quiz_screen)
def draw_register_drug_screen(self):
self.register_drug_widget = RegisterDrugs()
self.register_drug_widget.cancel_button.clicked.connect(self.start_screen)
self.register_drug_widget.save_button.clicked.connect(lambda: self.save_content(self.register_drug_widget))
buttons = [
self.register_drug_widget.cancel_button,
self.register_drug_widget.save_button,
self.register_drug_widget.add_new_line_button,
]
self._draw_screen(self.register_drug_widget, buttons)
def draw_register_situation_screen(self):
self.register_situation_widget = RegisterSituation()
self.register_situation_widget.cancel_button.clicked.connect(self.start_screen)
self.register_situation_widget.save_button.clicked.connect(
lambda: self.save_content(self.register_situation_widget)
)
buttons = [
self.register_situation_widget.cancel_button,
self.register_situation_widget.save_button,
self.register_situation_widget.add_new_line_button,
]
self._draw_screen(self.register_situation_widget, buttons)
def draw_situation_quiz_screen(self):
self.situation_quiz_widget = SituationQuiz()
self.situation_quiz_widget.cancel_button.clicked.connect(self.start_screen)
buttons = [self.situation_quiz_widget.cancel_button]
if hasattr(self.situation_quiz_widget, "answer_button"):
self.situation_quiz_widget.answer_button.clicked.connect(
lambda: self.check_situation_answer(self.situation_quiz_widget)
)
buttons.append(self.situation_quiz_widget.answer_button)
self._draw_screen(self.situation_quiz_widget, buttons)
def draw_choose_quiz_screen(self):
self.choose_drug_widget = ChooseDrugQuiz()
self.choose_drug_widget.cancel_button.clicked.connect(self.start_screen)
buttons = [self.choose_drug_widget.cancel_button]
if hasattr(self.choose_drug_widget, "start_button"):
self.choose_drug_widget.start_button.clicked.connect(
lambda: self.draw_quiz_screen(
self.choose_drug_widget.combo_options.currentText(),
quiz_type="choose"
)
)
buttons.append(self.choose_drug_widget.answer_button)
self._draw_screen(self.choose_drug_widget, buttons)
def draw_quiz_screen(self, drug, quiz_type):
quiz_widget = DrugQuiz(drug, quiz_type)
quiz_widget.cancel_button.clicked.connect(self.start_screen)
quiz_widget.answer_button.clicked.connect(lambda: self.check_drug_answer(quiz_widget))
buttons = [quiz_widget.cancel_button, quiz_widget.answer_button]
self._draw_screen(quiz_widget, buttons)
def save_content(self, widget):
widget.save()
self.start_screen()
def random_quiz(self):
current_content = read_current_drugs()
if not current_content:
self.answer_window = AnswerWindow("No substances registered. Please register some substances first.")
self.answer_window.show()
return
random_item = random.choice(list(current_content.keys()))
self.draw_quiz_screen(random_item, "random")
def check_drug_answer(self, widget):
widget.answer()
next_button = QPushButton("Next question")
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.start_screen)
if widget.quiz_type == "random":
next_button.clicked.connect(self.random_quiz)
else:
next_button.clicked.connect(lambda: self.draw_quiz_screen(widget.drug, widget.quiz_type))
statusBar = QStatusBar()
self.setStatusBar(statusBar)
statusBar.addWidget(next_button)
statusBar.addWidget(cancel_button)
statusBar.show()
def check_situation_answer(self, widget):
widget.answer()
next_button = QPushButton("Next question")
cancel_button = QPushButton("Cancel")
cancel_button.clicked.connect(self.start_screen)
next_button.clicked.connect(self.draw_situation_quiz_screen)
statusBar = QStatusBar()
self.setStatusBar(statusBar)
statusBar.addWidget(next_button)
statusBar.addWidget(cancel_button)
statusBar.show()
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()