-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmarker_gallery.py
More file actions
170 lines (141 loc) · 7.35 KB
/
marker_gallery.py
File metadata and controls
170 lines (141 loc) · 7.35 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
import logging
from PyQt6.QtWidgets import QWidget, QHBoxLayout, QVBoxLayout, QLabel, QScrollArea, QFrame, QMenu
from PyQt6.QtCore import Qt, pyqtSignal, QSize
from PyQt6.QtGui import QPixmap, QColor, QPalette
from translator import tr
class MarkerItem(QFrame):
"""A single item in the marker gallery."""
clicked = pyqtSignal(float) # timestamp
delete_requested = pyqtSignal(int) # marker_id
edit_requested = pyqtSignal(dict) # marker_data
def __init__(self, marker_data, parent=None):
super().__init__(parent)
self.marker_data = marker_data
self.marker_id = marker_data.get('id')
self.timestamp = marker_data.get('position_seconds', 0)
self.label_text = marker_data.get('label', "")
self.color = marker_data.get('color', "#FFD700")
self.setFixedSize(180, 140)
self.setCursor(Qt.CursorShape.PointingHandCursor)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
layout = QVBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)
layout.setSpacing(2)
# Thumbnail Label
self.thumb_label = QLabel()
self.thumb_label.setObjectName("markerThumb")
self.thumb_label.setFixedSize(170, 96) # 16:9 approx
self.thumb_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
self.thumb_label.setText("...")
layout.addWidget(self.thumb_label)
# Time Label with plate (Overlay on thumbnail)
m, s = divmod(int(self.timestamp), 60)
h, m = divmod(m, 60)
time_str = f"{h:02d}:{m:02d}:{s:02d}" if h > 0 else f"{m:02d}:{s:02d}"
self.time_label = QLabel(time_str, self.thumb_label)
self.time_label.setObjectName("markerTimeLabel")
self.time_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Position time_label at the bottom right of thumb_label
thumb_layout = QVBoxLayout(self.thumb_label)
thumb_layout.setContentsMargins(0, 0, 4, 4) # Small margin from edges
thumb_layout.addStretch()
time_hbox = QHBoxLayout()
time_hbox.addStretch()
time_hbox.addWidget(self.time_label)
thumb_layout.addLayout(time_hbox)
# Title Label with plate
self.title_label = QLabel(self.label_text)
self.title_label.setObjectName("markerTitleLabel")
self.title_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Structural styles in QSS, only border color (marker specific) remains here
self.title_label.setStyleSheet(f"border: 1px solid {self.color};")
self.title_label.setWordWrap(False)
layout.addWidget(self.title_label, 0, Qt.AlignmentFlag.AlignCenter)
def set_pixmap(self, pixmap):
logging.debug(f"MarkerItem.set_pixmap called for {self.marker_id}")
scaled = pixmap.scaled(self.thumb_label.size(), Qt.AspectRatioMode.KeepAspectRatioByExpanding, Qt.TransformationMode.SmoothTransformation)
self.thumb_label.setPixmap(scaled)
self.thumb_label.setText("")
logging.debug(f"MarkerItem.set_pixmap finished for {self.marker_id}")
def mousePressEvent(self, event):
if event.button() == Qt.MouseButton.LeftButton:
self.clicked.emit(self.timestamp)
def contextMenuEvent(self, event):
context_menu = QMenu(self)
edit_action = context_menu.addAction(tr('player.edit_marker') or "Edit Marker")
delete_action = context_menu.addAction(tr('player.delete_marker') or "Delete Marker")
action = context_menu.exec(event.globalPos())
if action == edit_action:
self.edit_requested.emit(self.marker_data)
elif action == delete_action:
self.delete_requested.emit(self.marker_id)
class MarkerGalleryWidget(QFrame):
"""Horizontal gallery of markers with screenshots."""
seek_requested = pyqtSignal(float)
delete_requested = pyqtSignal(int) # marker_id
edit_requested = pyqtSignal(dict) # marker_data
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowFlags(Qt.WindowType.ToolTip | Qt.WindowType.FramelessWindowHint | Qt.WindowType.WindowDoesNotAcceptFocus)
self.setAttribute(Qt.WidgetAttribute.WA_TranslucentBackground)
self.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.setFixedHeight(200)
layout = QVBoxLayout(self)
layout.setContentsMargins(10, 5, 10, 25)
# Scroll Area
self.scroll = QScrollArea()
self.scroll.setFocusPolicy(Qt.FocusPolicy.NoFocus)
self.scroll.setWidgetResizable(True)
self.scroll.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
self.scroll.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAlwaysOff)
self.scroll.setObjectName("markerGalleryScroll")
self.content_widget = QWidget()
self.content_widget.setObjectName("markerGalleryContent")
self.content_layout = QHBoxLayout(self.content_widget)
self.content_layout.setContentsMargins(0, 0, 0, 0)
self.content_layout.setSpacing(10)
self.content_layout.addStretch() # Initial stretch
self.scroll.setWidget(self.content_widget)
layout.addWidget(self.scroll)
self.items = {} # {marker_id: MarkerItem}
def set_markers(self, markers):
"""Update the gallery with a list of markers."""
# Clear existing
for i in reversed(range(self.content_layout.count())):
item = self.content_layout.itemAt(i)
if item.widget():
item.widget().setParent(None)
self.items.clear()
# Sort markers by time
sorted_markers = sorted(markers, key=lambda x: x.get('position_seconds', 0))
for m in sorted_markers:
item = MarkerItem(m)
item.clicked.connect(self.seek_requested)
item.edit_requested.connect(self.edit_requested)
item.delete_requested.connect(self.delete_requested)
self.content_layout.insertWidget(self.content_layout.count() - 1, item)
self.items[m['id']] = item
if not sorted_markers:
lbl = QLabel(tr('player.no_markers') or "No markers found")
lbl.setObjectName("noMarkersLabel")
lbl.setAlignment(Qt.AlignmentFlag.AlignCenter)
# Center the label in the layout
container = QWidget()
vbox = QVBoxLayout(container)
vbox.addStretch()
vbox.addWidget(lbl, 0, Qt.AlignmentFlag.AlignCenter)
vbox.addStretch()
self.content_layout.insertWidget(0, container)
def update_thumbnail(self, marker_id, pixmap):
"""Update thumbnail for a specific marker."""
logging.debug(f"MarkerGalleryWidget.update_thumbnail for {marker_id}")
if marker_id in self.items:
logging.debug(f"Found marker_id {marker_id} in items")
self.items[marker_id].set_pixmap(pixmap)
# Also check if it's a timestamp-based ID (for new markers)
elif f"ts_{marker_id}" in self.items:
logging.debug(f"Found ts_{marker_id} in items")
self.items[f"ts_{marker_id}"].set_pixmap(pixmap)
else:
logging.debug(f"marker_id {marker_id} NOT found in items. Keys: {list(self.items.keys())}")
logging.debug(f"MarkerGalleryWidget.update_thumbnail finished for {marker_id}")