WeChatMsg/app/ui_pc/mainview.py

204 lines
6.6 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-15 20:54:27 +08:00
from random import randint
from PyQt5.QtCore import *
2023-11-17 23:02:40 +08:00
from PyQt5.QtGui import QPixmap
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-15 20:54:27 +08:00
from app.Ui.Icon import Icon
from . import mainwindow
from .chat import ChatWindow
2023-11-15 21:57:29 +08:00
from .contact import ContactWindow
2023-11-15 20:54:27 +08:00
from .tool import ToolWindow
2023-11-17 23:02:40 +08:00
from ..person 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):
exitSignal = pyqtSignal()
# username = ''
def __init__(self, username, parent=None):
super(MainWinController, self).__init__(parent)
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.stackedWidget = QtWidgets.QStackedWidget(self.centralwidget)
self.action_desc.triggered.connect(self.about)
2023-11-17 23:54:34 +08:00
self.load_data()
2023-11-15 20:54:27 +08:00
self.init_ui()
2023-11-17 23:54:34 +08:00
self.load_num = 0
def load_data(self):
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()
self.set_my_info(wxid)
2023-11-15 20:54:27 +08:00
def init_ui(self):
2023-11-17 23:54:34 +08:00
# self.movie = QMovie("./app/data/loading.gif")
self.label = QLabel(self)
self.label.setGeometry(0, 0, self.width(), self.height())
# self.label.setMovie(self.movie)
# self.movie.start()
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-17 23:54:34 +08:00
tool_window.load_finish_signal.connect(self.loading)
self.stackedWidget.addWidget(tool_window)
self.listWidget.setCurrentRow(0)
self.stackedWidget.setCurrentIndex(0)
chat_window = ChatWindow()
self.stackedWidget.addWidget(chat_window)
contact_window = ContactWindow()
self.stackedWidget.addWidget(contact_window)
label = QLabel('我是页面')
2023-11-15 20:54:27 +08:00
label.setAlignment(Qt.AlignCenter)
# 设置label的背景颜色(这里随机)
# 这里加了一个margin边距(方便区分QStackedWidget和QLabel的颜色)
label.setStyleSheet('background: rgb(%d, %d, %d);margin: 50px;' % (
randint(0, 255), randint(0, 255), randint(0, 255)))
self.stackedWidget.addWidget(label)
2023-11-17 23:54:34 +08:00
tool_window.load_finish_signal.connect(self.loading)
contact_window.load_finish_signal.connect(self.loading)
chat_window.load_finish_signal.connect(self.loading)
# self.load_window_thread = LoadWindowThread(self.stackedWidget)
# self.load_window_thread.okSignal.connect(self.stop_loading)
# self.load_window_thread.start()
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()
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()
img_bytes = misc.get_avatar_buffer(wxid)
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)
2023-11-17 23:54:34 +08:00
dic = {
'wxid': wxid
}
with open('./app/data/info.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(dic))
2023-11-17 23:02:40 +08:00
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-18 14:41:40 +08:00
# print('加载一个了')
2023-11-17 23:54:34 +08:00
if self.load_num == 2:
2023-11-18 14:41:40 +08:00
# print('ok了')
2023-11-17 23:54:34 +08:00
self.label.setVisible(False)
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)
def __init__(self, stackedWidget):
super().__init__()
self.stackedWidget = stackedWidget
def run(self):
chat_window = ChatWindow()
self.stackedWidget.addWidget(chat_window)
contact_window = ContactWindow()
self.stackedWidget.addWidget(contact_window)
label = QLabel('我是页面')
label.setAlignment(Qt.AlignCenter)
# 设置label的背景颜色(这里随机)
# 这里加了一个margin边距(方便区分QStackedWidget和QLabel的颜色)
label.setStyleSheet('background: rgb(%d, %d, %d);margin: 50px;' % (
randint(0, 255), randint(0, 255), randint(0, 255)))
self.stackedWidget.addWidget(label)
self.okSignal.emit(True)