-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanswer_widget_factory.py
83 lines (61 loc) · 2.38 KB
/
answer_widget_factory.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
from abc import abstractmethod
from . import enums
from .answer_widget import (
AnswerWidget,
TextAnswerWidget,
TrueFalseAnswerWidget,
MultipleChoiceAnswer,
)
from PyQt6.QtWidgets import QApplication
class AnswerWidgetFactory:
@abstractmethod
def __init__(self) -> None:
pass
@abstractmethod
def create(self, flashcard, parent=None):
pass
@staticmethod
def get_correct_answer_widget_factory(flashcard):
if flashcard.card_type == enums.CardType.Text:
return TextAnswerWidgetFactory()
elif flashcard.card_type == enums.CardType.TrueFalse:
return TrueFalseAnswerWidgetFactory()
else:
return MultipleChoiceAnswerWidgetFactory()
class TextAnswerWidgetFactory(AnswerWidgetFactory):
def __init__(self) -> None:
super().__init__()
def create(self, flashcard, parent=None):
return TextAnswerWidget(flashcard.answer, parent)
class TrueFalseAnswerWidgetFactory(AnswerWidgetFactory):
def __init__(self) -> None:
super().__init__()
def create(self, flashcard, parent=None):
return TrueFalseAnswerWidget(flashcard.answer, parent)
class MultipleChoiceAnswerWidgetFactory(AnswerWidgetFactory):
def __init__(self) -> None:
super().__init__()
def create(self, flashcard, parent=None):
return MultipleChoiceAnswer(flashcard.answer, parent)
# Do testów
if __name__ == "__main__":
class Flashcard:
def __init__(self, question, answer, card_type) -> None:
self.question = question
self.answer = answer
self.card_type = card_type
import sys
app = QApplication(sys.argv)
text_flashcard = Flashcard("pytanie text", "odpowiedz text", 0)
true_false_flashcard = Flashcard("pytanie true false", "False", 1)
multiple_choice_flashcard = Flashcard(
"pytanie multiple choice", '{"name":0, "age":0, "car":1}', 2
)
factory = AnswerWidgetFactory.get_correct_answer_widget_factory(text_flashcard)
# factory = AnswerWidgetFactory.getCorrectAnswerWidgetFactory(true_false_flashcard)
# factory = AnswerWidgetFactory.getCorrectAnswerWidgetFactory(multiple_choice_flashcard)
widget = factory.create(text_flashcard)
# widget = factory.create(true_false_flashcard)
# widget = factory.create(multiple_choice_flashcard)
widget.show()
sys.exit(app.exec())