Skip to content

The only downloader you need. 下载器的集大成者。

feat(tree_view): 修复文件选择对话框在大屏上太小的问题

XiaoYouChRbfcb22629d67
diff --git a/app/view/components/tree_view.py b/app/view/components/tree_view.py
index 196993e1..c73c5edb 100644
--- a/app/view/components/tree_view.py
+++ b/app/view/components/tree_view.py
@@ -1,19 +1,62 @@
from __future__ import annotations
-from PySide6.QtCore import QSize
+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 = 1, maximumVisibleRows: int = 10):
+ def __init__(self, parent=None, minimumVisibleRows: int = 1,
+ maximumVisibleRows: int | None = None):
super().__init__(parent)
self._minimumVisibleRows = minimumVisibleRows
self._maximumVisibleRows = maximumVisibleRows
+ self.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)
+ self.expanded.connect(self.updateGeometry)
+ self.collapsed.connect(self.updateGeometry)
- def sizeHint(self) -> QSize:
+ 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._countVisible(QModelIndex())
+
+ def _countVisible(self, parent: QModelIndex) -> int:
model = self.model()
- rowCount = model.rowCount() if model else 0
- visibleRows = max(self._minimumVisibleRows, min(rowCount, self._maximumVisibleRows))
- rowHeight = self.sizeHintForRow(0) if rowCount > 0 else 30
+ if model is None:
+ return 0
+ count = 0
+ for row in range(model.rowCount(parent)):
+ count += 1
+ index = model.index(row, 0, parent)
+ if self.isExpanded(index):
+ count += self._countVisible(index)
+ return count
+
+ def _rowHeight(self) -> int:
+ if self.model() and self.model().rowCount() > 0:
+ return max(self.sizeHintForRow(0), 1)
+ return 30
+
+ def _contentWidth(self) -> int:
+ header = self.header()
+ if header.count() == 0:
+ return super().sizeHint().width()
+ return header.length() + self.frameWidth() * 2 + self.verticalScrollBar().sizeHint().width()
+
+ def _sizeForRows(self, count: int) -> QSize:
headerHeight = self.header().height() if self.header().isVisible() else 0
- return QSize(super().sizeHint().width(), headerHeight + visibleRows * rowHeight + 4)
+ return QSize(self._contentWidth(), headerHeight + count * self._rowHeight() + 4)
+
+ def minimumSizeHint(self) -> QSize:
+ return self._sizeForRows(min(self._minimumVisibleRows, self._visibleRowCount()))
+
+ def sizeHint(self) -> QSize:
+ count = self._visibleRowCount()
+ if self._maximumVisibleRows is not None:
+ count = min(count, self._maximumVisibleRows)
+ return self._sizeForRows(count).expandedTo(self.minimumSizeHint())
diff --git a/app/view/dialogs/file_select.py b/app/view/dialogs/file_select.py
index c719314f..8946e170 100644
--- a/app/view/dialogs/file_select.py
+++ b/app/view/dialogs/file_select.py
@@ -30,7 +30,7 @@ class FileSelectDialog(MessageBoxBase):
self.titleLabel = SubtitleLabel(self.tr("选择下载文件"), self)
self.summaryLabel = BodyLabel("", self)
- self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3, maximumVisibleRows=16)
+ self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3)
self.treeModel = QStandardItemModel(self.treeView)
self.selectAllButton = PrimaryPushButton(self.tr("全选"), self)
diff --git a/app/view/dialogs/subtitle_select.py b/app/view/dialogs/subtitle_select.py
index fc5a9155..c4a39637 100644
--- a/app/view/dialogs/subtitle_select.py
+++ b/app/view/dialogs/subtitle_select.py
@@ -22,7 +22,7 @@ class SubtitleSelectDialog(MessageBoxBase):
self.selectAllButton = PrimaryPushButton(self.tr("全选"), self)
self.clearButton = PushButton(self.tr("全不选"), self)
- self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3, maximumVisibleRows=16)
+ self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3)
self.treeModel = QStandardItemModel(self.treeView)
self._initWidget(set(selected))
diff --git a/features/bili_pack/cards.py b/features/bili_pack/cards.py
index 496c5583..1f365720 100644
--- a/features/bili_pack/cards.py
+++ b/features/bili_pack/cards.py
@@ -76,7 +76,7 @@ class SelectDialog(MessageBoxBase):
self.clearButton = PushButton(self.tr("全不选"), self)
self.invertButton = PushButton(self.tr("反选"), self)
- self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3, maximumVisibleRows=16)
+ self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3)
self.treeModel = QStandardItemModel(self.treeView)
self._initWidget()
diff --git a/features/yt_dlp_pack/cards.py b/features/yt_dlp_pack/cards.py
index 8d03b242..242c63c0 100644
--- a/features/yt_dlp_pack/cards.py
+++ b/features/yt_dlp_pack/cards.py
@@ -223,7 +223,7 @@ class VideoSelectDialog(MessageBoxBase):
self.clearButton = PushButton(self.tr("全不选"), self)
self.invertButton = PushButton(self.tr("反选"), self)
- self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3, maximumVisibleRows=16)
+ self.treeView = AutoSizingTreeView(self, minimumVisibleRows=3)
self.treeModel = QStandardItemModel(self.treeView)
self._initWidget()