/
sign-pdf-gui.py
1188 lines (874 loc) · 36.8 KB
/
sign-pdf-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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
import time
from subprocess import Popen, PIPE
import shlex
import os
import re
from glob import glob
import signal
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import (QApplication, QWidget, QMainWindow,
QInputDialog, QFileDialog, QVBoxLayout, QHBoxLayout, QPushButton,
QTableWidget, QTableWidgetItem, QCheckBox, QLabel, QDialog, QLineEdit,
QDialogButtonBox, QSpinBox, QComboBox, QMessageBox, QFormLayout,
QRadioButton, QStatusBar, QProgressBar, QFrame, QColorDialog,
QDoubleSpinBox, QStyle, QPlainTextEdit, QGridLayout)
from PyQt5.QtCore import Qt
from threading import Thread
from PyQt5.QtGui import QPixmap, QIcon, QColor, QPalette
class ImageOptions:
def __init__(self, info={}):
"""Optionally pass in an initial dictionary of items"""
if type(info) != type({}):
raise TypeError("ImageOptions requires a dictionary, but was given %s" % type(info))
self.info = info
return
def add_info(self, info_dict):
"""Add information via a dictionary"""
if type(info_dict) != type({}):
raise TypeError("Add_info requires a dictionary, but was given %s"
% type(info_dict))
for key in info_dict.keys():
self.info[key] = info_dict[key]
def has_info(self, key):
"""Check whether the required key is present or not"""
try:
value = self.info[key]
return True
except KeyError:
return False
def update_info(self, key, value):
""" Update the dictionary as long as the key already exits."""
if self.has_info(key):
self.info[key] = value
return True
else:
return False
def print_info(self, key):
"""Return the info corresponding to key"""
return self.info[key]
def get_dict(self):
return self.info
# https://gist.github.com/onlyjus/d7f99fb16e9e904e3ba0
class ProgressDialog(QDialog):
def __init__(self):
super(ProgressDialog,self).__init__()
self.setWindowTitle('Sign PDF GUI')
vbox = QVBoxLayout()
self.setLayout(vbox)
self.label = QLabel()
vbox.addWidget(self.label)
self.bar = QProgressBar()
vbox.addWidget(self.bar)
self.btns = QDialogButtonBox()
self.btns.setStandardButtons(QDialogButtonBox.Cancel |
QDialogButtonBox.Ok)
vbox.addWidget(self.btns)
# The Ok button is initially disabled
self.btns.button(QDialogButtonBox.Ok).setEnabled(False)
self.btns.accepted.connect(self.accept)
self.btns.rejected.connect(self.reject)
def setRange(self, min_, max_):
self.bar.setRange(min_, max_)
def setMinimum(self,value):
self.bar.setMinimum(value)
def setMaximum(self,value):
self.bar.setMaximum(value)
def setValue(self,value):
self.bar.setValue(value)
def setFormat(self,string):
self.bar.setFormat(string)
# Thread to do the work
class Worker:
def __init__(self, instance):
self.keepRunning = True
self.instance = instance
def terminate(self):
self.keepRunning = False
try:
os.kill(self.proc.pid, signal.SIGTERM)
except ProcessLookupError:
return
def run(self):
cmd = self.instance.cliApp
if self.instance.showGrid.isChecked():
cmd = cmd + ' -g '
if self.instance.otherConfigs['multiple']:
cmd = cmd + ' -m '
cmd = cmd + ' -t ' + str(self.instance.otherConfigs['threshold'])
cmd = cmd + ' -m ' + self.instance.otherConfigs['mask']
# m = List of image
m = len(self.instance.listImage)
for j in range(0,m):
imgDict = self.instance.listImage[j].get_dict()
imageOptions = '"' + imgDict['image'] + '"' + ','
if imgDict['type'] == 'template':
imageOptions = imageOptions + imgDict['template'] + ','
imageOptions = (imageOptions + imgDict['xvalue'] + ','
+ imgDict['yvalue'] + ',' + str(imgDict['scale']) + ','
+ str(imgDict['page']) + ',' + str(imgDict['rotation']))
cmd = cmd + ' -i ' + imageOptions
# n = List of pdf files
n = len(self.instance.listPdf)
self.instance.statusBar.showMessage("Starting...")
i = 0
while self.keepRunning and i < n:
ifileBasename = os.path.basename(self.instance.listPdf[i])
self.instance.pd.label.setText('Working on "'
+ ifileBasename + '"')
cmdFinal = cmd + ' "' + self.instance.listPdf[i] + '"'
if self.instance.printCmd.isChecked():
print(cmdFinal)
self.proc = Popen(shlex.split(cmdFinal), stdout=PIPE, stderr=PIPE,
shell=False)
stdout, stderr = self.proc.communicate()
rtn = self.proc.returncode
if rtn != 0:
self.instance.statusBar.showMessage(
"Problem in running code or canceled.")
return rtn
self.instance.pd.setValue(i+1)
# If reached the end of the bar, then enable the Ok button
if i+1 == n:
self.instance.pd.btns.button(
QDialogButtonBox.Ok).setEnabled(True)
i += 1
self.instance.statusBar.showMessage("Done!")
return
# File Dialog with preview for images selected
# https://stackoverflow.com/questions/47599170/qfiledialog-preview
class QFileDialogPreview(QFileDialog):
def __init__(self, *args, **kwargs):
QFileDialog.__init__(self, *args, **kwargs)
self.setOption(QFileDialog.DontUseNativeDialog, True)
box = QVBoxLayout()
self.setFixedSize(self.width() + 250, self.height())
self.mpPreview = QLabel("Preview", self)
self.mpPreview.setFixedSize(250, 250)
self.mpPreview.setAlignment(Qt.AlignCenter)
self.mpPreview.setObjectName("labelPreview")
box.addWidget(self.mpPreview)
box.addStretch()
self.layout().addLayout(box, 1, 3, 1, 1)
self.currentChanged.connect(self.onChange)
self.fileSelected.connect(self.onFileSelected)
self.filesSelected.connect(self.onFilesSelected)
self._fileSelected = None
self._filesSelected = None
def onChange(self, path):
pixmap = QPixmap(path)
if(pixmap.isNull()):
self.mpPreview.setText("Preview")
else:
self.mpPreview.setPixmap(pixmap.scaled(self.mpPreview.width(),
self.mpPreview.height(), Qt.KeepAspectRatio,
Qt.SmoothTransformation))
def onFileSelected(self, file):
self._fileSelected = file
def onFilesSelected(self, files):
self._filesSelected = files
def getFileSelected(self):
return self._fileSelected
def getFilesSelected(self):
return self._filesSelected
class OtherDialog(QDialog):
def __init__(self, inputDict):
super(OtherDialog,self).__init__()
self.setWindowTitle('Other configurations')
mltpl = inputDict['multiple']
thrshld = inputDict['threshold']
msk = inputDict['mask']
vbox = QVBoxLayout()
self.setLayout(vbox)
label = QLabel("(*) Only meant for image inserted by template")
vbox.addWidget(label)
hline = QFrame()
hline.setFrameStyle(hline.HLine)
hline.setLineWidth(1)
vbox.addWidget(hline)
formLayout = QFormLayout()
vbox.addLayout(formLayout)
self.multiple = QCheckBox()
self.multiple.setChecked(mltpl)
formLayout.addRow("Multiple Match (*):", self.multiple)
self.threshold = QDoubleSpinBox()
self.threshold.setDecimals(1)
self.threshold.setRange(0,1)
self.threshold.setSingleStep(0.1)
self.threshold.setValue(thrshld)
formLayout.addRow("Threshold (*):", self.threshold)
hbox = QHBoxLayout()
colorBtn = QPushButton("Select")
colorBtn.clicked.connect(self.colorBtn_clicked)
hbox.addWidget(colorBtn)
_, red, _, green, _, blue = msk.split(',')
self.backgroundColor = ( "background-color: rgb(" + red + ","
+ green + "," + blue + ")" )
self.borderColor = "border: 1px solid black"
self.frame = QFrame()
self.frame.setStyleSheet( self.borderColor + ";"
+ self.backgroundColor)
hbox.addWidget(self.frame)
formLayout.addRow("Image background color:", hbox)
self.btns = QDialogButtonBox()
self.btns.setStandardButtons(
QDialogButtonBox.Cancel | QDialogButtonBox.Ok)
vbox.addWidget(self.btns)
self.btns.accepted.connect(self.accept)
self.btns.rejected.connect(self.reject)
def colorBtn_clicked(self):
colorDialog = QColorDialog()
colorDialog.setOption(QColorDialog.ShowAlphaChannel)
color = colorDialog.getColor()
if color.isValid():
self.frame.setStyleSheet(self.borderColor + ";"
+ 'background-color: %s' % color.name())
return
def getMask(self):
''' Get the background color of the frame. '''
# Get the background color of the frame
frameColor = self.frame.palette().color(QPalette.Background)
red, green, blue, alpha = (frameColor.red(), frameColor.green(),
frameColor.blue(), frameColor.alpha())
# Create a mask list
maskList = []
for c in red, green, blue:
if int(c) == 0:
maskList.append(str(c))
maskList.append(str(int(c)+2))
elif int(c) == 255:
maskList.append(str(int(c)-2))
maskList.append(str(c))
else:
maskList.append(str(int(c)-1))
maskList.append(str(int(c)+1))
# Join the list with comma
mask = ','.join(maskList)
return mask
def accept(self):
mask = self.getMask()
# Create an empty dictionary
self._output = {}
# Fill the dictionary with values
self._output['multiple'] = self.multiple.isChecked()
self._output['threshold'] = self.threshold.value()
self._output['mask'] = mask
super(OtherDialog,self).accept()
return
def get_output(self):
return self._output
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__()
self.setWindowTitle('Sign PDF GUI')
self.author = kwargs['author']
self.version = kwargs['version']
self.cliApp = kwargs['cliApp']
self.title = 'Sign PDF GUI'
cliAppBasename = os.path.splitext(self.cliApp)[0]
scriptDir = os.path.dirname(os.path.realpath(__file__))
iconPath = os.path.sep.join([scriptDir,'icon',cliAppBasename])
self.setWindowIcon(QIcon(iconPath))
self.listPdf = [] # List of pdfs
self.listImage = [] # List of images
if 'pdflist' in kwargs.keys() and len(kwargs['pdflist']) > 0 :
n = len(kwargs['pdflist'])
for i in range(0,n):
self.listPdf.append(os.path.realpath(kwargs['pdflist'][i]))
self.left = 10
self.top = 10
self.width = 700
self.height = 480
self.setGeometry(self.left, self.top, self.width, self.height)
self.btnWidth = 100
self.btnHeight = 30
self.selectAllWidth = 70
widget = QWidget()
self.setCentralWidget(widget)
self.vbox = QVBoxLayout()
widget.setLayout(self.vbox)
multiple = False
threshold = 0.9
mask = "254,255,254,255,254,255"
self.otherConfigs = {
'multiple' : multiple,
'threshold' : threshold,
'mask' : mask
}
self.createPdfBox()
self.createPdfTable()
self.createImageBox()
self.createImageTable()
self.createButtonBox()
self.createStatusBar()
self.updatePdfTable()
def createPdfBox(self):
hbox = QHBoxLayout()
self.vbox.addLayout(hbox)
self.vbox.setAlignment(Qt.AlignTop)
addPdfBtn = QPushButton("Add PDF",self)
addPdfBtn.setFixedSize(self.btnWidth,self.btnHeight)
addPdfBtn.clicked.connect(self.addPdfFiles)
hbox.addWidget(addPdfBtn)
delPdfBtn = QPushButton("Remove PDF",self)
delPdfBtn.setFixedSize(self.btnWidth,self.btnHeight)
delPdfBtn.clicked.connect(self.delPdfFiles)
hbox.addWidget(delPdfBtn)
hbox.addStretch(1)
self.showGrid = QCheckBox("Show grid")
self.showGrid.stateChanged.connect(self.showGridState)
hbox.addWidget(self.showGrid)
self.printCmd = QCheckBox("Print cmd")
self.printCmd.stateChanged.connect(self.printCmdState)
hbox.addWidget(self.printCmd)
otherBtn = QPushButton("Others")
otherBtn.setFixedSize(self.btnWidth,self.btnHeight)
otherBtn.clicked.connect(self.otherBtn_clicked)
hbox.addWidget(otherBtn)
def createPdfTable(self):
npdf = len(self.listPdf)
self.pdfTable = QTableWidget()
self.pdfTable.setRowCount(npdf)
self.pdfTable.setColumnCount(2)
self.pdfTable.setHorizontalHeaderLabels(
"Select All;PDF Filename".split(";"))
header = self.pdfTable.horizontalHeader()
header.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
header.resizeSection(0,self.selectAllWidth)
self.vbox.setAlignment(Qt.AlignTop)
# self.pdfTable.setMaximumHeight(self.tableMaxHeight)
self.vbox.addWidget(self.pdfTable)
# If header is clicked
self.selectAllPdf = False
header.sectionClicked.connect(self.onPdfTableHeaderClicked)
def onPdfTableHeaderClicked(self, logicalIndex):
n = len(self.listPdf)
if logicalIndex == 0: # If the first row is clicked
if not self.selectAllPdf: # If self.selectAllPdf is False
self.selectAllPdf = True
for r in range(0,n):
self.pdfTable.cellWidget(r,0).findChild(
type(QCheckBox())).setChecked(True)
else:
self.selectAllPdf = False
for r in range(0,n):
self.pdfTable.cellWidget(r,0).findChild(
type(QCheckBox())).setChecked(False)
def createImageBox(self):
hbox = QHBoxLayout()
self.vbox.addLayout(hbox)
addImageBtn = QPushButton("Add Image")
addImageBtn.setFixedSize(self.btnWidth,self.btnHeight)
addImageBtn.clicked.connect(lambda x:
self.addEditImage(False))
hbox.addWidget(addImageBtn)
delImageBtn = QPushButton("Remove Image")
delImageBtn.setFixedSize(self.btnWidth,self.btnHeight)
delImageBtn.clicked.connect(self.delImageFiles)
hbox.addWidget(delImageBtn)
hbox.addStretch(1)
def delImageFiles(self):
# Clear the status message
self.statusBar.clearMessage()
self.selectAllImage = False
n = len(self.listImage)
for r in range(0,n):
if self.imageTable.cellWidget(r,0).findChild(
type(QCheckBox())).isChecked():
# Remove the element from the list
del self.listImage[r]
# Insert blank in the list so that the number of
# elements remain the same.
self.listImage.insert(r,"")
# Remove the blanks that had been inserted
while("" in self.listImage):
self.listImage.remove("")
# Update the table
self.updateImageTable()
return
def createImageTable(self):
n = len(self.listImage)
self.imageTable = QTableWidget()
self.imageTable.setRowCount(n)
self.imageTable.setColumnCount(9)
self.imageTable.setHorizontalHeaderLabels(
"Select All;Edit;Image;Template;XValue;YValue;Scale;Page;Rotation".split(";"))
header = self.imageTable.horizontalHeader()
# header.setSectionResizeMode(1,QtWidgets.QHeaderView.Stretch)
header.resizeSection(0,self.selectAllWidth)
header.resizeSection(1,46)
self.vbox.setAlignment(Qt.AlignTop)
# self.imageTable.setMaximumHeight(self.tableMaxHeight)
self.vbox.addWidget(self.imageTable)
# If header is clicked
self.selectAllImage = False
header.sectionClicked.connect(self.onImageTableHeaderClicked)
self.imageTable.itemChanged.connect(self.itemChangedText)
return
# If cell value have changed, then update them on the dictionary
def itemChangedText(self, item):
row = item.row()
col = item.column()
# Get the 'key' = column header
key = self.imageTable.horizontalHeaderItem(col).text().lower()
# Get the 'value' = cell value from the table
value = str(self.imageTable.item(row,col).text())
# Update the dictionary
self.listImage[row].update_info(key, value)
return
# If image header has been clicked
def onImageTableHeaderClicked(self, logicalIndex):
n = len(self.listImage)
if logicalIndex == 0: # If the first row is clicked
if not self.selectAllImage: # If self.selectAllImage is False
self.selectAllImage = True
for r in range(0,n):
self.imageTable.cellWidget(r,0).findChild(
type(QCheckBox())).setChecked(True)
else:
self.selectAllImage = False
for r in range(0,n):
self.imageTable.cellWidget(r,0).findChild(
type(QCheckBox())).setChecked(False)
def updateImageTable(self):
n = len(self.listImage)
# Block the signals so that any editing doesn't call any signal
self.imageTable.blockSignals(True)
# Clears the table
self.imageTable.setRowCount(0)
# Create an empty list of 'Edit' buttons
self.editImageBtn = []
for row in range(0,n):
# Insert a new row
self.imageTable.insertRow(row)
# Insert a checkbox in the first column (col = 0)
qw = QWidget()
self.checkbox = QCheckBox()
self.checkbox.setCheckState(Qt.Unchecked)
tabhlayout = QHBoxLayout(qw)
tabhlayout.addWidget(self.checkbox)
tabhlayout.setAlignment(Qt.AlignCenter)
tabhlayout.setContentsMargins(0, 0, 0, 0)
self.imageTable.setCellWidget(row, 0, qw)
# Insert a checkbox in the second column (col = 1)
# Append to the list of 'Edit' buttons
self.editImageBtn.append(QPushButton('Edit'))
self.editImageBtn[row].setStyleSheet('margin:3px')
self.imageTable.setCellWidget(row,1,self.editImageBtn[row])
self.editImageBtn[row].clicked.connect(lambda x:
self.addEditImage(True))
for col in range(2,9):
# Get the column header in lowercase
colheader = self.imageTable.horizontalHeaderItem(
col).text().lower()
if colheader == 'image' or colheader == 'template':
# If the column header is 'image' or 'template', then
# get the file basename and also make the cells read-only
itemString = os.path.basename(
str(self.listImage[row].info[colheader]))
item = QTableWidgetItem(itemString)
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
else:
# Just get the value from the dictionary
itemString = str(self.listImage[row].info[colheader])
item = QTableWidgetItem(itemString)
# Align and insert the value in the cell
item.setTextAlignment(Qt.AlignCenter)
self.imageTable.setItem(row,col,item)
self.imageTable.blockSignals(False)
# Add or edit the image info
def addEditImage(self, editBool):
# Clear the status message
self.statusBar.clearMessage()
dlg = ImageOptionsDialog(self)
# If edit then
if editBool:
# Get the row number 'r'
buttonClicked = self.sender()
index = self.imageTable.indexAt(buttonClicked.pos())
r = index.row()
# Get the r-th row dictionary
imageInfo = self.listImage[r].get_dict()
# Insert the image info in the dialog
dlg.editInfoDialog(imageInfo)
if dlg.exec_() == ImageOptionsDialog.Accepted:
data = dlg.get_output()
if editBool:
# If edit, the replace the original content with
# the new content
self.listImage[r] = data['image']
self.listImage[r] = ImageOptions(data)
else:
# If not editing old info, then add it to the end
self.listImage.append(data['image'])
k = len(self.listImage)
self.listImage[k-1] = ImageOptions(data)
# Update the image table
self.updateImageTable()
return
def createButtonBox(self):
hbox = QHBoxLayout()
self.vbox.addLayout(hbox)
aboutBtn = QPushButton("About")
aboutBtn.setFixedSize(self.btnWidth,self.btnHeight)
aboutBtn.clicked.connect(self.aboutBtn_clicked)
hbox.addWidget(aboutBtn, alignment=Qt.AlignLeft)
self.helpBtn = QPushButton("Help")
self.helpBtn.setFixedSize(self.btnWidth,self.btnHeight)
self.helpBtn.clicked.connect(self.helpBtn_clicked)
hbox.addWidget(self.helpBtn)
hbox.addStretch(1)
self.applyBtn = QPushButton("Apply")
self.applyBtn.setFixedSize(self.btnWidth,self.btnHeight)
self.applyBtn.clicked.connect(self.applyBtn_clicked)
hbox.addWidget(self.applyBtn)
quitBtn = QPushButton("Quit")
quitBtn.setFixedSize(self.btnWidth,self.btnHeight)
quitBtn.clicked.connect(self.close)
hbox.addWidget(quitBtn)
return
def aboutBtn_clicked(self):
# Clear the status message
self.statusBar.clearMessage()
infoMsg = ('Version: ' + self.version + '\n' +
'Author: ' + self.author )
dlg = MessageDialog(title = self.title + ': About',
text = 'Sign PDF GUI',
informativeText = infoMsg,
icon = QStyle.SP_MessageBoxInformation)
dlg.setMinimumWidth(300)
dlg.exec_()
return
def helpBtn_clicked(self):
cmd = self.cliApp + ' --help'
self.proc = Popen(shlex.split(cmd), stdout=PIPE, stderr=PIPE)
stdout, stderr = self.proc.communicate()
rtn = self.proc.returncode
if rtn != 0:
print("Problem getting help text.")
return rtn
msg = self.cliApp +' --help'
detailedMsg = stdout.decode("utf-8")
dlg = MessageDialog(title = self.title + ': Help',
icon = QStyle.SP_MessageBoxInformation,
text = msg,
detailedText = detailedMsg,
)
dlg.setMinimumSize(550,400)
dlg.exec_()
return
def applyBtn_clicked(self):
n = len(self.listPdf)
if n == 0:
dlg = MessageDialog(title = self.title + ': Error',
text = 'Warning: No pdf file supplied.',
icon = QStyle.SP_MessageBoxCritical)
dlg.exec_()
return
self.pd = ProgressDialog()
self.pd.setMinimum(0)
self.pd.setMaximum(n)
self.pd.setFormat("%v/%m")
self.work = Worker(self)
self.thread = Thread(target = self.work.run)
self.thread.start()
if self.pd.exec_() == ProgressDialog.Rejected:
self.work.terminate()
def createStatusBar(self):
self.statusBar = QStatusBar()
self.setStatusBar(self.statusBar)
return
def addPdfFiles(self):
# Clear the status message
self.statusBar.clearMessage()
options = QFileDialog.Options()
options |= QFileDialog.DontUseNativeDialog
files, _ = (
QFileDialog.getOpenFileNames(self,
"QFileDialog.getOpenFileNames()","","PDF Files (*.pdf)",
options=options))
if files:
self.listPdf.extend(files)
self.updatePdfTable()
def updatePdfTable(self):
n = len(self.listPdf)
self.pdfTable.setRowCount(0) # Clear the table
for r in range(0,n):
self.pdfTable.insertRow(r)
qw = QWidget()
self.checkbox = QCheckBox()
self.checkbox.setCheckState(Qt.Unchecked)
tabhlayout = QHBoxLayout(qw)
tabhlayout.addWidget(self.checkbox)
tabhlayout.setAlignment(Qt.AlignCenter)
tabhlayout.setContentsMargins(0, 0, 0, 0)
self.pdfTable.setCellWidget(r, 0, qw)
item = QTableWidgetItem(self.listPdf[r])
item.setFlags(Qt.ItemIsEnabled | Qt.ItemIsSelectable)
self.pdfTable.setItem(r, 1, item)
def showGridState(self):
self.statusBar.clearMessage()
return
def printCmdState(self):
self.statusBar.clearMessage()
return
def delPdfFiles(self):
# Clear the status message
self.statusBar.clearMessage()
self.selectAllPdf = False
n = len(self.listPdf)
for r in range(0,n):
if self.pdfTable.cellWidget(r,0).findChild(
type(QCheckBox())).isChecked():
# Remove the element from the list
del self.listPdf[r]
# Insert blank in the list so that the number of
# elements remain the same.
self.listPdf.insert(r,"")
# Remove the blanks that had been inserted
while("" in self.listPdf):
self.listPdf.remove("")
# Update the table
self.updatePdfTable()
return
def otherBtn_clicked(self):
dlg = OtherDialog(self.otherConfigs)
if dlg.exec_() == OtherDialog.Accepted:
self.otherConfigs = dlg.get_output()
return
class MessageDialog(QDialog):
def __init__(self, *args, **kwargs):
super(MessageDialog, self).__init__()
self.vbox = QVBoxLayout()
self.setLayout(self.vbox)
hbox = QHBoxLayout()
self.vbox.addLayout(hbox)
if 'title' in kwargs.keys():
self.setWindowTitle(kwargs['title'])
# https://stackoverflow.com/questions/25055552/how-do-i-add-standardpixmap-to-a-layout
# https://doc.qt.io/qtforpython/PySide2/QtWidgets/QStyle.html#PySide2.QtWidgets.PySide2.QtWidgets.QStyle.standardIcon
if 'icon' in kwargs.keys() and kwargs['icon'] != 'QStyle.SP_NoIcon':
self.icon = QLabel()
icon = self.style().standardIcon(kwargs['icon'])
self.icon.setPixmap(icon.pixmap(48))
hbox.addWidget(self.icon, alignment = Qt.AlignLeft)
self.text = QLabel()
hbox.addWidget(self.text, alignment = Qt.AlignLeft | Qt.AlignVCenter)
if 'text' in kwargs.keys():
self.text.setText(kwargs['text'])
hbox.addStretch(1)
self.grid = QGridLayout()
self.vbox.addLayout(self.grid)
self.informativeText = QLabel("")
if 'informativeText' in kwargs.keys():
self.informativeText.setText(kwargs['informativeText'])
self.grid.addWidget(self.informativeText, 0, 0)
self.detailedText = QPlainTextEdit()
self.detailedText.setReadOnly(True)
if 'detailedText' in kwargs.keys():
self.detailedText.insertPlainText(kwargs['detailedText'])
self.grid.addWidget(self.detailedText, 1, 0)
self.btns = QDialogButtonBox()
self.btns.setStandardButtons(QDialogButtonBox.Ok)
self.vbox.addWidget(self.btns)
self.btns.accepted.connect(self.close)
return
class ImageOptionsDialog(QDialog):
def __init__(self, *args, **kwargs):
super(ImageOptionsDialog,self).__init__(*args, **kwargs)
self.setWindowTitle('Select Image')
self.setFixedWidth(350)
vbox = QVBoxLayout()
self.setLayout(vbox)
insertImageLabel = QLabel("Insert image by:")
vbox.addWidget(insertImageLabel)
radioHbox = QHBoxLayout()
vbox.addLayout(radioHbox)
self.radioBtnCord = QRadioButton("Coordinate")
self.radioBtnCord.setChecked(True)
self.radioBtnCord.toggled.connect(self.onRadioBtnClicked)
radioHbox.addWidget(self.radioBtnCord)
self.insertImageBy = 'coordinate'
self.radioBtnTemp = QRadioButton("Template")
self.radioBtnTemp.setChecked(False)
self.radioBtnTemp.toggled.connect(self.onRadioBtnClicked)
radioHbox.addWidget(self.radioBtnTemp)
formLayout = QFormLayout()
vbox.addLayout(formLayout)
hbox = QHBoxLayout()
formLayout.addRow('Image:', hbox)
openImageBtn = QPushButton("Open")
hbox.addWidget(openImageBtn)
openImageBtn.setFixedSize(60,30)
openImageBtn.clicked.connect(lambda:
self.openImageBtn_clicked("coordinate"))
self.imgFileName = ''
self.imgFileLabel = QLabel(os.path.basename(self.imgFileName))
hbox.addWidget(self.imgFileLabel)
hbox = QHBoxLayout()
formLayout.addRow('Template:', hbox)
self.openTempBtn = QPushButton("Open")
hbox.addWidget(self.openTempBtn)
self.openTempBtn.setFixedSize(60,30)
self.openTempBtn.setEnabled(False)
self.openTempBtn.clicked.connect(lambda:
self.openImageBtn_clicked("template"))
self.tempFileName = 'Not applicable'
self.tempFileLabel = QLabel(os.path.basename(self.tempFileName))
hbox.addWidget(self.tempFileLabel)
self.xValue = QLineEdit()
self.xValue.setText("0")
formLayout.addRow('X-Cord/XOffset:', self.xValue)
self.yValue = QLineEdit()
self.yValue.setText("0")
formLayout.addRow('Y-Cord/YOffset:', self.yValue)
self.scale = QSpinBox()
self.scale.setRange(0,100)
self.scale.setSingleStep(1)
self.scale.setValue(15)
self.scale.setSuffix(" %")
formLayout.addRow('Scale:', self.scale)
self.page = QSpinBox()
self.page.setRange(1,100)