WeChatMsg/app/ui_pc/mainview.py

243 lines
8.1 KiB
Python
Raw Normal View History

2023-11-15 20:54:27 +08:00
# -*- coding: utf-8 -*-
"""
@File : mainview.py
@Author : Shuaikang Zhou
@Time : 2022/12/13 15:07
@IDE : Pycharm
@Version : Python3.10
@comment : 主窗口
"""
2023-11-17 23:54:34 +08:00
import json
2023-11-19 14:01:34 +08:00
import os.path
2023-11-15 20:54:27 +08:00
from PyQt5.QtCore import *
2023-11-23 22:58:27 +08:00
from PyQt5.QtGui import QPixmap, QFont, QDesktopServices
2023-11-15 20:54:27 +08:00
from PyQt5.QtWidgets import *
from app import config
2023-11-17 23:02:40 +08:00
from app.DataBase import msg, misc
2023-11-23 22:58:27 +08:00
from app.ui_pc.Icon import Icon
2023-11-15 20:54:27 +08:00
from . import mainwindow
from .chat import ChatWindow
2023-11-15 21:57:29 +08:00
from .contact import ContactWindow
2023-11-22 22:31:34 +08:00
from .tool.tool_window import ToolWindow
2023-11-27 21:23:26 +08:00
from ..DataBase.output_pc import Output
2023-11-23 22:58:27 +08:00
from ..person_pc import MePC
2023-11-15 20:54:27 +08:00
# 美化样式表
Stylesheet = """
QPushButton {
background-color: rgb(240,240,240);
border:none;
}
QPushButton:hover{
background-color: rgb(209,209,209);
}
/*去掉item虚线边框*/
QListWidget, QListView, QTreeWidget, QTreeView {
outline: 0px;
}
/*设置左侧选项的最小最大宽度,文字颜色和背景颜色*/
QListWidget {
min-width: 120px;
max-width: 120px;
color: black;
background: white;
border:none;
}
QListWidget::item{
height:60;
}
/*被选中时的背景颜色和左边框颜色*/
QListWidget::item:selected {
background: rgb(204, 204, 204);
border-left: 4px solid rgb(9, 187, 7);
color: black;
font-weight: bold;
}
/*鼠标悬停颜色*/
HistoryPanel::item:hover {
background: rgb(52, 52, 52);
}
"""
class MainWinController(QMainWindow, mainwindow.Ui_MainWindow):
2023-11-22 21:12:14 +08:00
exitSignal = pyqtSignal(bool)
okSignal = pyqtSignal(bool)
2023-11-15 20:54:27 +08:00
# username = ''
def __init__(self, username, parent=None):
super(MainWinController, self).__init__(parent)
2023-11-27 21:23:26 +08:00
self.outputThread0 = None
self.outputThread = None
2023-11-15 20:54:27 +08:00
self.setupUi(self)
self.setWindowIcon(Icon.MainWindow_Icon)
self.setStyleSheet(Stylesheet)
self.listWidget.clear()
self.resize(QSize(800, 600))
2023-11-15 20:54:27 +08:00
self.action_desc.triggered.connect(self.about)
2023-11-29 21:39:38 +08:00
self.load_flag = False
2023-11-17 23:54:34 +08:00
self.load_data()
self.load_num = 0
2023-11-22 21:12:14 +08:00
self.label = QLabel(self)
self.label.setGeometry((self.width() - 300) // 2, (self.height() - 100) // 2, 300, 100)
2023-11-23 22:58:27 +08:00
self.label.setPixmap(QPixmap(':/icons/icons/loading.svg'))
2023-11-17 23:54:34 +08:00
2023-11-25 00:20:35 +08:00
def load_data(self, flag=True):
2023-11-19 14:01:34 +08:00
if os.path.exists('./app/data/info.json'):
with open('./app/data/info.json', 'r', encoding='utf-8') as f:
dic = json.loads(f.read())
wxid = dic.get('wxid')
if wxid:
me = MePC()
2023-11-20 22:30:31 +08:00
me.wxid = dic.get('wxid')
me.name = dic.get('name')
me.mobile = dic.get('mobile')
me.wx_dir = dic.get('wx_dir')
2023-11-23 22:58:27 +08:00
print('wx_dir', me.wx_dir)
2023-11-19 14:01:34 +08:00
self.set_my_info(wxid)
2023-11-29 21:39:38 +08:00
self.load_flag = True
2023-11-19 14:01:34 +08:00
else:
QMessageBox.information(
self,
'温馨提示',
'点击 工具->获取信息 重启后可以显示本人头像哦'
)
2023-11-15 20:54:27 +08:00
def init_ui(self):
2023-11-27 21:23:26 +08:00
self.menu_output.setIcon(Icon.Output)
self.action_output_CSV.setIcon(Icon.ToCSV)
self.action_output_CSV.triggered.connect(self.output)
2023-11-23 22:58:27 +08:00
self.action_help_contact.triggered.connect(
2023-11-28 22:15:57 +08:00
lambda: QDesktopServices.openUrl(QUrl("https://blog.lc044.love/post/5")))
2023-11-23 22:58:27 +08:00
self.action_help_chat.triggered.connect(
2023-11-28 22:15:57 +08:00
lambda: QDesktopServices.openUrl(QUrl("https://blog.lc044.love/post/5")))
2023-11-23 22:58:27 +08:00
self.action_help_decrypt.triggered.connect(
2023-11-27 21:23:26 +08:00
lambda: QDesktopServices.openUrl(QUrl("https://blog.lc044.love/post/4")))
2023-11-22 21:12:14 +08:00
self.listWidget.setVisible(False)
self.stackedWidget.setVisible(False)
2023-11-15 20:54:27 +08:00
self.listWidget.currentRowChanged.connect(self.setCurrentIndex)
2023-11-17 23:54:34 +08:00
tool_item = QListWidgetItem(Icon.MyInfo_Icon, '工具', self.listWidget)
2023-11-15 20:54:27 +08:00
chat_item = QListWidgetItem(Icon.Chat_Icon, '聊天', self.listWidget)
contact_item = QListWidgetItem(Icon.Contact_Icon, '好友', self.listWidget)
myinfo_item = QListWidgetItem(Icon.MyInfo_Icon, '我的', self.listWidget)
2023-11-15 21:57:29 +08:00
tool_window = ToolWindow()
2023-11-17 23:02:40 +08:00
tool_window.get_info_signal.connect(self.set_my_info)
2023-11-25 00:20:35 +08:00
tool_window.decrypt_success_signal.connect(self.load_data)
2023-11-17 23:54:34 +08:00
tool_window.load_finish_signal.connect(self.loading)
self.stackedWidget.addWidget(tool_window)
2023-11-22 21:12:14 +08:00
self.chat_window = ChatWindow()
2023-11-19 14:01:34 +08:00
# chat_window = QWidget()
2023-11-22 21:12:14 +08:00
self.stackedWidget.addWidget(self.chat_window)
self.contact_window = ContactWindow()
self.stackedWidget.addWidget(self.contact_window)
2023-11-22 22:31:34 +08:00
label = QLabel('该功能暂不支持哦')
label.setFont(QFont("微软雅黑", 50))
2023-11-15 20:54:27 +08:00
label.setAlignment(Qt.AlignCenter)
self.stackedWidget.addWidget(label)
2023-11-17 23:54:34 +08:00
tool_window.load_finish_signal.connect(self.loading)
2023-11-22 21:12:14 +08:00
self.contact_window.load_finish_signal.connect(self.loading)
self.chat_window.load_finish_signal.connect(self.loading)
2023-11-15 20:54:27 +08:00
def setCurrentIndex(self, row):
self.stackedWidget.setCurrentIndex(row)
2023-11-17 23:54:34 +08:00
if row == 2:
self.stackedWidget.currentWidget().show_contacts()
2023-11-19 14:01:34 +08:00
if row == 1:
self.stackedWidget.currentWidget().show_chats()
2023-11-17 23:54:34 +08:00
def setWindow(self, window):
try:
window.load_finish_signal.connect(self.loading)
except:
pass
self.stackedWidget.addWidget(window)
2023-11-15 20:54:27 +08:00
2023-11-17 23:02:40 +08:00
def set_my_info(self, wxid):
self.avatar = QPixmap()
2023-11-19 14:01:34 +08:00
try:
img_bytes = misc.get_avatar_buffer(wxid)
except AttributeError:
return
2023-11-17 23:02:40 +08:00
if img_bytes[:4] == b'\x89PNG':
self.avatar.loadFromData(img_bytes, format='PNG')
else:
self.avatar.loadFromData(img_bytes, format='jfif')
self.avatar.scaled(60, 60)
me = MePC()
me.set_avatar(img_bytes)
self.myavatar.setScaledContents(True)
self.myavatar.setPixmap(self.avatar)
2023-11-17 23:54:34 +08:00
def stop_loading(self, a0):
self.label.setVisible(False)
def loading(self, a0):
self.load_num += 1
2023-11-22 21:12:14 +08:00
# self.label.setVisible(False)
print('加载一个了')
2023-11-17 23:54:34 +08:00
if self.load_num == 2:
2023-11-22 21:12:14 +08:00
print('ok了')
self.label.clear()
self.label.hide()
self.okSignal.emit(True)
self.listWidget.setVisible(True)
self.stackedWidget.setVisible(True)
2023-11-29 21:39:38 +08:00
if self.load_flag:
self.listWidget.setCurrentRow(1)
self.stackedWidget.setCurrentIndex(1)
else:
self.listWidget.setCurrentRow(0)
self.stackedWidget.setCurrentIndex(0)
2023-11-17 23:54:34 +08:00
2023-11-27 21:23:26 +08:00
def output(self):
if self.sender() == self.action_output_CSV:
self.outputThread = Output(None, type_=Output.CSV_ALL)
self.outputThread.okSignal.connect(
lambda x: self.message('聊天记录导出成功\n./data/聊天记录/messages.csv'))
self.outputThread.start()
def message(self, msg):
QMessageBox.about(self, "提醒", msg)
2023-11-15 20:54:27 +08:00
def about(self):
"""
关于
"""
QMessageBox.about(self, "关于",
f"版本:{config.version}\n"
f"QQ交流群:{config.contact}\n"
2023-11-18 14:41:40 +08:00
"地址https://github.com/LC044/WeChatMsg\n"
f"新特性:\n{''.join(['*' + i for i in config.description])}"
2023-11-15 20:54:27 +08:00
)
2023-11-16 23:16:38 +08:00
def close(self) -> bool:
del self.listWidget
del self.stackedWidget
msg.close()
self.contact_window.close()
2023-11-17 23:54:34 +08:00
class LoadWindowThread(QThread):
windowSignal = pyqtSignal(QWidget)
okSignal = pyqtSignal(bool)
2023-11-22 21:12:14 +08:00
def __init__(self):
2023-11-17 23:54:34 +08:00
super().__init__()
2023-11-22 21:12:14 +08:00
self.num = 0
def loading(self):
self.num += 1
print('加载一个了')
if self.num == 2:
self.okSignal.emit(True)
2023-11-17 23:54:34 +08:00
def run(self):
2023-11-22 21:12:14 +08:00
self.chat_window = ChatWindow()
self.contact_window = ContactWindow()
self.contact_window.load_finish_signal.connect(self.loading)
self.chat_window.load_finish_signal.connect(self.loading)
print('加载完成')
2023-11-17 23:54:34 +08:00
self.okSignal.emit(True)