-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkspace.py
More file actions
77 lines (64 loc) · 2.45 KB
/
workspace.py
File metadata and controls
77 lines (64 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import fitz # PyMuPDF
from PySide6.QtCore import Qt
from PySide6.QtGui import QImage, QPixmap
class Workspace:
def __init__(self):
self.pdf_files = [] # 存储PDF文件列表
def add_pdf(self, file_path):
pdf_file = PDFFile(file_path)
pdf_file.load_pages()
self.pdf_files.append(pdf_file)
def clear(self):
self.pdf_files = [] # 清空工作区中的PDF文件
def remove_pdf_by_name(self, pdf_name):
"""
根据PDF文件名删除工作区中的PDF文件及其页面。
Args:
pdf_name (str): 要删除的PDF文件名。
"""
self.pdf_files = [
pdf for pdf in self.pdf_files if not pdf.file_path.endswith(pdf_name)
]
def update(self):
"""
更新工作区,检查每个PDF文件的页面数量。
如果某个PDF文件的页面数量为0,则将其从工作区中移除。
"""
self.pdf_files = [pdf for pdf in self.pdf_files if pdf.pages]
class PDFFile:
def __init__(self, file_path):
self.file_path = file_path
self.pages = []
def load_pages(self):
doc = fitz.open(self.file_path)
for i in range(doc.page_count):
page = doc.load_page(i)
pix = page.get_pixmap(alpha=False)
image = QImage(
pix.samples, pix.width, pix.height, pix.stride, QImage.Format_RGB888
)
thumbnail = QPixmap.fromImage(image).scaled(
180, 240, Qt.KeepAspectRatio, Qt.SmoothTransformation
)
self.pages.append(PDFPage(i + 1, thumbnail, self.file_path))
class PDFPage:
def __init__(self, page_number, thumbnail, parent_pdf_path):
self.page_number = page_number
self.thumbnail = thumbnail
self.parent_pdf_path = parent_pdf_path
self.rotation = 0
def update_thumbnail(self):
"""
根据当前旋转角度重新生成缩略图。
"""
doc = fitz.open(self.parent_pdf_path)
page = doc.load_page(self.page_number - 1)
mat = fitz.Matrix(1, 1).prerotate(self.rotation) # 修正方法名为prerotate
pix = page.get_pixmap(matrix=mat, alpha=False)
image = QImage(
pix.samples, pix.width, pix.height, pix.stride, QImage.Format_RGB888
)
self.thumbnail = QPixmap.fromImage(image).scaled(
180, 240, Qt.KeepAspectRatio, Qt.SmoothTransformation
)
doc.close()