Add qml mod list model
This commit is contained in:
parent
049acf68be
commit
0a5cb83274
1 changed files with 54 additions and 0 deletions
54
src/mod_list.py
Normal file
54
src/mod_list.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue