-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.py
More file actions
1293 lines (1087 loc) · 53.8 KB
/
Copy pathGUI.py
File metadata and controls
1293 lines (1087 loc) · 53.8 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
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
# PDFDeWM - A tool to remove watermarks from PDF files.
# Copyright (C) 2025 nash-dir
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Main GUI application for PDFDeWM.
Provides a Tkinter-based interface for scanning and removing
watermarks from PDF files, with support for drag-and-drop,
cancellable operations, text watermark grouping, and persistent
user preferences.
"""
import json
import logging
import os
import sys
import threading
import time
import tkinter as tk
from collections import defaultdict
from pathlib import Path
from tkinter import ttk, messagebox
from typing import List, Dict, Any, Tuple
import queue
import fitz
try:
from PIL import Image, ImageTk, ImageDraw
except ImportError:
Image = None
ImageTk = None
ImageDraw = None
# Optional modern theme
try:
import sv_ttk
HAS_SV_TTK = True
except ImportError:
HAS_SV_TTK = False
from utils import FileManager, ThumbnailManager, ConfigManager
import core
logger = logging.getLogger("pdfdewm.gui")
# ── DPI Awareness (Windows) ────────────────────────────────────
def _enable_dpi_awareness():
"""Enable per-monitor DPI awareness on Windows 10+."""
if sys.platform == "win32":
try:
import ctypes
ctypes.windll.shcore.SetProcessDpiAwareness(2) # PROCESS_PER_MONITOR_DPI_AWARE
except (AttributeError, OSError):
try:
ctypes.windll.user32.SetProcessDPIAware()
except (AttributeError, OSError):
pass
# ── Tooltip ─────────────────────────────────────────────────────
class ToolTip:
"""Simple tooltip for Tkinter widgets."""
def __init__(self, widget, text: str, delay: int = 500):
self.widget = widget
self.text = text
self.delay = delay
self._tip_window = None
self._after_id = None
widget.bind("<Enter>", self._schedule)
widget.bind("<Leave>", self._hide)
def _schedule(self, event=None):
self._after_id = self.widget.after(self.delay, self._show)
def _show(self):
if self._tip_window:
return
x = self.widget.winfo_rootx() + 20
y = self.widget.winfo_rooty() + self.widget.winfo_height() + 5
self._tip_window = tw = tk.Toplevel(self.widget)
tw.wm_overrideredirect(True)
tw.wm_geometry(f"+{x}+{y}")
label = tk.Label(
tw, text=self.text, justify="left",
background="#ffffe0", relief="solid", borderwidth=1,
font=("TkDefaultFont", 9)
)
label.pack()
def _hide(self, event=None):
if self._after_id:
self.widget.after_cancel(self._after_id)
self._after_id = None
if self._tip_window:
self._tip_window.destroy()
self._tip_window = None
# ── Queue Logger ────────────────────────────────────────────────
class QueueLogger:
"""Thread-safe logger that redirects stdout/stderr to a queue."""
def __init__(self, q: queue.Queue):
self.queue = q
self.buffer = ""
self._lock = threading.Lock()
def write(self, text: str):
with self._lock:
self.buffer += text
if '\n' in self.buffer:
lines = self.buffer.split('\n')
for line in lines[:-1]:
self.queue.put(('log', line + '\n'))
self.buffer = lines[-1]
def flush(self):
with self._lock:
if self.buffer:
self.queue.put(('log', self.buffer + '\n'))
self.buffer = ""
# ── Main Application ───────────────────────────────────────────
class App(tk.Tk):
"""Main application window for PDFDeWM."""
def __init__(self):
super().__init__()
self.core = core
self.file_manager = FileManager()
self.thumbnail_manager = ThumbnailManager()
self.config = ConfigManager()
self.title("PDF Watermark Remover")
self.minsize(600, 600)
# Restore window geometry
saved_geo = self.config.get("window_geometry")
if saved_geo:
self.geometry(saved_geo)
else:
self.geometry("850x900")
self.input_files: List[str] = []
self.output_dir: str = self.config.recent_output_dir or ""
# Candidate storage:
# raw_candidates: original per-page/per-xref candidates from scan
# display_groups: grouped entries shown in the UI
self.raw_candidates: Dict[Tuple, Dict[str, Any]] = {}
self.display_groups: List[Dict[str, Any]] = []
self.task_queue: queue.Queue = queue.Queue()
self._cancel_event = threading.Event()
self._scan_start_time: float = 0
self.suffix_var = tk.StringVar(value=self.config.last_suffix)
self.copy_skipped_var = tk.BooleanVar(value=False)
self.overwrite_var = tk.BooleanVar(value=False)
self.text_keywords_var = tk.StringVar()
self.scan_threshold_var = tk.IntVar(value=self.config.last_threshold)
self.sanitize_var = tk.BooleanVar(value=False)
self.clean_metadata_var = tk.BooleanVar(value=False)
self._preview_photo = None # Keep reference to prevent GC
self.original_stdout = sys.stdout
self.original_stderr = sys.stderr
self.queue_logger = QueueLogger(self.task_queue)
self._setup_ui()
self._apply_theme()
self.redirect_logging()
self.protocol("WM_DELETE_WINDOW", self.on_closing)
self.after(100, self.process_queue)
self._bind_hotkeys()
self._setup_drag_and_drop()
self._update_empty_state()
if self.output_dir:
self.output_dir_entry.delete(0, "end")
self.output_dir_entry.insert(0, self.output_dir)
self.open_folder_button.config(state="normal")
# ─── Theme ───────────────────────────────────────────────────
def _apply_theme(self):
"""Apply sv_ttk theme if available."""
if HAS_SV_TTK:
saved_theme = self.config.get("theme", "light")
sv_ttk.set_theme(saved_theme)
# Explicitly force Malgun Gothic on ALL fonts to fix Korean character rendering
import tkinter.font as tkfont
for font_name in tkfont.names():
try:
f = tkfont.nametofont(font_name)
# Consolas for fixed width, Malgun Gothic for everything else
if "fixed" in font_name.lower():
f.configure(family="Consolas")
else:
f.configure(family="Malgun Gothic")
except Exception:
pass
style = ttk.Style()
style.configure('.', font=("Malgun Gothic", 10))
style.configure('Treeview', font=("Malgun Gothic", 10))
# Without sv_ttk the default ttk theme is used as-is.
def _toggle_theme(self):
"""Toggle between light and dark theme."""
if HAS_SV_TTK:
current = sv_ttk.get_theme()
new_theme = "dark" if current == "light" else "light"
sv_ttk.set_theme(new_theme)
self.config.set("theme", new_theme)
# ─── Hotkeys ─────────────────────────────────────────────────
def _bind_hotkeys(self):
"""Bind keyboard shortcuts for all main actions."""
self.bind_all("<Alt-a>", self.add_files)
self.bind_all("<Alt-Shift-A>", self.add_folder)
self.bind_all("<Alt-s>", self.select_output_dir)
self.bind_all("<Alt-d>", self.start_scan)
self.bind_all("<Alt-f>", self.start_removal)
self.bind_all("<Alt-t>", self.on_closing)
self.bind_all("<Alt-q>", lambda e: self._focus_and_select(self.suffix_entry))
self.bind_all("<Alt-w>", lambda e: self._focus_and_select(self.text_keyword_entry))
self.bind_all("<Alt-Shift-S>", self.select_output_dir)
self.bind_all("<Alt-Shift-D>", self.start_scan)
self.bind_all("<Alt-Shift-F>", self.start_removal)
self.bind_all("<Alt-Shift-T>", self.on_closing)
self.bind_all("<Alt-Shift-Q>", lambda e: self._focus_and_select(self.suffix_entry))
self.bind_all("<Alt-Shift-W>", lambda e: self._focus_and_select(self.text_keyword_entry))
def _focus_and_select(self, entry_widget):
entry_widget.focus_set()
entry_widget.selection_range(0, tk.END)
# ─── Drag & Drop ────────────────────────────────────────────
def _setup_drag_and_drop(self):
"""Configure drag-and-drop support."""
try:
from tkinterdnd2 import DND_FILES
self.file_listbox.drop_target_register(DND_FILES)
self.file_listbox.dnd_bind('<<Drop>>', self._on_drop)
except Exception:
# tkinterdnd2 not installed or DnD unsupported — feature stays off.
pass
def _on_drop(self, event):
raw = event.data
files = []
if raw.startswith('{'):
import re
files = re.findall(r'\{(.+?)\}', raw)
else:
files = raw.split()
for f in files:
f = f.strip()
p = Path(f)
if p.is_file() and p.suffix.lower() == '.pdf':
fstr = str(p)
if fstr not in self.input_files:
self.input_files.append(fstr)
self.file_listbox.insert("end", p.name)
elif p.is_dir():
for pdf in sorted(p.rglob("*.pdf")):
fstr = str(pdf)
if fstr not in self.input_files:
self.input_files.append(fstr)
self.file_listbox.insert("end", pdf.name)
self._update_file_count()
self._update_empty_state()
self._check_overwrite_warning()
# ─── Logging / Lifecycle ────────────────────────────────────
def redirect_logging(self):
sys.stdout = self.queue_logger
sys.stderr = self.queue_logger
def restore_logging(self):
if hasattr(sys.stdout, 'flush'):
sys.stdout.flush()
sys.stdout = self.original_stdout
sys.stderr = self.original_stderr
def on_closing(self, event=None):
self.config.last_suffix = self.suffix_var.get()
self.config.last_threshold = self.scan_threshold_var.get()
if self.output_dir:
self.config.recent_output_dir = self.output_dir
self.config.set("window_geometry", self.geometry())
self._cancel_event.set()
self.restore_logging()
self.destroy()
# ─── Empty State ────────────────────────────────────────────
def _update_empty_state(self):
"""Show/hide the empty state placeholder in the file listbox."""
if not self.input_files:
self.file_listbox.config(foreground="gray")
if self.file_listbox.size() == 0:
self.file_listbox.insert("end", " PDF 파일을 여기에 드래그하거나")
self.file_listbox.insert("end", " [파일 추가(Alt+A)]를 클릭하세요")
else:
self.file_listbox.config(foreground="")
def _clear_empty_state(self):
"""Remove the empty state placeholder if present."""
if not self.input_files and self.file_listbox.size() > 0:
self.file_listbox.delete(0, "end")
# ─── UI Setup ───────────────────────────────────────────────
def _setup_ui(self):
"""Build the complete application UI."""
# === Top Frame: File selection, output settings ===
top_frame = ttk.Frame(self, padding="10")
top_frame.pack(fill="x", side="top", pady=(0, 5))
top_frame.columnconfigure(1, weight=1)
file_button_frame = ttk.Frame(top_frame)
file_button_frame.grid(row=0, column=0, rowspan=2, sticky="n", padx=(0, 5))
btn_add_files = ttk.Button(file_button_frame, text="파일 추가(Alt+A)", command=self.add_files)
btn_add_files.pack(fill="x", pady=2)
ToolTip(btn_add_files, "PDF 파일을 선택하여 추가합니다")
btn_add_folder = ttk.Button(file_button_frame, text="폴더 추가(Alt+⇧A)", command=self.add_folder)
btn_add_folder.pack(fill="x", pady=2)
ToolTip(btn_add_folder, "폴더 내 모든 PDF를 재귀적으로 추가합니다")
btn_remove = ttk.Button(file_button_frame, text="선택 제거(Del)", command=self.remove_selected_files)
btn_remove.pack(fill="x", pady=2)
# Theme toggle (only if sv_ttk available)
if HAS_SV_TTK:
btn_theme = ttk.Button(file_button_frame, text="🌙 테마", command=self._toggle_theme)
btn_theme.pack(fill="x", pady=(10, 2))
ToolTip(btn_theme, "라이트/다크 모드를 전환합니다")
self.file_listbox = tk.Listbox(top_frame, height=5, selectmode="extended", font=("Malgun Gothic", 10))
self.file_listbox.grid(row=0, column=1, rowspan=2, sticky="ew")
list_scroll = ttk.Scrollbar(top_frame, orient="vertical", command=self.file_listbox.yview)
list_scroll.grid(row=0, column=2, rowspan=2, sticky="ns")
self.file_listbox.config(yscrollcommand=list_scroll.set)
self.file_listbox.bind("<Double-1>", self.open_selected_file)
self.file_listbox.bind("<Return>", self.open_selected_file)
self.file_listbox.bind("<Delete>", self.remove_selected_files)
self.file_listbox.bind("<BackSpace>", self.remove_selected_files)
# File count label
self.file_count_label = ttk.Label(top_frame, text="", foreground="gray")
self.file_count_label.grid(row=2, column=1, sticky="w", pady=(2, 0))
ttk.Label(top_frame, text="출력 폴더:").grid(row=3, column=0, sticky="w", pady=(10, 0))
self.output_dir_entry = ttk.Entry(top_frame)
self.output_dir_entry.grid(row=3, column=1, sticky="ew", pady=(10, 0))
btn_browse = ttk.Button(top_frame, text="찾아보기(Alt+S)", command=self.select_output_dir)
btn_browse.grid(row=3, column=2, sticky="e", pady=(10, 0), padx=(5, 0))
ttk.Label(top_frame, text="출력 접미사(Alt+Q):").grid(row=4, column=0, sticky="w", pady=(5, 0))
self.suffix_entry = ttk.Entry(top_frame, textvariable=self.suffix_var)
self.suffix_entry.grid(row=4, column=1, sticky="ew", pady=(5, 0))
# === Options ===
options_container = ttk.Frame(top_frame)
options_container.grid(row=5, column=0, columnspan=3, sticky="ew", pady=(5, 0))
options_container.columnconfigure(1, weight=1)
scan_options_frame = ttk.Frame(options_container)
scan_options_frame.grid(row=0, column=0, columnspan=2, pady=(0, 5), sticky="ew")
ttk.Label(scan_options_frame, text="이미지 스캔 임계값(%):").pack(side="left", padx=(0, 5))
self.threshold_spinbox = ttk.Spinbox(
scan_options_frame, from_=1, to=100, increment=1,
textvariable=self.scan_threshold_var, width=5
)
self.threshold_spinbox.pack(side="left")
ToolTip(self.threshold_spinbox, "이미지가 전체 페이지의 N% 이상에 나타나면 워터마크로 판별합니다")
ttk.Label(options_container, text="텍스트 키워드(Alt+W, ';'로 구분):").grid(
row=1, column=0, sticky="w", pady=(5, 0)
)
self.text_keyword_entry = ttk.Entry(options_container, textvariable=self.text_keywords_var)
self.text_keyword_entry.grid(row=1, column=1, sticky="ew", pady=(5, 0))
ToolTip(self.text_keyword_entry, "세미콜론으로 구분된 키워드를 입력하세요. 예: DRAFT;대외비;CONFIDENTIAL")
self.overwrite_warning_label = ttk.Label(
options_container, text="⚠️ 원본 입력 파일이 비가역적으로 덮어쓰기됩니다",
foreground="red", font=("TkDefaultFont", 9, "bold")
)
self.overwrite_warning_label.grid(row=2, column=1, sticky="w", padx=0, pady=(2, 0))
self.overwrite_warning_label.grid_remove()
# === Action buttons ===
action_frame = ttk.Frame(top_frame)
action_frame.grid(row=6, column=0, columnspan=3, pady=(10, 0), sticky="ew")
left_action_frame = ttk.Frame(action_frame)
left_action_frame.pack(side="left")
self.scan_button = ttk.Button(
left_action_frame, text="스캔(Alt+D)", command=self.start_scan
)
self.scan_button.pack(side="left", padx=(0, 2))
self.remove_button = ttk.Button(
left_action_frame, text="워터마크 제거(Alt+F)",
command=self.start_removal, state="disabled"
)
self.remove_button.pack(side="left", padx=2)
self.cancel_button = ttk.Button(
left_action_frame, text="취소", command=self._cancel_task, state="disabled"
)
self.cancel_button.pack(side="left", padx=2)
self.copy_skipped_checkbox = ttk.Checkbutton(
left_action_frame, text="미처리 파일 복사", variable=self.copy_skipped_var
)
self.copy_skipped_checkbox.pack(side="left", padx=7)
self.overwrite_checkbox = ttk.Checkbutton(
left_action_frame, text="기존 파일 덮어쓰기",
variable=self.overwrite_var, command=self._check_overwrite_warning
)
self.overwrite_checkbox.pack(side="left", padx=7)
right_action_frame = ttk.Frame(action_frame)
right_action_frame.pack(side="right")
self.metadata_checkbox = ttk.Checkbutton(
right_action_frame, text="메타데이터 정리", variable=self.clean_metadata_var
)
self.metadata_checkbox.pack(side="right", padx=(0, 5))
ToolTip(self.metadata_checkbox, "Author, Creator, Producer 등 민감한 메타데이터를 제거합니다")
self.sanitize_checkbox = ttk.Checkbutton(
right_action_frame, text="숨은 텍스트 제거", variable=self.sanitize_var
)
self.sanitize_checkbox.pack(side="right")
ToolTip(self.sanitize_checkbox, "PDF에 포함된 보이지 않는 텍스트를 제거합니다 (포렌식/보안용)")
self.suffix_var.trace_add("write", self._check_overwrite_warning)
# === Bottom Frame: Progress and Logs ===
bottom_frame = ttk.Frame(self, padding="10")
bottom_frame.pack(fill="x", side="bottom")
bottom_frame.columnconfigure(0, weight=1)
open_folder_frame = ttk.Frame(bottom_frame)
open_folder_frame.grid(row=0, column=0, sticky="ew", pady=(0, 5))
self.open_folder_button = ttk.Button(
open_folder_frame, text="출력 폴더 열기",
command=self._open_output_folder, state="disabled"
)
self.open_folder_button.pack(side="right")
progress_frame = ttk.Frame(bottom_frame)
progress_frame.grid(row=1, column=0, sticky="ew")
progress_frame.columnconfigure(0, weight=1)
self.status_label = ttk.Label(progress_frame, text="준비됨.")
self.status_label.grid(row=0, column=0, columnspan=2, sticky="w")
self.progress_bar = ttk.Progressbar(progress_frame, orient="horizontal", mode="determinate")
self.progress_bar.grid(row=1, column=0, sticky="ew", pady=(5, 0))
self.percent_label = ttk.Label(progress_frame, text="0%")
self.percent_label.grid(row=1, column=1, sticky="w", padx=(5, 0), pady=(5, 0))
log_frame = ttk.Labelframe(bottom_frame, text="로그", padding=5)
log_frame.grid(row=2, column=0, sticky="ew", pady=(10, 0))
log_frame.columnconfigure(0, weight=1)
log_frame.rowconfigure(0, weight=1)
self.log_text = tk.Text(log_frame, height=6, state="disabled", wrap="word", background="#f0f0f0", font=("Malgun Gothic", 9))
self.log_text.grid(row=0, column=0, sticky="nsew")
log_scroll = ttk.Scrollbar(log_frame, orient="vertical", command=self.log_text.yview)
log_scroll.grid(row=0, column=1, sticky="ns")
self.log_text.config(yscrollcommand=log_scroll.set)
# === Middle Frame: PanedWindow — Candidates (left) + Preview (right) ===
mid_frame = ttk.Frame(self, padding=(10, 0, 10, 10))
mid_frame.pack(fill="both", expand=True)
# Select All / Deselect All bar
select_bar = ttk.Frame(mid_frame)
select_bar.pack(fill="x", pady=(0, 5))
self.candidate_count_label = ttk.Label(select_bar, text="", foreground="gray")
self.candidate_count_label.pack(side="left")
self.one_page_warning_label = ttk.Label(
select_bar, text="⚠️ 1페이지 문서입니다. (1페이지 문서의 모든 이미지는 워터마크로 탐지됩니다.)",
foreground="red", font=("TkDefaultFont", 9, "bold")
)
self.one_page_warning_label.pack(side="left", padx=(10, 0))
self.one_page_warning_label.pack_forget()
btn_invert = ttk.Button(select_bar, text="선택 반전", command=self._invert_selection)
btn_invert.pack(side="right", padx=2)
btn_deselect = ttk.Button(select_bar, text="전체 해제", command=self._deselect_all)
btn_deselect.pack(side="right", padx=2)
btn_select = ttk.Button(select_bar, text="전체 선택", command=self._select_all)
btn_select.pack(side="right", padx=2)
paned = ttk.PanedWindow(mid_frame, orient="horizontal")
paned.pack(fill="both", expand=True)
# Left: Candidate list (scrollable canvas)
left_pane = ttk.Frame(paned)
paned.add(left_pane, weight=3)
self.canvas = tk.Canvas(left_pane, borderwidth=0, background="#ffffff")
self.thumbnail_frame = ttk.Frame(self.canvas, padding=5)
self.scrollbar = ttk.Scrollbar(left_pane, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.scrollbar.set)
self.scrollbar.pack(side="right", fill="y")
self.canvas.pack(side="left", fill="both", expand=True)
self.canvas_window = self.canvas.create_window((4, 4), window=self.thumbnail_frame, anchor="nw")
self.thumbnail_frame.bind(
"<Configure>", lambda e: self.canvas.configure(scrollregion=self.canvas.bbox("all"))
)
self.canvas.bind(
"<Configure>", lambda e: self.canvas.itemconfig(self.canvas_window, width=e.width - 8)
)
def _on_mousewheel(event):
if sys.platform == "darwin":
self.canvas.yview_scroll(int(-1*(event.delta)), "units")
else:
self.canvas.yview_scroll(int(-1*(event.delta/120)), "units")
def _on_mousewheel_up(event):
self.canvas.yview_scroll(-1, "units")
def _on_mousewheel_down(event):
self.canvas.yview_scroll(1, "units")
def _bind_to_mousewheel(event):
self.canvas.bind_all("<MouseWheel>", _on_mousewheel)
self.canvas.bind_all("<Button-4>", _on_mousewheel_up)
self.canvas.bind_all("<Button-5>", _on_mousewheel_down)
def _unbind_from_mousewheel(event):
self.canvas.unbind_all("<MouseWheel>")
self.canvas.unbind_all("<Button-4>")
self.canvas.unbind_all("<Button-5>")
left_pane.bind("<Enter>", _bind_to_mousewheel)
left_pane.bind("<Leave>", _unbind_from_mousewheel)
# Right: Preview panel
right_pane = ttk.Labelframe(paned, text="미리보기", padding=5)
paned.add(right_pane, weight=2)
self.preview_label = ttk.Label(
right_pane, text="후보를 클릭하면\n해당 페이지를 표시합니다",
anchor="center", justify="center", foreground="gray"
)
self.preview_label.pack(fill="both", expand=True)
self.preview_canvas = tk.Canvas(right_pane, background="#f8f8f8")
# preview_canvas is shown only when a preview is active
# ─── Overwrite Warning ──────────────────────────────────────
def _check_overwrite_warning(self, *args):
is_suffix_empty = not self.suffix_var.get().strip()
is_overwrite_checked = self.overwrite_var.get()
is_dangerous_overwrite = is_suffix_empty and is_overwrite_checked
is_output_dir_unsafe = False
if not self.output_dir:
is_output_dir_unsafe = True
else:
output_path = Path(self.output_dir).resolve()
for file_str in self.input_files:
input_path = Path(file_str).parent.resolve()
if output_path == input_path:
is_output_dir_unsafe = True
break
if is_dangerous_overwrite and is_output_dir_unsafe:
self.overwrite_warning_label.grid()
else:
self.overwrite_warning_label.grid_remove()
# ─── File Management ────────────────────────────────────────
def _update_file_count(self):
"""Update the file count label."""
count = len(self.input_files)
self.file_count_label.config(
text=f"{count}개 파일 로드됨" if count > 0 else ""
)
def add_files(self, event=None):
self._clear_empty_state()
new_files = self.file_manager.ask_for_files()
for file in new_files:
if file not in self.input_files:
self.input_files.append(file)
self.file_listbox.insert("end", Path(file).name)
if new_files:
self.config.recent_input_dir = str(Path(new_files[0]).parent)
self._update_file_count()
self._update_empty_state()
self._check_overwrite_warning()
def add_folder(self, event=None):
self._clear_empty_state()
folder = self.file_manager.ask_for_folder()
if folder:
for file in sorted(Path(folder).rglob("*.pdf")):
file_str = str(file)
if file_str not in self.input_files:
self.input_files.append(file_str)
display_name = file.relative_to(folder)
self.file_listbox.insert("end", display_name)
self.config.recent_input_dir = folder
self._update_file_count()
self._update_empty_state()
self._check_overwrite_warning()
def remove_selected_files(self, event=None):
selected_indices = self.file_listbox.curselection()
if not selected_indices:
return
files_to_remove = [self.input_files[i] for i in selected_indices]
target_index = selected_indices[0]
for index in reversed(selected_indices):
self.input_files.pop(index)
self.file_listbox.delete(index)
# Remove matching candidates
keys_to_delete = [
key for key in self.raw_candidates
if key[1] in files_to_remove
]
for key in keys_to_delete:
del self.raw_candidates[key]
self._rebuild_display_groups()
current_list_size = self.file_listbox.size()
if current_list_size > 0:
final_index = min(target_index, current_list_size - 1)
self.file_listbox.selection_clear(0, tk.END)
self.file_listbox.selection_set(final_index)
self.file_listbox.activate(final_index)
self.file_listbox.see(final_index)
self._update_file_count()
self._update_empty_state()
self._check_overwrite_warning()
def open_selected_file(self, event=None):
selected_indices = self.file_listbox.curselection()
if not selected_indices:
return
index = selected_indices[0]
if index >= len(self.input_files):
return # Empty state placeholder
file_path = self.input_files[index]
try:
if sys.platform == "win32":
os.startfile(file_path)
elif sys.platform == "darwin":
import subprocess
subprocess.call(('open', file_path))
else:
import subprocess
subprocess.call(('xdg-open', file_path))
except Exception as e:
messagebox.showerror("오류", f"파일을 열 수 없습니다:\n{file_path}\n{e}")
def select_output_dir(self, event=None):
directory = self.file_manager.ask_for_output_dir()
if directory:
self.output_dir = directory
self.output_dir_entry.delete(0, "end")
self.output_dir_entry.insert(0, self.output_dir)
self.config.recent_output_dir = directory
self.open_folder_button.config(state="normal")
self._check_overwrite_warning()
def _open_output_folder(self):
if not self.output_dir or not Path(self.output_dir).is_dir():
messagebox.showwarning("없음", "출력 폴더가 설정되지 않았거나 존재하지 않습니다.")
return
try:
if sys.platform == "win32":
os.startfile(self.output_dir)
elif sys.platform == "darwin":
import subprocess
subprocess.call(('open', self.output_dir))
else:
import subprocess
subprocess.call(('xdg-open', self.output_dir))
except Exception as e:
messagebox.showerror("오류", f"폴더를 열 수 없습니다:\n{e}")
# ─── Select All / Deselect All / Invert ─────────────────────
def _select_all(self):
for group in self.display_groups:
group['var'].set(True)
def _deselect_all(self):
for group in self.display_groups:
group['var'].set(False)
def _invert_selection(self):
for group in self.display_groups:
group['var'].set(not group['var'].get())
# ─── Cancel ─────────────────────────────────────────────────
def _cancel_task(self):
self._cancel_event.set()
self.status_label.config(text="취소 중...")
self.cancel_button.config(state="disabled")
def _set_buttons_running(self):
self.scan_button.config(state="disabled")
self.remove_button.config(state="disabled")
self.cancel_button.config(state="normal")
def _set_buttons_idle(self):
self.scan_button.config(state="normal")
self.remove_button.config(state="normal")
self.cancel_button.config(state="disabled")
# ─── Scan ───────────────────────────────────────────────────
def start_scan(self, event=None):
if self.scan_button['state'] == 'disabled' and event is not None:
return
if not self.input_files:
messagebox.showwarning("파일 없음", "먼저 PDF 파일을 추가해 주세요.")
return
self.clear_thumbnails()
self._cancel_event.clear()
self._set_buttons_running()
self._scan_start_time = time.time()
self.status_label.config(text="워터마크 후보 스캔 중...")
self.progress_bar["value"] = 0
self.percent_label.config(text="0%")
scan_threshold_percent = self.scan_threshold_var.get()
min_page_ratio = scan_threshold_percent / 100.0
raw_text = self.text_keywords_var.get()
text_keywords = [k.strip() for k in raw_text.strip().split(';') if k.strip()]
threading.Thread(
target=self.scan_worker,
args=(min_page_ratio, text_keywords),
daemon=True
).start()
def start_removal(self, event=None):
if self.remove_button['state'] == 'disabled' and event is not None:
return
# Build removal dict from display groups
candidates_to_remove = defaultdict(lambda: defaultdict(list))
selected_count = 0
for group in self.display_groups:
if not group['var'].get():
continue
selected_count += 1
for member_key in group['member_keys']:
ctype, fpath = member_key[0], member_key[1]
member_data = self.raw_candidates[member_key]
if ctype == 'image':
xref = member_data['xref']
if xref not in candidates_to_remove[fpath]['image']:
candidates_to_remove[fpath]['image'].append(xref)
elif ctype == 'text':
page_num = member_data['page']
bbox = member_data['bbox']
text_info = {'page': page_num, 'bbox': bbox}
candidates_to_remove[fpath]['text'].append(text_info)
if not self.output_dir or not Path(self.output_dir).is_dir():
self.select_output_dir()
if not self.output_dir:
return
sanitize = self.sanitize_var.get()
clean_metadata = self.clean_metadata_var.get()
if not candidates_to_remove and not self.copy_skipped_var.get() and not sanitize and not clean_metadata:
messagebox.showwarning(
"조치 없음",
"제거할 워터마크가 선택되지 않았고, '미처리 파일 복사'가 비활성이며, "
"'숨은 텍스트 제거'와 '메타데이터 정리'도 꺼져 있습니다."
)
return
suffix = self.suffix_var.get()
copy_skipped = self.copy_skipped_var.get()
overwrite = self.overwrite_var.get()
self._cancel_event.clear()
self._set_buttons_running()
self._scan_start_time = time.time()
input_root = None
if self.input_files:
try:
common_path = os.path.commonpath(self.input_files)
if os.path.isdir(common_path):
input_root = common_path
except ValueError:
pass
args = (
self.input_files, candidates_to_remove, suffix,
copy_skipped, overwrite, sanitize, clean_metadata,
input_root, selected_count
)
threading.Thread(target=self.removal_worker, args=args, daemon=True).start()
# ─── Queue Processing ───────────────────────────────────────
def process_queue(self):
try:
while True:
task_type, *payload = self.task_queue.get_nowait()
if task_type == 'scan_complete':
candidates = payload[0]
self.raw_candidates = candidates
self._rebuild_display_groups()
elapsed = time.time() - self._scan_start_time
count = len(self.display_groups)
self.status_label.config(
text=f"스캔 완료. {count}개 후보 발견. ({elapsed:.1f}초)"
)
self.progress_bar["value"] = 100
self.percent_label.config(text="100%")
self._set_buttons_idle()
elif task_type == 'scan_progress':
current, total = payload
progress = int((current / total) * 100) if total > 0 else 0
self.progress_bar["value"] = progress
self.percent_label.config(text=f"{progress}%")
self.status_label.config(text=f"스캔 중... ({current}/{total} 파일)")
elif task_type == 'scan_cancelled':
self.status_label.config(text="스캔이 취소되었습니다.")
self._set_buttons_idle()
elif task_type == 'scan_error':
self.status_label.config(text=f"스캔 실패: {payload[0]}")
self._set_buttons_idle()
elif task_type == 'removal_progress':
n, m = payload
self.status_label.config(text=f"파일 처리 중... ({n}/{m})")
progress = int((n / m) * 100)
self.progress_bar["value"] = progress
self.percent_label.config(text=f"{progress}%")
elif task_type == 'removal_complete':
processed, copied, skipped, failed, selected_count = payload
elapsed = time.time() - self._scan_start_time
self.status_label.config(text="워터마크 제거 완료!")
self.progress_bar["value"] = 100
self.percent_label.config(text="100%")
self.open_folder_button.config(state="normal")
self._show_completion_summary(processed, copied, skipped, failed, selected_count, elapsed)
self._set_buttons_idle()
self.remove_button.config(state="disabled")
elif task_type == 'removal_cancelled':
self.status_label.config(text="제거가 취소되었습니다.")
self._set_buttons_idle()
elif task_type == 'removal_error':
self.status_label.config(text=f"제거 실패: {payload[0]}")
self._set_buttons_idle()
elif task_type == 'log':
self.log_message(payload[0])
except queue.Empty:
pass
finally:
self.after(100, self.process_queue)
# ─── Completion Summary ─────────────────────────────────────
def _show_completion_summary(
self, processed: int, copied: int, skipped: int,
failed: int, selected_count: int, elapsed: float
):
"""Show a detailed completion summary dialog."""
lines = []
lines.append(f"처리 완료")
lines.append(f"")
lines.append(f"제거 대상: {selected_count}개 워터마크 그룹")
lines.append(f"처리됨: {processed}개 파일")
if copied > 0:
lines.append(f"복사됨: {copied}개 파일 (워터마크 없음)")
if skipped > 0:
lines.append(f"건너뜀: {skipped}개 파일")
if failed > 0:
lines.append(f"⚠️ 실패: {failed}개 파일 (로그 참조)")
lines.append(f"소요 시간: {elapsed:.1f}초")
if failed > 0:
messagebox.showwarning("처리 완료 (일부 실패)", "\n".join(lines))
else:
messagebox.showinfo("처리 완료", "\n".join(lines))
# ─── Worker Threads ─────────────────────────────────────────
def scan_worker(self, min_page_ratio: float, text_keywords: List[str]):
try:
candidates = self.core.scan_files_for_watermarks(
self.input_files,
min_page_ratio=min_page_ratio,
text_keywords=text_keywords,
cancel_flag=self._cancel_event,
)
if self._cancel_event.is_set():
self.task_queue.put(('scan_cancelled',))
return
self.task_queue.put(('scan_complete', candidates))
except Exception as e:
logger.error(f"Scan worker error: {e}")
self.task_queue.put(('scan_error', str(e)))
def removal_worker(
self, all_input_files: List[str],
candidates_by_file: Dict[str, Dict[str, List]],
suffix: str, copy_skipped: bool, overwrite: bool,
sanitize: bool, clean_metadata: bool,
input_root: str, selected_count: int
):
total_files = len(all_input_files)
processed = 0
copied = 0
skipped = 0
failed = 0
for i, file_path in enumerate(all_input_files):
if self._cancel_event.is_set():
self.task_queue.put(('removal_cancelled',))
return
self.task_queue.put(('removal_progress', i + 1, total_files))
try:
should_process = (
file_path in candidates_by_file
or sanitize
or clean_metadata
)
if should_process:
to_remove = candidates_by_file.get(file_path, {})
self.core.process_and_remove_watermarks(
file_path, self.output_dir, to_remove, suffix,
overwrite=overwrite,
sanitize_hidden_text=sanitize,
clean_metadata=clean_metadata,
input_dir_root=input_root,
)