-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextMessageHandler.py
97 lines (76 loc) · 3.11 KB
/
TextMessageHandler.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
import sys
import json
import asyncio
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.QtCore import Qt, QThread
from PyQt5.QtWebSockets import QWebSocket
from qasync import QEventLoop
import pyttsx3
from UI.Ui_mainWindow import Ui_MainWindow
from connection.data_definition import ChatHeader, ChatData
from ConnectionHandler import ConnectionHandler
# define the Text Message Box Handler
class TextMessageHandler:
def __init__(self, mainWindow: QMainWindow, ui: Ui_MainWindow, connectionHandler: ConnectionHandler):
# initialize all the necessary objects
self.mainWindow = mainWindow
self.ui = ui
self.connectionHandler = connectionHandler
# register the signal
self.ui.chat_input.returnPressed.connect(self.__onUserPressEnterInChat)
# register the signal
self.ui.ttsToggle.clicked.connect(self.__ttsToggleClicked)
# register the header callback
self.connectionHandler.connect_header_callback(ChatHeader.TEXT, self.__OnNewMessageComing)
self.ttsActive = False
self.isReading = False
self.readList = []
# start text to speech loop
asyncio.run_coroutine_threadsafe(self.__shouldIReadText(), self.connectionHandler.event_loop)
def __onUserPressEnterInChat(self):
# get the message from the UI
message = self.ui.chat_input.text()
if message == "":
return
# clear the chat input box
self.ui.chat_input.clear()
# check if the client is connected
if self.connectionHandler.isConnected == 0:
return
# build the full message
message = f"{self.connectionHandler.client.name} ({self.connectionHandler.client.client_id}): {message}"
# send the message to the server
self.connectionHandler.send_data(message, ChatHeader.TEXT)
def __OnNewMessageComing(self, data: ChatData):
# add the message into the chat box
message = data.data
self.ui.chatbox.append(message)
if (self.ttsActive):
self.readList.append(message)
def __ttsToggleClicked(self):
if not self.ttsActive:
self.ttsActive = True
else:
self.ttsActive = False
self.readList.clear()
async def __shouldIReadText(self):
while True:
# Checks if there is new message to read
if (len(self.readList) != 0 and self.isReading == False):
task = asyncio.create_task(self.__readText())
await task
await asyncio.sleep(0.5)
async def __readText(self):
self.isReading = True
message = self.readList.pop()
# Initialize the converter
converter = pyttsx3.init()
# set text to speed properties
converter.setProperty('rate', 150)
converter.setProperty('volume', 0.7)
converter.say(message)
# Call blocking function with executor
await self.connectionHandler.event_loop.run_in_executor(None, self.__playText, converter)
self.isReading = False
def __playText(self, converter):
converter.runAndWait()