2019-05-12 05:52:56 +10:00
|
|
|
from typing import Any, DefaultDict
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
from PyQt5.QtCore import QObject, pyqtSlot
|
|
|
|
|
|
|
|
from .list_model import ListModel
|
|
|
|
|
|
|
|
|
|
|
|
class ListModelMap(QObject):
|
2019-05-12 05:52:56 +10:00
|
|
|
def __init__(self, *models_args, parent: QObject = None, **models_kwargs
|
|
|
|
) -> None:
|
2019-04-23 04:24:45 +10:00
|
|
|
super().__init__(parent)
|
2019-05-12 05:52:56 +10:00
|
|
|
models_kwargs["parent"] = self
|
2019-04-13 23:41:02 +10:00
|
|
|
|
|
|
|
# Set the parent to prevent item garbage-collection on the C++ side
|
|
|
|
self.dict: DefaultDict[Any, ListModel] = \
|
2019-05-03 04:20:21 +10:00
|
|
|
DefaultDict(
|
2019-05-12 05:52:56 +10:00
|
|
|
lambda: ListModel(*models_args, **models_kwargs)
|
2019-05-03 04:20:21 +10:00
|
|
|
)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
2019-05-10 03:55:02 +10:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "%s(%r)" % (type(self).__name__, self.dict)
|
2019-04-27 06:52:26 +10:00
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
def __getitem__(self, key) -> ListModel:
|
|
|
|
return self.dict[key]
|
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, key, value: ListModel) -> None:
|
2019-04-13 23:41:02 +10:00
|
|
|
value.setParent(self)
|
2019-04-13 03:18:46 +10:00
|
|
|
self.dict[key] = value
|
|
|
|
|
|
|
|
|
|
|
|
def __detitem__(self, key) -> None:
|
|
|
|
del self.dict[key]
|
|
|
|
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.dict)
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
|
|
|
return len(self.dict)
|
2019-05-10 03:55:02 +10:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(result=str)
|
|
|
|
def repr(self) -> str:
|
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str, result="QVariant")
|
|
|
|
def get(self, key) -> ListModel:
|
|
|
|
return self.dict[key]
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(str, result=bool)
|
|
|
|
def has(self, key) -> bool:
|
|
|
|
return key in self.dict
|