-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgui.py
282 lines (222 loc) · 9.59 KB
/
gui.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
#!/usr/bin/env python
# encoding=utf-8
"""
this gui based on PySide (QT for Python) and used the example - findfiles.pyw as template.
see for details: https://wiki.qt.io/Qt_for_Python
"""
import os
import sys
import traceback
from PySide6 import QtCore, QtWidgets
from PySide6.QtCore import Qt
from pdf_white_cut import worker
from pdf_white_cut.logger import logger
logger.warning("GUI is not stable enough, recommand to use cli.")
class Window(QtWidgets.QDialog):
def __init__(self):
super(Window, self).__init__()
browseButton = self.createButton("&Browse...", self.browseInDir)
browse2Button = self.createButton("&Browse...", self.browseOutDir)
findButton = self.createButton("&Find PDF", self.find)
actionButton = self.createButton("&Cut White", self.doAction)
allButton = self.createButton("&Select All", self.selectAll)
unallButton = self.createButton("&Unselect All", self.unselectAll)
self.fileComboBox = self.createComboBox("*.pdf")
self.textComboBox = self.createComboBox()
self.directoryComboBox = self.createComboBox(QtCore.QDir.currentPath())
self.directory2ComboBox = self.createComboBox(
os.path.join(QtCore.QDir.currentPath(), "cases/output")
)
fileLabel = QtWidgets.QLabel("Named:")
directoryLabel = QtWidgets.QLabel("Input directory:")
directory2Label = QtWidgets.QLabel("Output directory:")
self.filesFoundLabel = QtWidgets.QLabel()
self.createFilesTable()
buttonsLayout = QtWidgets.QHBoxLayout()
buttonsLayout.addStretch()
buttonsLayout.addWidget(findButton)
buttonsLayout.addWidget(actionButton)
checkButtonLayout = QtWidgets.QHBoxLayout()
checkButtonLayout.addStretch()
checkButtonLayout.addWidget(allButton)
checkButtonLayout.addWidget(unallButton)
mainLayout = QtWidgets.QGridLayout()
mainLayout.addWidget(fileLabel, 0, 0)
mainLayout.addWidget(self.fileComboBox, 0, 1, 1, 2)
mainLayout.addWidget(directoryLabel, 2, 0)
mainLayout.addWidget(self.directoryComboBox, 2, 1)
mainLayout.addWidget(browseButton, 2, 2)
mainLayout.addWidget(directory2Label, 3, 0)
mainLayout.addWidget(self.directory2ComboBox, 3, 1)
mainLayout.addWidget(browse2Button, 3, 2)
mainLayout.addWidget(self.filesTable, 4, 0, 1, 3)
mainLayout.addWidget(self.filesFoundLabel, 5, 0, 1, 3)
mainLayout.addLayout(checkButtonLayout, 6, 0)
mainLayout.addLayout(buttonsLayout, 6, 1, 1, 2)
self.setLayout(mainLayout)
self.setWindowTitle("PDF Cut White")
self.resize(700, 500)
def browseInDir(self):
directory = QtWidgets.QFileDialog.getExistingDirectory(
self, "Input Dir", QtCore.QDir.currentPath()
)
if directory:
if self.directoryComboBox.findText(directory) == -1:
self.directoryComboBox.addItem(directory)
self.directoryComboBox.setCurrentIndex(
self.directoryComboBox.findText(directory)
)
def browseOutDir(self):
directory = QtWidgets.QFileDialog.getExistingDirectory(
self, "Output Dir", QtCore.QDir.currentPath()
)
if directory:
if self.directory2ComboBox.findText(directory) == -1:
self.directory2ComboBox.addItem(directory)
self.directory2ComboBox.setCurrentIndex(
self.directory2ComboBox.findText(directory)
)
@staticmethod
def updateComboBox(comboBox):
if comboBox.findText(comboBox.currentText()) == -1:
comboBox.addItem(comboBox.currentText())
def setCheck(self, flag):
cnt = self.filesTable.rowCount()
for row in range(cnt):
checkitem = self.filesTable.item(row, 0)
if checkitem.checkState() == QtCore.Qt.Unchecked:
checkitem.setCheckState(flag)
def selectAll(self):
self.setCheck(QtCore.Qt.Checked)
def unselectAll(self):
self.setCheck(QtCore.Qt.UnChecked)
def doAction(self):
"""
do action to cut the white and output new pdf
"""
indir = self.directoryComboBox.currentText()
outdir = self.directory2ComboBox.currentText()
success = True
msg = ""
cnt = self.filesTable.rowCount()
for row in range(cnt):
checkitem = self.filesTable.item(row, 0)
if checkitem.checkState() == QtCore.Qt.Unchecked:
continue
name = self.filesTable.item(row, 1).text()
ignore = QtWidgets.QSpinBox(self.filesTable.cellWidget(row, 3)).value()
# qstring to string
input = os.path.join(indir, name)
output = os.path.join(outdir, name)
try:
worker.cut_pdf(str(input), str(output), ignore=int(ignore))
except Exception as e:
print("error while cut white")
traceback.print_exc()
msg = traceback.format_exc()
success = False
break
if success:
QtWidgets.QMessageBox.information(
self, "Info", "Completed!", QtWidgets.QMessageBox.Ok
)
else:
QtWidgets.QMessageBox.warning(
self,
"Error",
"Error while process: \n%s" % (msg),
QtWidgets.QMessageBox.Ok,
)
# cutwhite.batch_action(indir,outdir)
def find(self):
self.filesTable.setRowCount(0)
fileName = self.fileComboBox.currentText()
text = self.textComboBox.currentText()
path = self.directoryComboBox.currentText()
self.updateComboBox(self.fileComboBox)
self.updateComboBox(self.textComboBox)
self.updateComboBox(self.directoryComboBox)
self.currentDir = QtCore.QDir(path)
if not fileName:
fileName = "*"
files = self.currentDir.entryList(
[fileName], QtCore.QDir.Files | QtCore.QDir.NoSymLinks
)
if text:
files = self.findFiles(files, text)
self.showFiles(files)
def findFiles(self, files, text):
progressDialog = QtWidgets.QProgressDialog(self)
progressDialog.setCancelButtonText("&Cancel")
progressDialog.setRange(0, files.count())
progressDialog.setWindowTitle("Find Files")
foundFiles = []
for i in range(files.count()):
progressDialog.setValue(i)
progressDialog.setLabelText(
"Searching file number %d of %d..." % (i, files.count())
)
QtWidgets.qApp.processEvents()
if progressDialog.wasCanceled():
break
inFile = QtCore.QFile(self.currentDir.absoluteFilePath(files[i]))
progressDialog.close()
return foundFiles
def showFiles(self, files):
for fn in files:
file = QtCore.QFile(self.currentDir.absoluteFilePath(fn))
size = QtCore.QFileInfo(file).size()
fileNameItem = QtWidgets.QTableWidgetItem(fn)
fileNameItem.setFlags(fileNameItem.flags() ^ QtCore.Qt.ItemIsEditable)
sizeItem = QtWidgets.QTableWidgetItem("%d KB" % (int((size + 1023) / 1024)))
sizeItem.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignRight)
sizeItem.setFlags(sizeItem.flags() ^ QtCore.Qt.ItemIsEditable)
# a check item to choose the spec files
checkItem = QtWidgets.QTableWidgetItem()
checkItem.setCheckState(QtCore.Qt.Checked)
checkItem.setTextAlignment(QtCore.Qt.AlignVCenter | QtCore.Qt.AlignHCenter)
# new ignore item
ignoreItem = QtWidgets.QSpinBox()
ignoreItem.setValue(0)
ignoreItem.stepType
# ignoreItem.setFlags(ignoreItem.flags() | QtCore.Qt.ItemIsEditable)
row = self.filesTable.rowCount()
self.filesTable.insertRow(row)
self.filesTable.setItem(row, 0, checkItem)
self.filesTable.setItem(row, 1, fileNameItem)
self.filesTable.setItem(row, 2, sizeItem)
self.filesTable.setCellWidget(row, 3, ignoreItem) # Added new column item
self.filesFoundLabel.setText(
"%d file(s) found (Double click on a file to open it)" % len(files)
)
def createButton(self, text, member):
button = QtWidgets.QPushButton(text)
button.clicked.connect(member)
return button
def createComboBox(self, text=""):
comboBox = QtWidgets.QComboBox()
comboBox.setEditable(True)
comboBox.addItem(text)
comboBox.setSizePolicy(
QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Preferred
)
return comboBox
def createFilesTable(self):
self.filesTable = QtWidgets.QTableWidget(0, 4) # Updated column count to 4
self.filesTable.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows)
self.filesTable.setHorizontalHeaderLabels(
("Choose", "File Name", "Size", "Ignore")
) # Added new column header
# self.filesTable.verticalHeader().hide()
self.filesTable.setShowGrid(False)
self.filesTable.cellActivated.connect(self.openFileOfItem)
def openFileOfItem(self, row, column):
item = self.filesTable.item(row, 0)
QtWidgets.QDesktopServices.openUrl(
QtCore.QUrl(self.currentDir.absoluteFilePath(item.text()))
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())