Add qml mod list model

This commit is contained in:
Toast 2025-05-22 12:40:21 +02:00
parent 049acf68be
commit 0a5cb83274

54
src/mod_list.py Normal file
View file

@ -0,0 +1,54 @@
from PySide6.QtQml import QmlElement
from PySide6.QtCore import QAbstractListModel, QModelIndex
from mod import Mod, InvalidModError
import os
QML_IMPORT_NAME = "Leek"
QML_IMPORT_MAJOR_VERSION = 1
# TODO: Don't harcode the mods path
GAME_PATH = "/home/toast/.local/share/Steam/steamapps/common/Hatsune Miku Project DIVA Mega Mix Plus/"
# Qt follows C++ naming conventions
# ruff: noqa: N802
@QmlElement
class QModListModel(QAbstractListModel):
def __init__(self, parent=None) -> None:
super().__init__(parent=parent)
mods: list[Mod] = []
with os.scandir(GAME_PATH + "mods/") as dirs:
for dir in dirs:
try:
new_mod: Mod = Mod(dir.path + "/" )
mods.append(new_mod)
except InvalidModError as e:
print(f"Found invalid mod at {dir.path}: {e.message}")
continue
self.mods = mods
def roleNames(self) -> dict[int, bytes]:
return {
0: b"name",
1: b"description",
2: b"enabled"
}
def rowCount(self, parent=QModelIndex()) -> int:
return len(self.mods)
def data(self, index: QModelIndex, role: int) -> None | str | bool:
i: int = index.row()
result: None | str | bool
if not index.isValid():
result = None
elif role == 0:
result = self.mods[i].name
elif role == 1:
result = self.mods[i].description
elif role == 2:
result = self.mods[i].enabled
else:
result = None
return result