Skip to content

Commit

Permalink
Refactor + BasicGameSaveGameInfo(maximum_widget_size)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZashIn committed Sep 27, 2023
1 parent 171083f commit cc95749
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 43 deletions.
84 changes: 44 additions & 40 deletions basic_features/basic_save_game_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -171,44 +169,50 @@ 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 {}

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
5 changes: 2 additions & 3 deletions games/game_bladeandsorcery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down

0 comments on commit cc95749

Please sign in to comment.