Leek/src/leek/mod_list.py

49 lines
1.3 KiB
Python

from PySide6.QtQml import QmlElement
from PySide6.QtCore import QAbstractListModel, QModelIndex
from leek.mod import InvalidModError
from leek.qmod import QMod
from pathlib import Path
from leek.utils import GameFinder
QML_IMPORT_NAME = "Leek"
QML_IMPORT_MAJOR_VERSION = 1
# Qt follows C++ naming conventions
# ruff: noqa: N802
@QmlElement
class QModListModel(QAbstractListModel):
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
mods: list[QMod] = []
mod_path: Path = GameFinder.find() / "mods"
for dir in mod_path.iterdir():
if dir.name == ".stfolder":
continue
try:
new_mod: QMod = QMod(dir, self)
mods.append(new_mod)
except InvalidModError as e:
print(f"Found invalid mod at {dir}: {e.message}")
continue
self.mods = mods
def roleNames(self) -> dict[int, bytes]:
return {0: b"mod"}
def rowCount(self, parent=QModelIndex()) -> int:
return len(self.mods)
def data(self, index: QModelIndex, role: int) -> None | QMod:
i: int = index.row()
result: None | QMod
if not index.isValid():
result = None
elif role == 0:
result = self.mods[i]
else:
result = None
return result