-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
145 lines (121 loc) · 5.15 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from typing import Any, Dict, List
from PySide6 import QtCore, QtWidgets
from PySide6.QtCore import Qt
from typing import Union
class ModModel(QtCore.QAbstractTableModel):
"""An implementation for handling mod data in a QT.QTableView."""
def __init__(
self, *args: tuple[str], settings: Dict[str, Dict | str], profile: Any, **kwargs
):
super(ModModel, self).__init__(*args, **kwargs)
self.profile = profile
self.game_setting = settings
self.mod_order = []
self.headers = ("enabled", "name", "type", "path")
self.parse_mods_from_settings()
def parse_mods_from_settings(self):
"""Read game_setting and update mod_order with the current profile."""
if not self.game_setting:
return
try:
profile = self.game_setting["profiles"]
assert type(profile) is dict
self.mod_order = profile.get(self.profile)
except AttributeError:
pass
def headerData(self, section: int, orientation: Qt.Orientation, role: int):
"""Overridden function to support own headers."""
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.headers[section]
return super().headerData(section, orientation, role)
def parse_path(self, row: Dict):
"""Attempt to strip away the unneccecary parts of a path for display."""
mod_settings = self.game_setting["mods"]
assert type(mod_settings) is dict
relative_path = mod_settings.get(row.get("name"))
try:
assert isinstance(relative_path, str)
path_parsed = relative_path.replace(
self.game_setting.get("default_mod_folder"), "."
)
except:
return relative_path
return path_parsed
def data(
self,
index: Union[QtCore.QModelIndex, QtCore.QPersistentModelIndex],
role: int = Qt.ItemDataRole.DisplayRole,
) -> Any:
"""Model-specific function to assist in displaying of data."""
cur_profile = self.game_setting["profiles"][self.profile]
row = cur_profile[index.row()]
assert type(row) is dict
if (
role == QtCore.Qt.CheckStateRole
and self.headers[index.column()] == "enabled"
):
if row.get("enabled"):
return Qt.Checked
else:
return Qt.Unchecked
if role == QtCore.Qt.DisplayRole:
if self.headers[index.column()] == "type":
return row.get("type", "basic")
if self.headers[index.column()] == "path":
return self.parse_path(row)
else:
return row.get(self.headers[index.column()])
def setData(self, index: QtCore.QModelIndex, value, role: int) -> bool:
"""Overridden funciton to help with checkboxes."""
cur_profile = self.game_setting["profiles"][self.profile]
assert type(cur_profile) is list
if role == Qt.CheckStateRole and self.headers[index.column()] == "enabled":
if value == Qt.Checked:
cur_profile[index.row()]["enabled"] = True
else:
cur_profile[index.row()]["enabled"] = False
self.layoutChanged.emit()
return super().setData(index, value, role=role)
def flags(self, index: QtCore.QModelIndex):
"""Overridden function to support checkboxes"""
if index.column() == 0:
return Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsSelectable
else:
return super().flags(index)
def rowCount(self, index=None) -> int:
assert type(self.mod_order) is list
return len(self.mod_order)
def columnCount(self, index=None) -> int:
try:
return len(self.headers)
except IndexError:
return 0
class SourceModel(QtCore.QAbstractTableModel):
"""An implementation for handling mod data in a QT.QTableView."""
def __init__(self, *args: tuple[str], sources: Dict[str, str], **kwargs):
super(SourceModel, self).__init__(*args, **kwargs)
self.sources = sources
self.headers = ("title", "installed", "added", "updated", "size", "url")
def headerData(self, section: int, orientation: Qt.Orientation, role: int):
"""Overridden function to support own headers."""
if role == Qt.DisplayRole and orientation == Qt.Horizontal:
return self.headers[section]
return super().headerData(section, orientation, role)
def data(
self,
index: Union[QtCore.QModelIndex, QtCore.QPersistentModelIndex],
role: int = Qt.ItemDataRole.DisplayRole,
) -> Any:
"""Model-specific function to assist in displaying of data."""
row = self.sources[index.row()]
assert type(row) is dict
if role == QtCore.Qt.DisplayRole:
return row.get(self.headers[index.column()])
def rowCount(self, index=None) -> int:
assert type(self.sources) is list
return len(self.sources)
def columnCount(self, index=None) -> int:
try:
return len(self.headers)
except IndexError:
return 0