2026-06-29 16:30:27 +08:00
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
2026-07-01 22:29:33 +08:00
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
import fitz
|
|
|
|
|
|
from PySide6.QtCore import QSize, Qt, Signal
|
|
|
|
|
|
from PySide6.QtGui import QDragEnterEvent, QDropEvent, QIcon, QImage, QPixmap
|
2026-06-29 16:30:27 +08:00
|
|
|
|
from PySide6.QtWidgets import (
|
|
|
|
|
|
QAbstractItemView,
|
|
|
|
|
|
QDialog,
|
|
|
|
|
|
QDialogButtonBox,
|
2026-07-01 22:29:33 +08:00
|
|
|
|
QFileDialog,
|
|
|
|
|
|
QHBoxLayout,
|
2026-06-29 16:30:27 +08:00
|
|
|
|
QLabel,
|
|
|
|
|
|
QListWidget,
|
|
|
|
|
|
QListWidgetItem,
|
2026-07-01 22:29:33 +08:00
|
|
|
|
QMessageBox,
|
|
|
|
|
|
QPushButton,
|
2026-06-29 16:30:27 +08:00
|
|
|
|
QVBoxLayout,
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
from pdfreaderlite.core.document import PdfDocument
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PageGrid(QListWidget):
|
2026-07-01 22:29:33 +08:00
|
|
|
|
pdf_files_dropped = Signal(list, int)
|
|
|
|
|
|
|
2026-06-29 16:30:27 +08:00
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
self.setViewMode(QListWidget.ViewMode.IconMode)
|
|
|
|
|
|
self.setMovement(QListWidget.Movement.Snap)
|
|
|
|
|
|
self.setResizeMode(QListWidget.ResizeMode.Adjust)
|
|
|
|
|
|
self.setFlow(QListWidget.Flow.LeftToRight)
|
|
|
|
|
|
self.setWrapping(True)
|
|
|
|
|
|
self.setSpacing(12)
|
|
|
|
|
|
self.setIconSize(QSize(140, 190))
|
|
|
|
|
|
self.setGridSize(QSize(178, 230))
|
|
|
|
|
|
self.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
|
|
|
|
|
|
self.setDefaultDropAction(Qt.DropAction.MoveAction)
|
2026-07-01 22:29:33 +08:00
|
|
|
|
self.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
|
2026-06-29 16:30:27 +08:00
|
|
|
|
|
|
|
|
|
|
def dropEvent(self, event: QDropEvent) -> None:
|
2026-07-01 22:29:33 +08:00
|
|
|
|
paths = self._dropped_pdf_paths(event)
|
|
|
|
|
|
if paths:
|
|
|
|
|
|
row = self.indexAt(event.position().toPoint()).row()
|
|
|
|
|
|
self.pdf_files_dropped.emit(paths, row if row >= 0 else self.count())
|
|
|
|
|
|
event.acceptProposedAction()
|
|
|
|
|
|
return
|
2026-06-29 16:30:27 +08:00
|
|
|
|
super().dropEvent(event)
|
|
|
|
|
|
self._renumber_items()
|
|
|
|
|
|
|
|
|
|
|
|
def dragEnterEvent(self, event: QDragEnterEvent) -> None:
|
2026-07-01 22:29:33 +08:00
|
|
|
|
if self._dropped_pdf_paths(event):
|
|
|
|
|
|
event.acceptProposedAction()
|
|
|
|
|
|
return
|
2026-06-29 16:30:27 +08:00
|
|
|
|
event.acceptProposedAction()
|
|
|
|
|
|
|
2026-07-01 22:29:33 +08:00
|
|
|
|
def dragMoveEvent(self, event) -> None:
|
|
|
|
|
|
if self._dropped_pdf_paths(event):
|
|
|
|
|
|
event.acceptProposedAction()
|
|
|
|
|
|
return
|
|
|
|
|
|
super().dragMoveEvent(event)
|
|
|
|
|
|
|
|
|
|
|
|
def _dropped_pdf_paths(self, event: QDragEnterEvent | QDropEvent) -> list[str]:
|
|
|
|
|
|
if not event.mimeData().hasUrls():
|
|
|
|
|
|
return []
|
|
|
|
|
|
paths: list[str] = []
|
|
|
|
|
|
for url in event.mimeData().urls():
|
|
|
|
|
|
if not url.isLocalFile():
|
|
|
|
|
|
continue
|
|
|
|
|
|
path = Path(url.toLocalFile())
|
|
|
|
|
|
if path.suffix.lower() == ".pdf":
|
|
|
|
|
|
paths.append(str(path))
|
|
|
|
|
|
return paths
|
|
|
|
|
|
|
2026-06-29 16:30:27 +08:00
|
|
|
|
def _renumber_items(self) -> None:
|
|
|
|
|
|
for row in range(self.count()):
|
|
|
|
|
|
item = self.item(row)
|
2026-07-01 22:29:33 +08:00
|
|
|
|
entry = item.data(Qt.ItemDataRole.UserRole)
|
|
|
|
|
|
if isinstance(entry, dict) and entry.get("source") == "external":
|
|
|
|
|
|
item.setText(
|
|
|
|
|
|
f"位置 {row + 1}\n插入 {Path(str(entry['path'])).name}\n第 {int(entry['page_index']) + 1} 页"
|
|
|
|
|
|
)
|
|
|
|
|
|
else:
|
|
|
|
|
|
original_page = int(entry) + 1
|
|
|
|
|
|
item.setText(f"位置 {row + 1}\n原第 {original_page} 页")
|
2026-06-29 16:30:27 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PageManagerDialog(QDialog):
|
|
|
|
|
|
def __init__(self, document: PdfDocument, parent=None) -> None:
|
|
|
|
|
|
super().__init__(parent)
|
|
|
|
|
|
self.document = document
|
2026-07-01 22:29:33 +08:00
|
|
|
|
self.rotations: dict[int, int] = {}
|
2026-06-29 16:30:27 +08:00
|
|
|
|
self.setWindowTitle("页面管理")
|
|
|
|
|
|
self.resize(920, 680)
|
|
|
|
|
|
|
|
|
|
|
|
self.grid = PageGrid()
|
2026-07-01 22:29:33 +08:00
|
|
|
|
self.grid.pdf_files_dropped.connect(self.insert_external_pdfs)
|
|
|
|
|
|
hint = QLabel("拖拽缩略图调整页面顺序,可多选后批量删除、旋转或提取,点击“应用”后写入当前 PDF。")
|
2026-06-29 16:30:27 +08:00
|
|
|
|
hint.setWordWrap(True)
|
2026-07-01 22:29:33 +08:00
|
|
|
|
delete_button = QPushButton("删除所选")
|
|
|
|
|
|
rotate_left_button = QPushButton("左旋所选")
|
|
|
|
|
|
rotate_right_button = QPushButton("右旋所选")
|
|
|
|
|
|
extract_button = QPushButton("提取所选")
|
|
|
|
|
|
delete_button.clicked.connect(self.delete_selected_pages)
|
|
|
|
|
|
rotate_left_button.clicked.connect(lambda: self.rotate_selected_pages(-90))
|
|
|
|
|
|
rotate_right_button.clicked.connect(lambda: self.rotate_selected_pages(90))
|
|
|
|
|
|
extract_button.clicked.connect(self.extract_selected_pages)
|
|
|
|
|
|
|
|
|
|
|
|
action_layout = QHBoxLayout()
|
|
|
|
|
|
action_layout.addWidget(delete_button)
|
|
|
|
|
|
action_layout.addWidget(rotate_left_button)
|
|
|
|
|
|
action_layout.addWidget(rotate_right_button)
|
|
|
|
|
|
action_layout.addWidget(extract_button)
|
|
|
|
|
|
action_layout.addStretch(1)
|
2026-06-29 16:30:27 +08:00
|
|
|
|
|
|
|
|
|
|
buttons = QDialogButtonBox(
|
|
|
|
|
|
QDialogButtonBox.StandardButton.Apply | QDialogButtonBox.StandardButton.Cancel
|
|
|
|
|
|
)
|
|
|
|
|
|
buttons.button(QDialogButtonBox.StandardButton.Apply).setText("应用")
|
|
|
|
|
|
buttons.rejected.connect(self.reject)
|
|
|
|
|
|
buttons.button(QDialogButtonBox.StandardButton.Apply).clicked.connect(self.accept)
|
|
|
|
|
|
|
|
|
|
|
|
layout = QVBoxLayout(self)
|
|
|
|
|
|
layout.addWidget(hint)
|
2026-07-01 22:29:33 +08:00
|
|
|
|
layout.addLayout(action_layout)
|
2026-06-29 16:30:27 +08:00
|
|
|
|
layout.addWidget(self.grid, 1)
|
|
|
|
|
|
layout.addWidget(buttons)
|
|
|
|
|
|
|
|
|
|
|
|
self._load_pages()
|
|
|
|
|
|
|
|
|
|
|
|
def page_order(self) -> list[int]:
|
|
|
|
|
|
return [
|
|
|
|
|
|
self.grid.item(row).data(Qt.ItemDataRole.UserRole)
|
|
|
|
|
|
for row in range(self.grid.count())
|
2026-07-01 22:29:33 +08:00
|
|
|
|
if isinstance(self.grid.item(row).data(Qt.ItemDataRole.UserRole), int)
|
2026-06-29 16:30:27 +08:00
|
|
|
|
]
|
|
|
|
|
|
|
2026-07-01 22:29:33 +08:00
|
|
|
|
def page_plan(self) -> list[dict[str, object]]:
|
|
|
|
|
|
plan: list[dict[str, object]] = []
|
|
|
|
|
|
for row in range(self.grid.count()):
|
|
|
|
|
|
entry = self.grid.item(row).data(Qt.ItemDataRole.UserRole)
|
|
|
|
|
|
if isinstance(entry, dict):
|
|
|
|
|
|
plan.append(entry)
|
|
|
|
|
|
else:
|
|
|
|
|
|
plan.append({"source": "current", "page_index": int(entry)})
|
|
|
|
|
|
return plan
|
|
|
|
|
|
|
|
|
|
|
|
def selected_pages(self) -> list[int]:
|
|
|
|
|
|
pages = [
|
|
|
|
|
|
item.data(Qt.ItemDataRole.UserRole)
|
|
|
|
|
|
for item in self.grid.selectedItems()
|
|
|
|
|
|
if isinstance(item.data(Qt.ItemDataRole.UserRole), int)
|
|
|
|
|
|
]
|
|
|
|
|
|
return sorted(set(pages))
|
|
|
|
|
|
|
|
|
|
|
|
def delete_selected_pages(self) -> None:
|
|
|
|
|
|
selected_items = self.grid.selectedItems()
|
|
|
|
|
|
if not selected_items:
|
|
|
|
|
|
return
|
|
|
|
|
|
if self.grid.count() - len(selected_items) <= 0:
|
|
|
|
|
|
QMessageBox.information(self, "删除页面", "PDF 至少需要保留一页。")
|
|
|
|
|
|
return
|
|
|
|
|
|
reply = QMessageBox.question(
|
|
|
|
|
|
self,
|
|
|
|
|
|
"删除页面",
|
|
|
|
|
|
f"确定从当前页面管理列表中删除 {len(selected_items)} 页吗?",
|
|
|
|
|
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No,
|
|
|
|
|
|
)
|
|
|
|
|
|
if reply != QMessageBox.StandardButton.Yes:
|
|
|
|
|
|
return
|
|
|
|
|
|
for item in selected_items:
|
|
|
|
|
|
self.grid.takeItem(self.grid.row(item))
|
|
|
|
|
|
self.grid._renumber_items()
|
|
|
|
|
|
|
|
|
|
|
|
def insert_external_pdfs(self, paths: list[str], insert_row: int) -> None:
|
|
|
|
|
|
row = max(0, min(insert_row, self.grid.count()))
|
|
|
|
|
|
inserted = 0
|
|
|
|
|
|
for path in paths:
|
|
|
|
|
|
try:
|
|
|
|
|
|
with fitz.open(path) as source:
|
|
|
|
|
|
for page_index in range(source.page_count):
|
|
|
|
|
|
item = self._external_page_item(path, source, page_index)
|
|
|
|
|
|
self.grid.insertItem(row + inserted, item)
|
|
|
|
|
|
inserted += 1
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
QMessageBox.warning(self, "插入 PDF", f"无法读取外部 PDF:\n{path}\n\n{exc}")
|
|
|
|
|
|
if inserted:
|
|
|
|
|
|
self.grid._renumber_items()
|
|
|
|
|
|
|
|
|
|
|
|
def rotate_selected_pages(self, degrees: int) -> None:
|
|
|
|
|
|
pages = self.selected_pages()
|
|
|
|
|
|
if not pages:
|
|
|
|
|
|
return
|
|
|
|
|
|
for page_index in pages:
|
|
|
|
|
|
self.rotations[page_index] = (self.rotations.get(page_index, 0) + degrees) % 360
|
|
|
|
|
|
self._renumber_with_rotation()
|
|
|
|
|
|
|
|
|
|
|
|
def extract_selected_pages(self) -> None:
|
|
|
|
|
|
pages = self.selected_pages()
|
|
|
|
|
|
if not pages:
|
|
|
|
|
|
return
|
|
|
|
|
|
target, _ = QFileDialog.getSaveFileName(
|
|
|
|
|
|
self,
|
|
|
|
|
|
"提取所选页面",
|
|
|
|
|
|
"PDFReaderLite_提取页面.pdf",
|
|
|
|
|
|
"PDF 文件 (*.pdf)",
|
|
|
|
|
|
)
|
|
|
|
|
|
if not target:
|
|
|
|
|
|
return
|
|
|
|
|
|
try:
|
|
|
|
|
|
self.document.extract_pages([page + 1 for page in pages], target)
|
|
|
|
|
|
QMessageBox.information(self, "提取页面", "所选页面已提取为新 PDF。")
|
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
|
QMessageBox.critical(self, "提取失败", f"无法提取所选页面:\n{exc}")
|
|
|
|
|
|
|
|
|
|
|
|
def _renumber_with_rotation(self) -> None:
|
|
|
|
|
|
self.grid._renumber_items()
|
|
|
|
|
|
for row in range(self.grid.count()):
|
|
|
|
|
|
item = self.grid.item(row)
|
|
|
|
|
|
original_page = item.data(Qt.ItemDataRole.UserRole)
|
|
|
|
|
|
if not isinstance(original_page, int):
|
|
|
|
|
|
continue
|
|
|
|
|
|
rotation = self.rotations.get(original_page, 0)
|
|
|
|
|
|
if rotation:
|
|
|
|
|
|
item.setText(f"{item.text()}\n旋转 {rotation}°")
|
|
|
|
|
|
|
2026-06-29 16:30:27 +08:00
|
|
|
|
def _load_pages(self) -> None:
|
|
|
|
|
|
for page_index in range(self.document.page_count):
|
|
|
|
|
|
image = self.document.render_thumbnail(page_index, width=150)
|
|
|
|
|
|
item = QListWidgetItem(
|
|
|
|
|
|
QIcon(QPixmap.fromImage(image)),
|
|
|
|
|
|
f"位置 {page_index + 1}\n原第 {page_index + 1} 页",
|
|
|
|
|
|
)
|
|
|
|
|
|
item.setData(Qt.ItemDataRole.UserRole, page_index)
|
|
|
|
|
|
item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
|
|
|
|
self.grid.addItem(item)
|
2026-07-01 22:29:33 +08:00
|
|
|
|
|
|
|
|
|
|
def _external_page_item(self, path: str, source: fitz.Document, page_index: int) -> QListWidgetItem:
|
|
|
|
|
|
page = source.load_page(page_index)
|
|
|
|
|
|
zoom = 150 / page.rect.width
|
|
|
|
|
|
pixmap = page.get_pixmap(matrix=fitz.Matrix(zoom, zoom), alpha=False)
|
|
|
|
|
|
image = QImage(
|
|
|
|
|
|
pixmap.samples,
|
|
|
|
|
|
pixmap.width,
|
|
|
|
|
|
pixmap.height,
|
|
|
|
|
|
pixmap.stride,
|
|
|
|
|
|
QImage.Format.Format_RGB888,
|
|
|
|
|
|
).copy()
|
|
|
|
|
|
item = QListWidgetItem(
|
|
|
|
|
|
QIcon(QPixmap.fromImage(image)),
|
|
|
|
|
|
f"插入 {Path(path).name}\n第 {page_index + 1} 页",
|
|
|
|
|
|
)
|
|
|
|
|
|
item.setData(
|
|
|
|
|
|
Qt.ItemDataRole.UserRole,
|
|
|
|
|
|
{"source": "external", "path": path, "page_index": page_index},
|
|
|
|
|
|
)
|
|
|
|
|
|
item.setTextAlignment(Qt.AlignmentFlag.AlignCenter)
|
|
|
|
|
|
return item
|