Files
leonpan-pc/app/view/main_window.py
2025-11-03 22:28:58 +08:00

240 lines
9.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding: utf-8
from loguru import logger
from PyQt6.QtCore import QSize
from PyQt6.QtGui import QColor, QIcon
from PyQt6.QtWidgets import QApplication, QWidget
from qfluentwidgets import NavigationAvatarWidget, NavigationItemPosition, SplashScreen
from qfluentwidgets import FluentIcon
from app.core import cfg, qconfig, userConfig, GetUserAvatarThread,lang,signalBus
from app.view.app_info_interface import AppInfoInterface
from app.view.ownFiled_interface import OwnFiledInterface
from app.view.setting_interface import SettingInterface
from app.view.storagespace_interface import StoragespaceInterface
from app.view.task_interface import TaskInterface
from app.view.share_interface import ShareInterface
from app.view.widgets.custom_fluent_window import CustomFluentWindow
from app.view.widgets.preview_box import OptimizedPreviewBox, PreviewTextBox
from app.view.widgets.share_folder_messageBox import ShareFolderMessageBox
class MainWindow(CustomFluentWindow):
def __init__(self):
logger.info("开始初始化主窗口")
super().__init__()
self.initWindow()
self.ownFiledInterface = OwnFiledInterface(self)
self.storagespaceInterface = StoragespaceInterface(self)
self.taskInterface = TaskInterface(self)
self.shareInterface = ShareInterface(self)
self.appInfoInterface = AppInfoInterface(self)
self.connectSignalToSlot()
self.initNavigation()
logger.info("主窗口初始化完成")
def connectSignalToSlot(self):
logger.debug("连接信号和槽")
signalBus.micaEnableChanged.connect(self.setMicaEffectEnabled)
# 预览信号连接
signalBus.imagePreviewSignal.connect(self.imagePreview)
signalBus.txtPreviewSignal.connect(self.txtPreview)
# 背景信号连接
signalBus.backgroundChanged.connect(self.updateBackground)
signalBus.opacityChanged.connect(self.updateBackground)
# 下载上传任务信号连接
signalBus.addUploadFileTask.connect(self.addUploadFileTask)
signalBus.addDownloadFileTask.connect(self.addDownloadFileTask)
signalBus.shareFolderViewSignal.connect(self.shareFolderView)
# 语言变更信号连接
signalBus.languageChanged.connect(self.updateNavigation)
def updateNavigation(self):
# 更新导航项文本
# 使用MSFluentWindow的方法更新导航项文本
self.navigationInterface.setItemText(self.ownFiledInterface.objectName(), lang("我的文件"))
self.navigationInterface.setItemText(
self.storagespaceInterface.objectName(), lang("存储配额")
)
self.navigationInterface.setItemText(self.taskInterface.objectName(), lang("任务管理"))
self.navigationInterface.setItemText(self.shareInterface.objectName(), lang("我的分享"))
self.navigationInterface.setItemText(self.appInfoInterface.objectName(), lang("应用信息"))
def initNavigation(self):
logger.info("开始初始化导航界面")
# MSFluentWindow自带导航设置不需要直接设置navigationInterface
# 注意MSFluentWindow的addSubInterface方法支持selectedIcon参数
self.addSubInterface(
self.ownFiledInterface,
FluentIcon.FOLDER,
lang("我的文件"),
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.storagespaceInterface,
FluentIcon.APPLICATION,
lang("存储配额"),
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.taskInterface,
FluentIcon.PASTE,
lang("任务管理"),
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.shareInterface,
FluentIcon.SHARE,
lang("我的分享"),
position=NavigationItemPosition.TOP,
)
self.addSubInterface(
self.appInfoInterface,
FluentIcon.INFO,
lang("应用信息"),
position=NavigationItemPosition.BOTTOM,
)
# 创建默认头像widget先使用本地默认头像
self.avatarWidget = NavigationAvatarWidget(
userConfig.userName, ":app/images/logo.png"
)
# 使用MSFluentWindow的navigationInterface添加头像widget
# 直接使用原始图标而不是avatarWidget.avatar
self.navigationInterface.addItem(
routeKey="settingInterface",
icon=QIcon(":app/images/logo.png"),
text="", # 不显示文本,只显示头像
onClick=self.setPersonalInfoWidget,
selectable=False,
position=NavigationItemPosition.BOTTOM,
)
self.settingInterface = SettingInterface(self)
self.stackedWidget.addWidget(self.settingInterface)
self.splashScreen.finish()
logger.info("导航界面初始化完成")
self.avatarThread = GetUserAvatarThread("l")
self.avatarThread.avatarPixmap.connect(self.onAvatarDownloaded)
self.avatarThread.start()
def shareFolderView(self, _id):
w = ShareFolderMessageBox(_id, self)
if w.exec():
...
def addUploadFileTask(self, filePath):
logger.info(f"添加上传文件任务: {filePath}")
self.taskInterface.uploadScrollWidget.addUploadTask(filePath)
self.stackedWidget.setCurrentWidget(self.taskInterface)
self.navigationInterface.setCurrentItem("taskInterface")
self.taskInterface._changePivot("Upload")
def addDownloadFileTask(self, suffix, fileName, _id):
logger.info(f"添加下载文件任务: {fileName}")
self.taskInterface.downloadScrollWidget.addDownloadTask(suffix, fileName, _id)
self.stackedWidget.setCurrentWidget(self.taskInterface)
self.navigationInterface.setCurrentItem("taskInterface")
self.taskInterface._changePivot("Download")
def setPersonalInfoWidget(self):
# 使用MSFluentWindow的方式切换到设置界面
self.stackedWidget.setCurrentWidget(self.settingInterface)
# 由于设置项是通过addItem添加的非selectable项不需要设置currentItem
def onAvatarDownloaded(self, pixmap):
userConfig.setUserAvatarPixmap(pixmap)
self.avatarWidget.setAvatar(pixmap)
self.settingInterface.updateAvatar(pixmap)
# 更新导航项中的头像图标
if hasattr(self, 'navigationInterface'):
# 创建临时QIcon
icon = QIcon()
icon.addPixmap(pixmap)
# 使用QFluentWidgets的正确API更新导航项图标
# 由于NavigationBar没有setItemIcon方法我们重新创建导航项
# 先获取现有的设置项并移除
try:
# 移除现有的设置项
self.navigationInterface.removeItem("settingInterface")
# 重新添加设置项,使用新的头像图标
self.navigationInterface.addItem(
routeKey="settingInterface",
icon=icon,
text="", # 不显示文本,只显示头像
onClick=self.setPersonalInfoWidget,
selectable=False,
position=NavigationItemPosition.BOTTOM,
)
except Exception as e:
logger.error(f"更新导航头像失败: {str(e)}")
def initWindow(self):
logger.info("开始初始化窗口设置")
self.resize(960, 780)
self.setMinimumWidth(760)
self.setWindowIcon(QIcon(":app/images/logo.png"))
self.setWindowTitle(lang("LeonPan"))
logger.debug("已设置窗口基本属性")
self.setCustomBackgroundColor(QColor(240, 244, 249), QColor(32, 32, 32))
self.setMicaEffectEnabled(cfg.get(cfg.micaEnabled))
logger.debug("已设置窗口背景和Mica效果")
self.setBackgroundImage(
qconfig.get(cfg.customBackground), qconfig.get(cfg.customOpactity)
) # create splash screen
# 使用自定义的背景设置方法
# create splash screen
self.splashScreen = SplashScreen(self.windowIcon(), self)
self.splashScreen.setIconSize(QSize(106, 106))
self.splashScreen.raise_()
logger.debug("已创建并设置启动屏幕")
desktop = QApplication.primaryScreen().availableGeometry()
w, h = desktop.width(), desktop.height()
self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)
logger.debug("已移动窗口到屏幕中心")
self.show()
QApplication.processEvents()
logger.info("窗口初始化完成并显示")
def resizeEvent(self, e):
super().resizeEvent(e)
if hasattr(self, "splashScreen"):
self.splashScreen.resize(self.size())
# 窗口大小改变时更新背景
def imagePreview(self, _id):
# 直接传递文件ID给OptimizedPreviewBox让它内部使用getFileUrl获取临时URL
self.previewBox = OptimizedPreviewBox(self, fileId=_id)
if self.previewBox.exec():
pass
def txtPreview(self, _id):
# 直接使用fileUri不再构建URL
# _id 应该是 cloudreve:// 格式的URI
self.previewBox = PreviewTextBox(self, "", _id)
if self.previewBox.exec():
pass
def updateBackground(self):
"""更新窗口背景"""
self.setBackgroundImage(
qconfig.get(cfg.customBackground), qconfig.get(cfg.customOpactity)
)