-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
223 lines (179 loc) · 7.85 KB
/
app.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
"""
Main application file.
"""
from functools import partial
from pathlib import Path
import pdfrw
from PySide6 import QtWidgets
from packages.ui.aesthetic import AestheticWindow
from packages.logic import toolkit
class MainWindow(AestheticWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("MetaPDFix")
self.setFixedSize(500, 550)
self.current_file = None
##################################################
# Layouts.
##################################################
self.main_layout = None
self.btn_layout = None
self.main_tg_layout = None
self.left_tg_layout = None
self.right_tg_layout = None
self.ui_manage_layouts()
##################################################
# Widgets.
##################################################
self.btn_open = None
self.btn_clear = None
self.btn_save = None
self.lbl_filename = None
self.lbl_title = None
self.lbl_author = None
self.lbl_creation_date = None
self.lbl_subject = None
self.lbl_modification_date = None
self.lbl_creator = None
self.lbl_producer = None
self.le_filename = None
self.le_title = None
self.le_author = None
self.le_creation_date = None
self.le_subject = None
self.le_modification_date = None
self.le_creator = None
self.le_producer = None
self.ui_manage_widgets()
##################################################
# Icons.
##################################################
self.ui_manage_icons()
##################################################
# Connections.
##################################################
self.logic_connect_widgets()
def ui_manage_icons(self) -> None:
"""Icons are managed here."""
self.setWindowIcon(self.icons.get("logo"))
self.btn_open.setIcon(self.icons.get("open"))
self.btn_clear.setIcon(self.icons.get("clear"))
self.btn_save.setIcon(self.icons.get("save"))
def ui_manage_layouts(self) -> None:
"""Layouts are managed here."""
self.main_layout = QtWidgets.QVBoxLayout(self)
self.btn_layout = QtWidgets.QHBoxLayout()
self.main_tg_layout = QtWidgets.QHBoxLayout()
self.left_tg_layout = QtWidgets.QVBoxLayout()
self.right_tg_layout = QtWidgets.QVBoxLayout()
self.main_layout.addLayout(self.btn_layout)
self.main_layout.addLayout(self.main_tg_layout)
self.main_tg_layout.addLayout(self.left_tg_layout)
self.main_tg_layout.addLayout(self.right_tg_layout)
def ui_manage_widgets(self) -> None:
"""Widgets are managed here."""
self.btn_open = QtWidgets.QPushButton("Open")
self.btn_clear = QtWidgets.QPushButton("Clear")
self.btn_save = QtWidgets.QPushButton("Save")
self.lbl_filename = QtWidgets.QLabel("Filename")
self.lbl_title = QtWidgets.QLabel("Title")
self.lbl_author = QtWidgets.QLabel("Author")
self.lbl_creation_date = QtWidgets.QLabel("Creation date")
self.lbl_subject = QtWidgets.QLabel("Subject")
self.lbl_modification_date = QtWidgets.QLabel("Modification date")
self.lbl_creator = QtWidgets.QLabel("Creator")
self.lbl_producer = QtWidgets.QLabel("Producer")
self.le_filename = QtWidgets.QLineEdit()
self.le_title = QtWidgets.QLineEdit()
self.le_title.tag = "Title"
self.le_author = QtWidgets.QLineEdit()
self.le_author.tag = "Author"
self.le_creation_date = QtWidgets.QLineEdit()
self.le_creation_date.tag = "CreationDate"
self.le_subject = QtWidgets.QLineEdit()
self.le_subject.tag = "Subject"
self.le_modification_date = QtWidgets.QLineEdit()
self.le_modification_date.tag = "ModDate"
self.le_creator = QtWidgets.QLineEdit()
self.le_creator.tag = "Creator"
self.le_producer = QtWidgets.QLineEdit()
self.le_producer.tag = "Producer"
self.btn_layout.addWidget(self.btn_open)
self.btn_layout.addWidget(self.btn_clear)
self.btn_layout.addWidget(self.btn_save)
self.left_tg_layout.addWidget(self.lbl_filename)
self.left_tg_layout.addWidget(self.le_filename)
self.left_tg_layout.addWidget(self.lbl_author)
self.left_tg_layout.addWidget(self.le_author)
self.left_tg_layout.addWidget(self.lbl_subject)
self.left_tg_layout.addWidget(self.le_subject)
self.left_tg_layout.addWidget(self.lbl_creator)
self.left_tg_layout.addWidget(self.le_creator)
self.right_tg_layout.addWidget(self.lbl_title)
self.right_tg_layout.addWidget(self.le_title)
self.right_tg_layout.addWidget(self.lbl_creation_date)
self.right_tg_layout.addWidget(self.le_creation_date)
self.right_tg_layout.addWidget(self.lbl_modification_date)
self.right_tg_layout.addWidget(self.le_modification_date)
self.right_tg_layout.addWidget(self.lbl_producer)
self.right_tg_layout.addWidget(self.le_producer)
def ui_update_tags(self, clear: bool = False) -> None:
"""Updates the fields with the metadata of the currently edited PDF.
Args:
clear (bool): Clear all fields if True.
"""
if self.current_file is None:
return
self.le_filename.setText(self.current_file.stem)
pdf: pdfrw.PdfReader = pdfrw.PdfReader(self.current_file)
for QLineEdit in self.fields:
QLineEdit.setText(getattr(pdf.Info, QLineEdit.tag))
if clear:
for QLineEdit in self.fields:
QLineEdit.clear()
def logic_connect_widgets(self) -> None:
"""Connections are managed here."""
self.btn_open.clicked.connect(self.logic_open_file)
self.btn_clear.clicked.connect(partial(self.ui_update_tags, True))
self.btn_save.clicked.connect(self.logic_save_file)
def logic_open_file(self) -> None:
"""Opens a file dialog to select a PDF file and updates the UI with the file's tags."""
caption: str = "Select PDF File"
file_filter: str = "PDF Files (*.pdf)"
file, _ = QtWidgets.QFileDialog.getOpenFileName(self, caption=caption, dir="", filter=file_filter)
if toolkit.check_file(file=file):
self.current_file = Path(file)
self.ui_update_tags()
else:
error_message: str = "File doesn't exist or isn't a PDF."
QtWidgets.QMessageBox.critical(self, "File Issue", error_message)
def logic_save_file(self) -> None:
"""Saves the current PDF file with updated metadata."""
if self.current_file is None:
return
new_location: tuple[Path, str] = (self.current_file, self.le_filename.text())
file: pdfrw.PdfReader = pdfrw.PdfReader(self.current_file)
metadata: dict = {QLineEdit.tag: QLineEdit.text() for QLineEdit in self.fields}
success: bool = toolkit.overwrite_metadata(new_location=new_location, file=file, metadata=metadata)
if success:
success_message: str = "Metadata has been successfully modified."
QtWidgets.QMessageBox.information(self, "Success", success_message)
else:
error_message: str = "Unable to overwrite metadata with the current values."
QtWidgets.QMessageBox.critical(self, "Error", error_message)
@property
def fields(self):
return {
self.le_title,
self.le_author,
self.le_creation_date,
self.le_subject,
self.le_modification_date,
self.le_creator,
self.le_producer
}
if __name__ == '__main__':
root = QtWidgets.QApplication()
application = MainWindow()
application.show()
root.exec()