forked from AugustusKaufmann/PDF-Editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialogs.py
297 lines (242 loc) · 12.2 KB
/
dialogs.py
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import os
import fitz # PyMuPDF
import logging
from PyQt6.QtWidgets import (QDialog, QVBoxLayout, QScrollArea, QWidget, QGridLayout, QLabel, QFrame, QDialogButtonBox, QRadioButton, QButtonGroup, QMessageBox)
from PyQt6.QtGui import QImage, QPixmap
from PyQt6.QtCore import Qt
from functools import partial
from widgets import DraggableLabel
class RearrangePagesDialog(QDialog):
def __init__(self, pdf_path, parent=None):
super().__init__(parent)
self.pdf_path = pdf_path
self.initUI()
def initUI(self):
try:
self.setWindowTitle('Rearrange Pages')
self.setGeometry(200, 200, 800, 600)
layout = QVBoxLayout(self)
self.setLayout(layout)
self.grid_layout = QGridLayout()
self.pages_widgets = []
pdf_document = fitz.open(self.pdf_path)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
pix = page.get_pixmap(matrix=fitz.Matrix(0.3, 0.3)) # Scale down content for better visibility
qimage = QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
page_label = DraggableLabel(dialog=self)
page_label.setPixmap(pixmap)
page_label.setObjectName(str(page_num))
page_label.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken)
page_label.setLineWidth(2)
page_label.setFixedSize(200, 260) # Increased size for better visibility
page_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Add the page label to a vertical layout with a label showing the page number
page_layout = QVBoxLayout()
page_layout.addWidget(page_label)
page_number_label = QLabel(f"Page {page_num + 1}")
page_number_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
page_layout.addWidget(page_number_label)
page_widget = QWidget()
page_widget.setLayout(page_layout)
page_widget.setObjectName(str(page_num))
self.pages_widgets.append(page_widget)
self.grid_layout.addWidget(page_widget, page_num // 4, page_num % 4)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_content = QWidget()
scroll_content.setLayout(self.grid_layout)
scroll_area.setWidget(scroll_content)
layout.addWidget(scroll_area)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
except Exception as e:
logging.error(f"Failed to initialize UI: {e}")
QMessageBox.critical(self, 'Error', f'Failed to initialize UI: {e}')
def get_new_order(self):
try:
return [int(self.grid_layout.itemAt(i).widget().objectName()) for i in range(self.grid_layout.count())]
except Exception as e:
logging.error(f"Failed to get new order: {e}")
QMessageBox.critical(self, 'Error', f'Failed to get new order: {e}')
def swapWidgets(self, source_index, target_index):
try:
if source_index != target_index:
widget = self.pages_widgets.pop(source_index)
self.pages_widgets.insert(target_index, widget)
# Clear the grid layout
for i in reversed(range(self.grid_layout.count())):
widget_to_remove = self.grid_layout.itemAt(i).widget()
self.grid_layout.removeWidget(widget_to_remove)
widget_to_remove.setParent(None)
# Rebuild the grid layout with the new order
for index, widget in enumerate(self.pages_widgets):
self.grid_layout.addWidget(widget, index // 4, index % 4)
except Exception as e:
logging.error(f"Failed to swap widgets: {e}")
QMessageBox.critical(self, 'Error', f'Failed to swap widgets: {e}')
class EncryptionOptionsDialog(QDialog):
def __init__(self, pdf_path, parent=None):
super().__init__(parent)
self.pdf_path = pdf_path
self.change_password_selected = False
self.decrypt_selected = False
self.initUI()
def initUI(self):
self.setWindowTitle('Encryption Options')
self.setGeometry(300, 300, 300, 200)
layout = QVBoxLayout(self)
self.change_password_button = QRadioButton("Change Password")
self.decrypt_button = QRadioButton("Decrypt PDF")
self.button_group = QButtonGroup(self)
self.button_group.addButton(self.change_password_button)
self.button_group.addButton(self.decrypt_button)
layout.addWidget(self.change_password_button)
layout.addWidget(self.decrypt_button)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
def accept(self):
if self.change_password_button.isChecked():
self.change_password_selected = True
elif self.decrypt_button.isChecked():
self.decrypt_selected = True
super().accept()
class MergePDFsDialog(QDialog):
def __init__(self, open_pdfs, parent=None):
super().__init__(parent)
self.open_pdfs = open_pdfs
self.selected_pdfs = []
self.initUI()
def initUI(self):
self.setWindowTitle('Merge PDFs')
self.setGeometry(200, 200, 800, 600)
layout = QVBoxLayout(self)
self.setLayout(layout)
self.grid_layout = QGridLayout()
self.preview_labels = []
for i, pdf_path in enumerate(self.open_pdfs):
pdf_document = fitz.open(pdf_path)
page = pdf_document.load_page(0)
pix = page.get_pixmap(matrix=fitz.Matrix(0.3, 0.3)) # Scale down content for better visibility
qimage = QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
preview_label = QLabel()
preview_label.setPixmap(pixmap)
preview_label.setObjectName(pdf_path)
preview_label.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken)
preview_label.setLineWidth(2)
preview_label.setFixedSize(200, 260)
preview_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
preview_label.mousePressEvent = partial(self.toggleSelection, pdf_path=pdf_path)
page_layout = QVBoxLayout()
page_layout.addWidget(preview_label)
page_number_label = QLabel(f"{i + 1}")
page_number_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
page_layout.addWidget(page_number_label)
page_widget = QWidget()
page_widget.setLayout(page_layout)
page_widget.setObjectName(str(i))
self.preview_labels.append(page_widget)
self.grid_layout.addWidget(page_widget, i // 4, i % 4)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_content = QWidget()
scroll_content.setLayout(self.grid_layout)
scroll_area.setWidget(scroll_content)
layout.addWidget(scroll_area)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
def toggleSelection(self, event, pdf_path):
if pdf_path in self.selected_pdfs:
self.selected_pdfs.remove(pdf_path)
else:
self.selected_pdfs.append(pdf_path)
self.updateSelections()
def updateSelections(self):
for i, pdf_path in enumerate(self.open_pdfs):
for label in self.preview_labels:
if label.objectName() == str(i):
preview_label = label.findChild(QLabel)
if pdf_path in self.selected_pdfs:
order = self.selected_pdfs.index(pdf_path) + 1
preview_label.setText(f"{order}")
else:
pix = fitz.open(pdf_path).load_page(0).get_pixmap(matrix=fitz.Matrix(0.3, 0.3))
qimage = QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
preview_label.setPixmap(pixmap)
def get_selected_pdfs(self):
return self.selected_pdfs
class SplitPDFDialog(QDialog):
def __init__(self, pdf_path, parent=None):
super().__init__(parent)
self.pdf_path = pdf_path
self.selected_pages = []
self.initUI()
def initUI(self):
self.setWindowTitle('Split PDF')
self.setGeometry(200, 200, 800, 600)
layout = QVBoxLayout(self)
self.setLayout(layout)
self.grid_layout = QGridLayout()
self.page_labels = []
pdf_document = fitz.open(self.pdf_path)
for page_num in range(len(pdf_document)):
page = pdf_document.load_page(page_num)
pix = page.get_pixmap(matrix=fitz.Matrix(0.3, 0.3)) # Scale down content for better visibility
qimage = QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
page_label = QLabel()
page_label.setPixmap(pixmap)
page_label.setObjectName(str(page_num))
page_label.setFrameStyle(QFrame.Shape.Panel | QFrame.Shadow.Sunken)
page_label.setLineWidth(2)
page_label.setFixedSize(200, 260)
page_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
page_label.mousePressEvent = partial(self.toggleSelection, page_num=page_num)
page_layout = QVBoxLayout()
page_layout.addWidget(page_label)
page_number_label = QLabel(f"Page {page_num + 1}")
page_number_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
page_layout.addWidget(page_number_label)
page_widget = QWidget()
page_widget.setLayout(page_layout)
page_widget.setObjectName(str(page_num))
self.page_labels.append(page_widget)
self.grid_layout.addWidget(page_widget, page_num // 4, page_num % 4)
scroll_area = QScrollArea()
scroll_area.setWidgetResizable(True)
scroll_content = QWidget()
scroll_content.setLayout(self.grid_layout)
scroll_area.setWidget(scroll_content)
layout.addWidget(scroll_area)
button_box = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
button_box.accepted.connect(self.accept)
button_box.rejected.connect(self.reject)
layout.addWidget(button_box)
def toggleSelection(self, event, page_num):
if page_num in self.selected_pages:
self.selected_pages.remove(page_num)
else:
self.selected_pages.append(page_num)
self.updateSelections()
def updateSelections(self):
for i, page_widget in enumerate(self.page_labels):
page_label = page_widget.findChild(QLabel)
if int(page_widget.objectName()) in self.selected_pages:
order = self.selected_pages.index(int(page_widget.objectName())) + 1
page_label.setText(f"{order}")
else:
pix = fitz.open(self.pdf_path).load_page(int(page_widget.objectName())).get_pixmap(matrix=fitz.Matrix(0.3, 0.3))
qimage = QImage(pix.samples, pix.width, pix.height, pix.stride, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimage)
page_label.setPixmap(pixmap)
def get_selected_pages(self):
return self.selected_pages