WeChatMsg/app/ui_pc/chat/chat_info.py

155 lines
5.1 KiB
Python
Raw Normal View History

2023-11-20 23:08:10 +08:00
import traceback
2023-11-18 13:25:56 +08:00
from PyQt5.QtCore import QThread, pyqtSignal
2023-11-18 11:51:58 +08:00
from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QHBoxLayout
2023-11-20 23:08:10 +08:00
from app.DataBase import msg, hard_link
2023-11-18 14:41:40 +08:00
from app.components.bubble_message import BubbleMessage, ChatWidget, Notice
2023-11-25 00:20:35 +08:00
from app.person_pc import MePC
2023-11-20 23:08:10 +08:00
from app.util import get_abs_path
2023-11-24 23:49:37 +08:00
from app.util.emoji import get_emoji
class ChatInfo(QWidget):
def __init__(self, contact, parent=None):
super().__init__(parent)
2023-11-18 14:41:40 +08:00
self.last_timestamp = 0
2023-11-18 20:13:56 +08:00
self.last_str_time = ''
2023-11-18 13:25:56 +08:00
self.last_pos = 0
self.contact = contact
self.init_ui()
self.show_chats()
def init_ui(self):
self.label_reamrk = QLabel(self.contact.remark)
self.hBoxLayout = QHBoxLayout()
self.hBoxLayout.addWidget(self.label_reamrk)
self.vBoxLayout = QVBoxLayout()
self.vBoxLayout.setSpacing(0)
2023-11-18 11:51:58 +08:00
self.vBoxLayout.addLayout(self.hBoxLayout)
self.chat_window = ChatWidget()
2023-11-18 13:25:56 +08:00
self.chat_window.scrollArea.verticalScrollBar().valueChanged.connect(self.verticalScrollBar)
2023-11-18 11:51:58 +08:00
self.vBoxLayout.addWidget(self.chat_window)
2023-11-18 14:41:40 +08:00
self.setLayout(self.vBoxLayout)
def show_chats(self):
self.show_chat_thread = ShowChatThread(self.contact)
2023-11-18 14:41:40 +08:00
self.show_chat_thread.showSingal.connect(self.add_message)
self.show_chat_thread.finishSingal.connect(self.show_finish)
self.show_chat_thread.start()
def show_finish(self, ok):
2023-11-18 13:25:56 +08:00
self.setScrollBarPos()
def verticalScrollBar(self, pos):
"""
滚动条到0之后自动更新聊天记录
:param pos:
:return:
"""
# print(pos)
if pos > 0:
return
2023-11-18 14:41:40 +08:00
# 记录当前滚动条最大值
2023-11-18 13:25:56 +08:00
self.last_pos = self.chat_window.verticalScrollBar().maximum()
2023-11-18 14:41:40 +08:00
self.update_history_messages()
def update_history_messages(self):
2023-11-18 13:25:56 +08:00
self.show_chat_thread.start()
def setScrollBarPos(self):
"""
将滚动条位置设置为上次看到的地方
:param pos:
:return:
"""
2023-11-18 14:41:40 +08:00
self.chat_window.update()
self.chat_window.show()
2023-11-18 13:25:56 +08:00
pos = self.chat_window.verticalScrollBar().maximum() - self.last_pos
self.chat_window.set_scroll_bar_value(pos)
2023-11-18 14:41:40 +08:00
def is_5_min(self, timestamp):
if abs(timestamp - self.last_timestamp) > 300:
self.last_timestamp = timestamp
return True
return False
def add_message(self, message):
try:
type_ = message[2]
2023-11-18 14:41:40 +08:00
str_content = message[7]
str_time = message[8]
# print(type_, type(type_))
2023-11-17 23:02:40 +08:00
is_send = message[4]
avatar = MePC().avatar if is_send else self.contact.avatar
2023-11-18 14:41:40 +08:00
timestamp = message[5]
2023-11-20 23:08:10 +08:00
if type_ == 1:
2023-11-18 14:41:40 +08:00
if self.is_5_min(timestamp):
2023-11-18 20:13:56 +08:00
time_message = Notice(self.last_str_time)
self.last_str_time = str_time
2023-11-18 14:41:40 +08:00
self.chat_window.add_message_item(time_message, 0)
bubble_message = BubbleMessage(
2023-11-18 14:41:40 +08:00
str_content,
2023-11-17 23:02:40 +08:00
avatar,
type_,
is_send
)
2023-11-18 13:25:56 +08:00
self.chat_window.add_message_item(bubble_message, 0)
2023-11-20 23:08:10 +08:00
elif type_ == 3:
2023-11-22 00:22:50 +08:00
# return
2023-11-20 23:08:10 +08:00
if self.is_5_min(timestamp):
time_message = Notice(self.last_str_time)
self.last_str_time = str_time
self.chat_window.add_message_item(time_message, 0)
image_path = hard_link.get_image(content=str_content, thumb=False)
image_path = get_abs_path(image_path)
bubble_message = BubbleMessage(
image_path,
avatar,
type_,
is_send
)
self.chat_window.add_message_item(bubble_message, 0)
2023-11-24 23:49:37 +08:00
elif type_ == 47:
# return
if self.is_5_min(timestamp):
time_message = Notice(self.last_str_time)
self.last_str_time = str_time
self.chat_window.add_message_item(time_message, 0)
image_path = get_emoji(str_content, thumb=True)
bubble_message = BubbleMessage(
image_path,
avatar,
3,
is_send
)
self.chat_window.add_message_item(bubble_message, 0)
except:
print(message)
2023-11-20 23:08:10 +08:00
traceback.print_exc()
class ShowChatThread(QThread):
showSingal = pyqtSignal(tuple)
finishSingal = pyqtSignal(int)
2023-11-18 13:25:56 +08:00
msg_id = 0
# heightSingal = pyqtSignal(int)
def __init__(self, contact):
super().__init__()
2023-11-18 13:25:56 +08:00
self.last_message_id = 9999999
self.wxid = contact.wxid
def run(self) -> None:
2023-11-18 13:25:56 +08:00
messages = msg.get_message_by_num(self.wxid, self.last_message_id)
if messages:
self.last_message_id = messages[-1][0]
for message in messages:
self.showSingal.emit(message)
2023-11-18 13:25:56 +08:00
self.msg_id += 1
self.finishSingal.emit(1)