Skip to content

Commit f694e37

Browse files
author
Fernando Cezar
committed
treat cases for empty databases
1 parent da93f3b commit f694e37

2 files changed

Lines changed: 42 additions & 19 deletions

File tree

main.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os
32
import random
43
import sys
@@ -41,7 +40,6 @@ def __init__(self, message):
4140
self.layout.addWidget(self.button)
4241

4342

44-
# Subclass QMainWindow to customize your application's main window
4543
class MainWindow(QMainWindow):
4644
def __init__(self):
4745
super().__init__()
@@ -100,26 +98,27 @@ def draw_register_situation_screen(self):
10098
def draw_situation_quiz_screen(self):
10199
self.situation_quiz_widget = SituationQuiz()
102100
self.situation_quiz_widget.cancel_button.clicked.connect(self.start_screen)
103-
self.situation_quiz_widget.answer_button.clicked.connect(
104-
lambda: self.check_situation_answer(self.situation_quiz_widget)
105-
)
106-
buttons = [
107-
self.situation_quiz_widget.cancel_button,
108-
self.situation_quiz_widget.answer_button,
109-
]
101+
buttons = [self.situation_quiz_widget.cancel_button]
102+
if hasattr(self.situation_quiz_widget, "answer_button"):
103+
self.situation_quiz_widget.answer_button.clicked.connect(
104+
lambda: self.check_situation_answer(self.situation_quiz_widget)
105+
)
106+
buttons.append(self.situation_quiz_widget.answer_button)
110107
self._draw_screen(self.situation_quiz_widget, buttons)
111108

112109
def draw_choose_quiz_screen(self):
113110
self.choose_drug_widget = ChooseDrugQuiz()
114111
self.choose_drug_widget.cancel_button.clicked.connect(self.start_screen)
115-
self.choose_drug_widget.start_button.clicked.connect(
116-
lambda: self.draw_quiz_screen(self.choose_drug_widget.combo_options.currentText(), quiz_type="choose")
117-
)
118-
buttons = [
119-
self.choose_drug_widget.cancel_button,
120-
self.choose_drug_widget.start_button,
121-
]
122-
112+
buttons = [self.choose_drug_widget.cancel_button]
113+
if hasattr(self.choose_drug_widget, "start_button"):
114+
self.choose_drug_widget.start_button.clicked.connect(
115+
lambda: self.draw_quiz_screen(
116+
self.choose_drug_widget.combo_options.currentText(),
117+
quiz_type="choose"
118+
)
119+
)
120+
buttons.append(self.choose_drug_widget.answer_button)
121+
123122
self._draw_screen(self.choose_drug_widget, buttons)
124123

125124
def draw_quiz_screen(self, drug, quiz_type):
@@ -135,6 +134,10 @@ def save_content(self, widget):
135134

136135
def random_quiz(self):
137136
current_content = read_current_drugs()
137+
if not current_content:
138+
self.answer_window = AnswerWindow("No substances registered. Please register some substances first.")
139+
self.answer_window.show()
140+
return
138141
random_item = random.choice(list(current_content.keys()))
139142
self.draw_quiz_screen(random_item, "random")
140143

widgets/screens.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,18 @@ def __init__(self):
179179
current_situations = read_current_situations()
180180
current_drugs = read_current_drugs()
181181
current_drugs = list(current_drugs.keys())
182+
if not current_situations or not current_drugs:
183+
self.draw_empty()
184+
else:
185+
self.draw_question(current_situations, current_drugs)
186+
187+
self.cancel_button = QPushButton("Cancel")
182188

189+
def draw_empty(self):
190+
self.addWidget(QLabel("No situations or substances registered!"), 1, 0)
191+
self.addWidget(QLabel("Register situations and substances on the home screen"), 2, 0)
192+
193+
def draw_question(self, current_situations, current_drugs):
183194
random_item = random.choice(current_situations)
184195
self.correct_answer = random_item[1]
185196

@@ -195,7 +206,7 @@ def __init__(self):
195206
self.addWidget(self.drugs_box, 1, 1)
196207

197208
self.answer_button = QPushButton("Answer")
198-
self.cancel_button = QPushButton("Cancel")
209+
199210

200211
def answer(self):
201212
answer = self.drugs_box.currentData()
@@ -243,10 +254,19 @@ def __init__(self):
243254
super().__init__()
244255
all_options = read_current_drugs()
245256
options = list(all_options.keys())
257+
if not options:
258+
self.draw_empty()
259+
else:
260+
self.draw_question(options)
246261

262+
self.cancel_button = QPushButton("Cancel")
263+
264+
def draw_empty(self):
265+
self.addWidget(QLabel("No substances registered!\nRegister substances on the home screen"))
266+
267+
def draw_options(self, options):
247268
self.combo_options = QComboBox()
248269
self.combo_options.addItems(options)
249270
self.addWidget(self.combo_options)
250271

251272
self.start_button = QPushButton("Start quiz")
252-
self.cancel_button = QPushButton("Cancel")

0 commit comments

Comments
 (0)