Skip to content

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

feat(Task): 任意状态下均可修改文件选择

XiaoYouChR7b971c99878f
diff --git a/app/services/task_service.py b/app/services/task_service.py
index 5956b48d..d7837c57 100644
--- a/app/services/task_service.py
+++ b/app/services/task_service.py
@@ -238,6 +238,37 @@ class TaskService(QObject):
task.category = categoryId
self._flushTimer.start()
+ def applySelection(self, task: Task, selectedIndexes: set[int]) -> None:
+ from app.models.task import TaskStatus
+
+ selectedSet = set(selectedIndexes)
+ wasCompleted = task.status == TaskStatus.COMPLETED
+
+ def apply():
+ task.setSelection(selectedSet)
+ if wasCompleted and task.files and any(f.selected and not f.completed for f in task.files):
+ task.completedAt = 0
+ self._unwatchFile(task)
+ self._schedule(task)
+ self._flushTimer.start()
+
+ # 正在下载的文件被取消勾选:暂停-重启一个来回,其余文件断点续传
+ if self._queue.isRunning(task.taskId) and self._isRunningStepDeselected(task, selectedSet):
+ def afterStopped():
+ apply()
+ self._schedule(task)
+ self._cancelRun(task, finished=afterStopped)
+ return
+ apply()
+
+ def _isRunningStepDeselected(self, task: Task, selectedSet: set[int]) -> bool:
+ from app.models.task import TaskStatus
+ for step in task.steps:
+ if step.status == TaskStatus.RUNNING:
+ fileIndex = getattr(step, "fileIndex", None)
+ return fileIndex is not None and fileIndex not in selectedSet
+ return False
+
def startAll(self) -> None:
from app.models.task import TaskStatus
for task in self._store.tasks.values():
diff --git a/features/bili_pack/cards.py b/features/bili_pack/cards.py
index 642309c0..e09b167e 100644
--- a/features/bili_pack/cards.py
+++ b/features/bili_pack/cards.py
@@ -6,11 +6,12 @@ from PySide6.QtWidgets import QAbstractItemView, QHBoxLayout, QHeaderView
from qfluentwidgets import (
BodyLabel, ComboBox, FluentIcon, MessageBoxBase,
PrimaryPushButton, PushButton, SubtitleLabel,
- ToolTipFilter, TransparentToolButton,
+ ToolButton, ToolTipFilter, TransparentToolButton,
)
from app.format import toReadableSize
from app.view.cards.draft_cards import UniversalDraftCard
+from app.view.cards.task_cards import UniversalTaskCard
from app.view.components.tree_view import AutoSizingTreeView
from .task import BilibiliTask, DownloadMode
@@ -99,6 +100,33 @@ class BilibiliDraftCard(UniversalDraftCard):
+class BilibiliTaskCard(UniversalTaskCard):
+ def __init__(self, task: BilibiliTask, parent=None):
+ super().__init__(task, parent)
+ self.selectFilesButton = None
+ if task.mode != DownloadMode.COVER and task.files and len(task.files) > 1:
+ self.selectFilesButton = ToolButton(FluentIcon.LIBRARY, self)
+ self.hBoxLayout.insertWidget(
+ self.hBoxLayout.indexOf(self.verifyHashButton),
+ self.selectFilesButton,
+ )
+ self.selectFilesButton.setToolTip(self.tr("选择分P"))
+ self.selectFilesButton.installEventFilter(ToolTipFilter(self.selectFilesButton))
+ self.selectFilesButton.clicked.connect(self._onSelectPagesClicked)
+
+ def _onSelectPagesClicked(self) -> None:
+ from app.services.task_service import taskService
+ dialog = PageSelectDialog(self._task, self.window())
+ try:
+ if dialog.exec():
+ selected = dialog.selectedPageNumbers()
+ if selected:
+ taskService.applySelection(self._task, {n - 1 for n in selected})
+ self.refresh(force=True)
+ finally:
+ dialog.deleteLater()
+
+
class SubtitleSelectDialog(MessageBoxBase):
def __init__(self, choices: list[tuple[str, str]], selected: list[str], parent=None):
diff --git a/features/bili_pack/pack.py b/features/bili_pack/pack.py
index 83855613..965974d0 100644
--- a/features/bili_pack/pack.py
+++ b/features/bili_pack/pack.py
@@ -292,6 +292,10 @@ class BilibiliPack(FeaturePack):
from .cards import BilibiliDraftCard
return BilibiliDraftCard(task, parent)
+ def taskCard(self, task, parent=None):
+ from .cards import BilibiliTaskCard
+ return BilibiliTaskCard(task, parent)
+
def optionCards(self, task, parent=None):
from app.view.components.option_cards import OutputFolderCard
return [OutputFolderCard(parent, initial=task.outputFolder)]
diff --git a/features/bittorrent_pack/cards.py b/features/bittorrent_pack/cards.py
index 82530653..e316e514 100644
--- a/features/bittorrent_pack/cards.py
+++ b/features/bittorrent_pack/cards.py
@@ -10,13 +10,13 @@ from app.view.dialogs.file_select import FileSelectDialog
from .task import BTTask
-def openFileSelection(task: BTTask, parent) -> set[int] | None:
+def openFileSelection(task: BTTask, parent, apply=None) -> set[int] | None:
dialog = TorrentFileSelectDialog(task, parent)
try:
if not dialog.exec():
return None
selectedIndexes = dialog.selectedIndexes()
- task.setSelection(selectedIndexes)
+ (apply or task.setSelection)(selectedIndexes)
return selectedIndexes
finally:
dialog.deleteLater()
@@ -134,12 +134,9 @@ class BTTaskCard(UniversalTaskCard):
self.uploadLabel.hide()
super().refresh(force)
btStatus = self._seedingSummary(task)
- if btStatus and not self._fileMissing:
+ if btStatus and not self._isFileMissing:
self.statusLabel.setText(btStatus)
- self.selectFilesButton.setEnabled(
- task.status != TaskStatus.COMPLETED or any(not f.selected for f in task.files)
- )
def _refreshDownloading(self, task: BTTask):
self.statusLabel.hide()
@@ -187,5 +184,9 @@ class BTTaskCard(UniversalTaskCard):
return " · ".join(parts)
def _onSelectFilesClicked(self):
- openFileSelection(self._task, self.window())
+ from app.services.task_service import taskService
+ openFileSelection(
+ self._task, self.window(),
+ apply=lambda indexes: taskService.applySelection(self._task, indexes),
+ )
self.refresh(force=True)
diff --git a/features/bittorrent_pack/task.py b/features/bittorrent_pack/task.py
index 4478c33d..aed04831 100644
--- a/features/bittorrent_pack/task.py
+++ b/features/bittorrent_pack/task.py
@@ -90,6 +90,10 @@ class BTTask(Task):
return
self._fileSelectionVersion += 1
self.fileSize = sum(f.size for f in self.files if f.selected)
+ # 完成后补选新文件:步骤打回 WAITING,等待重新调度补下
+ if self.step.status == TaskStatus.COMPLETED and any(f.selected and not f.completed for f in self.files):
+ self.step.setStatus(TaskStatus.WAITING, sync=False)
+ self.updateStatus()
def deleteFiles(self):
super().deleteFiles()
diff --git a/features/ftp_pack/cards.py b/features/ftp_pack/cards.py
index 565f10a9..9a36df4d 100644
--- a/features/ftp_pack/cards.py
+++ b/features/ftp_pack/cards.py
@@ -9,13 +9,13 @@ from app.view.dialogs.file_select import FileSelectDialog
from .task import FtpTask
-def openFileSelection(task: FtpTask, parent) -> set[int] | None:
+def openFileSelection(task: FtpTask, parent, apply=None) -> set[int] | None:
dialog = FileSelectDialog(task, parent)
try:
if not dialog.exec():
return None
selectedIndexes = dialog.selectedIndexes()
- task.setSelection(selectedIndexes)
+ (apply or task.setSelection)(selectedIndexes)
return selectedIndexes
finally:
dialog.deleteLater()
@@ -68,24 +68,25 @@ class FtpDraftCard(UniversalDraftCard):
class FtpTaskCard(UniversalTaskCard):
def __init__(self, task: FtpTask, parent=None):
super().__init__(task, parent)
- self.selectFilesButton = ToolButton(FluentIcon.LIBRARY, self)
- self.hBoxLayout.insertWidget(
- self.hBoxLayout.indexOf(self.verifyHashButton),
- self.selectFilesButton,
- )
- self.selectFilesButton.clicked.connect(self._onSelectFilesClicked)
+ self.selectFilesButton = None
+ if task.files and len(task.files) > 1:
+ self.selectFilesButton = ToolButton(FluentIcon.LIBRARY, self)
+ self.hBoxLayout.insertWidget(
+ self.hBoxLayout.indexOf(self.verifyHashButton),
+ self.selectFilesButton,
+ )
+ self.selectFilesButton.clicked.connect(self._onSelectFilesClicked)
def refresh(self, force: bool = False) -> None:
super().refresh(force=force)
- hasMultipleFiles = bool(self.task.files and len(self.task.files) > 1)
- self.selectFilesButton.setVisible(hasMultipleFiles)
- self.selectFilesButton.setEnabled(self.task.status != TaskStatus.RUNNING)
-
- if self.task.status in {TaskStatus.WAITING, TaskStatus.COMPLETED} and hasMultipleFiles and not self._fileMissing:
+ if self.task.status in {TaskStatus.WAITING, TaskStatus.COMPLETED} and self.selectFilesButton is not None and not self._isFileMissing:
selected = sum(1 for f in self.task.files if f.selected)
self.statusLabel.setText(self.tr("{0}/{1} 个文件").format(selected, len(self.task.files)))
def _onSelectFilesClicked(self):
- if self.task.status == TaskStatus.RUNNING:
- return
- openFileSelection(self.task, self.window())
+ from app.services.task_service import taskService
+ openFileSelection(
+ self.task, self.window(),
+ apply=lambda indexes: taskService.applySelection(self.task, indexes),
+ )
+ self.refresh(force=True)
diff --git a/features/huggingface_pack/cards.py b/features/huggingface_pack/cards.py
index 07ad7f5b..c872bd45 100644
--- a/features/huggingface_pack/cards.py
+++ b/features/huggingface_pack/cards.py
@@ -73,19 +73,16 @@ class HuggingFaceTaskCard(UniversalTaskCard):
def refresh(self, force: bool = False) -> None:
super().refresh(force=force)
- if self.selectFilesButton is not None:
- self.selectFilesButton.setEnabled(self._task.status != TaskStatus.RUNNING)
-
- if self._task.status in {TaskStatus.WAITING, TaskStatus.COMPLETED} and self.selectFilesButton is not None and not self._fileMissing:
+ if self._task.status in {TaskStatus.WAITING, TaskStatus.COMPLETED} and self.selectFilesButton is not None and not self._isFileMissing:
selected = sum(1 for f in self._task.files if f.selected)
self.statusLabel.setText(self.tr("{0}/{1} 个文件").format(selected, len(self._task.files)))
def _onSelectFilesClicked(self) -> None:
- if self._task.status == TaskStatus.RUNNING:
- return
+ from app.services.task_service import taskService
dialog = FileSelectDialog(self._task, self.window())
try:
if dialog.exec():
- self._task.setSelection(dialog.selectedIndexes())
+ taskService.applySelection(self._task, dialog.selectedIndexes())
+ self.refresh(force=True)
finally:
dialog.deleteLater()
diff --git a/features/yt_dlp_pack/cards.py b/features/yt_dlp_pack/cards.py
index 0bfd97df..7b6debb2 100644
--- a/features/yt_dlp_pack/cards.py
+++ b/features/yt_dlp_pack/cards.py
@@ -6,7 +6,7 @@ from PySide6.QtWidgets import QAbstractItemView, QHBoxLayout, QHeaderView, QWidg
from qfluentwidgets import (
BodyLabel, ComboBox, FluentIcon, IndeterminateProgressRing,
MessageBoxBase, PrimaryPushButton, ProgressBar, PushButton, SubtitleLabel,
- ToolTipFilter, TransparentToolButton,
+ ToolButton, ToolTipFilter, TransparentToolButton,
)
from app.format import toReadableSize
@@ -407,6 +407,29 @@ class VideoSelectDialog(MessageBoxBase):
class YtDlpTaskCard(UniversalTaskCard):
+ def __init__(self, task: YouTubeTask, parent=None):
+ super().__init__(task, parent)
+ self.selectFilesButton = None
+ if task.files and len(task.files) > 1:
+ self.selectFilesButton = ToolButton(FluentIcon.LIBRARY, self)
+ self.hBoxLayout.insertWidget(
+ self.hBoxLayout.indexOf(self.verifyHashButton),
+ self.selectFilesButton,
+ )
+ self.selectFilesButton.setToolTip(self.tr("选择视频"))
+ self.selectFilesButton.installEventFilter(ToolTipFilter(self.selectFilesButton))
+ self.selectFilesButton.clicked.connect(self._onSelectVideosClicked)
+
+ def _onSelectVideosClicked(self) -> None:
+ from app.services.task_service import taskService
+ dialog = VideoSelectDialog(self._task.files, self.window())
+ try:
+ if dialog.exec():
+ taskService.applySelection(self._task, dialog.selectedIndices())
+ self.refresh(force=True)
+ finally:
+ dialog.deleteLater()
+
def _buildProgressBar(self) -> QWidget:
bar = ProgressBar(self)
bar.setCustomBackgroundColor(QColor(0, 0, 0, 0), QColor(0, 0, 0, 0))