diff --git a/basic_features/basic_save_game_info.py b/basic_features/basic_save_game_info.py index 4acb0ef..9adf2f9 100644 --- a/basic_features/basic_save_game_info.py +++ b/basic_features/basic_save_game_info.py @@ -55,36 +55,38 @@ def get_filedate_metadata(p: Path, save: mobase.ISaveGame) -> Mapping[str, str]: class BasicGameSaveGameInfoWidget(mobase.ISaveGameInfoWidget): - _preview_width = 320 + """Save game info widget to display metadata and a preview.""" def __init__( self, parent: QWidget | None, - get_preview: Callable[ - [Path], QPixmap | QImage | Path | str | None - ] = lambda p: None, - get_metadata: Callable[ - [Path, mobase.ISaveGame], Mapping[str, Any] | None - ] = get_filedate_metadata, + get_preview: Callable[[Path], QPixmap | QImage | Path | str | None] + | None = lambda p: None, + get_metadata: Callable[[Path, mobase.ISaveGame], Mapping[str, Any] | None] + | None = get_filedate_metadata, + max_width: int = 320, ): """ Args: parent: parent widget - get_preview (optional): `callback(savegame_path)` returning the path - to the saves preview image. + get_preview (optional): `callback(savegame_path)` returning the + saves preview image or the path to it. get_metadata (optional): `callback(savegame_path, ISaveGame)` returning the saves metadata. By default the saves file date is shown. + max_width (optional): The maximum widget and (scaled) preview width. + Defaults to 320. """ super().__init__(parent) - self._get_preview = get_preview - self._get_metadata = get_metadata + self._get_preview = get_preview or (lambda p: None) + self._get_metadata = get_metadata or get_filedate_metadata + self._max_width = max_width or 320 layout = QVBoxLayout() # Metadata form self._metadata_widget = QWidget() - self._metadata_widget.setMaximumWidth(self._preview_width) + self._metadata_widget.setMaximumWidth(self._max_width) self._metadata_layout = form_layout = QFormLayout(self._metadata_widget) form_layout.setContentsMargins(0, 0, 0, 0) form_layout.setVerticalSpacing(2) @@ -133,7 +135,7 @@ def setSave(self, save: mobase.ISaveGame): pixmap = preview if pixmap and not pixmap.isNull(): # Scale the pixmap and show it: - pixmap = pixmap.scaledToWidth(self._preview_width) + pixmap = pixmap.scaledToWidth(self._max_width) self._label.setPixmap(pixmap) self._label.show() else: @@ -155,10 +157,6 @@ def setSave(self, save: mobase.ISaveGame): self.adjustSize() self.show() - def set_maximum_width(self, width: int): - self._preview_width = width - self._metadata_widget.setMaximumWidth(width) - def _new_form_row(self, label: str = "", field: str = ""): qLabel = QLabel(text=label) qLabel.setAlignment(Qt.AlignmentFlag.AlignTop) @@ -171,20 +169,42 @@ def _new_form_row(self, label: str = "", field: str = ""): qField.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred) return qLabel, qField + def set_maximum_width(self, width: int): + self._max_width = width + self._metadata_widget.setMaximumWidth(width) + class BasicGameSaveGameInfo(mobase.SaveGameInfo): + _get_widget: Callable[[QWidget | None], mobase.ISaveGameInfoWidget | None] | None + def __init__( self, get_preview: Callable[[Path], QPixmap | QImage | Path | str | None] | None = None, get_metadata: Callable[[Path, mobase.ISaveGame], Mapping[str, Any] | None] - | None = get_filedate_metadata, + | None = None, + max_width: int = 0, ): """Args from: `BasicGameSaveGameInfoWidget`.""" super().__init__() - self._get_preview = get_preview - self._get_metadata = get_metadata - self._maximum_widget_size = 0 + self._get_widget = lambda parent: BasicGameSaveGameInfoWidget( + parent, get_preview, get_metadata, max_width + ) + + @classmethod + def with_widget( + cls, + widget: type[mobase.ISaveGameInfoWidget] | None, + ) -> Self: + """ + + Args: + widget: a custom `ISaveGameInfoWidget` instead of the default + `BasicGameSaveGameInfoWidget`. + """ + self = cls() + self._get_widget = lambda parent: widget(parent) if widget else None + return self def getMissingAssets(self, save: mobase.ISaveGame) -> dict[str, Sequence[str]]: return {} @@ -192,23 +212,7 @@ def getMissingAssets(self, save: mobase.ISaveGame) -> dict[str, Sequence[str]]: def getSaveGameWidget( self, parent: QWidget | None = None ) -> mobase.ISaveGameInfoWidget | None: - if self._get_preview is None: - if self._get_metadata is None: - return None - else: - widget = BasicGameSaveGameInfoWidget( - parent, get_metadata=self._get_metadata - ) - elif self._get_metadata is None: - widget = BasicGameSaveGameInfoWidget(parent, self._get_preview) + if self._get_widget: + return self._get_widget(parent) else: - widget = BasicGameSaveGameInfoWidget( - parent, self._get_preview, self._get_metadata - ) - if self._maximum_widget_size > 0: - widget.set_maximum_width(self._maximum_widget_size) - return widget - - def with_maximum_widget_size(self, width: int) -> Self: - self._maximum_widget_size = width - return self + return None diff --git a/games/game_bladeandsorcery.py b/games/game_bladeandsorcery.py index ca4664d..6e0569c 100644 --- a/games/game_bladeandsorcery.py +++ b/games/game_bladeandsorcery.py @@ -83,10 +83,9 @@ class BaSGame(BasicGame): def init(self, organizer: mobase.IOrganizer) -> bool: BasicGame.init(self, organizer) - # self._featureMap[mobase.SaveGameInfo] = BaSSaveGameInfo() self._featureMap[mobase.SaveGameInfo] = BasicGameSaveGameInfo( - get_metadata=bas_parse_metadata - ).with_maximum_widget_size(400) + get_metadata=bas_parse_metadata, max_width=400 + ) return True def listSaves(self, folder: QDir) -> list[mobase.ISaveGame]: