|
| 1 | +from PyQt6.QtCore import Qt, QAbstractListModel, QModelIndex |
| 2 | +from utils import ms_to_hms_ms |
| 3 | + |
| 4 | +class VideoListModel(QAbstractListModel): |
| 5 | + def __init__(self, videos=None): |
| 6 | + super().__init__() |
| 7 | + self.videos = videos or [] |
| 8 | + |
| 9 | + def rowCount(self, parent=QModelIndex()): |
| 10 | + return len(self.videos) |
| 11 | + |
| 12 | + def data(self, index, role): |
| 13 | + if not index.isValid() or not (0 <= index.row() < len(self.videos)): |
| 14 | + return None |
| 15 | + video = self.videos[index.row()] |
| 16 | + if role == Qt.ItemDataRole.DisplayRole: |
| 17 | + path = video.get("path", "unknown") |
| 18 | + n_events = len(video.get("annotations", [])) |
| 19 | + return f"{path} ({n_events} events)" |
| 20 | + if role == Qt.ItemDataRole.UserRole: |
| 21 | + return video |
| 22 | + return None |
| 23 | + |
| 24 | + def set_videos(self, videos): |
| 25 | + self.beginResetModel() |
| 26 | + self.videos = videos or [] |
| 27 | + self.endResetModel() |
| 28 | + |
| 29 | +class AnnotationListModel(QAbstractListModel): |
| 30 | + def __init__(self, annotations=None): |
| 31 | + super().__init__() |
| 32 | + self.annotations = annotations or [] |
| 33 | + |
| 34 | + def rowCount(self, parent=QModelIndex()): |
| 35 | + return len(self.annotations) |
| 36 | + |
| 37 | + def data(self, index, role): |
| 38 | + if not index.isValid() or not (0 <= index.row() < len(self.annotations)): |
| 39 | + return None |
| 40 | + ann = self.annotations[index.row()] |
| 41 | + if role == Qt.ItemDataRole.DisplayRole: |
| 42 | + return f"[{ms_to_hms_ms(ann['position'])}] {ann['label']}" |
| 43 | + if role == Qt.ItemDataRole.UserRole: |
| 44 | + return ann |
| 45 | + return None |
| 46 | + |
| 47 | + def set_annotations(self, annotations): |
| 48 | + self.beginResetModel() |
| 49 | + self.annotations = annotations or [] |
| 50 | + self.endResetModel() |
| 51 | + |
| 52 | + def add_annotation(self, annotation): |
| 53 | + idx = 0 |
| 54 | + while idx < len(self.annotations) and annotation["position"] > self.annotations[idx]["position"]: |
| 55 | + idx += 1 |
| 56 | + self.beginInsertRows(QModelIndex(), idx, idx) |
| 57 | + self.annotations.insert(idx, annotation) |
| 58 | + self.endInsertRows() |
| 59 | + return idx |
| 60 | + |
| 61 | + def remove_annotation(self, idx): |
| 62 | + self.beginRemoveRows(QModelIndex(), idx, idx) |
| 63 | + del self.annotations[idx] |
| 64 | + self.endRemoveRows() |
0 commit comments