pyqt-todolist/view/UserLabel.py

115 lines
4 KiB
Python
Raw Permalink Normal View History

2022-04-30 10:00:43 +08:00
import os
2022-04-26 21:10:49 +08:00
from PyQt5.QtCore import Qt, pyqtSignal
2022-04-30 10:00:43 +08:00
from PyQt5.QtGui import QPixmap, QIcon
from PyQt5.QtWidgets import QLabel, QHBoxLayout, QVBoxLayout, QMenu, QWidget
from utils.BasicUtils import read_init_file, return_work_dir
2022-04-26 22:41:33 +08:00
from utils.CreateConfigure import CreateConfigure
from utils.Sync import Sync
from view.LoginWidget import LoginWidget
2022-04-26 21:10:49 +08:00
from view.SettingWidget import SettingWidget
from view.Tip_UI import TipUi
2022-04-01 22:25:19 +08:00
class User(QLabel):
2022-04-26 21:10:49 +08:00
update_signal = pyqtSignal()
2022-04-26 22:41:33 +08:00
sync_signal = pyqtSignal()
2022-04-26 21:10:49 +08:00
2022-04-01 22:25:19 +08:00
def __init__(self):
super(User, self).__init__()
self.widget = QWidget(self)
self.widget.setMaximumWidth(300)
self.setObjectName('User')
2022-04-07 19:34:28 +08:00
2022-04-01 22:25:19 +08:00
layout = QHBoxLayout()
info_layout = QVBoxLayout()
self.pic_label = QLabel('')
self.pic_label.setObjectName('pic_label')
2022-04-01 22:25:19 +08:00
self.pic_label.setPixmap(
QPixmap(os.path.abspath('../') + '/images/user.svg').scaled(50, 50, Qt.IgnoreAspectRatio,
Qt.SmoothTransformation))
layout.addWidget(self.pic_label, 2)
2022-04-17 17:25:35 +08:00
self.user_name = QLabel("本地账号")
self.user_name.setObjectName('user_name')
2022-04-17 17:25:35 +08:00
self.user_mail = QLabel('未同步')
self.user_mail.setStyleSheet('font-size:14px')
2022-04-01 22:25:19 +08:00
2022-04-26 22:41:33 +08:00
self.widget.setLayout(layout)
# 设置右键菜单
self.context_menu = QMenu(self)
2022-04-01 22:25:19 +08:00
info_layout.addWidget(self.user_name)
info_layout.addWidget(self.user_mail)
layout.addLayout(info_layout, 4)
layout.addStretch(1)
2022-04-26 22:41:33 +08:00
self.update_signal.connect(self.iniUI)
self.iniUI()
2022-04-01 22:25:19 +08:00
2022-04-26 22:41:33 +08:00
def iniUI(self):
self.context_menu.clear()
# 读取文件内容
account = read_init_file()
if account and account[0] != 'None':
2022-04-26 22:41:33 +08:00
self.user_name.setText(account[1])
self.user_mail.setText(account[3])
else:
self.user_name.setText("本地账号")
self.user_mail.setText('未同步')
# self.user_mail.setObjectName('user_mail')
2022-04-30 10:00:43 +08:00
# 设置右键菜单
self.context_menu = QMenu(self)
# 背景透明
self.context_menu.setAttribute(Qt.WA_TranslucentBackground)
# 无边框、去掉自带阴影
self.context_menu.setWindowFlags(
self.context_menu.windowFlags() | Qt.FramelessWindowHint | Qt.NoDropShadowWindowHint)
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/setting.svg'), '设置', self.setting)
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/sync.svg'), '同步', self.sync)
if account and account[0] != 'None':
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/exit.svg'), '登出', self.exit_account)
else:
self.context_menu.addAction(QIcon(os.getcwd() + '/../images/login.svg'), '登录', self.login_account)
2022-04-07 19:34:28 +08:00
2022-04-26 22:41:33 +08:00
def contextMenuEvent(self, event):
self.context_menu.exec_(event.globalPos())
def setting(self):
2022-04-26 21:10:49 +08:00
self.setting_widget = SettingWidget()
self.setting_widget.show()
def sync(self):
account = read_init_file()
if account and account[0] != 'None':
2022-04-28 17:07:42 +08:00
self.sync_action = Sync()
TipUi.show_tip('同步中。。。')
self.sync_action.sync_signal.connect(self.sync_status)
self.sync_action.start()
else:
self.login_account()
pass
2022-04-01 22:25:19 +08:00
2022-04-26 22:41:33 +08:00
def sync_status(self):
TipUi.show_tip('同步成功!')
2022-04-28 17:07:42 +08:00
self.sync_signal.emit()
2022-04-26 22:41:33 +08:00
print('Success!')
def exit_account(self):
2022-04-26 22:41:33 +08:00
config_path = return_work_dir() + 'PyQtToDoList.ini'
try:
if os.path.exists(config_path):
os.remove(config_path)
CreateConfigure()
except Exception as e:
print(e)
self.update_signal.emit()
def login_account(self):
self.login_window = LoginWidget()
self.login_window.login_signal.connect(self.iniUI)
self.login_window.show()