Files
leonpan-pc/app/view/widgets/ownfile_scroll_widget.py
2025-10-29 22:20:21 +08:00

92 lines
2.6 KiB
Python

# coding: utf-8
from loguru import logger
from PyQt6.QtCore import Qt
from PyQt6.QtWidgets import QVBoxLayout, QWidget
from qfluentwidgets import (
BreadcrumbBar,
setFont,
)
from app.view.components.linkage_switching import OwnFileLinkageSwitching
class OwnFileScrollWidget(QWidget):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.currentPath = "/"
self.breadcrumbBar = BreadcrumbBar(self)
self.ownFileLinkageSwitching = OwnFileLinkageSwitching("/", self)
self.__initWidget()
def __initWidget(self):
self.setObjectName("OwnFileScrollWidget")
self.breadcrumbBar.addItem("/", "/")
setFont(self.breadcrumbBar, 18)
self.breadcrumbBar.currentItemChanged.connect(self.clickChangeDir)
self.breadcrumbBar.setSpacing(15)
self.__initLayout()
def __initLayout(self):
self.vBoxLayout = QVBoxLayout(self)
self.vBoxLayout.setContentsMargins(0, 0, 0, 0)
self.vBoxLayout.setSpacing(5)
self.vBoxLayout.addWidget(self.breadcrumbBar, 0, Qt.AlignmentFlag.AlignTop)
self.vBoxLayout.addWidget(self.ownFileLinkageSwitching)
def loadDict(self, paths):
self.ownFileLinkageSwitching.loadDict(paths)
def onChangeDir(self, path):
"""处理目录变更"""
logger.info(f"变更目录: {path}")
if path == "":
self.currentPath = "/"
else:
self.currentPath = path
self.ownFileLinkageSwitching.loadDict(path)
# 更新面包屑导航
displayName = path.split("/")[-1] if path != "/" else "/"
self.breadcrumbBar.addItem(displayName, displayName)
def clickChangeDir(self, name):
"""处理面包屑导航项点击事件"""
logger.info(f"面包屑导航项点击: {name}")
# 获取点击的路径
if name == "":
name = "/"
pathList = []
for item in self.breadcrumbBar.items:
if item.text == name:
pathList.append(item.text)
break
else:
pathList.append(item.text)
for i in pathList:
if i == "":
pathList.remove(i)
path = "/".join(pathList) if pathList else "/"
path = path[1:]
if path == "":
path = "/"
if path == self.currentPath:
logger.debug("路径未变化,跳过导航")
return
self.onChangeDir(path)
def refreshCurrentDirectory(self):
"""刷新当前目录的文件卡片"""
logger.info(f"刷新当前目录: {self.currentPath}")
self.loadDict(self.currentPath)