The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
refactor(bittorrent_pack): 简化 BitTorrent 文件选择对话框实现
XiaoYouChReb3e22bc4ddc
diff --git a/app/view/components/dialogs.py b/app/view/components/dialogs.pyindex fe40bee1..f5b473c0 100644--- a/app/view/components/dialogs.py+++ b/app/view/components/dialogs.py@@ -1,13 +1,41 @@import hashlib-from pathlib import Path--from PySide6.QtCore import Qt, QThread, Signal-from PySide6.QtWidgets import QButtonGroup, QFileDialog, QHBoxLayout, QVBoxLayout, QWidget+from pathlib import Path, PurePosixPath++from PySide6.QtCore import QFileInfo, QSignalBlocker, Qt, QThread, Signal+from PySide6.QtGui import QStandardItem, QStandardItemModel+from PySide6.QtWidgets import (+ QAbstractItemView,+ QButtonGroup,+ QFileDialog,+ QFileIconProvider,+ QHeaderView,+ QHBoxLayout,+ QVBoxLayout,+ QWidget,+)from qfluentwidgets import (- MessageBoxBase, SubtitleLabel, BodyLabel, CheckBox, RadioButton, LineEdit, ToolButton, FluentIcon, ToolTipFilter,- ComboBox, ProgressBar+ Action,+ BodyLabel,+ CheckBox,+ ComboBox,+ DropDownPushButton,+ FluentIcon,+ InfoBar,+ LineEdit,+ MessageBoxBase,+ ProgressBar,+ PrimaryPushButton,+ PushButton,+ RadioButton,+ RoundMenu,+ SubtitleLabel,+ ToolButton,+ ToolTipFilter,)+from app.supports.utils import getReadableSize+from app.view.components.tree_view import AutoSizingTreeView+class DeleteTaskDialog(MessageBoxBase):@@ -267,3 +295,288 @@ class FileHashDialog(MessageBoxBase):self.yesButton.setEnabled(True)self.cancelButton.setEnabled(True)self.yesButton.setText(self.tr("重新校验"))+++class FileSelectDialog(MessageBoxBase):+ _FILE_TYPE_RULES = (+ ("video", "视频", FluentIcon.VIDEO, {+ ".avi", ".flv", ".m2ts", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg", ".rmvb", ".ts", ".webm", ".wmv",+ }),+ ("audio", "音频", FluentIcon.MUSIC, {+ ".aac", ".ape", ".flac", ".m4a", ".mp3", ".ogg", ".opus", ".wav", ".wma",+ }),+ ("image", "图片", FluentIcon.PHOTO, {+ ".avif", ".bmp", ".gif", ".jpeg", ".jpg", ".png", ".svg", ".tif", ".tiff", ".webp",+ }),+ ("subtitle", "字幕", FluentIcon.CHAT, {+ ".ass", ".idx", ".lrc", ".psb", ".smi", ".srt", ".ssa", ".sub", ".sup", ".vtt",+ }),+ ("document", "文档", FluentIcon.DOCUMENT, {+ ".chm", ".csv", ".doc", ".docx", ".epub", ".md", ".nfo", ".odt", ".pdf", ".ppt", ".pptx", ".rtf",+ ".txt", ".xls", ".xlsx",+ }),+ ("archive", "压缩包", FluentIcon.ZIP_FOLDER, {+ ".001", ".7z", ".bz2", ".cab", ".gz", ".iso", ".rar", ".tar", ".tbz2", ".tgz", ".xz", ".zip", ".zst",+ ".tar.bz2", ".tar.gz", ".tar.xz", ".tar.zst",+ }),+ ("application", "程序", FluentIcon.APPLICATION, {+ ".apk", ".appimage", ".bat", ".com", ".deb", ".dmg", ".exe", ".iso", ".jar", ".msi", ".pkg", ".rpm", ".sh",+ }),+ )+ _FILE_TYPE_META: dict[str, tuple[str, "FluentIcon"]] = {+ key: (label, icon)+ for key, label, icon, _ in _FILE_TYPE_RULES+ }+ _FILE_TYPE_SUFFIXES: dict[str, set[str]] = {+ key: suffixes+ for key, _, _, suffixes in _FILE_TYPE_RULES+ }++ @classmethod+ def _fileSuffix(cls, path: str) -> str:+ suffixes = [s.lower() for s in PurePosixPath(path).suffixes]+ if not suffixes:+ return ""+ if len(suffixes) > 1:+ combined = "".join(suffixes[-2:])+ if combined in cls._FILE_TYPE_SUFFIXES["archive"]:+ return combined+ return suffixes[-1]++ @classmethod+ def _fileType(cls, path: str) -> str:+ suffix = cls._fileSuffix(path)+ for key, _, _, suffixes in cls._FILE_TYPE_RULES:+ if suffix in suffixes:+ return key+ return "other"++ def __init__(self, task, parent=None):+ super().__init__(parent=parent)+ self.task = task+ self._fileItems: dict[int, QStandardItem] = {}++ self.widget.setMinimumSize(720, 520)+ self.titleLabel = SubtitleLabel(self.tr("选择下载文件"), self.widget)+ self.summaryLabel = BodyLabel("", self.widget)+ self.treeView = AutoSizingTreeView(self.widget)+ self.treeModel = QStandardItemModel(self.treeView)+ self.actionsWidget = QWidget(self.widget)+ self.actionsLayout = QHBoxLayout(self.actionsWidget)+ self.selectAllButton = PrimaryPushButton(self.tr("全选"), self.actionsWidget)+ self.clearButton = PushButton(self.tr("全不选"), self.actionsWidget)+ self.invertButton = PushButton(self.tr("反选"), self.actionsWidget)+ self.selectByTypeButton = DropDownPushButton(self.tr("按类型选择"), self.actionsWidget)+ self.selectByTypeMenu = RoundMenu(parent=self)++ self.yesButton.setText(self.tr("应用"))+ self.cancelButton.setText(self.tr("取消"))++ self._initWidget()+ self._buildTree()+ self._updateSummary()++ def _fileDisplayPath(self, file) -> str:+ raise NotImplementedError++ def _fileTypePath(self, file) -> str:+ return self._fileDisplayPath(file)++ def _initWidget(self):+ self.treeModel.setHorizontalHeaderLabels([self.tr("文件"), self.tr("大小")])+ self.treeView.setModel(self.treeModel)+ self.treeView.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)+ self.treeView.header().setSectionResizeMode(0, QHeaderView.ResizeMode.Interactive)+ self.treeView.header().setSectionResizeMode(1, QHeaderView.ResizeMode.Interactive)+ self.treeModel.itemChanged.connect(self._onItemChanged)++ self.actionsLayout.setContentsMargins(0, 0, 0, 0)+ self.actionsLayout.setSpacing(8)+ self.actionsLayout.addWidget(self.selectAllButton)+ self.actionsLayout.addWidget(self.clearButton)+ self.actionsLayout.addWidget(self.invertButton)+ self.actionsLayout.addWidget(self.selectByTypeButton)+ self.actionsLayout.addStretch(1)++ self.selectAllButton.clicked.connect(self._selectAll)+ self.clearButton.clicked.connect(self._clearAll)+ self.invertButton.clicked.connect(self._invertSelection)+ self._initTypeMenu()++ self.viewLayout.addWidget(self.titleLabel)+ self.viewLayout.addWidget(self.summaryLabel)+ self.viewLayout.addSpacing(8)+ self.viewLayout.addWidget(self.treeView)+ self.viewLayout.addSpacing(8)+ self.viewLayout.addWidget(self.actionsWidget)++ def _buildTree(self):+ folderItems: dict[tuple[str, ...], QStandardItem] = {}+ provider = QFileIconProvider()+ root = self.treeModel.invisibleRootItem()++ for file in self.task.files:+ path = PurePosixPath(self._fileDisplayPath(file))+ parts = path.parts+ parent = root+ prefix: list[str] = []++ for part in parts[:-1]:+ prefix.append(part)+ key = tuple(prefix)+ item = folderItems.get(key)+ if item is None:+ item = QStandardItem(part)+ item.setEditable(False)+ item.setCheckable(True)+ item.setCheckState(Qt.CheckState.Unchecked)+ item.setIcon(provider.icon(QFileIconProvider.IconType.Folder))+ sizeItem = QStandardItem("")+ sizeItem.setEditable(False)+ parent.appendRow([item, sizeItem])+ folderItems[key] = item+ parent = item++ name = parts[-1] if parts else self._fileDisplayPath(file)+ item = QStandardItem(name)+ item.setEditable(False)+ item.setCheckable(True)+ item.setCheckState(Qt.CheckState.Checked if file.selected else Qt.CheckState.Unchecked)+ item.setIcon(provider.icon(QFileInfo(name)))+ sizeItem = QStandardItem(getReadableSize(file.size))+ sizeItem.setEditable(False)+ parent.appendRow([item, sizeItem])+ self._fileItems[file.index] = item++ with QSignalBlocker(self.treeModel):+ for i in range(root.rowCount()):+ self._syncBranchCheckState(root.child(i))++ self.treeView.expandAll()+ self.treeView.resizeColumnToContents(0)++ def _initTypeMenu(self):+ for typeKey, count in self._availableFileTypes().items():+ label, icon = self._FILE_TYPE_META.get(typeKey, (self.tr("其他"), FluentIcon.FOLDER))+ action = Action(icon, self.tr("仅选{0} ({1})").format(self.tr(label), count), self)+ action.triggered.connect(lambda _, key=typeKey: self._selectOnlyFileType(key))+ self.selectByTypeMenu.addAction(action)++ self.selectByTypeButton.setMenu(self.selectByTypeMenu)+ self.selectByTypeButton.setEnabled(bool(self.selectByTypeMenu.actions()))++ def _syncBranchCheckState(self, item: QStandardItem):+ if item.rowCount() == 0:+ return item.checkState()++ states = [self._syncBranchCheckState(item.child(i)) for i in range(item.rowCount())]+ if all(state == Qt.CheckState.Checked for state in states):+ item.setCheckState(Qt.CheckState.Checked)+ elif all(state == Qt.CheckState.Unchecked for state in states):+ item.setCheckState(Qt.CheckState.Unchecked)+ else:+ item.setCheckState(Qt.CheckState.PartiallyChecked)+ return item.checkState()++ def _syncAncestorCheckStates(self, item: QStandardItem | None):+ while item is not None:+ states = [item.child(i).checkState() for i in range(item.rowCount())]+ if all(state == Qt.CheckState.Checked for state in states):+ item.setCheckState(Qt.CheckState.Checked)+ elif all(state == Qt.CheckState.Unchecked for state in states):+ item.setCheckState(Qt.CheckState.Unchecked)+ else:+ item.setCheckState(Qt.CheckState.PartiallyChecked)+ item = item.parent()++ def _setChildrenCheckState(self, item: QStandardItem, state: Qt.CheckState):+ for i in range(item.rowCount()):+ child = item.child(i)+ child.setCheckState(state)+ self._setChildrenCheckState(child, state)++ def _availableFileTypes(self) -> dict[str, int]:+ counts: dict[str, int] = {}+ for file in self.task.files:+ typeKey = self._fileType(self._fileTypePath(file))+ counts[typeKey] = counts.get(typeKey, 0) + 1+ return counts++ def _collectSelectedIndexes(self) -> set[int]:+ return {+ file.index+ for file in self.task.files+ if self._fileItems[file.index].checkState() == Qt.CheckState.Checked+ }++ def _setSelectedIndexes(self, selectedIndexes: set[int]):+ with QSignalBlocker(self.treeModel):+ for file in self.task.files:+ item = self._fileItems[file.index]+ state = Qt.CheckState.Checked if file.index in selectedIndexes else Qt.CheckState.Unchecked+ item.setCheckState(state)+ self._syncAncestorCheckStates(item.parent())+ self._updateSummary()++ def _updateSummary(self):+ selectedIndexes = self._collectSelectedIndexes()+ selectedFiles = [file for file in self.task.files if file.index in selectedIndexes]+ self.summaryLabel.setText(+ self.tr("已选择 {0}/{1} 个文件,共 {2}").format(+ len(selectedFiles),+ self.task.totalFileCount,+ getReadableSize(sum(file.size for file in selectedFiles)),+ )+ )++ def _onItemChanged(self, item: QStandardItem):+ if item.column() != 0:+ return++ with QSignalBlocker(self.treeModel):+ if item.rowCount() > 0 and item.checkState() != Qt.CheckState.PartiallyChecked:+ self._setChildrenCheckState(item, item.checkState())+ self._syncAncestorCheckStates(item.parent())+ self._updateSummary()+ self.treeView.viewport().update()++ def _setRootCheckState(self, state: Qt.CheckState):+ if state == Qt.CheckState.Checked:+ self._setSelectedIndexes({file.index for file in self.task.files})+ else:+ self._setSelectedIndexes(set())++ def _selectAll(self):+ self._setRootCheckState(Qt.CheckState.Checked)+ self.treeView.viewport().update()++ def _clearAll(self):+ self._setRootCheckState(Qt.CheckState.Unchecked)+ self.treeView.viewport().update()++ def _invertSelection(self):+ currentSelected = self._collectSelectedIndexes()+ self._setSelectedIndexes({+ file.index for file in self.task.files if file.index not in currentSelected+ })+ self.treeView.viewport().update()++ def _selectOnlyFileType(self, typeKey: str):+ self._setSelectedIndexes({+ file.index for file in self.task.files if self._fileType(self._fileTypePath(file)) == typeKey+ })+ self.treeView.viewport().update()++ def validate(self) -> bool:+ if self._collectSelectedIndexes():+ return True++ InfoBar.warning(+ self.tr("至少选择一个文件"),+ self.tr("当前没有任何文件被勾选"),+ parent=self,+ )+ return False++ def selectedIndexes(self) -> set[int]:+ return self._collectSelectedIndexes()diff --git a/app/view/components/tree_view.py b/app/view/components/tree_view.pynew file mode 100644index 00000000..3b83dc64--- /dev/null+++ b/app/view/components/tree_view.py@@ -0,0 +1,68 @@+from PySide6.QtCore import QModelIndex, QSize+from PySide6.QtWidgets import QSizePolicy+from qfluentwidgets import TreeView+++class AutoSizingTreeView(TreeView):+ def __init__(self, parent=None, minimumVisibleRows: int = 5):+ super().__init__(parent)+ self._minimumVisibleRows = minimumVisibleRows+ self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)+ self.expanded.connect(self.updateGeometry)+ self.collapsed.connect(self.updateGeometry)++ def setModel(self, model):+ super().setModel(model)+ if model is not None:+ model.rowsInserted.connect(self.updateGeometry)+ model.rowsRemoved.connect(self.updateGeometry)+ model.modelReset.connect(self.updateGeometry)++ def _visibleRowCount(self) -> int:+ return self._countVisibleRows(QModelIndex())++ def _countVisibleRows(self, parent: QModelIndex) -> int:+ model = self.model()+ if model is None:+ return 0+ count = 0+ for row in range(model.rowCount(parent)):+ index = model.index(row, 0, parent)+ count += 1+ if self.isExpanded(index):+ count += self._countVisibleRows(index)+ return count++ def _rowHeight(self) -> int:+ if self.model() and self.model().rowCount() > 0:+ h = self.sizeHintForRow(0)+ if h > 0:+ return h+ return self.fontMetrics().height() + 4++ def _viewChromeHeight(self) -> int:+ margins = self.contentsMargins()+ viewportMargins = self.viewportMargins()+ header = self.header()+ headerHeight = header.height() if header and not header.isHidden() else 0+ return (+ margins.top()+ + margins.bottom()+ + viewportMargins.top()+ + viewportMargins.bottom()+ + self.frameWidth() * 2+ + headerHeight+ )++ def _sizeHintForRowCount(self, rowCount: int) -> QSize:+ height = self._viewChromeHeight() + self._rowHeight() * rowCount+ return QSize(super().sizeHint().width(), height)++ def minimumSizeHint(self) -> QSize:+ return self._sizeHintForRowCount(min(self._minimumVisibleRows, self._visibleRowCount()))++ def maximumSizeHint(self) -> QSize:+ return self._sizeHintForRowCount(self._visibleRowCount())++ def sizeHint(self) -> QSize:+ return self.maximumSizeHint().expandedTo(self.minimumSizeHint())diff --git a/features/bittorrent_pack/cards.py b/features/bittorrent_pack/cards.pyindex c8a0e092..3035da87 100644--- a/features/bittorrent_pack/cards.py+++ b/features/bittorrent_pack/cards.py@@ -1,75 +1,31 @@import shutilfrom pathlib import Path, PurePosixPath-from PySide6.QtCore import Qt, QFileInfo, QSignalBlocker-from PySide6.QtGui import QStandardItem, QStandardItemModel+from PySide6.QtCore import Qt, QFileInfofrom PySide6.QtWidgets import (- QAbstractItemView,- QHeaderView,QHBoxLayout,QVBoxLayout,- QWidget, QFileIconProvider,+ QWidget,+ QFileIconProvider,)from qfluentwidgets import (- Action,BodyLabel,CaptionLabel,- DropDownPushButton,FluentIcon,IconWidget,- InfoBar,- MessageBoxBase,PrimaryPushButton,- PushButton,- RoundMenu,StrongBodyLabel,ToolButton,- SubtitleLabel,- TreeView)from app.bases.models import TaskStatusfrom app.supports.utils import getReadableSize, getReadableTimefrom app.view.components.cards import ResultCard, UniversalTaskCard+from app.view.components.dialogs import FileSelectDialogfrom app.view.components.labels import IconBodyLabelfrom .task import BitTorrentFile, BitTorrentTask-_TORRENT_FILE_TYPE_RULES = (- ("video", "视频", FluentIcon.VIDEO, {- ".avi", ".flv", ".m2ts", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg", ".rmvb", ".ts", ".webm", ".wmv",- }),- ("audio", "音频", FluentIcon.MUSIC, {- ".aac", ".ape", ".flac", ".m4a", ".mp3", ".ogg", ".opus", ".wav", ".wma",- }),- ("image", "图片", FluentIcon.PHOTO, {- ".avif", ".bmp", ".gif", ".jpeg", ".jpg", ".png", ".svg", ".tif", ".tiff", ".webp",- }),- ("subtitle", "字幕", FluentIcon.CHAT, {- ".ass", ".idx", ".lrc", ".psb", ".smi", ".srt", ".ssa", ".sub", ".sup", ".vtt",- }),- ("document", "文档", FluentIcon.DOCUMENT, {- ".chm", ".csv", ".doc", ".docx", ".epub", ".md", ".nfo", ".odt", ".pdf", ".ppt", ".pptx", ".rtf",- ".txt", ".xls", ".xlsx",- }),- ("archive", "压缩包", FluentIcon.ZIP_FOLDER, {- ".001", ".7z", ".bz2", ".cab", ".gz", ".iso", ".rar", ".tar", ".tbz2", ".tgz", ".xz", ".zip", ".zst",- ".tar.bz2", ".tar.gz", ".tar.xz", ".tar.zst",- }),- ("application", "程序", FluentIcon.APPLICATION, {- ".apk", ".appimage", ".bat", ".com", ".deb", ".dmg", ".exe", ".iso", ".jar", ".msi", ".pkg", ".rpm", ".sh",- }),-)--_TORRENT_FILE_TYPE_META = {- key: (label, icon)- for key, label, icon, _ in _TORRENT_FILE_TYPE_RULES-}-_TORRENT_FILE_TYPE_SUFFIXES = {- key: suffixes- for key, _, _, suffixes in _TORRENT_FILE_TYPE_RULES-}-def _removePath(path: Path):if not path.exists():@@ -92,256 +48,16 @@ def _openFileSelection(task: BitTorrentTask, parent) -> set[int] | None:dialog.deleteLater()-def _torrentFileSuffix(path: str) -> str:- suffixes = [suffix.lower() for suffix in PurePosixPath(path).suffixes]- if not suffixes:- return ""-- if len(suffixes) > 1:- combined = "".join(suffixes[-2:])- if combined in _TORRENT_FILE_TYPE_SUFFIXES["archive"]:- return combined-- return suffixes[-1]---def _torrentFileType(path: str) -> str:- suffix = _torrentFileSuffix(path)- for key, _, _, suffixes in _TORRENT_FILE_TYPE_RULES:- if suffix in suffixes:- return key- return "other"---class TorrentFileSelectDialog(MessageBoxBase):+class TorrentFileSelectDialog(FileSelectDialog):def __init__(self, task: BitTorrentTask, parent=None):- super().__init__(parent=parent)- self.task = task- self._fileItems: dict[int, QStandardItem] = {}-- self.widget.setMinimumSize(720, 520)- self.titleLabel = SubtitleLabel(self.tr("选择下载文件"), self.widget)- self.summaryLabel = BodyLabel("", self.widget)- self.treeView = TreeView(self.widget)- self.treeModel = QStandardItemModel(self.treeView)- self.actionsWidget = QWidget(self.widget)- self.actionsLayout = QHBoxLayout(self.actionsWidget)- self.selectAllButton = PrimaryPushButton(self.tr("全选"), self.actionsWidget)- self.clearButton = PushButton(self.tr("全不选"), self.actionsWidget)- self.invertButton = PushButton(self.tr("反选"), self.actionsWidget)- self.selectByTypeButton = DropDownPushButton(self.tr("按类型选择"), self.actionsWidget)- self.selectByTypeMenu = RoundMenu(parent=self)-- self.yesButton.setText(self.tr("应用"))- self.cancelButton.setText(self.tr("取消"))-- self._initWidget()- self._buildTree()- self._updateSummary()-- def _initWidget(self):- self.treeModel.setHorizontalHeaderLabels([self.tr("文件"), self.tr("大小")])- self.treeView.setModel(self.treeModel)- self.treeView.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)- self.treeView.header().setSectionResizeMode(0, QHeaderView.ResizeMode.Interactive)- self.treeView.header().setSectionResizeMode(1, QHeaderView.ResizeMode.Interactive)- self.treeModel.itemChanged.connect(self._onItemChanged)-- self.actionsLayout.setContentsMargins(0, 0, 0, 0)- self.actionsLayout.setSpacing(8)- self.actionsLayout.addWidget(self.selectAllButton)- self.actionsLayout.addWidget(self.clearButton)- self.actionsLayout.addWidget(self.invertButton)- self.actionsLayout.addWidget(self.selectByTypeButton)- self.actionsLayout.addStretch(1)-- self.selectAllButton.clicked.connect(self._selectAll)- self.clearButton.clicked.connect(self._clearAll)- self.invertButton.clicked.connect(self._invertSelection)- self._initTypeMenu()-- self.viewLayout.addWidget(self.titleLabel)- self.viewLayout.addWidget(self.summaryLabel)- self.viewLayout.addSpacing(8)- self.viewLayout.addWidget(self.treeView)- self.viewLayout.addSpacing(8)- self.viewLayout.addWidget(self.actionsWidget)-- def _buildTree(self):- folderItems: dict[tuple[str, ...], QStandardItem] = {}- provider = QFileIconProvider()- root = self.treeModel.invisibleRootItem()- for file in self.task.files:- path = PurePosixPath(self.task.mappedRelativePath(file))- parts = path.parts- parent = root- prefix: list[str] = []-- for part in parts[:-1]:- prefix.append(part)- key = tuple(prefix)- item = folderItems.get(key)- if item is None:- item = QStandardItem(part)- item.setEditable(False)- item.setCheckable(True)- item.setCheckState(Qt.CheckState.Unchecked)- item.setIcon(provider.icon(QFileIconProvider.IconType.Folder))- sizeItem = QStandardItem("")- sizeItem.setEditable(False)- parent.appendRow([item, sizeItem])- folderItems[key] = item- parent = item-- name = parts[-1]- item = QStandardItem(name)- item.setEditable(False)- item.setCheckable(True)- item.setCheckState(Qt.CheckState.Checked if file.selected else Qt.CheckState.Unchecked)- item.setIcon(provider.icon(QFileInfo(name)))- sizeItem = QStandardItem(getReadableSize(file.size))- sizeItem.setEditable(False)- parent.appendRow([item, sizeItem])- self._fileItems[file.index] = item-- with QSignalBlocker(self.treeModel):- for i in range(root.rowCount()):- self._syncBranchCheckState(root.child(i))-- self.treeView.expandAll()- self.treeView.resizeColumnToContents(0)-- def _initTypeMenu(self):- for typeKey, count in self._availableFileTypes().items():- label, icon = _TORRENT_FILE_TYPE_META.get(typeKey, (self.tr("其他"), FluentIcon.FOLDER))- action = Action(icon, self.tr("仅选{0} ({1})").format(self.tr(label), count), self)- action.triggered.connect(lambda _, key=typeKey: self._selectOnlyFileType(key))- self.selectByTypeMenu.addAction(action)-- self.selectByTypeButton.setMenu(self.selectByTypeMenu)- self.selectByTypeButton.setEnabled(bool(self.selectByTypeMenu.actions()))-- def _availableFileTypes(self) -> dict[str, int]:- counts: dict[str, int] = {}- for file in self.task.files:- typeKey = _torrentFileType(file.path)- counts[typeKey] = counts.get(typeKey, 0) + 1- return counts-- def _syncBranchCheckState(self, item: QStandardItem):- if item.rowCount() == 0:- return item.checkState()-- states = [self._syncBranchCheckState(item.child(i)) for i in range(item.rowCount())]-- if all(state == Qt.CheckState.Checked for state in states):- item.setCheckState(Qt.CheckState.Checked)- elif all(state == Qt.CheckState.Unchecked for state in states):- item.setCheckState(Qt.CheckState.Unchecked)- else:- item.setCheckState(Qt.CheckState.PartiallyChecked)- return item.checkState()-- def _syncAncestorCheckStates(self, item: QStandardItem | None):- while item is not None:- states = [item.child(i).checkState() for i in range(item.rowCount())]- if all(state == Qt.CheckState.Checked for state in states):- item.setCheckState(Qt.CheckState.Checked)- elif all(state == Qt.CheckState.Unchecked for state in states):- item.setCheckState(Qt.CheckState.Unchecked)- else:- item.setCheckState(Qt.CheckState.PartiallyChecked)- item = item.parent()-- def _setChildrenCheckState(self, item: QStandardItem, state: Qt.CheckState):- for i in range(item.rowCount()):- child = item.child(i)- child.setCheckState(state)- self._setChildrenCheckState(child, state)-- def _collectSelectedIndexes(self) -> set[int]:- return {- file.index- for file in self.task.files- if self._fileItems[file.index].checkState() == Qt.CheckState.Checked- }-- def _setSelectedIndexes(self, selectedIndexes: set[int]):- with QSignalBlocker(self.treeModel):- for file in self.task.files:- item = self._fileItems[file.index]- state = Qt.CheckState.Checked if file.index in selectedIndexes else Qt.CheckState.Unchecked- item.setCheckState(state)- self._syncAncestorCheckStates(item.parent())- self._updateSummary()-- def _updateSummary(self):- selectedIndexes = self._collectSelectedIndexes()- selectedFiles = [file for file in self.task.files if file.index in selectedIndexes]-- self.summaryLabel.setText(- self.tr("已选择 {0}/{1} 个文件,共 {2}").format(- len(selectedFiles),- self.task.totalFileCount,- getReadableSize(sum(file.size for file in selectedFiles)),- )- )-- def _onItemChanged(self, item: QStandardItem):- if item.column() != 0:- return-- with QSignalBlocker(self.treeModel):- if item.rowCount() > 0 and item.checkState() != Qt.CheckState.PartiallyChecked:- self._setChildrenCheckState(item, item.checkState())- self._syncAncestorCheckStates(item.parent())- self._updateSummary()- self.treeView.viewport().update()-- def _setRootCheckState(self, state: Qt.CheckState):- if state == Qt.CheckState.Checked:- self._setSelectedIndexes({file.index for file in self.task.files})- return- self._setSelectedIndexes(set())-- def _selectAll(self):- self._setRootCheckState(Qt.CheckState.Checked)- self.treeView.viewport().update()-- def _clearAll(self):- self._setRootCheckState(Qt.CheckState.Unchecked)- self.treeView.viewport().update()-- def _invertSelection(self):- currentSelected = self._collectSelectedIndexes()- self._setSelectedIndexes({- file.index- for file in self.task.files- if file.index not in currentSelected- })- self.treeView.viewport().update()-- def _selectOnlyFileType(self, typeKey: str):- self._setSelectedIndexes({- file.index- for file in self.task.files- if _torrentFileType(file.path) == typeKey- })- self.treeView.viewport().update()-- def validate(self) -> bool:- if self._collectSelectedIndexes():- return True+ super().__init__(task, parent)+ self.task: BitTorrentTask = task- InfoBar.warning(- self.tr("至少选择一个文件"),- self.tr("当前没有任何文件被勾选"),- parent=self,- )- return False+ def _fileDisplayPath(self, file: BitTorrentFile) -> str:+ return self.task.mappedRelativePath(file)- def selectedIndexes(self) -> set[int]:- return self._collectSelectedIndexes()+ def _fileTypePath(self, file: BitTorrentFile) -> str:+ return file.pathclass BitTorrentResultCard(ResultCard):diff --git a/features/ftp_pack/cards.py b/features/ftp_pack/cards.pyindex 75d8ddb2..014debda 100644--- a/features/ftp_pack/cards.py+++ b/features/ftp_pack/cards.py@@ -1,75 +1,31 @@import shutil-from pathlib import Path, PurePosixPath+from pathlib import Path-from PySide6.QtCore import QEvent, QFileInfo, QSignalBlocker, Qt-from PySide6.QtGui import QMouseEvent, QStandardItem, QStandardItemModel+from PySide6.QtCore import QEvent, QFileInfo, Qtfrom PySide6.QtWidgets import (- QAbstractItemView,QFileIconProvider,- QHeaderView,QHBoxLayout,QVBoxLayout,QWidget,)from qfluentwidgets import (- Action,BodyLabel,CaptionLabel,- DropDownPushButton,FluentIcon,IconWidget,- InfoBar,LineEdit,- MessageBoxBase,PrimaryPushButton,- PushButton,- RoundMenu,StrongBodyLabel,ToolButton,- TreeView,)from app.bases.models import TaskStatusfrom app.supports.utils import getReadableSize, getReadableTime, openFile, openFolderfrom app.view.components.cards import ResultCard, UniversalTaskCard+from app.view.components.dialogs import FileSelectDialogfrom .task import FtpTask-_FILE_TYPE_RULES = (- ("video", "视频", FluentIcon.VIDEO, {- ".avi", ".flv", ".m2ts", ".m4v", ".mkv", ".mov", ".mp4", ".mpeg", ".mpg", ".rmvb", ".ts", ".webm", ".wmv",- }),- ("audio", "音频", FluentIcon.MUSIC, {- ".aac", ".ape", ".flac", ".m4a", ".mp3", ".ogg", ".opus", ".wav", ".wma",- }),- ("image", "图片", FluentIcon.PHOTO, {- ".avif", ".bmp", ".gif", ".jpeg", ".jpg", ".png", ".svg", ".tif", ".tiff", ".webp",- }),- ("subtitle", "字幕", FluentIcon.CHAT, {- ".ass", ".idx", ".lrc", ".psb", ".smi", ".srt", ".ssa", ".sub", ".sup", ".vtt",- }),- ("document", "文档", FluentIcon.DOCUMENT, {- ".chm", ".csv", ".doc", ".docx", ".epub", ".md", ".nfo", ".odt", ".pdf", ".ppt", ".pptx", ".rtf",- ".txt", ".xls", ".xlsx",- }),- ("archive", "压缩包", FluentIcon.ZIP_FOLDER, {- ".001", ".7z", ".bz2", ".cab", ".gz", ".iso", ".rar", ".tar", ".tbz2", ".tgz", ".xz", ".zip", ".zst",- ".tar.bz2", ".tar.gz", ".tar.xz", ".tar.zst",- }),- ("application", "程序", FluentIcon.APPLICATION, {- ".apk", ".appimage", ".bat", ".com", ".deb", ".dmg", ".exe", ".iso", ".jar", ".msi", ".pkg", ".rpm", ".sh",- }),-)--_FILE_TYPE_META = {- key: (label, icon)- for key, label, icon, _ in _FILE_TYPE_RULES-}-_FILE_TYPE_SUFFIXES = {- key: suffixes- for key, _, _, suffixes in _FILE_TYPE_RULES-}-def _removePath(path: Path):if not path.exists():@@ -80,26 +36,6 @@ def _removePath(path: Path):path.unlink(missing_ok=True)-def _ftpFileSuffix(path: str) -> str:- suffixes = [suffix.lower() for suffix in PurePosixPath(path).suffixes]- if not suffixes:- return ""-- if len(suffixes) > 1:- combined = "".join(suffixes[-2:])- if combined in _FILE_TYPE_SUFFIXES["archive"]:- return combined- return suffixes[-1]---def _ftpFileType(path: str) -> str:- suffix = _ftpFileSuffix(path)- for key, _, _, suffixes in _FILE_TYPE_RULES:- if suffix in suffixes:- return key- return "other"--def _openFileSelection(task: FtpTask, parent) -> set[int] | None:dialog = FtpFileSelectDialog(task, parent)try:@@ -112,247 +48,13 @@ def _openFileSelection(task: FtpTask, parent) -> set[int] | None:dialog.deleteLater()-class FtpFileSelectDialog(MessageBoxBase):+class FtpFileSelectDialog(FileSelectDialog):def __init__(self, task: FtpTask, parent=None):- super().__init__(parent=parent)- self.task = task- self._fileItems: dict[int, QStandardItem] = {}-- self.widget.setMinimumSize(720, 520)- self.titleLabel = StrongBodyLabel(self.tr("选择下载文件"), self.widget)- self.summaryLabel = BodyLabel("", self.widget)- self.treeView = TreeView(self.widget)- self.treeModel = QStandardItemModel(self.treeView)- self.actionsWidget = QWidget(self.widget)- self.actionsLayout = QHBoxLayout(self.actionsWidget)- self.selectAllButton = PrimaryPushButton(self.tr("全选"), self.actionsWidget)- self.clearButton = PushButton(self.tr("全不选"), self.actionsWidget)- self.invertButton = PushButton(self.tr("反选"), self.actionsWidget)- self.selectByTypeButton = DropDownPushButton(self.tr("按类型选择"), self.actionsWidget)- self.selectByTypeMenu = RoundMenu(parent=self)-- self.yesButton.setText(self.tr("应用"))- self.cancelButton.setText(self.tr("取消"))-- self._initWidget()- self._buildTree()- self._updateSummary()-- def _initWidget(self):- self.treeModel.setHorizontalHeaderLabels([self.tr("文件"), self.tr("大小")])- self.treeView.setModel(self.treeModel)- self.treeView.setEditTriggers(QAbstractItemView.EditTrigger.NoEditTriggers)- self.treeView.header().setSectionResizeMode(0, QHeaderView.ResizeMode.Interactive)- self.treeView.header().setSectionResizeMode(1, QHeaderView.ResizeMode.Interactive)- self.treeModel.itemChanged.connect(self._onItemChanged)-- self.actionsLayout.setContentsMargins(0, 0, 0, 0)- self.actionsLayout.setSpacing(8)- self.actionsLayout.addWidget(self.selectAllButton)- self.actionsLayout.addWidget(self.clearButton)- self.actionsLayout.addWidget(self.invertButton)- self.actionsLayout.addWidget(self.selectByTypeButton)- self.actionsLayout.addStretch(1)-- self.selectAllButton.clicked.connect(self._selectAll)- self.clearButton.clicked.connect(self._clearAll)- self.invertButton.clicked.connect(self._invertSelection)- self._initTypeMenu()-- self.viewLayout.addWidget(self.titleLabel)- self.viewLayout.addWidget(self.summaryLabel)- self.viewLayout.addSpacing(8)- self.viewLayout.addWidget(self.treeView)- self.viewLayout.addSpacing(8)- self.viewLayout.addWidget(self.actionsWidget)-- def _buildTree(self):- folderItems: dict[tuple[str, ...], QStandardItem] = {}- provider = QFileIconProvider()- root = self.treeModel.invisibleRootItem()-- for file in self.task.files:- path = PurePosixPath(file.relativePath)- parts = path.parts- parent = root- prefix: list[str] = []-- for part in parts[:-1]:- prefix.append(part)- key = tuple(prefix)- item = folderItems.get(key)- if item is None:- item = QStandardItem(part)- item.setEditable(False)- item.setCheckable(True)- item.setCheckState(Qt.CheckState.Unchecked)- item.setIcon(provider.icon(QFileIconProvider.IconType.Folder))- sizeItem = QStandardItem("")- sizeItem.setEditable(False)- parent.appendRow([item, sizeItem])- folderItems[key] = item- parent = item-- item = QStandardItem(parts[-1] if parts else file.relativePath)- item.setEditable(False)- item.setCheckable(True)- item.setCheckState(- Qt.CheckState.Checked if file.selected else Qt.CheckState.Unchecked- )- item.setIcon(provider.icon(QFileInfo(item.text())))- sizeItem = QStandardItem(getReadableSize(file.size))- sizeItem.setEditable(False)- parent.appendRow([item, sizeItem])- self._fileItems[file.index] = item-- with QSignalBlocker(self.treeModel):- for i in range(root.rowCount()):- self._syncBranchCheckState(root.child(i))-- self.treeView.expandAll()- self.treeView.resizeColumnToContents(0)-- def _initTypeMenu(self):- for typeKey, count in self._availableFileTypes().items():- label, icon = _FILE_TYPE_META.get(typeKey, (self.tr("其他"), FluentIcon.FOLDER))- action = Action(- icon,- self.tr("仅选{0} ({1})").format(self.tr(label), count),- self,- )- action.triggered.connect(lambda _, key=typeKey: self._selectOnlyFileType(key))- self.selectByTypeMenu.addAction(action)-- self.selectByTypeButton.setMenu(self.selectByTypeMenu)- self.selectByTypeButton.setEnabled(bool(self.selectByTypeMenu.actions()))-- def _availableFileTypes(self) -> dict[str, int]:- counts: dict[str, int] = {}- for file in self.task.files:- typeKey = _ftpFileType(file.relativePath)- counts[typeKey] = counts.get(typeKey, 0) + 1- return counts-- def _syncBranchCheckState(self, item: QStandardItem):- if item.rowCount() == 0:- return item.checkState()-- states = [self._syncBranchCheckState(item.child(i)) for i in range(item.rowCount())]- if all(state == Qt.CheckState.Checked for state in states):- item.setCheckState(Qt.CheckState.Checked)- elif all(state == Qt.CheckState.Unchecked for state in states):- item.setCheckState(Qt.CheckState.Unchecked)- else:- item.setCheckState(Qt.CheckState.PartiallyChecked)- return item.checkState()-- def _syncAncestorCheckStates(self, item: QStandardItem | None):- while item is not None:- states = [item.child(i).checkState() for i in range(item.rowCount())]- if all(state == Qt.CheckState.Checked for state in states):- item.setCheckState(Qt.CheckState.Checked)- elif all(state == Qt.CheckState.Unchecked for state in states):- item.setCheckState(Qt.CheckState.Unchecked)- else:- item.setCheckState(Qt.CheckState.PartiallyChecked)- item = item.parent()-- def _setChildrenCheckState(self, item: QStandardItem, state: Qt.CheckState):- for i in range(item.rowCount()):- child = item.child(i)- child.setCheckState(state)- self._setChildrenCheckState(child, state)-- def _collectSelectedIndexes(self) -> set[int]:- return {- file.index- for file in self.task.files- if self._fileItems[file.index].checkState() == Qt.CheckState.Checked- }-- def _setSelectedIndexes(self, selectedIndexes: set[int]):- with QSignalBlocker(self.treeModel):- for file in self.task.files:- item = self._fileItems[file.index]- state = (- Qt.CheckState.Checked- if file.index in selectedIndexes- else Qt.CheckState.Unchecked- )- item.setCheckState(state)- self._syncAncestorCheckStates(item.parent())- self._updateSummary()-- def _updateSummary(self):- selectedIndexes = self._collectSelectedIndexes()- selectedFiles = [file for file in self.task.files if file.index in selectedIndexes]- self.summaryLabel.setText(- self.tr("已选择 {0}/{1} 个文件,共 {2}").format(- len(selectedFiles),- self.task.totalFileCount,- getReadableSize(sum(file.size for file in selectedFiles)),- )- )-- def _onItemChanged(self, item: QStandardItem):- if item.column() != 0:- return-- with QSignalBlocker(self.treeModel):- if item.rowCount() > 0 and item.checkState() != Qt.CheckState.PartiallyChecked:- self._setChildrenCheckState(item, item.checkState())- self._syncAncestorCheckStates(item.parent())- self._updateSummary()- self.treeView.viewport().update()-- def _setRootCheckState(self, state: Qt.CheckState):- if state == Qt.CheckState.Checked:- self._setSelectedIndexes({file.index for file in self.task.files})- return- self._setSelectedIndexes(set())-- def _selectAll(self):- self._setRootCheckState(Qt.CheckState.Checked)- self.treeView.viewport().update()-- def _clearAll(self):- self._setRootCheckState(Qt.CheckState.Unchecked)- self.treeView.viewport().update()-- def _invertSelection(self):- currentSelected = self._collectSelectedIndexes()- self._setSelectedIndexes(- {- file.index- for file in self.task.files- if file.index not in currentSelected- }- )- self.treeView.viewport().update()-- def _selectOnlyFileType(self, typeKey: str):- self._setSelectedIndexes(- {- file.index- for file in self.task.files- if _ftpFileType(file.relativePath) == typeKey- }- )- self.treeView.viewport().update()-- def validate(self) -> bool:- if self._collectSelectedIndexes():- return True-- InfoBar.warning(- self.tr("至少选择一个文件"),- self.tr("当前没有任何文件被勾选"),- parent=self,- )- return False+ super().__init__(task, parent)+ self.task: FtpTask = task- def selectedIndexes(self) -> set[int]:- return self._collectSelectedIndexes()+ def _fileDisplayPath(self, file) -> str:+ return file.relativePathclass FtpResultCard(ResultCard):@@ -407,7 +109,7 @@ class FtpResultCard(ResultCard):def eventFilter(self, obj, event: QEvent):if obj is self.titleLabel:- if event.type() == QEvent.Type.MouseButtonDblClick and isinstance(event, QMouseEvent):+ if event.type() == QEvent.Type.MouseButtonDblClick:if event.button() == Qt.MouseButton.LeftButton:self._enterEditMode()return True