|
| 1 | +"""Contains the asset management dialog.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import os |
| 6 | + |
| 7 | +from PySide6.QtGui import QAction, QIcon |
| 8 | +from PySide6.QtWidgets import QDialog, QDialogButtonBox, QFileDialog, QMessageBox, QToolBar, QVBoxLayout, QWidget |
| 9 | + |
| 10 | +from controller.utils.process_notifications import ProcessNotifier, get_process_notifier |
| 11 | +from model.media_assets.image import LocalImage |
| 12 | +from model.media_assets.media_type import MediaType |
| 13 | +from view.utility_widgets.asset_selection_widget import AssetSelectionWidget |
| 14 | + |
| 15 | +_SUPPORTED_FILE_ENDINGS: dict[MediaType, list[str]] = { |
| 16 | + MediaType.TEXT: [".txt"], |
| 17 | + MediaType.IMAGE: [".jpg", ".jpeg", ".png", ".bmp", ".gif", ".pbm", ".pgm", ".ppm", ".xbm", ".xpm"], |
| 18 | + MediaType.VIDEO: [".mp4"], |
| 19 | + MediaType.AUDIO: [".wav", ".mp3", ".flac"], |
| 20 | + MediaType.MODEL_3D: [".stl", ".obj"] |
| 21 | +} |
| 22 | + |
| 23 | + |
| 24 | +class AssetManagementDialog(QDialog): |
| 25 | + """Provide a GUI method to manage assets.""" |
| 26 | + |
| 27 | + def __init__(self, parent: QWidget | None = None, show_file_path: str | None = None) -> None: |
| 28 | + """Initialize the asset management dialog.""" |
| 29 | + super().__init__(parent) |
| 30 | + layout = QVBoxLayout() |
| 31 | + |
| 32 | + self._action_button_group = QToolBar() |
| 33 | + |
| 34 | + self._load_asset_file = QAction() |
| 35 | + self._load_asset_file.setIcon(QIcon.fromTheme("document-open")) |
| 36 | + self._load_asset_file.setText("Load asset from file") |
| 37 | + self._load_asset_file.triggered.connect(self._open_file) |
| 38 | + self._action_button_group.addAction(self._load_asset_file) |
| 39 | + |
| 40 | + layout.addWidget(self._action_button_group) |
| 41 | + |
| 42 | + self._asset_display = AssetSelectionWidget(self) |
| 43 | + layout.addWidget(self._asset_display) |
| 44 | + |
| 45 | + self._close_button_group = QDialogButtonBox(QDialogButtonBox.StandardButton.Close) |
| 46 | + self._close_button_group.button(QDialogButtonBox.StandardButton.Close).clicked.connect(self.close) |
| 47 | + layout.addWidget(self._close_button_group) |
| 48 | + |
| 49 | + self.setLayout(layout) |
| 50 | + self.setModal(True) |
| 51 | + self._dialog: QFileDialog | None = None |
| 52 | + self._show_file_path = show_file_path if show_file_path is not None else "" |
| 53 | + self.setMinimumWidth(800) |
| 54 | + |
| 55 | + def _open_file(self) -> None: |
| 56 | + self._dialog = QFileDialog(self, "Open file") |
| 57 | + self._dialog.setModal(True) |
| 58 | + self._dialog.setNameFilters( |
| 59 | + [f"{k.get_long_description()} ({" ".join(f"*{va}" for va in v)})" |
| 60 | + for k, v in _SUPPORTED_FILE_ENDINGS.items()] |
| 61 | + ) |
| 62 | + self._dialog.setDefaultSuffix(".jpg") |
| 63 | + self._dialog.setFileMode(QFileDialog.FileMode.ExistingFiles) |
| 64 | + self._dialog.setDirectory(os.path.expanduser("~") if self._show_file_path is None else self._show_file_path) |
| 65 | + self._dialog.filesSelected.connect(self._process_loading_files) |
| 66 | + self._dialog.open() |
| 67 | + |
| 68 | + def _process_loading_files(self, list_of_files: list[str]) -> None: |
| 69 | + self._dialog.close() |
| 70 | + if len(list_of_files) == 0: |
| 71 | + return |
| 72 | + accumulated_errors = "" |
| 73 | + pn = get_process_notifier("Asset Loading", len(list_of_files)) |
| 74 | + for file_path in list_of_files: |
| 75 | + pn.current_step_description = f"Loading Asset from {file_path}" |
| 76 | + extension = os.path.splitext(file_path)[1] |
| 77 | + try: |
| 78 | + if extension in _SUPPORTED_FILE_ENDINGS[MediaType.IMAGE]: |
| 79 | + LocalImage(file_path, show_file_path=self._show_file_path) |
| 80 | + else: |
| 81 | + accumulated_errors += f"Unable to load asset from {file_path}: extension {extension} unknown.\n" |
| 82 | + except Exception as e: |
| 83 | + accumulated_errors += f"Unable to load asset from {file_path}: {e}\n" |
| 84 | + pn.current_step_number += 1 |
| 85 | + self._asset_display.reload_model() |
| 86 | + pn.close() |
| 87 | + if len(accumulated_errors) > 0: |
| 88 | + msg_box = QMessageBox(QMessageBox.Icon.Critical, "Loading Assets Failed", |
| 89 | + "Errors occurred during asset loading:", parent=self, |
| 90 | + detailedText=accumulated_errors) |
| 91 | + msg_box.show() |
| 92 | + self._dialog = msg_box |
0 commit comments