2019-03-22 14:28:14 +11:00
|
|
|
import logging
|
2019-04-13 03:18:46 +10:00
|
|
|
from typing import (
|
2019-04-18 07:07:20 +10:00
|
|
|
Any, Callable, Dict, Iterable, List, Mapping, MutableSequence, Optional,
|
|
|
|
Sequence, Tuple, Union
|
2019-04-13 03:18:46 +10:00
|
|
|
)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
from PyQt5.QtCore import (
|
2019-04-15 04:32:51 +10:00
|
|
|
QAbstractListModel, QModelIndex, QObject, Qt, pyqtProperty, pyqtSignal,
|
|
|
|
pyqtSlot
|
2019-03-22 14:28:14 +11:00
|
|
|
)
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
from .list_item import ListItem
|
2019-04-21 07:36:21 +10:00
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
Index = Union[int, str]
|
2019-04-21 07:36:21 +10:00
|
|
|
NewItem = Union[ListItem, Mapping[str, Any], Sequence]
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
class _GetFail:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class _PopFail:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
class ListModel(QAbstractListModel):
|
2019-04-30 13:51:37 +10:00
|
|
|
rolesSet = pyqtSignal()
|
2019-04-23 01:31:06 +10:00
|
|
|
changed = pyqtSignal()
|
|
|
|
countChanged = pyqtSignal(int)
|
2019-04-15 04:32:51 +10:00
|
|
|
|
2019-04-13 23:41:02 +10:00
|
|
|
def __init__(self,
|
2019-04-21 07:36:21 +10:00
|
|
|
initial_data: Optional[List[NewItem]] = None,
|
2019-05-03 04:20:21 +10:00
|
|
|
container: Callable[..., MutableSequence] = list,
|
|
|
|
parent: QObject = None) -> None:
|
2019-04-13 23:41:02 +10:00
|
|
|
super().__init__(parent)
|
2019-04-21 07:36:21 +10:00
|
|
|
self._data: MutableSequence[ListItem] = container()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
if initial_data:
|
|
|
|
self.extend(initial_data)
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2019-04-18 07:07:20 +10:00
|
|
|
return "%s(%r)" % (type(self).__name__, self._data)
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
|
2019-05-03 06:10:41 +10:00
|
|
|
def __contains__(self, index: Index) -> bool:
|
|
|
|
if isinstance(index, str):
|
|
|
|
try:
|
|
|
|
self.indexWhere(self.mainKey, index)
|
|
|
|
return True
|
|
|
|
except ValueError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return index in self._data
|
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
def __getitem__(self, index: Index) -> ListItem:
|
2019-04-30 13:51:37 +10:00
|
|
|
return self.get(index)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
def __setitem__(self, index: Index, value: NewItem) -> None:
|
2019-04-13 03:18:46 +10:00
|
|
|
self.set(index, value)
|
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
def __delitem__(self, index: Index) -> None:
|
2019-04-13 03:18:46 +10:00
|
|
|
self.remove(index)
|
|
|
|
|
|
|
|
|
|
|
|
def __len__(self) -> int:
|
2019-04-23 01:31:06 +10:00
|
|
|
return len(self._data)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
def __iter__(self) -> Iterable[NewItem]:
|
2019-04-22 10:55:24 +10:00
|
|
|
return iter(self._data)
|
|
|
|
|
|
|
|
|
2019-05-02 07:31:02 +10:00
|
|
|
@pyqtSlot(result=str)
|
|
|
|
def repr(self) -> str:
|
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtProperty("QStringList", notify=rolesSet)
|
2019-04-21 07:36:21 +10:00
|
|
|
def roles(self) -> Tuple[str, ...]:
|
|
|
|
return self._data[0].roles if self._data else () # type: ignore
|
|
|
|
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtProperty("QVariant", notify=rolesSet)
|
|
|
|
def mainKey(self) -> Optional[str]:
|
|
|
|
return self._data[0].mainKey if self._data else None
|
|
|
|
|
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
def roleNumbers(self) -> Dict[str, int]:
|
|
|
|
return {name: Qt.UserRole + i
|
|
|
|
for i, name in enumerate(self.roles, 1)} \
|
|
|
|
if self._data else {}
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
def roleNames(self) -> Dict[int, bytes]:
|
2019-05-07 03:07:00 +10:00
|
|
|
return {Qt.UserRole + i: bytes(name, "utf-8")
|
|
|
|
for i, name in enumerate(self.roles, 1)} \
|
2019-04-21 07:36:21 +10:00
|
|
|
if self._data else {}
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
def data(self, index: QModelIndex, role: int = Qt.DisplayRole) -> Any:
|
|
|
|
if role <= Qt.UserRole:
|
|
|
|
return None
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
return getattr(self._data[index.row()],
|
|
|
|
str(self.roleNames()[role], "utf8"))
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
def rowCount(self, _: QModelIndex = QModelIndex()) -> int:
|
2019-04-23 01:31:06 +10:00
|
|
|
return len(self)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
def _convert_new_value(self, value: NewItem) -> ListItem:
|
|
|
|
def convert() -> ListItem:
|
|
|
|
if self._data and isinstance(value, Mapping):
|
2019-04-23 00:08:42 +10:00
|
|
|
if not set(value.keys()) <= set(self.roles):
|
|
|
|
raise ValueError(
|
|
|
|
f"{value}: must have all these keys: {self.roles}"
|
|
|
|
)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
return type(self._data[0])(**value)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
if not self._data and isinstance(value, Mapping):
|
|
|
|
raise NotImplementedError("First item must be set from Python")
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
if self._data and isinstance(value, type(self._data[0])):
|
|
|
|
return value
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
if not self._data and isinstance(value, ListItem):
|
|
|
|
return value
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
raise TypeError("%r: must be mapping or %s" % (
|
|
|
|
value,
|
|
|
|
type(self._data[0]).__name__ if self._data else "ListItem"
|
|
|
|
))
|
|
|
|
|
|
|
|
value = convert()
|
|
|
|
value.setParent(self)
|
|
|
|
return value
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-04-23 01:31:06 +10:00
|
|
|
@pyqtProperty(int, notify=countChanged)
|
2019-04-21 07:36:21 +10:00
|
|
|
def count(self) -> int:
|
2019-04-23 01:31:06 +10:00
|
|
|
return len(self)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
2019-04-12 18:33:09 +10:00
|
|
|
@pyqtSlot(str, "QVariant", result=int)
|
|
|
|
def indexWhere(self, prop: str, is_value: Any) -> int:
|
2019-04-18 07:07:20 +10:00
|
|
|
for i, item in enumerate(self._data):
|
2019-04-12 18:33:09 +10:00
|
|
|
if getattr(item, prop) == is_value:
|
|
|
|
return i
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
raise ValueError(f"No item in model data with "
|
2019-04-12 18:33:09 +10:00
|
|
|
f"property {prop!r} set to {is_value!r}.")
|
|
|
|
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(int, result="QVariant")
|
|
|
|
@pyqtSlot(str, result="QVariant")
|
2019-05-03 04:20:21 +10:00
|
|
|
@pyqtSlot(int, "QVariant", result="QVariant")
|
|
|
|
@pyqtSlot(str, "QVariant", result="QVariant")
|
|
|
|
def get(self, index: Index, default: Any = _GetFail()) -> ListItem:
|
|
|
|
try:
|
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
return self._data[i_index]
|
|
|
|
|
|
|
|
except (ValueError, IndexError):
|
|
|
|
if isinstance(default, _GetFail):
|
|
|
|
raise
|
|
|
|
return default
|
2019-04-15 06:12:07 +10:00
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
@pyqtSlot(int, "QVariantMap")
|
|
|
|
def insert(self, index: int, value: NewItem) -> None:
|
2019-03-22 14:28:14 +11:00
|
|
|
value = self._convert_new_value(value)
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
self.beginInsertRows(QModelIndex(), index, index)
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
had_data = bool(self._data)
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data.insert(index, value)
|
2019-04-30 13:51:37 +10:00
|
|
|
if not had_data:
|
|
|
|
self.rolesSet.emit()
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endInsertRows()
|
2019-04-23 01:31:06 +10:00
|
|
|
|
|
|
|
self.countChanged.emit(len(self))
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
@pyqtSlot("QVariantMap")
|
|
|
|
def append(self, value: NewItem) -> None:
|
2019-04-23 01:31:06 +10:00
|
|
|
self.insert(len(self), value)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
@pyqtSlot(list)
|
2019-04-21 07:36:21 +10:00
|
|
|
def extend(self, values: Iterable[NewItem]) -> None:
|
2019-04-13 03:18:46 +10:00
|
|
|
for val in values:
|
|
|
|
self.append(val)
|
|
|
|
|
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
@pyqtSlot(int, "QVariantMap", result=int)
|
|
|
|
@pyqtSlot(int, "QVariantMap", "QStringList", result=int)
|
|
|
|
@pyqtSlot(str, "QVariantMap", result=int)
|
|
|
|
@pyqtSlot(str, "QVariantMap", "QStringList", result=int)
|
2019-04-30 13:51:37 +10:00
|
|
|
def update(self,
|
|
|
|
index: Index,
|
|
|
|
value: NewItem,
|
2019-05-03 04:20:21 +10:00
|
|
|
ignore_roles: Sequence[str] = ()) -> int:
|
2019-04-21 07:36:21 +10:00
|
|
|
value = self._convert_new_value(value)
|
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
2019-04-22 05:20:20 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
to_update = self[i_index]
|
2019-04-30 13:51:37 +10:00
|
|
|
|
|
|
|
for role in self.roles:
|
|
|
|
if role not in ignore_roles:
|
|
|
|
try:
|
|
|
|
setattr(to_update, role, getattr(value, role))
|
|
|
|
except AttributeError: # constant/not settable
|
|
|
|
pass
|
2019-04-21 07:36:21 +10:00
|
|
|
|
2019-05-07 03:07:00 +10:00
|
|
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
|
|
|
updated = [number for name, number in self.roleNumbers().items()
|
|
|
|
if name not in ignore_roles]
|
|
|
|
self.dataChanged.emit(qidx, qidx, updated)
|
2019-04-21 07:36:21 +10:00
|
|
|
self.changed.emit()
|
2019-05-03 04:20:21 +10:00
|
|
|
return i_index
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(str, "QVariantMap")
|
|
|
|
@pyqtSlot(str, "QVariantMap", int)
|
2019-05-03 04:20:21 +10:00
|
|
|
@pyqtSlot(str, "QVariantMap", int, int)
|
|
|
|
@pyqtSlot(str, "QVariantMap", int, int, "QStringList")
|
2019-04-30 13:51:37 +10:00
|
|
|
def upsert(self,
|
2019-05-03 04:20:21 +10:00
|
|
|
where_main_key_is: Any,
|
|
|
|
update_with: NewItem,
|
|
|
|
new_index_if_insert: Optional[int] = None,
|
|
|
|
new_index_if_update: Optional[int] = None,
|
|
|
|
ignore_roles: Sequence[str] = ()) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
try:
|
2019-05-03 04:20:21 +10:00
|
|
|
index = self.update(where_main_key_is, update_with, ignore_roles)
|
2019-04-30 13:51:37 +10:00
|
|
|
except (IndexError, ValueError):
|
2019-05-03 04:20:21 +10:00
|
|
|
self.insert(new_index_if_insert or len(self), update_with)
|
|
|
|
else:
|
|
|
|
if new_index_if_update:
|
|
|
|
self.move(index, new_index_if_update)
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
@pyqtSlot(int, list)
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(str, list)
|
|
|
|
def set(self, index: Index, value: NewItem) -> None:
|
2019-05-03 04:20:21 +10:00
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
|
|
|
value = self._convert_new_value(value)
|
|
|
|
self._data[i_index] = value
|
2019-03-22 14:28:14 +11:00
|
|
|
self.dataChanged.emit(qidx, qidx, self.roleNames())
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(int, str, "QVariant")
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(str, str, "QVariant")
|
|
|
|
def setProperty(self, index: Index, prop: str, value: Any) -> None:
|
2019-05-03 04:20:21 +10:00
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
setattr(self[i_index], prop, value)
|
|
|
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
2019-05-07 03:07:00 +10:00
|
|
|
self.dataChanged.emit(qidx, qidx, (self.roleNumbers()[prop],))
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(int, int)
|
|
|
|
@pyqtSlot(int, int, int)
|
2019-05-03 06:10:41 +10:00
|
|
|
@pyqtSlot(str, int)
|
|
|
|
@pyqtSlot(str, int, int)
|
|
|
|
def move(self, from_: Index, to: int, n: int = 1) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
# pylint: disable=invalid-name
|
2019-05-03 06:10:41 +10:00
|
|
|
i_from: int = self.indexWhere(self.mainKey, from_) \
|
|
|
|
if isinstance(from_, str) else from_
|
|
|
|
|
|
|
|
qlast = i_from + n - 1
|
2019-03-22 14:28:14 +11:00
|
|
|
|
2019-05-03 06:10:41 +10:00
|
|
|
if (n <= 0) or (i_from == to) or (qlast == to) or \
|
2019-04-23 01:31:06 +10:00
|
|
|
not (len(self) > qlast >= 0) or \
|
|
|
|
not len(self) >= to >= 0:
|
2019-03-22 14:28:14 +11:00
|
|
|
return
|
|
|
|
|
|
|
|
qidx = QModelIndex()
|
2019-05-03 06:10:41 +10:00
|
|
|
qto = min(len(self), to + n if to > i_from else to)
|
|
|
|
# print(f"self.beginMoveRows(qidx, {i_from}, {qlast}, qidx, {qto})")
|
|
|
|
valid = self.beginMoveRows(qidx, i_from, qlast, qidx, qto)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
if not valid:
|
2019-04-22 10:55:24 +10:00
|
|
|
logging.warning("Invalid move operation - %r", locals())
|
2019-03-22 14:28:14 +11:00
|
|
|
return
|
|
|
|
|
2019-05-03 06:10:41 +10:00
|
|
|
last = i_from + n
|
|
|
|
cut = self._data[i_from:last]
|
|
|
|
del self._data[i_from:last]
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data[to:to] = cut
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
self.endMoveRows()
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(int)
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(str)
|
|
|
|
def remove(self, index: Index) -> None:
|
2019-05-03 04:20:21 +10:00
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
|
|
|
|
|
|
|
self.beginRemoveRows(QModelIndex(), i_index, i_index)
|
|
|
|
del self._data[i_index]
|
|
|
|
self.endRemoveRows()
|
|
|
|
|
|
|
|
self.countChanged.emit(len(self))
|
|
|
|
self.changed.emit()
|
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(int, result="QVariant")
|
|
|
|
@pyqtSlot(str, result="QVariant")
|
|
|
|
def pop(self, index: Index, default: Any = _PopFail()) -> ListItem:
|
|
|
|
try:
|
|
|
|
i_index: int = self.indexWhere(self.mainKey, index) \
|
|
|
|
if isinstance(index, str) else index
|
|
|
|
item = self[i_index]
|
|
|
|
|
|
|
|
except (ValueError, IndexError):
|
|
|
|
if isinstance(default, _PopFail):
|
|
|
|
raise
|
|
|
|
return default
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
self.beginRemoveRows(QModelIndex(), i_index, i_index)
|
|
|
|
del self._data[i_index]
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endRemoveRows()
|
2019-04-23 01:31:06 +10:00
|
|
|
|
|
|
|
self.countChanged.emit(len(self))
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-05-03 04:20:21 +10:00
|
|
|
return item
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot()
|
|
|
|
def clear(self) -> None:
|
|
|
|
# Reimplemented for performance reasons (begin/endRemoveRows)
|
2019-04-23 01:31:06 +10:00
|
|
|
self.beginRemoveRows(QModelIndex(), 0, len(self))
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data.clear()
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endRemoveRows()
|
2019-04-23 01:31:06 +10:00
|
|
|
|
|
|
|
self.countChanged.emit(len(self))
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|