Skip to content

Commit efb0620

Browse files
refactor: remove placeholder video, fix type hints in preview_thumb.py (#906)
* type fixes * remove `stop_file_use()` method and release the file properly. * remove "placeholder_mp4" from resources.json
1 parent 6598630 commit efb0620

File tree

5 files changed

+23
-33
lines changed

5 files changed

+23
-33
lines changed

src/tagstudio/qt/resources.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,5 @@
122122
"thumb_loading": {
123123
"path": "qt/images/thumb_loading.png",
124124
"mode": "pil"
125-
},
126-
"placeholder_mp4": {
127-
"path": "qt/videos/placeholder.mp4",
128-
"mode": "rb"
129125
}
130126
}

src/tagstudio/qt/ts_qt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ def delete_files_callback(self, origin_path: str | Path, origin_id: int | None =
10451045
for i, tup in enumerate(pending):
10461046
e_id, f = tup
10471047
if (origin_path == f) or (not origin_path):
1048-
self.preview_panel.thumb.stop_file_use()
1048+
self.preview_panel.thumb.media_player.stop()
10491049
if delete_file(self.lib.library_dir / f):
10501050
self.main_window.statusbar.showMessage(
10511051
Translations.format(

src/tagstudio/qt/widgets/media_player.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,9 +416,9 @@ def has_video_changed(self, video_available: bool) -> None:
416416
self.scene().removeItem(self.video_preview)
417417

418418
def stop(self) -> None:
419-
"""Clear the filepath and stop the player."""
419+
"""Clear the filepath, stop the player and release the source."""
420420
self.filepath = None
421-
self.player.stop()
421+
self.player.setSource(QUrl())
422422

423423
def play(self, filepath: Path) -> None:
424424
"""Set the source of the QMediaPlayer and play."""

src/tagstudio/qt/widgets/preview/preview_thumb.py

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
import io
66
import time
7-
import typing
87
from pathlib import Path
8+
from typing import TYPE_CHECKING, override
99
from warnings import catch_warnings
1010

1111
import cv2
@@ -24,12 +24,11 @@
2424
from tagstudio.qt.helpers.qbutton_wrapper import QPushButtonWrapper
2525
from tagstudio.qt.helpers.rounded_pixmap_style import RoundedPixmapStyle
2626
from tagstudio.qt.platform_strings import open_file_str, trash_term
27-
from tagstudio.qt.resource_manager import ResourceManager
2827
from tagstudio.qt.translations import Translations
2928
from tagstudio.qt.widgets.media_player import MediaPlayer
3029
from tagstudio.qt.widgets.thumb_renderer import ThumbRenderer
3130

32-
if typing.TYPE_CHECKING:
31+
if TYPE_CHECKING:
3332
from tagstudio.qt.ts_qt import QtDriver
3433

3534
logger = structlog.get_logger(__name__)
@@ -39,7 +38,7 @@
3938
class PreviewThumb(QWidget):
4039
"""The Preview Panel Widget."""
4140

42-
def __init__(self, library: Library, driver: "QtDriver"):
41+
def __init__(self, library: Library, driver: "QtDriver") -> None:
4342
super().__init__()
4443

4544
self.is_connected = False
@@ -54,6 +53,7 @@ def __init__(self, library: Library, driver: "QtDriver"):
5453
self.image_layout.setStackingMode(QStackedLayout.StackingMode.StackAll)
5554
self.image_layout.setContentsMargins(0, 0, 0, 0)
5655

56+
self.opener: FileOpenerHelper | None = None
5757
self.open_file_action = QAction(Translations["file.open_file"], self)
5858
self.open_explorer_action = QAction(open_file_str(), self)
5959
self.delete_action = QAction(
@@ -133,17 +133,17 @@ def _set_mp_max_size(self, size: QSize) -> None:
133133
def _has_video_changed(self, video: bool) -> None:
134134
self.update_image_size((self.size().width(), self.size().height()))
135135

136-
def _stacked_page_setup(self, page: QWidget, widget: QWidget):
136+
def _stacked_page_setup(self, page: QWidget, widget: QWidget) -> None:
137137
layout = QHBoxLayout(page)
138138
layout.addWidget(widget)
139139
layout.setAlignment(widget, Qt.AlignmentFlag.AlignCenter)
140140
layout.setContentsMargins(0, 0, 0, 0)
141141
page.setLayout(layout)
142142

143-
def set_image_ratio(self, ratio: float):
143+
def set_image_ratio(self, ratio: float) -> None:
144144
self.image_ratio = ratio
145145

146-
def update_image_size(self, size: tuple[int, int], ratio: float | None = None):
146+
def update_image_size(self, size: tuple[int, int], ratio: float | None = None) -> None:
147147
if ratio:
148148
self.set_image_ratio(ratio)
149149

@@ -204,7 +204,7 @@ def get_preview_size(self) -> tuple[int, int]:
204204
self.size().height(),
205205
)
206206

207-
def switch_preview(self, preview: str):
207+
def switch_preview(self, preview: str) -> None:
208208
if preview in ["audio", "video"]:
209209
self.media_player.show()
210210
self.image_layout.setCurrentWidget(self.media_player_page)
@@ -229,7 +229,7 @@ def switch_preview(self, preview: str):
229229
self.gif_buffer.close()
230230
self.preview_gif.hide()
231231

232-
def _display_fallback_image(self, filepath: Path, ext: str) -> dict:
232+
def _display_fallback_image(self, filepath: Path, ext: str) -> dict[str, int]:
233233
"""Renders the given file as an image, no matter its media type.
234234
235235
Useful for fallback scenarios.
@@ -244,9 +244,9 @@ def _display_fallback_image(self, filepath: Path, ext: str) -> dict:
244244
)
245245
return self._update_image(filepath, ext)
246246

247-
def _update_image(self, filepath: Path, ext: str) -> dict:
247+
def _update_image(self, filepath: Path, ext: str) -> dict[str, int]:
248248
"""Update the static image preview from a filepath."""
249-
stats: dict = {}
249+
stats: dict[str, int] = {}
250250
self.switch_preview("image")
251251

252252
image: Image.Image | None = None
@@ -287,9 +287,9 @@ def _update_image(self, filepath: Path, ext: str) -> dict:
287287

288288
return stats
289289

290-
def _update_animation(self, filepath: Path, ext: str) -> dict:
290+
def _update_animation(self, filepath: Path, ext: str) -> dict[str, int]:
291291
"""Update the animated image preview from a filepath."""
292-
stats: dict = {}
292+
stats: dict[str, int] = {}
293293

294294
# Ensure that any movie and buffer from previous animations are cleared.
295295
if self.preview_gif.movie():
@@ -351,8 +351,8 @@ def _get_video_res(self, filepath: str) -> tuple[bool, QSize]:
351351
image = Image.fromarray(frame)
352352
return (success, QSize(image.width, image.height))
353353

354-
def _update_media(self, filepath: Path, type: MediaType) -> dict:
355-
stats: dict = {}
354+
def _update_media(self, filepath: Path, type: MediaType) -> dict[str, int]:
355+
stats: dict[str, int] = {}
356356

357357
self.media_player.play(filepath)
358358

@@ -380,9 +380,9 @@ def _update_media(self, filepath: Path, type: MediaType) -> dict:
380380
stats["duration"] = self.media_player.player.duration() * 1000
381381
return stats
382382

383-
def update_preview(self, filepath: Path, ext: str) -> dict:
383+
def update_preview(self, filepath: Path, ext: str) -> dict[str, int]:
384384
"""Render a single file preview."""
385-
stats: dict = {}
385+
stats: dict[str, int] = {}
386386

387387
# Video
388388
if MediaCategories.is_ext_in_category(
@@ -448,17 +448,11 @@ def update_preview(self, filepath: Path, ext: str) -> dict:
448448

449449
return stats
450450

451-
def hide_preview(self):
451+
def hide_preview(self) -> None:
452452
"""Completely hide the file preview."""
453453
self.switch_preview("")
454454

455-
def stop_file_use(self):
456-
"""Stops the use of the currently previewed file. Used to release file permissions."""
457-
logger.info("[PreviewThumb] Stopping file use in video playback...")
458-
# This swaps the video out for a placeholder so the previous video's file
459-
# is no longer in use by this object.
460-
self.media_player.play(ResourceManager.get_path("placeholder_mp4"))
461-
462-
def resizeEvent(self, event: QResizeEvent) -> None: # noqa: N802
455+
@override
456+
def resizeEvent(self, event: QResizeEvent) -> None:
463457
self.update_image_size((self.size().width(), self.size().height()))
464458
return super().resizeEvent(event)
-2.53 KB
Binary file not shown.

0 commit comments

Comments
 (0)