WeChatMsg/app/Ui/contact/contact.py

324 lines
12 KiB
Python
Raw Normal View History

2023-01-23 09:46:25 +08:00
# -*- coding: utf-8 -*-
"""
2023-02-02 00:26:44 +08:00
@File : contact.py
2023-01-23 09:46:25 +08:00
@Author : Shuaikang Zhou
@Time : 2022/12/13 15:07
@IDE : Pycharm
@Version : Python3.10
@comment : ···
"""
from typing import Dict
2023-01-23 09:46:25 +08:00
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import app.Ui.MyComponents.Contact as MyLabel
from .analysis import analysis
2023-01-23 09:46:25 +08:00
from .contactUi import *
from .emotion import emotion
2023-05-20 23:55:21 +08:00
from .report import report
2023-01-23 09:46:25 +08:00
from .userinfo import userinfoUi
from ...DataBase import data, output
EMOTION = 1
ANALYSIS = 2
2023-01-23 09:46:25 +08:00
2023-02-02 00:26:44 +08:00
2023-04-05 01:50:58 +08:00
class StackedWidget():
def __init__(self):
pass
2023-01-23 09:46:25 +08:00
class ContactController(QWidget, Ui_Dialog):
exitSignal = pyqtSignal()
urlSignal = pyqtSignal(QUrl)
# username = ''
def __init__(self, Me, parent=None):
super(ContactController, self).__init__(parent)
self.emotionView = None
self.analysisView = None
2023-01-23 09:46:25 +08:00
self.chatroomFlag = None
self.ta_avatar = None
self.setupUi(self)
self.setWindowTitle('WeChat')
2023-10-29 23:15:46 +08:00
self.setWindowIcon(QIcon('./app/data/icons/logo.svg'))
2023-04-05 00:01:06 +08:00
# self.setStyleSheet('''QWidget{background-color:rgb(255, 255, 255);}''')
2023-01-23 09:46:25 +08:00
self.initui()
self.Me = Me
2023-11-02 23:50:04 +08:00
self.Thread = ChatMsg(self.Me.wxid, None)
self.contacts: Dict[str, MyLabel.ContactUi] = {}
2023-01-23 09:46:25 +08:00
self.last_btn = None
self.chat_flag = True
self.show_flag = False
self.ta_username = None
self.last_pos = 0
self.last_msg_time = 0 # 上次信息的时间
self.last_talkerId = None
self.now_talkerId = None
self.last_analysis = None
self.now_analysis = None
self.view_emotion = {}
self.view_analysis = {}
2023-01-23 09:46:25 +08:00
self.showContact()
self.now_btn = self.userinfo
self.last_btn = None
2023-01-23 09:46:25 +08:00
def initui(self):
2023-04-05 00:01:06 +08:00
# 槽函数连接
self.btn_back.clicked.connect(self.back)
2023-10-26 22:45:44 +08:00
# self.btn_output.clicked.connect(self.output)
self.btn_analysis.clicked.connect(self.analysis)
self.btn_emotion.clicked.connect(self.emotionale_Analysis)
2023-05-20 23:55:21 +08:00
self.btn_report.clicked.connect(self.annual_report)
2023-04-05 00:01:06 +08:00
self.lay0 = QVBoxLayout()
2023-04-05 01:50:58 +08:00
# self.widget.setLayout(self.lay0)
# self.widget.setStyleSheet('''QWidget{background-color:rgb(255, 255, 255);}''')
2023-04-24 11:04:08 +08:00
# self.stackedWidget.setStyleSheet('''QWidget{background-color:rgb(240, 240, 240);}''')
self.stackedWidget.setStyleSheet('''QWidget{background-color:rgb(255, 255, 255);}''')
self.frame = QtWidgets.QFrame()
self.frame.setObjectName("frame")
self.userinfo = userinfoUi.Ui_Frame() # 联系人信息界面
self.userinfo.setupUi(self.frame)
self.userinfo.progressBar.setVisible(False)
2023-04-05 01:50:58 +08:00
self.stackedWidget.addWidget(self.frame)
# self.lay0.addWidget(self.frame)
2023-10-26 22:45:44 +08:00
menu = QMenu(self)
self.toDocxAct = QAction(QIcon('app/data/icons/word.svg'), '导出Docx', self)
self.toCSVAct = QAction(QIcon('app/data/icons/csv.svg'), '导出CSV', self)
self.toHtmlAct = QAction(QIcon('app/data/icons/html.svg'), '导出HTML', self)
self.toolButton_output.setPopupMode(QToolButton.MenuButtonPopup)
menu.addAction(self.toDocxAct)
menu.addAction(self.toCSVAct)
menu.addAction(self.toHtmlAct)
self.toolButton_output.setMenu(menu)
# self.toolButton_output.addSeparator()
self.toHtmlAct.triggered.connect(self.output)
self.toDocxAct.triggered.connect(self.output)
self.toCSVAct.triggered.connect(self.output)
2023-01-23 09:46:25 +08:00
def showContact(self):
"""
2023-02-02 00:26:44 +08:00
显示联系人
2023-01-23 09:46:25 +08:00
:return:
"""
print('show')
if self.show_flag:
return
self.show_flag = True
rconversations = data.get_rconversation()
max_hight = max(len(rconversations) * 80, 680)
2023-04-05 00:01:06 +08:00
# 设置滚动区域的高度
2023-01-23 09:46:25 +08:00
self.scrollAreaWidgetContents.setGeometry(
QtCore.QRect(0, 0, 300, max_hight))
2023-04-05 00:01:06 +08:00
2023-01-23 09:46:25 +08:00
for i in range(len(rconversations)):
rconversation = rconversations[i]
username = rconversation[1]
2023-04-05 00:01:06 +08:00
# 创建联系人按钮对象
# 将实例化对象添加到self.contacts储存起来
# pushButton_2 = Contact(self.scrollAreaWidgetContents, i, rconversation)
pushButton_2 = MyLabel.ContactUi(self.scrollAreaWidgetContents, i, rconversation)
2023-01-23 09:46:25 +08:00
pushButton_2.setGeometry(QtCore.QRect(0, 80 * i, 300, 80))
pushButton_2.setLayoutDirection(QtCore.Qt.LeftToRight)
pushButton_2.clicked.connect(pushButton_2.show_msg)
pushButton_2.usernameSingal.connect(self.Contact)
self.contacts[username] = pushButton_2
def Contact(self, talkerId):
"""
聊天界面 点击联系人头像时候显示聊天数据
:param talkerId:
:return:
"""
self.now_talkerId = talkerId
2023-04-05 01:50:58 +08:00
# self.frame.setVisible(True)
self.setViewVisible(self.now_talkerId)
2023-01-23 09:46:25 +08:00
# 把当前按钮设置为灰色
if self.last_talkerId and self.last_talkerId != talkerId:
print('对方账号:', self.last_talkerId)
self.contacts[self.last_talkerId].setStyleSheet(
2023-04-05 00:01:06 +08:00
"QPushButton {background-color: rgb(220,220,220);}"
"QPushButton:hover{background-color: rgb(208,208,208);}\n"
2023-01-23 09:46:25 +08:00
)
self.last_talkerId = talkerId
self.contacts[talkerId].setStyleSheet(
"QPushButton {background-color: rgb(198,198,198);}"
"QPushButton:hover{background-color: rgb(209,209,209);}\n"
)
2023-04-05 00:01:06 +08:00
# 设置联系人的基本信息
conRemark = self.contacts[talkerId].contact.conRemark
nickname = self.contacts[talkerId].contact.nickname
alias = self.contacts[talkerId].contact.alias
2023-04-05 01:50:58 +08:00
2023-01-23 09:46:25 +08:00
self.label_remark.setText(conRemark)
self.ta_username = talkerId
if '@chatroom' in talkerId:
self.chatroomFlag = True
else:
self.chatroomFlag = False
self.userinfo.l_remark.setText(conRemark)
pixmap = self.contacts[talkerId].contact.avatar
2023-01-23 09:46:25 +08:00
self.userinfo.l_avatar.setPixmap(pixmap)
self.userinfo.l_nickname.setText(f'昵称:{nickname}')
self.userinfo.l_username.setText(f'微信号:{alias}')
self.userinfo.lineEdit.setText(conRemark)
def output(self):
2023-02-02 00:26:44 +08:00
"""
导出聊天记录
:return:
"""
2023-04-05 01:50:58 +08:00
self.setViewVisible(self.now_talkerId)
2023-10-26 22:45:44 +08:00
if self.sender() == self.toDocxAct:
self.outputThread = output.Output(self.Me, self.now_talkerId)
self.outputThread.progressSignal.connect(self.output_progress)
self.outputThread.rangeSignal.connect(self.set_progressBar_range)
self.outputThread.okSignal.connect(self.hide_progress_bar)
self.outputThread.start()
elif self.sender() == self.toCSVAct:
print('开始导出csv')
self.outputThread = output.Output(self.Me, self.now_talkerId, type_=output.Output.CSV)
self.outputThread.progressSignal.connect(self.output_progress)
self.outputThread.rangeSignal.connect(self.set_progressBar_range)
self.outputThread.okSignal.connect(self.hide_progress_bar)
self.outputThread.start()
print('导出csv')
elif self.sender() == self.toHtmlAct:
print('功能暂未实现')
QMessageBox.warning(self,
"别急别急",
"马上就实现该功能"
)
2023-02-02 00:26:44 +08:00
def hide_progress_bar(self, int):
2023-03-06 00:39:40 +08:00
reply = QMessageBox(self)
reply.setIcon(QMessageBox.Information)
reply.setWindowTitle('OK')
2023-10-29 23:15:46 +08:00
reply.setText(f"导出聊天记录成功\n在.\\data\\目录下")
2023-03-06 00:39:40 +08:00
reply.addButton("确认", QMessageBox.AcceptRole)
reply.addButton("取消", QMessageBox.RejectRole)
api = reply.exec_()
2023-02-02 00:26:44 +08:00
self.userinfo.progressBar.setVisible(False)
def set_progressBar_range(self, value):
self.userinfo.progressBar.setVisible(True)
self.userinfo.progressBar.setRange(0, value)
def output_progress(self, value):
self.userinfo.progressBar.setProperty('value', value)
2023-01-31 20:41:49 +08:00
def analysis(self):
2023-02-02 00:26:44 +08:00
"""
聊天分析
:return:
"""
2023-04-05 00:01:06 +08:00
self.frame.setVisible(False) # 将userinfo界面设置为不可见
# 判断talkerId是否已经分析过了
# 是:则清空其他界面,直接显示该界面
# 否:清空其他界面,创建用户界面并显示
2023-05-20 23:55:21 +08:00
if 'room' in self.now_talkerId:
QMessageBox.warning(
self, '警告',
'暂不支持群组'
)
return
if self.now_talkerId in self.view_analysis:
self.setViewVisible(self.now_talkerId, mod=ANALYSIS)
return True
else:
self.setViewVisible(self.now_talkerId)
self.view_analysis[self.now_talkerId] = analysis.AnalysisController(self.now_talkerId)
2023-04-05 01:50:58 +08:00
# self.lay0.addWidget(self.view_analysis[self.now_talkerId])
self.last_analysis = self.now_talkerId
2023-04-05 01:50:58 +08:00
self.stackedWidget.addWidget(self.view_analysis[self.now_talkerId])
self.setViewVisible(self.now_talkerId, mod=ANALYSIS)
def emotionale_Analysis(self):
print('情感分析', data.get_conRemark(self.now_talkerId))
2023-04-05 00:01:06 +08:00
self.frame.setVisible(False) # 将userinfo界面设置为不可见
# 判断talkerId是否已经分析过了
# 是:则清空其他界面,直接显示该界面
# 否:清空其他界面,创建用户界面并显示
if self.now_talkerId in self.view_emotion:
self.setViewVisible(self.now_talkerId, mod=EMOTION)
return True
else:
self.setViewVisible(self.now_talkerId)
self.view_emotion[self.now_talkerId] = emotion.EmotionController(self.ta_username)
2023-04-05 01:50:58 +08:00
# self.lay0.addWidget(self.view_emotion[self.now_talkerId])
self.last_analysis = self.now_talkerId
2023-04-05 01:50:58 +08:00
self.stackedWidget.addWidget(self.view_emotion[self.now_talkerId])
self.setViewVisible(self.now_talkerId, mod=EMOTION)
2023-01-31 20:41:49 +08:00
pass
2023-01-23 09:46:25 +08:00
2023-05-20 23:55:21 +08:00
def annual_report(self):
2023-11-01 23:38:58 +08:00
QMessageBox.warning(
self,
"提示",
"敬请期待"
)
return
2023-05-20 23:55:21 +08:00
self.report = report.ReportController(123)
self.report.show()
def showUserinfo(self):
2023-04-05 01:50:58 +08:00
pass
# self.analysisView = analysis.AnalysisController(self.now_talkerId)
# # self.lay0 = QHBoxLayout()
# # self.widget.setLayout(self.lay0)
# self.lay0.addWidget(self.analysisView)
def setViewVisible(self, wxid: str, mod=None):
"""
设置当前可见窗口
"""
match mod:
case None:
self.stackedWidget.setCurrentWidget(self.frame)
case 1:
if not self.last_analysis:
return False
self.stackedWidget.setCurrentWidget(self.view_emotion[self.now_talkerId])
case 2:
if not self.last_analysis:
return False
self.stackedWidget.setCurrentWidget(self.view_analysis[self.now_talkerId])
def back(self):
2023-04-05 00:01:06 +08:00
"""
将userinfo界面设置为可见其他界面设置为不可见
"""
self.frame.setVisible(True)
self.setViewVisible(self.now_talkerId)
2023-01-23 09:46:25 +08:00
class ChatMsg(QThread):
"""
发送信息线程
"""
isSend_signal = pyqtSignal(tuple)
okSignal = pyqtSignal(int)
def __init__(self, my_u, ta_u, parent=None):
super().__init__(parent)
self.sec = 2 # 默认1000秒
self.my_u = my_u
self.ta_u = ta_u
self.my_avatar = data.get_avator(my_u)
self.msg_id = 0
def run(self):
self.ta_avatar = data.get_avator(self.ta_u)
messages = data.get_message(self.ta_u, self.msg_id)
# messages.reverse()
for message in messages:
self.isSend_signal.emit(message)
self.msg_id += 1
self.okSignal.emit(1)