2023-11-17 21:34:22 +08:00
|
|
|
from PyQt5.QtCore import QThread, pyqtSignal
|
|
|
|
from PyQt5.QtWidgets import QWidget, QMessageBox, QAction, QLineEdit
|
|
|
|
|
|
|
|
from app.DataBase import micro_msg, misc
|
|
|
|
from app.components import ContactQListWidgetItem
|
|
|
|
from app.person import ContactPC
|
2023-11-19 23:27:34 +08:00
|
|
|
from app.util import search
|
2023-11-17 21:34:22 +08:00
|
|
|
from .chatUi import Ui_Form
|
|
|
|
from .chat_info import ChatInfo
|
|
|
|
from ..Icon import Icon
|
|
|
|
|
|
|
|
# 美化样式表
|
|
|
|
Stylesheet = """
|
|
|
|
|
|
|
|
/*去掉item虚线边框*/
|
|
|
|
QListWidget, QListView, QTreeWidget, QTreeView {
|
|
|
|
outline: 0px;
|
|
|
|
border:none;
|
|
|
|
background-color:rgb(240,240,240)
|
|
|
|
}
|
|
|
|
/*设置左侧选项的最小最大宽度,文字颜色和背景颜色*/
|
|
|
|
QListWidget {
|
|
|
|
min-width: 250px;
|
|
|
|
max-width: 250px;
|
|
|
|
min-height: 80px;
|
|
|
|
max-height: 1200px;
|
|
|
|
color: black;
|
|
|
|
border:none;
|
|
|
|
}
|
|
|
|
QListWidget::item{
|
|
|
|
height:60px;
|
|
|
|
width:250px;
|
|
|
|
}
|
|
|
|
/*被选中时的背景颜色和左边框颜色*/
|
|
|
|
QListWidget::item:selected {
|
|
|
|
background: rgb(204, 204, 204);
|
|
|
|
border-bottom: 2px solid rgb(9, 187, 7);
|
|
|
|
border-left:none;
|
|
|
|
color: black;
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
/*鼠标悬停颜色*/
|
|
|
|
HistoryPanel::item:hover {
|
|
|
|
background: rgb(52, 52, 52);
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
class ChatWindow(QWidget, Ui_Form):
|
2023-11-17 23:54:34 +08:00
|
|
|
load_finish_signal = pyqtSignal(bool)
|
|
|
|
|
2023-11-17 21:34:22 +08:00
|
|
|
def __init__(self, parent=None):
|
|
|
|
super().__init__(parent)
|
|
|
|
self.show_thread = None
|
|
|
|
self.setupUi(self)
|
|
|
|
self.ok_flag = False
|
|
|
|
self.setStyleSheet(Stylesheet)
|
2023-11-19 23:27:34 +08:00
|
|
|
self.contacts = [[], []]
|
2023-11-17 21:34:22 +08:00
|
|
|
self.init_ui()
|
|
|
|
self.show_chats()
|
|
|
|
|
|
|
|
def init_ui(self):
|
|
|
|
search_action = QAction(self.lineEdit)
|
|
|
|
search_action.setIcon(Icon.Search_Icon)
|
|
|
|
self.lineEdit.addAction(search_action, QLineEdit.LeadingPosition)
|
2023-11-19 23:27:34 +08:00
|
|
|
self.lineEdit.returnPressed.connect(self.search_contact)
|
2023-11-17 21:34:22 +08:00
|
|
|
self.listWidget.clear()
|
|
|
|
self.listWidget.currentRowChanged.connect(self.setCurrentIndex)
|
|
|
|
self.listWidget.setCurrentRow(0)
|
|
|
|
self.stackedWidget.setCurrentIndex(0)
|
|
|
|
|
|
|
|
def show_chats(self):
|
|
|
|
print('chat0')
|
|
|
|
if self.ok_flag:
|
|
|
|
return
|
|
|
|
micro_msg.init_database()
|
|
|
|
if not micro_msg.is_database_exist():
|
|
|
|
QMessageBox.critical(self, "错误", "数据库不存在\n请先解密数据库")
|
|
|
|
return
|
|
|
|
self.show_thread = ShowContactThread()
|
|
|
|
self.show_thread.showSingal.connect(self.show_chat)
|
2023-11-17 23:54:34 +08:00
|
|
|
self.show_thread.load_finish_signal.connect(self.stop_loading)
|
2023-11-17 21:34:22 +08:00
|
|
|
self.show_thread.start()
|
|
|
|
self.ok_flag = True
|
|
|
|
|
2023-11-19 23:27:34 +08:00
|
|
|
def search_contact(self):
|
|
|
|
content = self.lineEdit.text()
|
|
|
|
if not content:
|
|
|
|
return
|
|
|
|
index = self.search_contact_index(content)
|
|
|
|
self.select_contact_by_index(index)
|
|
|
|
|
|
|
|
def search_contact_index(self, content: str) -> int:
|
|
|
|
return search.search_by_content(content, self.contacts)
|
|
|
|
|
|
|
|
def select_contact_by_index(self, index):
|
|
|
|
self.stackedWidget.setCurrentIndex(index)
|
|
|
|
self.listWidget.setCurrentRow(index)
|
|
|
|
|
2023-11-17 21:34:22 +08:00
|
|
|
def show_chat(self, contact):
|
2023-11-19 23:27:34 +08:00
|
|
|
self.contacts[0].append(contact.remark)
|
|
|
|
self.contacts[1].append(contact.nickName)
|
2023-11-18 11:51:58 +08:00
|
|
|
contact_item = ContactQListWidgetItem(contact.remark, contact.smallHeadImgUrl, contact.smallHeadImgBLOG)
|
2023-11-17 21:34:22 +08:00
|
|
|
self.listWidget.addItem(contact_item)
|
|
|
|
self.listWidget.setItemWidget(contact_item, contact_item.widget)
|
|
|
|
chat_info_window = ChatInfo(contact)
|
|
|
|
self.stackedWidget.addWidget(chat_info_window)
|
|
|
|
|
|
|
|
def setCurrentIndex(self, row):
|
2023-11-18 14:41:40 +08:00
|
|
|
# print(row)
|
2023-11-17 21:34:22 +08:00
|
|
|
self.stackedWidget.setCurrentIndex(row)
|
|
|
|
|
2023-11-17 23:54:34 +08:00
|
|
|
def stop_loading(self, a0):
|
|
|
|
# self.label.setVisible(False)
|
|
|
|
self.load_finish_signal.emit(True)
|
|
|
|
|
2023-11-17 21:34:22 +08:00
|
|
|
|
|
|
|
class ShowContactThread(QThread):
|
|
|
|
showSingal = pyqtSignal(ContactPC)
|
2023-11-17 23:54:34 +08:00
|
|
|
load_finish_signal = pyqtSignal(bool)
|
2023-11-17 21:34:22 +08:00
|
|
|
|
|
|
|
# heightSingal = pyqtSignal(int)
|
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
def run(self) -> None:
|
|
|
|
contact_info_lists = micro_msg.get_contact()
|
|
|
|
for contact_info_list in contact_info_lists:
|
|
|
|
# UserName, Alias,Type,Remark,NickName,PYInitial,RemarkPYInitial,ContactHeadImgUrl.smallHeadImgUrl,ContactHeadImgUrl,bigHeadImgUrl
|
|
|
|
contact_info = {
|
|
|
|
'UserName': contact_info_list[0],
|
|
|
|
'Alias': contact_info_list[1],
|
|
|
|
'Type': contact_info_list[2],
|
|
|
|
'Remark': contact_info_list[3],
|
|
|
|
'NickName': contact_info_list[4],
|
|
|
|
'smallHeadImgUrl': contact_info_list[7]
|
|
|
|
}
|
|
|
|
contact = ContactPC(contact_info)
|
|
|
|
contact.smallHeadImgBLOG = misc.get_avatar_buffer(contact.wxid)
|
|
|
|
contact.set_avatar(contact.smallHeadImgBLOG)
|
|
|
|
self.showSingal.emit(contact)
|
|
|
|
# pprint(contact.__dict__)
|
2023-11-17 23:54:34 +08:00
|
|
|
self.load_finish_signal.emit(True)
|