-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
99 lines (86 loc) · 3.46 KB
/
app.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
import threading
from PySide6.QtCore import QObject
from ChatBot import ChatBotFactory, ChatBot
from GUI import AppGUI
from PySide6.QtWidgets import QApplication
import sys
from data import DataLoader
from event import MainWindowHintEvent
from event_type import *
class App(QObject):
"""
The app class.
"""
_instance = None
def __new__(cls, *args, **kwargs):
"""
The singleton.
:param args:
:param kwargs:
"""
if cls._instance is None:
cls._instance = super(App, cls).__new__(cls)
return cls._instance
def __init__(self):
super().__init__()
App._instance = self
self.app = QApplication([])
self.data_loader = DataLoader()
# install event filter
self.data_loader.installEventFilter(self)
self.gui:AppGUI|None = None
self.chatbot_factory:ChatBotFactory|None = None
def run(self):
self.data_loader.load_data()
sys.exit(self.app.exec())
def eventFilter(self, obj, event):
if event.type() == DataLoadedEventType:
self.init_gui(event)
elif event.type() == SaveDataEventType:
self.data_loader.save_data(event.data_type)
elif event.type() == SendMessageEventType:
if isinstance(obj, AppGUI):
self.chatbot_factory.receive_message(event.history_id, event.message, event.is_speak)
if isinstance(obj, ChatBotFactory):
self.gui.receive_message(event.history_id, event.message)
elif event.type() == SpeakMessageEventType:
if isinstance(obj, AppGUI):
self.chatbot_factory.speak_it(event.history_id, event.message)
if isinstance(obj, ChatBotFactory):
self.gui.speak_message(event.history_id, event.message)
elif event.type() == AddChatBotEventType:
self.chatbot_factory.create_chatbot(event.data)
elif event.type() == DeleteChatBotEventType:
self.chatbot_factory.delete_chatbot(event.chatbot_id)
elif event.type() == ChatBotThreadStatusChangedEventType:
self.gui.onChatbotThreadStatusChanged.emit(event.chatbot_id, event.status)
elif event.type() == StopChatbotThreadEventType:
self.chatbot_factory.stopChatbot.emit(event.chatbot_id)
return super().eventFilter(obj, event)
def event(self, e):
if isinstance(e, MainWindowHintEvent):
self.gui.hint(e.hint_type, e.hint_message, self.gui.default_hint_area, e.interval)
return True
return super().event(e)
def init_gui(self, event: QEvent):
self.gui = AppGUI(event.config_data, event.chatbots_data)
self.gui.InitFinished.connect(lambda : self.init_chatbot_factory(event))
self.gui.installEventFilter(self)
if event.first_time:
self.gui.open_global_config_dialog(first_time=True)
else:
self.gui.InitFinished.emit()
self.gui.show()
def init_chatbot_factory(self, event: QEvent):
self.chatbot_factory = ChatBotFactory(event.config_data, event.chatbots_data)
self.gui.ConfigSaved.connect(self.chatbot_factory.configSaved)
self.gui.stopChat.connect(self.chatbot_factory.stopChat)
self.chatbot_factory.installEventFilter(self)
@staticmethod
def get_instance():
if not App._instance:
App._instance = App()
return App._instance
if __name__ == '__main__':
app = App()
app.run()