-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1577 lines (1297 loc) · 62.1 KB
/
main.py
File metadata and controls
1577 lines (1297 loc) · 62.1 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
import sys
import os
import re
from datetime import datetime
from PyQt6.QtWidgets import (QApplication, QMainWindow, QWidget, QVBoxLayout,
QHBoxLayout, QPushButton, QListWidget, QLabel,
QMessageBox, QAbstractItemView, QRadioButton, QButtonGroup,
QSlider, QGroupBox, QLineEdit, QTabWidget, QCheckBox, QSizePolicy)
from PyQt6.QtCore import Qt, QMimeData, QThread, pyqtSignal
from PyQt6.QtGui import QDragEnterEvent, QDropEvent, QIntValidator, QIcon
from sorter import sort_files
from stitcher import stitch_images
from grid_preview import PreviewDialog
from slicer import slice_image, slice_grid_image
from merger import merge_images_to_pdf
from converter import convert_pdf_to_images, convert_psd_to_images, convert_ppt_to_images
from PyQt6.QtGui import QPixmap, QCursor
from PyQt6.QtCore import QTimer, QPoint
class PreviewListWidget(QListWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.setMouseTracking(True)
self.preview_timer = QTimer(self)
self.preview_timer.setSingleShot(True)
self.preview_timer.timeout.connect(self.show_preview)
self.hover_item = None
self.preview_label = None
self.itemEntered.connect(self.on_item_entered)
# We need to detect when mouse leaves item to stop timer
# itemEntered triggers when mouse MOVES onto an item.
def on_item_entered(self, item):
self.hide_preview() # Hide previous if any
self.hover_item = item
self.preview_timer.start(2000) # 2 seconds
def mouseMoveEvent(self, event):
super().mouseMoveEvent(event)
# If we moved, we might need to reset or check if we are still on same item?
# itemEntered handles switching items.
# But if we move OUT of an item to whitespace?
item = self.itemAt(event.pos())
if item != self.hover_item:
self.hover_item = item
self.hide_preview()
self.preview_timer.stop()
if item:
self.preview_timer.start(2000)
def leaveEvent(self, event):
self.hide_preview()
self.preview_timer.stop()
self.hover_item = None
super().leaveEvent(event)
def show_preview(self):
if not self.hover_item:
return
path = self.hover_item.data(Qt.ItemDataRole.UserRole)
if not path or not os.path.exists(path):
return
# Create Popup
if self.preview_label is None:
self.preview_label = QLabel(self, windowFlags=Qt.WindowType.ToolTip)
self.preview_label.setStyleSheet("border: 2px solid #333; background: white;")
self.preview_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Load and scale image
try:
pixmap = QPixmap(path)
if not pixmap.isNull():
scaled = pixmap.scaled(200, 200, Qt.AspectRatioMode.KeepAspectRatio)
self.preview_label.setPixmap(scaled)
self.preview_label.adjustSize()
# Position near cursor
pos = QCursor.pos()
self.preview_label.move(pos.x() + 20, pos.y() + 20)
self.preview_label.show()
except Exception:
pass
def hide_preview(self):
if self.preview_label:
self.preview_label.hide()
class StitcherThread(QThread):
finished_signal = pyqtSignal(bool, str)
def __init__(self, image_paths, output_dir, split_count, target_width, max_kb, mode='vertical', rows=2, cols=2, output_format='AUTO', custom_name=None):
super().__init__()
self.image_paths = image_paths
self.output_dir = output_dir
self.split_count = split_count
self.target_width = target_width
self.max_kb = max_kb
self.target_width = target_width
self.max_kb = max_kb
self.mode = mode
self.rows = rows
self.cols = cols
self.output_format = output_format
self.custom_name = custom_name
def run(self):
try:
success, message = stitch_images(self.image_paths, self.output_dir, self.split_count, self.target_width, self.max_kb, self.mode, self.rows, self.cols, self.output_format, self.custom_name)
self.finished_signal.emit(success, message)
except Exception as e:
self.finished_signal.emit(False, str(e))
class MergerThread(QThread):
finished_signal = pyqtSignal(bool, str)
def __init__(self, image_paths, output_path, max_kb):
super().__init__()
self.image_paths = image_paths
self.output_path = output_path
self.max_kb = max_kb
def run(self):
try:
success, message = merge_images_to_pdf(self.image_paths, self.output_path, self.max_kb)
self.finished_signal.emit(success, message)
except Exception as e:
self.finished_signal.emit(False, str(e))
class ConverterThread(QThread):
finished_signal = pyqtSignal(bool, str)
def __init__(self, file_paths, output_dir, output_format):
super().__init__()
self.file_paths = file_paths
self.output_dir = output_dir
self.output_format = output_format
def run(self):
success_count = 0
fail_count = 0
details = ""
try:
for path in self.file_paths:
ext = os.path.splitext(path)[1].lower()
res = False
msg = ""
if ext == '.pdf':
res, msg = convert_pdf_to_images(path, self.output_dir, self.output_format)
elif ext == '.psd':
res, msg = convert_psd_to_images(path, self.output_dir, self.output_format)
elif ext in ['.ppt', '.pptx']:
res, msg = convert_ppt_to_images(path, self.output_dir, self.output_format)
else:
msg = "Unsupported format"
if res:
success_count += 1
details += f"\n[成功] {os.path.basename(path)}: {msg}"
else:
fail_count += 1
details += f"\n[失败] {os.path.basename(path)}: {msg}"
final_msg = f"处理完成: 成功 {success_count} 个, 失败 {fail_count} 个。\n{details}"
self.finished_signal.emit(fail_count == 0, final_msg)
except Exception as e:
self.finished_signal.emit(False, str(e))
class SlicerThread(QThread):
finished_signal = pyqtSignal(bool, str)
def __init__(self, image_paths, output_dir, count, smart_mode, target_width, max_kb, direction='horizontal', rows=None, cols=None, output_format='AUTO', custom_name=None):
super().__init__()
self.image_paths = image_paths
self.output_dir = output_dir
self.count = count
self.smart_mode = smart_mode
self.target_width = target_width
self.max_kb = max_kb
self.direction = direction
self.rows = rows
self.cols = cols
self.output_format = output_format
self.custom_name = custom_name
def run(self):
success = True
message = ""
try:
for i, img_path in enumerate(self.image_paths):
# Handle custom name for multiple files?
# If custom name is "MyPic", multiple input files might conflict or need indexing.
# Let's assume custom_name applies mainly to single file slicing or prefixing.
# If multiple files, we probably should append index to folder name or similar.
c_name = self.custom_name
if c_name and len(self.image_paths) > 1:
c_name = f"{c_name}_{i+1}"
if self.direction == 'grid':
max_kb_val = self.max_kb if self.max_kb > 0 else None
s, m = slice_grid_image(img_path, self.output_dir, self.rows, self.cols, self.target_width, max_kb_val, self.output_format, c_name)
else:
max_kb_val = self.max_kb if self.max_kb > 0 else None
s, m = slice_image(img_path, self.output_dir, self.count, self.smart_mode, self.target_width, max_kb_val, self.direction, self.output_format, c_name)
if not s:
success = False
message += f"\nFailed {os.path.basename(img_path)}: {m}"
else:
message += f"\nProcessed {os.path.basename(img_path)}: {m}"
if success:
message = "All images processed successfully!" + message
else:
message = "Some images failed." + message
self.finished_signal.emit(success, message)
except Exception as e:
self.finished_signal.emit(False, str(e))
class ImageMatrixApp(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("ImageMatrix (影像矩阵) - 拼图 & 切图工具")
self.setGeometry(100, 100, 500, 600)
# Data storage
self.merge_images = []
self.slice_images = []
# Store full items in the widget for combine tab to support reordering
# But we still need a list to track dropping?
# Actually for combine tab we update the widget directly with UserRole
self.stitch_thread = None
self.slicer_thread = None
self.merger_thread = None
self.converter_thread = None
self.convert_files = []
self.initUI()
def initUI(self):
central_widget = QWidget()
self.setCentralWidget(central_widget)
main_layout = QVBoxLayout(central_widget)
# Tabs
self.tabs = QTabWidget()
main_layout.addWidget(self.tabs)
# Tab 1: Merge
self.merge_tab = QWidget()
self.init_merge_tab()
self.tabs.addTab(self.merge_tab, "拼图 (Merge)")
# Tab 2: Slice
self.slice_tab = QWidget()
self.init_slice_tab()
self.tabs.addTab(self.slice_tab, "切图 (Slice)")
# Tab 3: Combine
self.combine_tab = QWidget()
self.init_combine_tab()
self.tabs.addTab(self.combine_tab, "合图 (Combine)")
# Tab 4: Convert (Extract)
self.convert_tab = QWidget()
self.init_convert_tab()
self.tabs.addTab(self.convert_tab, "导图 (Convert)")
# Global Enable Drag & Drop
self.setAcceptDrops(True)
def init_merge_tab(self):
layout = QVBoxLayout(self.merge_tab)
# Drop Label
self.merge_drop_label = QLabel("请将图片拖拽到此处\n(支持 .jpg, .jpeg, .png, .pdf)")
self.merge_drop_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.merge_drop_label.setStyleSheet(self._get_drop_style())
layout.addWidget(self.merge_drop_label)
# List Widget
self.merge_list = QListWidget()
self.merge_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
layout.addWidget(self.merge_list)
# Controls
controls_layout = QVBoxLayout()
# Size Selection
size_group = QGroupBox("导出宽度选择")
size_group.setStyleSheet(self._get_group_style())
size_layout = QHBoxLayout()
self.m_radio_original = QRadioButton("原图")
self.m_radio_750 = QRadioButton("750px")
self.m_radio_1080 = QRadioButton("1080px")
self.m_radio_original.setChecked(True)
self.m_radio_custom = QRadioButton("自定义")
self.m_custom_input = QLineEdit()
self.m_custom_input.setPlaceholderText("宽度")
self.m_custom_input.setValidator(QIntValidator(1, 20000))
self.m_custom_input.setFixedWidth(60)
self.m_custom_input.setEnabled(False)
self.m_radio_custom.toggled.connect(lambda c: self.m_custom_input.setEnabled(c))
m_size_btn_group = QButtonGroup(self.merge_tab)
m_size_btn_group.addButton(self.m_radio_original)
m_size_btn_group.addButton(self.m_radio_750)
m_size_btn_group.addButton(self.m_radio_1080)
m_size_btn_group.addButton(self.m_radio_custom)
size_layout.addWidget(self.m_radio_original)
size_layout.addWidget(self.m_radio_750)
size_layout.addWidget(self.m_radio_1080)
size_layout.addWidget(self.m_radio_custom)
size_layout.addWidget(self.m_custom_input)
size_group.setLayout(size_layout)
controls_layout.addWidget(size_group)
# Export Format Selection
format_group = QGroupBox("导出格式")
format_group.setStyleSheet(self._get_group_style())
format_layout = QHBoxLayout()
self.m_radio_fmt_auto = QRadioButton("自动 (默认)")
self.m_radio_fmt_jpg = QRadioButton("JPG")
self.m_radio_fmt_png = QRadioButton("PNG")
self.m_radio_fmt_pdf = QRadioButton("PDF")
self.m_radio_fmt_auto.setChecked(True)
m_fmt_group = QButtonGroup(self.merge_tab)
m_fmt_group.addButton(self.m_radio_fmt_auto)
m_fmt_group.addButton(self.m_radio_fmt_jpg)
m_fmt_group.addButton(self.m_radio_fmt_png)
m_fmt_group.addButton(self.m_radio_fmt_pdf)
format_layout.addWidget(self.m_radio_fmt_auto)
format_layout.addWidget(self.m_radio_fmt_jpg)
format_layout.addWidget(self.m_radio_fmt_png)
format_layout.addWidget(self.m_radio_fmt_pdf)
format_group.setLayout(format_layout)
controls_layout.addWidget(format_group)
# Mode Selection (New)
mode_group = QGroupBox("拼接模式")
mode_group.setStyleSheet(self._get_group_style())
mode_layout = QVBoxLayout()
mode_btn_layout = QHBoxLayout()
self.m_radio_v = QRadioButton("垂直拼接 (默认)")
self.m_radio_h = QRadioButton("水平拼接")
self.m_radio_grid = QRadioButton("宫格拼接")
self.m_radio_v.setChecked(True)
m_mode_group = QButtonGroup(self.merge_tab)
m_mode_group.addButton(self.m_radio_v)
m_mode_group.addButton(self.m_radio_h)
m_mode_group.addButton(self.m_radio_grid)
mode_btn_layout.addWidget(self.m_radio_v)
mode_btn_layout.addWidget(self.m_radio_h)
mode_btn_layout.addWidget(self.m_radio_grid)
mode_layout.addLayout(mode_btn_layout)
# --- Grid UI for Merge ---
self.m_grid_container = QWidget()
self.m_grid_container.setVisible(False)
grid_layout = QVBoxLayout(self.m_grid_container)
grid_layout.setContentsMargins(0, 0, 0, 0)
# Presets (Copied from Slice Tab Logic)
presets_label = QLabel("快速预设:")
grid_layout.addWidget(presets_label)
# Row 1
presets_layout = QHBoxLayout()
presets = [("2x2", 2, 2), ("3x3", 3, 3), ("4x4", 4, 4), ("5x5", 5, 5)]
for label, r, c in presets:
btn = QPushButton(label)
btn.setFixedWidth(50)
btn.clicked.connect(lambda checked, r=r, c=c: self.set_merge_grid_val(r, c))
presets_layout.addWidget(btn)
presets_layout.addStretch()
grid_layout.addLayout(presets_layout)
# Row 2
presets_layout_2 = QHBoxLayout()
presets2 = [("32x32", 32, 32), ("128x128", 128, 128), ("512x512", 512, 512), ("1024x1024", 1024, 1024)]
for label, r, c in presets2:
btn = QPushButton(label)
width = 80 if "1024" in label else 65
btn.setFixedWidth(width)
btn.clicked.connect(lambda checked, r=r, c=c: self.set_merge_grid_val(r, c))
presets_layout_2.addWidget(btn)
presets_layout_2.addStretch()
grid_layout.addLayout(presets_layout_2)
# Custom Input
custom_grid_layout = QHBoxLayout()
custom_grid_layout.addWidget(QLabel("自定义:"))
self.m_grid_rows = QLineEdit()
self.m_grid_rows.setPlaceholderText("行")
self.m_grid_rows.setValidator(QIntValidator(1, 5000))
self.m_grid_cols = QLineEdit()
self.m_grid_cols.setPlaceholderText("列")
self.m_grid_cols.setValidator(QIntValidator(1, 5000))
custom_grid_layout.addWidget(self.m_grid_rows)
custom_grid_layout.addWidget(QLabel("x"))
custom_grid_layout.addWidget(self.m_grid_cols)
grid_layout.addLayout(custom_grid_layout)
mode_layout.addWidget(self.m_grid_container)
mode_group.setLayout(mode_layout)
controls_layout.addWidget(mode_group)
# Connect mode change
self.m_radio_v.toggled.connect(self.update_merge_ui_text)
self.m_radio_h.toggled.connect(self.update_merge_ui_text)
self.m_radio_grid.toggled.connect(self.update_merge_ui_text)
# File Size Limit Selection
# File Size Limit Selection
limit_group = QGroupBox("图片大小限制 (KB)")
limit_group.setStyleSheet(self._get_group_style())
limit_layout = QVBoxLayout()
# New Limit UI with Radio Buttons
self.m_radio_limit_preset = QRadioButton("预设: 150 KB")
self.m_radio_limit_preset.setChecked(True)
self.m_limit_slider = QSlider(Qt.Orientation.Horizontal)
self.m_limit_slider.setMinimum(0)
self.m_limit_slider.setMaximum(5) # 0:Unlimited, 1:150, 2:300, 3:500, 4:750, 5:1000
self.m_limit_slider.setValue(1)
self.m_limit_slider.setTickPosition(QSlider.TickPosition.TicksBelow)
self.m_limit_slider.setTickInterval(1)
self.m_limit_slider.valueChanged.connect(lambda v: self.update_limit_label(v, self.m_radio_limit_preset))
# When slider interacts, auto-check preset radio
self.m_limit_slider.sliderPressed.connect(lambda: self.m_radio_limit_preset.setChecked(True))
self.m_radio_limit_custom = QRadioButton("自定义:")
self.m_limit_custom_input = QLineEdit()
self.m_limit_custom_input.setPlaceholderText("KB")
self.m_limit_custom_input.setValidator(QIntValidator(1, 20000))
self.m_limit_custom_input.setFixedWidth(60)
self.m_limit_custom_input.setEnabled(False)
self.m_radio_limit_custom.toggled.connect(lambda c: self.m_limit_custom_input.setEnabled(c))
limit_btn_group = QButtonGroup(self.merge_tab)
limit_btn_group.addButton(self.m_radio_limit_preset)
limit_btn_group.addButton(self.m_radio_limit_custom)
# Layout row 1: Radio + Slider
row1 = QHBoxLayout()
row1.addWidget(self.m_radio_limit_preset)
row1.addWidget(self.m_limit_slider)
# Layout row 2: Radio + Input
row2 = QHBoxLayout()
row2.addWidget(self.m_radio_limit_custom)
row2.addWidget(self.m_limit_custom_input)
row2.addStretch()
limit_layout.addLayout(row1)
limit_layout.addLayout(row2)
limit_group.setLayout(limit_layout)
controls_layout.addWidget(limit_group)
# Split Selection
self.m_split_group = QGroupBox("分组拼接设置")
self.m_split_group.setStyleSheet(self._get_group_style())
split_layout = QVBoxLayout()
self.m_split_label = QLabel("拼接成:1 张长图")
self.m_split_slider = QSlider(Qt.Orientation.Horizontal)
self.m_split_slider.setMinimum(1)
self.m_split_slider.setMaximum(1)
self.m_split_slider.valueChanged.connect(lambda v: self.m_split_label.setText(f"拼接成:{v} 张图"))
split_layout.addWidget(self.m_split_label)
split_layout.addWidget(self.m_split_slider)
self.m_split_group.setLayout(split_layout)
controls_layout.addWidget(self.m_split_group)
layout.addLayout(controls_layout)
# Filename Input (New)
name_group = QGroupBox("导出文件名 (选填)")
name_group.setStyleSheet(self._get_group_style())
name_layout = QHBoxLayout()
self.m_name_input = QLineEdit()
self.m_name_input.setPlaceholderText("默认为自动生成的日期时间戳")
name_layout.addWidget(self.m_name_input)
name_group.setLayout(name_layout)
layout.addWidget(name_group)
# Buttons
btn_layout = QHBoxLayout()
self.m_clear_btn = QPushButton("清空")
self.m_clear_btn.clicked.connect(self.clear_merge_list)
self.m_clear_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.m_clear_btn.setStyleSheet("padding: 10px;")
btn_layout.addWidget(self.m_clear_btn)
self.m_start_btn = QPushButton("开始拼接 (保存到桌面)")
self.m_start_btn.clicked.connect(self.start_stitching)
self.m_start_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.m_start_btn.setStyleSheet("background-color: #4CAF50; color: white; font-weight: bold; padding: 10px;")
btn_layout.addWidget(self.m_start_btn)
layout.addLayout(btn_layout)
def init_slice_tab(self):
layout = QVBoxLayout(self.slice_tab)
# Drop Label
self.slice_drop_label = QLabel("请将图片拖拽到此处")
self.slice_drop_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.slice_drop_label.setStyleSheet(self._get_drop_style())
layout.addWidget(self.slice_drop_label)
# List Widget
self.slice_list = QListWidget()
layout.addWidget(self.slice_list)
# Controls
controls_layout = QVBoxLayout()
# 1. Width Selection (Full Row)
size_group = QGroupBox("导出宽度")
size_group.setStyleSheet(self._get_group_style())
size_layout = QHBoxLayout()
self.s_radio_original = QRadioButton("原图")
self.s_radio_custom = QRadioButton("指定")
self.s_radio_original.setChecked(True)
self.s_custom_input = QLineEdit()
self.s_custom_input.setPlaceholderText("例如 750")
self.s_custom_input.setValidator(QIntValidator(1, 20000))
self.s_custom_input.setFixedWidth(60)
self.s_custom_input.setEnabled(False)
self.s_radio_custom.toggled.connect(lambda c: self.s_custom_input.setEnabled(c))
s_size_btn_group = QButtonGroup(self.slice_tab)
s_size_btn_group.addButton(self.s_radio_original)
s_size_btn_group.addButton(self.s_radio_custom)
size_layout.addWidget(self.s_radio_original)
size_layout.addWidget(self.s_radio_custom)
size_layout.addWidget(self.s_custom_input)
size_layout.addStretch()
size_group.setLayout(size_layout)
controls_layout.addWidget(size_group)
# 2. Export Format Selection (New)
format_group = QGroupBox("导出格式")
format_group.setStyleSheet(self._get_group_style())
format_layout = QHBoxLayout()
self.s_radio_fmt_auto = QRadioButton("自动 (默认)")
self.s_radio_fmt_jpg = QRadioButton("JPG")
self.s_radio_fmt_png = QRadioButton("PNG")
self.s_radio_fmt_pdf = QRadioButton("PDF")
self.s_radio_fmt_auto.setChecked(True)
s_fmt_group = QButtonGroup(self.slice_tab)
s_fmt_group.addButton(self.s_radio_fmt_auto)
s_fmt_group.addButton(self.s_radio_fmt_jpg)
s_fmt_group.addButton(self.s_radio_fmt_png)
s_fmt_group.addButton(self.s_radio_fmt_pdf)
format_layout.addWidget(self.s_radio_fmt_auto)
format_layout.addWidget(self.s_radio_fmt_jpg)
format_layout.addWidget(self.s_radio_fmt_png)
format_layout.addWidget(self.s_radio_fmt_pdf)
format_group.setLayout(format_layout)
controls_layout.addWidget(format_group)
# 2. Limit (Group Box style similar to Merge Tab)
limit_group = QGroupBox("图片大小限制 (KB)")
limit_group.setStyleSheet(self._get_group_style())
limit_layout = QVBoxLayout()
self.s_radio_limit_preset = QRadioButton("预设: 150 KB")
self.s_radio_limit_preset.setChecked(True)
self.s_limit_slider = QSlider(Qt.Orientation.Horizontal)
self.s_limit_slider.setMinimum(0)
self.s_limit_slider.setMaximum(5)
self.s_limit_slider.setValue(1)
self.s_limit_slider.setTickPosition(QSlider.TickPosition.TicksBelow)
self.s_limit_slider.setTickInterval(1)
self.s_limit_slider.valueChanged.connect(lambda v: self.update_limit_label(v, self.s_radio_limit_preset))
self.s_limit_slider.sliderPressed.connect(lambda: self.s_radio_limit_preset.setChecked(True))
self.s_radio_limit_custom = QRadioButton("自定义:")
self.s_limit_custom_input = QLineEdit()
self.s_limit_custom_input.setPlaceholderText("KB")
self.s_limit_custom_input.setValidator(QIntValidator(1, 20000))
self.s_limit_custom_input.setFixedWidth(60)
self.s_limit_custom_input.setEnabled(False)
self.s_radio_limit_custom.toggled.connect(lambda c: self.s_limit_custom_input.setEnabled(c))
s_limit_btn_group = QButtonGroup(self.slice_tab)
s_limit_btn_group.addButton(self.s_radio_limit_preset)
s_limit_btn_group.addButton(self.s_radio_limit_custom)
row1 = QHBoxLayout()
row1.addWidget(self.s_radio_limit_preset)
row1.addWidget(self.s_limit_slider)
row2 = QHBoxLayout()
row2.addWidget(self.s_radio_limit_custom)
row2.addWidget(self.s_limit_custom_input)
row2.addStretch()
limit_layout.addLayout(row1)
limit_layout.addLayout(row2)
limit_group.setLayout(limit_layout)
controls_layout.addWidget(limit_group)
# 3. Settings Group
slice_group = QGroupBox("切图模式 & 设置")
slice_group.setStyleSheet(self._get_group_style())
slice_inner_layout = QVBoxLayout()
# Direction/Mode Selection
dir_layout = QHBoxLayout()
self.s_dir_label = QLabel("模式:")
self.s_radio_h = QRadioButton("水平切")
self.s_radio_v = QRadioButton("垂直切")
self.s_radio_grid = QRadioButton("宫格 (Grid)")
self.s_radio_h.setChecked(True)
self.s_radio_h.toggled.connect(self.update_slice_ui_text)
self.s_radio_v.toggled.connect(self.update_slice_ui_text)
self.s_radio_grid.toggled.connect(self.update_slice_ui_text)
dir_btn_group = QButtonGroup(self.slice_tab)
dir_btn_group.addButton(self.s_radio_h)
dir_btn_group.addButton(self.s_radio_v)
dir_btn_group.addButton(self.s_radio_grid)
dir_layout.addWidget(self.s_dir_label)
dir_layout.addWidget(self.s_radio_h)
dir_layout.addWidget(self.s_radio_v)
dir_layout.addWidget(self.s_radio_grid)
slice_inner_layout.addLayout(dir_layout)
# --- Linear UI (Slider & Smart Check) ---
self.s_linear_container = QWidget()
linear_layout = QVBoxLayout(self.s_linear_container)
linear_layout.setContentsMargins(0, 0, 0, 0)
count_layout = QHBoxLayout()
self.s_count_label = QLabel("切成:5 行")
self.s_count_slider = QSlider(Qt.Orientation.Horizontal)
self.s_count_slider.setMinimum(1)
self.s_count_slider.setMaximum(50)
self.s_count_slider.setValue(5)
self.s_count_slider.setTickPosition(QSlider.TickPosition.TicksBelow)
self.s_count_slider.setTickInterval(5)
self.s_count_slider.valueChanged.connect(self.update_slice_ui_text)
count_layout.addWidget(self.s_count_label)
linear_layout.addLayout(count_layout)
linear_layout.addWidget(self.s_count_slider)
self.s_smart_check = QCheckBox("智能吸附 (避开内容)")
self.s_smart_check.setChecked(True)
linear_layout.addWidget(self.s_smart_check)
slice_inner_layout.addWidget(self.s_linear_container)
# --- Grid UI ---
self.s_grid_container = QWidget()
self.s_grid_container.setVisible(False)
grid_layout = QVBoxLayout(self.s_grid_container)
grid_layout.setContentsMargins(0, 0, 0, 0)
# Presets
presets_label = QLabel("快速预设:")
grid_layout.addWidget(presets_label)
# Row 1: 2x2, 3x3, 4x4, 5x5
presets_layout = QHBoxLayout()
presets = [("2x2", 2, 2), ("3x3", 3, 3), ("4x4", 4, 4), ("5x5", 5, 5)]
for label, r, c in presets:
btn = QPushButton(label)
btn.setFixedWidth(50)
btn.clicked.connect(lambda checked, r=r, c=c: self.set_grid_val(r, c))
presets_layout.addWidget(btn)
presets_layout.addStretch()
grid_layout.addLayout(presets_layout)
# Row 2: 32x32, 128x128, 512x512, 1024x1024
presets_layout_2 = QHBoxLayout()
presets2 = [("32x32", 32, 32), ("128x128", 128, 128), ("512x512", 512, 512), ("1024x1024", 1024, 1024)]
for label, r, c in presets2:
btn = QPushButton(label)
# Adjust width slightly for longer text
width = 80 if "1024" in label else 65
btn.setFixedWidth(width)
btn.clicked.connect(lambda checked, r=r, c=c: self.set_grid_val(r, c))
presets_layout_2.addWidget(btn)
presets_layout_2.addStretch()
grid_layout.addLayout(presets_layout_2)
# Custom Input
custom_grid_layout = QHBoxLayout()
custom_grid_layout.addWidget(QLabel("自定义:"))
self.s_grid_rows = QLineEdit()
self.s_grid_rows.setPlaceholderText("行(Rows)")
self.s_grid_rows.setValidator(QIntValidator(1, 5000))
self.s_grid_cols = QLineEdit()
self.s_grid_cols.setPlaceholderText("列(Cols)")
self.s_grid_cols.setValidator(QIntValidator(1, 5000))
custom_grid_layout.addWidget(self.s_grid_rows)
custom_grid_layout.addWidget(QLabel("x"))
custom_grid_layout.addWidget(self.s_grid_cols)
self.s_preview_btn = QPushButton("预览效果")
self.s_preview_btn.clicked.connect(self.preview_grid)
custom_grid_layout.addWidget(self.s_preview_btn)
grid_layout.addLayout(custom_grid_layout)
slice_inner_layout.addWidget(self.s_grid_container)
slice_group.setLayout(slice_inner_layout)
controls_layout.addWidget(slice_group)
layout.addLayout(controls_layout)
# Filename Input (New)
name_group = QGroupBox("导出文件名 (选填)")
name_group.setStyleSheet(self._get_group_style())
name_layout = QHBoxLayout()
self.s_name_input = QLineEdit()
self.s_name_input.setPlaceholderText("默认为文件夹/原图名")
name_layout.addWidget(self.s_name_input)
name_group.setLayout(name_layout)
layout.addWidget(name_group)
# Buttons
btn_layout = QHBoxLayout()
self.s_clear_btn = QPushButton("清空")
self.s_clear_btn.clicked.connect(self.clear_slice_list)
self.s_clear_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.s_clear_btn.setStyleSheet("padding: 10px;")
btn_layout.addWidget(self.s_clear_btn)
self.s_start_btn = QPushButton("开始切图 (保存到桌面)")
self.s_start_btn.clicked.connect(self.start_slicing)
self.s_start_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.s_start_btn.setStyleSheet("background-color: #2196F3; color: white; font-weight: bold; padding: 10px;")
btn_layout.addWidget(self.s_start_btn)
layout.addLayout(btn_layout)
# Setup Delete Functionality for Lists
self.setup_list_actions(self.merge_list, self.delete_merge_items, self.rename_merge_items)
self.setup_list_actions(self.slice_list, self.delete_slice_items, self.rename_slice_items)
def init_combine_tab(self):
layout = QVBoxLayout(self.combine_tab)
# Drop Label
self.combine_drop_label = QLabel("请将文件或文件夹拖拽到此处\n(支持 .jpg, .png, .pdf)\n列表支持拖拽调整顺序")
self.combine_drop_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.combine_drop_label.setStyleSheet(self._get_drop_style())
layout.addWidget(self.combine_drop_label)
# List Widget
self.combine_list = PreviewListWidget()
self.combine_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
self.combine_list.setDragDropMode(QAbstractItemView.DragDropMode.InternalMove)
self.combine_list.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
layout.addWidget(self.combine_list)
# Controls
controls_layout = QVBoxLayout()
# Sorting Group (New)
sort_group = QGroupBox("排序 (Sort)")
sort_group.setStyleSheet(self._get_group_style())
sort_layout = QHBoxLayout()
btn_sort_name_asc = QPushButton("名称 A-Z")
btn_sort_name_asc.clicked.connect(lambda: self.sort_combine_list('name', True))
btn_sort_name_desc = QPushButton("名称 Z-A")
btn_sort_name_desc.clicked.connect(lambda: self.sort_combine_list('name', False))
btn_sort_size_asc = QPushButton("大小 小->大")
btn_sort_size_asc.clicked.connect(lambda: self.sort_combine_list('size', True))
btn_sort_size_desc = QPushButton("大小 大->小")
btn_sort_size_desc.clicked.connect(lambda: self.sort_combine_list('size', False))
sort_layout.addWidget(btn_sort_name_asc)
sort_layout.addWidget(btn_sort_name_desc)
sort_layout.addWidget(btn_sort_size_asc)
sort_layout.addWidget(btn_sort_size_desc)
sort_group.setLayout(sort_layout)
controls_layout.addWidget(sort_group)
# Limit Group
limit_layout = QHBoxLayout()
self.c_limit_label = QLabel("单页限制: 1 MB")
self.c_limit_slider = QSlider(Qt.Orientation.Horizontal)
self.c_limit_slider.setMinimum(0)
self.c_limit_slider.setMaximum(5)
self.c_limit_slider.setValue(1) # Default to index 1 -> 1MB? Or 0? Let's say 1
self.c_limit_slider.setTickPosition(QSlider.TickPosition.TicksBelow)
self.c_limit_slider.setTickInterval(1)
self.c_limit_slider.valueChanged.connect(lambda v: self.update_limit_label_new(v, self.c_limit_label))
limit_layout.addWidget(self.c_limit_label)
limit_layout.addWidget(self.c_limit_slider)
limit_group = QGroupBox("PDF大小限制 (Compress)")
limit_group.setStyleSheet(self._get_group_style())
limit_group.setLayout(limit_layout)
controls_layout.addWidget(limit_group)
# Filename Input
name_group = QGroupBox("导出文件名 (选填)")
name_group.setStyleSheet(self._get_group_style())
name_layout = QHBoxLayout()
self.c_name_input = QLineEdit()
self.c_name_input.setPlaceholderText("默认为 combine_日期.pdf")
name_layout.addWidget(self.c_name_input)
name_group.setLayout(name_layout)
controls_layout.addWidget(name_group)
layout.addLayout(controls_layout)
# Buttons
btn_layout = QHBoxLayout()
self.c_clear_btn = QPushButton("清空")
self.c_clear_btn.clicked.connect(self.clear_combine_list)
self.c_clear_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.c_clear_btn.setStyleSheet("padding: 10px;")
btn_layout.addWidget(self.c_clear_btn)
self.c_start_btn = QPushButton("开始合并 (保存到桌面)")
self.c_start_btn.clicked.connect(self.start_combining)
self.c_start_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.c_start_btn.setStyleSheet("background-color: #FF9800; color: white; font-weight: bold; padding: 10px;")
btn_layout.addWidget(self.c_start_btn)
layout.addLayout(btn_layout)
self.setup_list_actions(self.combine_list, self.delete_combine_items, self.rename_combine_items)
def init_convert_tab(self):
layout = QVBoxLayout(self.convert_tab)
# Drop Label
self.convert_drop_label = QLabel("请将 PDF / PSD / PPT 文件拖拽到此处")
self.convert_drop_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.convert_drop_label.setStyleSheet(self._get_drop_style())
layout.addWidget(self.convert_drop_label)
# List
self.convert_list = QListWidget()
self.convert_list.setSelectionMode(QAbstractItemView.SelectionMode.ExtendedSelection)
layout.addWidget(self.convert_list)
# Controls
controls_layout = QVBoxLayout()
# Format Selection
format_group = QGroupBox("导出格式")
format_group.setStyleSheet(self._get_group_style())
format_layout = QHBoxLayout()
self.cv_radio_jpg = QRadioButton("JPG (推荐)")
self.cv_radio_png = QRadioButton("PNG")
self.cv_radio_jpg.setChecked(True)
cv_fmt_group = QButtonGroup(self.convert_tab)
cv_fmt_group.addButton(self.cv_radio_jpg)
cv_fmt_group.addButton(self.cv_radio_png)
format_layout.addWidget(self.cv_radio_jpg)
format_layout.addWidget(self.cv_radio_png)
format_group.setLayout(format_layout)
controls_layout.addWidget(format_group)
layout.addLayout(controls_layout)
# Buttons
btn_layout = QHBoxLayout()
self.cv_clear_btn = QPushButton("清空")
self.cv_clear_btn.clicked.connect(self.clear_convert_list)
self.cv_clear_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.cv_clear_btn.setStyleSheet("padding: 10px;")
btn_layout.addWidget(self.cv_clear_btn)
self.cv_start_btn = QPushButton("开始转换 (保存到桌面)")
self.cv_start_btn.clicked.connect(self.start_converting)
self.cv_start_btn.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Fixed)
self.cv_start_btn.setStyleSheet("background-color: #9C27B0; color: white; font-weight: bold; padding: 10px;")
btn_layout.addWidget(self.cv_start_btn)
layout.addLayout(btn_layout)
self.setup_list_actions(self.convert_list, self.delete_convert_items, lambda: None)
def _get_drop_style(self):
return """
QLabel {
border: 2px dashed #aaa;
border-radius: 10px;
padding: 15px;
font-size: 14px;
color: #555;
background-color: #f0f0f0;
}
"""
def _get_group_style(self):
return """
QGroupBox {
border: 1px solid #d0d0d0;
border-radius: 5px;
margin-top: 10px;
padding-top: 10px;
}
QGroupBox::title {
subcontrol-origin: margin;
subcontrol-position: top left;
padding: 0 3px;
color: #555;
}
"""
def setup_list_actions(self, list_widget, delete_slot, rename_slot):
list_widget.setContextMenuPolicy(Qt.ContextMenuPolicy.CustomContextMenu)
list_widget.customContextMenuRequested.connect(lambda pos: self.show_context_menu(pos, list_widget, delete_slot, rename_slot))
def keyPressEvent(self, event):
if event.key() == Qt.Key.Key_Delete:
if self.tabs.currentIndex() == 0 and self.merge_list.hasFocus():
self.delete_merge_items()
elif self.tabs.currentIndex() == 1 and self.slice_list.hasFocus():
self.delete_slice_items()
elif self.tabs.currentIndex() == 2 and self.combine_list.hasFocus():
self.delete_combine_items()
elif self.tabs.currentIndex() == 3 and self.convert_list.hasFocus():
self.delete_convert_items()
super().keyPressEvent(event)
def show_context_menu(self, pos, list_widget, delete_slot, rename_slot):
from PyQt6.QtWidgets import QMenu
menu = QMenu(self)
rename_action = menu.addAction("重命名 (Rename)")
rename_action.triggered.connect(rename_slot)
delete_action = menu.addAction("删除 (Delete)")
delete_action.triggered.connect(delete_slot)
menu.exec(list_widget.mapToGlobal(pos))
def delete_merge_items(self):
self._delete_items(self.merge_list, self.merge_images)
self.m_split_slider.setMaximum(max(1, len(self.merge_images)))
def delete_slice_items(self):
self._delete_items(self.slice_list, self.slice_images)
def _delete_items(self, list_widget, data_list):
items = list_widget.selectedItems()
if not items:
return
# Build list of rows to delete to handle indices correctly
rows_to_delete = sorted([list_widget.row(item) for item in items], reverse=True)
for row in rows_to_delete:
del data_list[row]