-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathsubtitle_window.py
More file actions
887 lines (757 loc) · 30.6 KB
/
subtitle_window.py
File metadata and controls
887 lines (757 loc) · 30.6 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
"""
Subtitle window - clean text-only window for OBS capture.
Uses QPainterPath for outlined text rendering.
Usage:
- Middle-click drag to move the window
- Configure via tray menu → Subtitle Mode → Settings
- OBS: Window Capture → select "LiveTranslate Subtitle" → check "Allow Transparency"
"""
import time
from pathlib import Path
import json
from PyQt6.QtCore import (
Qt, QPoint, QRect, pyqtSignal, pyqtSlot, pyqtProperty,
QPropertyAnimation, QParallelAnimationGroup, QEasingCurve, QTimer,
)
from PyQt6.QtGui import (
QColor,
QFont,
QFontMetrics,
QPainter,
QPainterPath,
QPen,
QBrush,
QPixmap,
)
from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout
def _resolve_image_path(path: str) -> str:
"""Resolve image path (relative to project dir or absolute)."""
if not path:
return ""
p = Path(path)
if p.is_absolute():
return str(p) if p.exists() else ""
resolved = Path(__file__).parent / p
return str(resolved) if resolved.exists() else ""
# Default subtitle window settings
DEFAULT_SUBTITLE_WIN_SETTINGS = {
"enabled": False,
"sentences": 1,
"window_width": 1000,
"line_spacing": 8,
"bg_color": "#000000",
"bg_opacity": 76,
"bg_image": "",
"border_radius": 8,
"auto_hide_timeout": 5,
"auto_hide_animation": "fade",
"auto_hide_duration": 300,
"lines": [
{
"type": "original",
"enabled": True,
"font_family": "Microsoft YaHei",
"font_size": 24,
"color": "#FFFFFF",
"opacity": 255,
"outline_enabled": True,
"outline_color": "#000000",
"outline_width": 2,
"align": "center",
"bg_image": "",
"entry_animation": "none",
"exit_animation": "none",
"animation_duration": 300,
},
{
"type": "translation",
"lang": "zh",
"enabled": True,
"font_family": "Microsoft YaHei",
"font_size": 28,
"color": "#FFD700",
"opacity": 255,
"outline_enabled": True,
"outline_color": "#000000",
"outline_width": 2,
"align": "center",
"bg_image": "",
"entry_animation": "none",
"exit_animation": "none",
"animation_duration": 300,
},
],
}
def _merge_settings(base, override):
result = {**base}
for k, v in (override or {}).items():
if k == "lines" and isinstance(v, list):
result["lines"] = v
else:
result[k] = v
return result
def _hex_to_rgba(hex_color: str, opacity: int) -> str:
hex_color = hex_color.lstrip("#")
r = int(hex_color[0:2], 16)
g = int(hex_color[2:4], 16)
b = int(hex_color[4:6], 16)
return f"rgba({r},{g},{b},{opacity})"
class _SubtitleTextWidget(QWidget):
"""Renders outlined text using QPainterPath, with automatic word-wrap.
Supports entry/exit animations via custom properties.
"""
height_changed = pyqtSignal()
def __init__(self, parent=None):
super().__init__(parent)
self._text = ""
self._wrapped_lines = []
self._font = QFont("Microsoft YaHei", 24)
self._color = QColor(255, 255, 255)
self._outline_enabled = True
self._outline_color = QColor(0, 0, 0)
self._outline_width = 2
self._align = "center"
self._bg_pixmap = None
self._text_cache = None
self._last_width = 0
self.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
# Animation state
self._content_opacity_val = 1.0
self._slide_offset_x_val = 0.0
self._slide_offset_y_val = 0.0
self._entry_animation = "none"
self._exit_animation = "none"
self._animation_duration = 300
self._anim_group = None
self._pending_text = None
# --- pyqtProperty for content opacity ---
def _get_content_opacity(self):
return self._content_opacity_val
def _set_content_opacity(self, val):
self._content_opacity_val = val
self.update()
content_opacity = pyqtProperty(float, _get_content_opacity, _set_content_opacity)
# --- pyqtProperty for slide offsets ---
def _get_slide_offset_x(self):
return self._slide_offset_x_val
def _set_slide_offset_x(self, val):
self._slide_offset_x_val = val
self.update()
slide_offset_x = pyqtProperty(float, _get_slide_offset_x, _set_slide_offset_x)
def _get_slide_offset_y(self):
return self._slide_offset_y_val
def _set_slide_offset_y(self, val):
self._slide_offset_y_val = val
self.update()
slide_offset_y = pyqtProperty(float, _get_slide_offset_y, _set_slide_offset_y)
def set_config(self, cfg: dict):
self._font = QFont(cfg.get("font_family", "Microsoft YaHei"), cfg.get("font_size", 24))
c = QColor(cfg.get("color", "#FFFFFF"))
c.setAlpha(cfg.get("opacity", 255))
self._color = c
self._outline_enabled = cfg.get("outline_enabled", True)
self._outline_color = QColor(cfg.get("outline_color", "#000000"))
self._outline_width = cfg.get("outline_width", 2)
self._align = cfg.get("align", "center")
resolved = _resolve_image_path(cfg.get("bg_image", ""))
self._bg_pixmap = QPixmap(resolved) if resolved else None
self._entry_animation = cfg.get("entry_animation", "none")
self._exit_animation = cfg.get("exit_animation", "none")
self._animation_duration = cfg.get("animation_duration", 300)
self._text_cache = None
self._update_height()
self.update()
def set_text(self, text: str):
if self._text and text != self._text and self._exit_animation != "none":
self._pending_text = text
self._stop_all_animations()
self.animate_out(callback=self._apply_pending_text)
return
self._apply_text_immediate(text)
def _apply_pending_text(self):
text = getattr(self, "_pending_text", "")
self._pending_text = None
self._apply_text_immediate(text)
def _apply_text_immediate(self, text: str):
# Stop any running animations and reset to final state
self._stop_all_animations()
self._content_opacity_val = 1.0
self._slide_offset_x_val = 0.0
self._slide_offset_y_val = 0.0
self._pending_text = None
self._text = text
self._text_cache = None
self._update_height()
self.update()
self.height_changed.emit()
if text:
self.animate_in()
def _stop_all_animations(self):
if self._anim_group and self._anim_group.state() != self._anim_group.State.Stopped:
self._anim_group.stop()
self._anim_group = None
def animate_in(self):
anim_type = self._entry_animation
if anim_type == "none":
self._content_opacity_val = 1.0
self._slide_offset_x_val = 0.0
self._slide_offset_y_val = 0.0
self.update()
return
dur = self._animation_duration
group = QParallelAnimationGroup(self)
# Opacity animation (all types fade in)
opacity_anim = QPropertyAnimation(self, b"content_opacity", self)
opacity_anim.setDuration(dur)
opacity_anim.setStartValue(0.0)
opacity_anim.setEndValue(1.0)
opacity_anim.setEasingCurve(QEasingCurve.Type.OutCubic)
group.addAnimation(opacity_anim)
w = self.width() or 200
h = self.height() or 40
if anim_type == "slide_left":
slide = QPropertyAnimation(self, b"slide_offset_x", self)
slide.setDuration(dur)
slide.setStartValue(float(-w))
slide.setEndValue(0.0)
slide.setEasingCurve(QEasingCurve.Type.OutCubic)
group.addAnimation(slide)
elif anim_type == "slide_right":
slide = QPropertyAnimation(self, b"slide_offset_x", self)
slide.setDuration(dur)
slide.setStartValue(float(w))
slide.setEndValue(0.0)
slide.setEasingCurve(QEasingCurve.Type.OutCubic)
group.addAnimation(slide)
elif anim_type == "slide_up":
slide = QPropertyAnimation(self, b"slide_offset_y", self)
slide.setDuration(dur)
slide.setStartValue(float(h))
slide.setEndValue(0.0)
slide.setEasingCurve(QEasingCurve.Type.OutCubic)
group.addAnimation(slide)
elif anim_type == "slide_down":
slide = QPropertyAnimation(self, b"slide_offset_y", self)
slide.setDuration(dur)
slide.setStartValue(float(-h))
slide.setEndValue(0.0)
slide.setEasingCurve(QEasingCurve.Type.OutCubic)
group.addAnimation(slide)
self._content_opacity_val = 0.0
self.update()
self._anim_group = group
group.start()
def animate_out(self, callback=None, anim_type=None, duration=None):
if anim_type is None:
anim_type = self._exit_animation
if duration is None:
duration = self._animation_duration
if anim_type == "none":
self._content_opacity_val = 0.0
self.update()
if callback:
callback()
return
self._stop_all_animations()
group = QParallelAnimationGroup(self)
opacity_anim = QPropertyAnimation(self, b"content_opacity", self)
opacity_anim.setDuration(duration)
opacity_anim.setStartValue(self._content_opacity_val)
opacity_anim.setEndValue(0.0)
opacity_anim.setEasingCurve(QEasingCurve.Type.InCubic)
group.addAnimation(opacity_anim)
w = self.width() or 200
h = self.height() or 40
if anim_type == "slide_left":
slide = QPropertyAnimation(self, b"slide_offset_x", self)
slide.setDuration(duration)
slide.setStartValue(0.0)
slide.setEndValue(float(-w))
slide.setEasingCurve(QEasingCurve.Type.InCubic)
group.addAnimation(slide)
elif anim_type == "slide_right":
slide = QPropertyAnimation(self, b"slide_offset_x", self)
slide.setDuration(duration)
slide.setStartValue(0.0)
slide.setEndValue(float(w))
slide.setEasingCurve(QEasingCurve.Type.InCubic)
group.addAnimation(slide)
elif anim_type == "slide_up":
slide = QPropertyAnimation(self, b"slide_offset_y", self)
slide.setDuration(duration)
slide.setStartValue(0.0)
slide.setEndValue(float(-h))
slide.setEasingCurve(QEasingCurve.Type.InCubic)
group.addAnimation(slide)
elif anim_type == "slide_down":
slide = QPropertyAnimation(self, b"slide_offset_y", self)
slide.setDuration(duration)
slide.setStartValue(0.0)
slide.setEndValue(float(h))
slide.setEasingCurve(QEasingCurve.Type.InCubic)
group.addAnimation(slide)
if callback:
group.finished.connect(callback)
self._anim_group = group
group.start()
def split_text(self, text: str) -> list:
"""Split text into segments that fit within available width."""
fm = QFontMetrics(self._font)
ow = self._outline_width if self._outline_enabled else 0
avail_w = self.width() - ow * 2
if avail_w <= 0 or fm.horizontalAdvance(text) <= avail_w:
return [text]
segments = []
while text:
if fm.horizontalAdvance(text) <= avail_w:
segments.append(text)
break
best = 0
for i in range(1, len(text) + 1):
if fm.horizontalAdvance(text[:i]) > avail_w:
break
best = i
if best == 0:
best = 1
# Prefer breaking at word/punctuation boundary
break_at = best
for j in range(best - 1, max(best // 2, 0), -1):
if text[j] in ' ,,。、!!??;;::.':
break_at = j + 1
break
segments.append(text[:break_at].rstrip())
text = text[break_at:].lstrip()
return segments or [text]
def _rewrap(self):
"""Recalculate wrapped lines from current text."""
if not self._text:
self._wrapped_lines = []
else:
self._wrapped_lines = self.split_text(self._text)
def desired_height(self) -> int:
fm = QFontMetrics(self._font)
ow = self._outline_width if self._outline_enabled else 0
n = max(len(self._wrapped_lines), 1)
return fm.lineSpacing() * n + ow * 2 + 4
def _update_height(self):
self._rewrap()
self.setFixedHeight(self.desired_height())
def resizeEvent(self, event):
super().resizeEvent(event)
w = self.width()
if w != self._last_width:
self._last_width = w
self._rewrap()
self._text_cache = None
self.setFixedHeight(self.desired_height())
def _render_text_pixmap(self):
lines = self._wrapped_lines or [self._text]
w = self.width()
h = self.desired_height()
if w <= 0 or h <= 0:
self._text_cache = None
return
dpr = self.devicePixelRatioF()
pw, ph = int(w * dpr), int(h * dpr)
if pw <= 0 or ph <= 0:
self._text_cache = None
return
pix = QPixmap(pw, ph)
pix.setDevicePixelRatio(dpr)
pix.fill(QColor(0, 0, 0, 0))
painter = QPainter(pix)
if not painter.isActive():
self._text_cache = None
return
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
fm = QFontMetrics(self._font)
ow = self._outline_width if self._outline_enabled else 0
y = ow + fm.ascent()
path = QPainterPath()
for line in lines:
text_w = fm.horizontalAdvance(line)
if self._align == "center":
lx = (w - text_w) / 2
elif self._align == "right":
lx = w - text_w - ow
else:
lx = ow
path.addText(lx, y, self._font, line)
y += fm.lineSpacing()
if self._outline_enabled and self._outline_width > 0:
pen = QPen(self._outline_color, self._outline_width * 2,
Qt.PenStyle.SolidLine, Qt.PenCapStyle.RoundCap, Qt.PenJoinStyle.RoundJoin)
painter.setPen(pen)
painter.setBrush(Qt.BrushStyle.NoBrush)
painter.drawPath(path)
painter.setPen(Qt.PenStyle.NoPen)
painter.setBrush(QBrush(self._color))
painter.drawPath(path)
painter.end()
self._text_cache = pix
def paintEvent(self, event):
if not self._text:
return
if self._text_cache is None:
self._render_text_pixmap()
if self._text_cache is None:
return
painter = QPainter(self)
painter.setOpacity(self._content_opacity_val)
if self._bg_pixmap and not self._bg_pixmap.isNull():
painter.drawPixmap(self.rect(), self._bg_pixmap)
painter.drawPixmap(
int(self._slide_offset_x_val),
int(self._slide_offset_y_val),
self._text_cache,
)
painter.end()
class SubtitleWindow(QWidget):
"""Clean text-only subtitle window for OBS capture.
Middle-click anywhere to drag. Window width is fixed (set in settings),
height auto-fits to text content.
"""
update_text_signal = pyqtSignal(str, str) # original, translations_json
position_changed = pyqtSignal()
window_closed = pyqtSignal()
def __init__(self, settings=None):
super().__init__()
self._settings = _merge_settings(DEFAULT_SUBTITLE_WIN_SETTINGS, settings)
self._text_widgets = []
self._sentences = [] # [(original, {lang: text, ...}), ...]
self._drag_pos = None
self._bg_pixmap = None
# Auto-hide state
self._auto_hide_timer = QTimer(self)
self._auto_hide_timer.setSingleShot(True)
self._auto_hide_timer.timeout.connect(self._on_auto_hide_timeout)
self._is_hidden_by_timeout = False
# Pending overflow segments for delayed insertion
self._pending_segment_timers = []
# Minimum display time: queue rapid updates instead of replacing instantly
self._last_insert_time = 0.0
self._min_display_ms = 1500 # minimum ms before a sentence can be replaced
self._height_anim = None
self._setup_ui()
self.update_text_signal.connect(self._on_update_text)
@staticmethod
def _is_pos_visible(x, y, margin=50):
for screen in QApplication.screens():
geo = screen.availableGeometry()
if geo.left() <= x + margin and x < geo.right() and geo.top() <= y + margin and y < geo.bottom():
return True
return False
def _clamp_to_screen(self):
x, y = self.x(), self.y()
if self._is_pos_visible(x, y):
return
screen = QApplication.screenAt(QPoint(x, y))
if screen is None:
screen = QApplication.primaryScreen()
geo = screen.availableGeometry()
nx = max(geo.left(), min(x, geo.right() - self.width()))
ny = max(geo.top(), min(y, geo.bottom() - self.height()))
self.move(nx, ny)
def _setup_ui(self):
self.setWindowFlags(
Qt.WindowType.FramelessWindowHint
| Qt.WindowType.WindowStaysOnTopHint
)
self.setWindowTitle("LiveTranslate Subtitle")
self.setAttribute(Qt.WidgetAttribute.WA_ShowWithoutActivating)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground, True)
s = self._settings
w = s.get("window_width", 1000)
saved_x = s.get("window_x")
saved_y = s.get("window_y")
if saved_x is not None and saved_y is not None:
if self._is_pos_visible(saved_x, saved_y):
self.move(saved_x, saved_y)
else:
self.move(100, 100)
else:
self.move(100, 100)
self.setFixedWidth(w)
self._main_layout = QVBoxLayout(self)
self._main_layout.setContentsMargins(0, 0, 0, 0)
self._main_layout.setSpacing(0)
# Content area
self._content = QWidget()
self._content.setAttribute(Qt.WidgetAttribute.WA_TransparentForMouseEvents)
self._content_layout = QVBoxLayout(self._content)
self._content_layout.setContentsMargins(16, 8, 16, 8)
self._content_layout.setSpacing(s.get("line_spacing", 8))
self._rebuild_text_widgets()
self._main_layout.addWidget(self._content)
self._apply_background()
self._fit_height_animated()
def _rebuild_text_widgets(self):
for w in self._text_widgets:
self._content_layout.removeWidget(w)
w.deleteLater()
self._text_widgets = []
for line_cfg in self._settings.get("lines", []):
if not line_cfg.get("enabled", True):
continue
tw = _SubtitleTextWidget()
tw.set_config(line_cfg)
tw.height_changed.connect(self._fit_height_animated)
self._text_widgets.append(tw)
self._content_layout.addWidget(tw)
def _apply_background(self):
s = self._settings
resolved = _resolve_image_path(s.get("bg_image", ""))
if resolved:
self._bg_pixmap = QPixmap(resolved)
self._content.setStyleSheet("background: transparent;")
else:
self._bg_pixmap = None
color = s.get("bg_color", "#000000")
opacity = s.get("bg_opacity", 0)
if opacity == 0:
self._content.setStyleSheet("background: transparent;")
else:
rgba = _hex_to_rgba(color, opacity)
radius = s.get("border_radius", 8)
self._content.setStyleSheet(f"background: {rgba}; border-radius: {radius}px;")
self.update()
def _calc_target_height(self):
margins = self._content_layout.contentsMargins()
spacing = self._content_layout.spacing()
total = margins.top() + margins.bottom()
for i, tw in enumerate(self._text_widgets):
total += tw.desired_height()
if i > 0:
total += spacing
return max(total, 20)
def _fit_height_snap(self):
new_h = self._calc_target_height()
old_h = self.height()
if new_h == old_h:
return
if self._height_anim and self._height_anim.state() != QPropertyAnimation.State.Stopped:
self._height_anim.stop()
self.move(self.x(), self.y() - (new_h - old_h) // 2)
self.setFixedHeight(new_h)
self._clamp_to_screen()
self.position_changed.emit()
def _fit_height_animated(self):
new_h = self._calc_target_height()
old_h = self.height()
if new_h == old_h:
return
if self._height_anim and self._height_anim.state() != QPropertyAnimation.State.Stopped:
self._height_anim.stop()
target_y = self.y() - (new_h - old_h) // 2
self.setMinimumHeight(min(old_h, new_h))
self.setMaximumHeight(max(old_h, new_h))
anim = QPropertyAnimation(self, b"geometry")
anim.setDuration(150)
anim.setStartValue(QRect(self.x(), self.y(), self.width(), old_h))
anim.setEndValue(QRect(self.x(), target_y, self.width(), new_h))
anim.setEasingCurve(QEasingCurve.Type.OutCubic)
def on_finished():
self.setFixedHeight(new_h)
self._clamp_to_screen()
self.position_changed.emit()
anim.finished.connect(on_finished)
self._height_anim = anim
anim.start()
def apply_settings(self, settings: dict):
self._settings = _merge_settings(DEFAULT_SUBTITLE_WIN_SETTINGS, settings)
for w in self._text_widgets:
self._content_layout.removeWidget(w)
w.deleteLater()
self._text_widgets = []
for line_cfg in self._settings.get("lines", []):
if not line_cfg.get("enabled", True):
continue
tw = _SubtitleTextWidget()
tw.set_config(line_cfg)
tw.height_changed.connect(self._fit_height_animated)
self._text_widgets.append(tw)
self._content_layout.addWidget(tw)
self._content_layout.setSpacing(self._settings.get("line_spacing", 8))
w = self._settings.get("window_width", 1000)
self.setFixedWidth(w)
self._apply_background()
self._refresh_display()
# Reset auto-hide timer with new settings
self._restart_auto_hide_timer()
# --- Auto-hide ---
def _restart_auto_hide_timer(self):
timeout = self._settings.get("auto_hide_timeout", 0)
self._auto_hide_timer.stop()
if timeout > 0 and self._sentences:
self._auto_hide_timer.setInterval(timeout * 1000)
self._auto_hide_timer.start()
def _on_auto_hide_timeout(self):
if self._is_hidden_by_timeout:
return
self._is_hidden_by_timeout = True
anim_type = self._settings.get("auto_hide_animation", "fade")
duration = self._settings.get("auto_hide_duration", 300)
for tw in self._text_widgets:
tw.animate_out(anim_type=anim_type, duration=duration)
def _restore_from_auto_hide(self):
if not self._is_hidden_by_timeout:
return
self._is_hidden_by_timeout = False
anim_type = self._settings.get("auto_hide_animation", "fade")
duration = self._settings.get("auto_hide_duration", 300)
# Reverse the hide animation type for restore
restore_type = anim_type
if anim_type == "slide_down":
restore_type = "slide_up"
elif anim_type == "slide_up":
restore_type = "slide_down"
elif anim_type == "slide_left":
restore_type = "slide_right"
elif anim_type == "slide_right":
restore_type = "slide_left"
for tw in self._text_widgets:
tw._stop_all_animations()
# Set hidden state
tw._content_opacity_val = 0.0
if restore_type == "slide_left":
tw._slide_offset_x_val = float(-(tw.width() or 200))
elif restore_type == "slide_right":
tw._slide_offset_x_val = float(tw.width() or 200)
elif restore_type == "slide_up":
tw._slide_offset_y_val = float(tw.height() or 40)
elif restore_type == "slide_down":
tw._slide_offset_y_val = float(-(tw.height() or 40))
tw.update()
# Use the entry animation mechanism with overridden type
old_entry = tw._entry_animation
old_dur = tw._animation_duration
tw._entry_animation = restore_type if restore_type != "none" else "fade"
tw._animation_duration = duration
tw.animate_in()
tw._entry_animation = old_entry
tw._animation_duration = old_dur
# --- Middle-click drag ---
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.MiddleButton:
self._drag_pos = (
event.globalPosition().toPoint()
- self.frameGeometry().topLeft()
)
super().mousePressEvent(event)
def mouseMoveEvent(self, event):
if self._drag_pos and event.buttons() & Qt.MouseButton.MiddleButton:
self.move(event.globalPosition().toPoint() - self._drag_pos)
super().mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if event.button() == Qt.MouseButton.MiddleButton:
if self._drag_pos:
self._drag_pos = None
self.position_changed.emit()
super().mouseReleaseEvent(event)
def closeEvent(self, event):
self.window_closed.emit()
super().closeEvent(event)
def paintEvent(self, event):
if self._bg_pixmap and not self._bg_pixmap.isNull():
painter = QPainter(self)
painter.drawPixmap(self.rect(), self._bg_pixmap)
painter.end()
super().paintEvent(event)
# --- Text updates ---
def update_text(self, original: str, translations: dict | str):
"""Thread-safe text update.
translations: dict mapping lang code to translated text,
or a plain string (backward compat, treated as primary target).
"""
if isinstance(translations, str):
# Backward compat: wrap in dict with empty key
translations = {"": translations}
self.update_text_signal.emit(original, json.dumps(translations, ensure_ascii=False))
@pyqtSlot(str, str)
def _on_update_text(self, original: str, translations_json: str):
translations = json.loads(translations_json)
self._cancel_pending_segments()
# Respect minimum display time: delay if previous sentence was inserted recently
now_ms = time.monotonic() * 1000
elapsed = now_ms - self._last_insert_time
base_delay = max(0, int(self._min_display_ms - elapsed)) if self._last_insert_time > 0 else 0
if base_delay == 0:
self._insert_sentence(original, translations)
else:
timer = QTimer(self)
timer.setSingleShot(True)
timer.setInterval(base_delay)
timer.timeout.connect(lambda o=original, t=translations: self._insert_sentence(o, t))
timer.start()
self._pending_segment_timers.append(timer)
def _insert_sentence(self, original: str, translations: dict):
"""Insert a single sentence and refresh display."""
max_sentences = self._settings.get("sentences", 1)
self._sentences.append((original, translations))
if len(self._sentences) > max_sentences:
self._sentences = self._sentences[-max_sentences:]
if self._is_hidden_by_timeout:
self._restore_from_auto_hide()
self._refresh_display()
self._restart_auto_hide_timer()
self._last_insert_time = time.monotonic() * 1000
def _cancel_pending_segments(self):
"""Cancel any pending delayed segment insertions."""
for timer in self._pending_segment_timers:
timer.stop()
timer.deleteLater()
self._pending_segment_timers.clear()
def _refresh_display(self):
if not self._sentences:
for tw in self._text_widgets:
tw.set_text("")
self._fit_height_snap()
return
lines_cfg = [ln for ln in self._settings.get("lines", []) if ln.get("enabled", True)]
wi = 0
for cfg in lines_cfg:
if wi >= len(self._text_widgets):
break
tw = self._text_widgets[wi]
line_type = cfg.get("type", "original")
if line_type == "original":
texts = [s[0] for s in self._sentences if s[0]]
else:
lang = cfg.get("lang", "")
texts = []
for _, tl_dict in self._sentences:
if isinstance(tl_dict, str):
if tl_dict:
texts.append(tl_dict)
elif lang and lang in tl_dict:
texts.append(tl_dict[lang])
elif "" in tl_dict and tl_dict[""]:
texts.append(tl_dict[""])
else:
for v in tl_dict.values():
if v:
texts.append(v)
break
tw.set_text(" | ".join(texts) if len(texts) > 1 else (texts[0] if texts else ""))
wi += 1
def get_target_languages(self) -> set:
"""Return set of unique target language codes from enabled translation lines."""
langs = set()
for cfg in self._settings.get("lines", []):
if cfg.get("enabled", True) and cfg.get("type") == "translation":
lang = cfg.get("lang", "")
if lang:
langs.add(lang)
return langs
def clear(self):
self._sentences.clear()
self._cancel_pending_segments()
self._auto_hide_timer.stop()
self._is_hidden_by_timeout = False
for tw in self._text_widgets:
tw._stop_all_animations()
tw._content_opacity_val = 1.0
tw._slide_offset_x_val = 0.0
tw._slide_offset_y_val = 0.0
tw.set_text("")
self._fit_height_snap()