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-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-23 04:24:45 +10:00
|
|
|
parent: QObject,
|
2019-04-21 07:36:21 +10:00
|
|
|
initial_data: Optional[List[NewItem]] = None,
|
2019-04-23 04:24:45 +10:00
|
|
|
container: Callable[..., MutableSequence] = list) -> 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
|
|
|
|
|
|
|
def __getitem__(self, index):
|
2019-04-30 13:51:37 +10:00
|
|
|
return self.get(index)
|
2019-04-13 03:18:46 +10:00
|
|
|
|
|
|
|
|
|
|
|
def __setitem__(self, index, value) -> None:
|
|
|
|
self.set(index, value)
|
|
|
|
|
|
|
|
|
|
|
|
def __delitem__(self, index) -> None:
|
|
|
|
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-04-22 10:55:24 +10:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self._data)
|
|
|
|
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtProperty("QVariant", 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-03-22 14:28:14 +11:00
|
|
|
def roleNames(self) -> Dict[int, bytes]:
|
|
|
|
return {Qt.UserRole + i: bytes(f, "utf-8")
|
2019-04-21 07:36:21 +10:00
|
|
|
for i, f in enumerate(self.roles, 1)} \
|
|
|
|
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")
|
|
|
|
def get(self, index: Index) -> ListItem:
|
|
|
|
if isinstance(index, str):
|
|
|
|
index = self.indexWhere(self.mainKey, index)
|
|
|
|
|
|
|
|
return self._data[index] # type: ignore
|
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-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(int, "QVariantMap")
|
|
|
|
@pyqtSlot(int, "QVariantMap", "QVariant")
|
|
|
|
@pyqtSlot(str, "QVariantMap")
|
|
|
|
@pyqtSlot(str, "QVariantMap", "QVariant")
|
|
|
|
def update(self,
|
|
|
|
index: Index,
|
|
|
|
value: NewItem,
|
|
|
|
ignore_roles: Sequence[str] = ()) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
value = self._convert_new_value(value)
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
if isinstance(index, str):
|
|
|
|
index = self.indexWhere(self.mainKey or value.mainKey, index)
|
2019-04-22 05:20:20 +10:00
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
to_update = self._data[index] # type: ignore
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
qidx = QAbstractListModel.index(self, index, 0)
|
|
|
|
self.dataChanged.emit(qidx, qidx, self.roleNames())
|
|
|
|
self.changed.emit()
|
|
|
|
|
|
|
|
|
2019-04-30 13:51:37 +10:00
|
|
|
@pyqtSlot(str, "QVariantMap")
|
|
|
|
@pyqtSlot(str, "QVariantMap", int)
|
|
|
|
@pyqtSlot(str, "QVariantMap", int, list)
|
|
|
|
def upsert(self,
|
|
|
|
where_main_key_is_value: Any,
|
|
|
|
update_with: NewItem,
|
|
|
|
index_if_insert: Optional[int] = None,
|
|
|
|
ignore_roles: Sequence[str] = ()) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
try:
|
2019-04-30 13:51:37 +10:00
|
|
|
self.update(where_main_key_is_value, update_with, ignore_roles)
|
|
|
|
except (IndexError, ValueError):
|
|
|
|
self.insert(index_if_insert or len(self), update_with)
|
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:
|
|
|
|
if isinstance(index, str):
|
|
|
|
index = self.indexWhere(self.mainKey, index)
|
|
|
|
|
2019-04-13 22:59:10 +10:00
|
|
|
qidx = QAbstractListModel.index(self, index, 0)
|
2019-03-22 14:28:14 +11:00
|
|
|
value = self._convert_new_value(value)
|
2019-04-30 13:51:37 +10:00
|
|
|
self._data[index] = value # type: ignore
|
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:
|
|
|
|
if isinstance(index, str):
|
|
|
|
index = self.indexWhere(self.mainKey, index)
|
|
|
|
|
|
|
|
setattr(self._data[index], prop, value) # type: ignore
|
2019-04-13 22:59:10 +10:00
|
|
|
qidx = QAbstractListModel.index(self, index, 0)
|
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, int)
|
|
|
|
@pyqtSlot(int, int, int)
|
|
|
|
def move(self, from_: int, to: int, n: int = 1) -> None:
|
2019-04-21 07:36:21 +10:00
|
|
|
# pylint: disable=invalid-name
|
2019-03-22 14:28:14 +11:00
|
|
|
qlast = from_ + n - 1
|
|
|
|
|
|
|
|
if (n <= 0) or (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-04-23 01:31:06 +10:00
|
|
|
qto = min(len(self), to + n if to > from_ else to)
|
2019-03-22 14:28:14 +11:00
|
|
|
# print(f"self.beginMoveRows(qidx, {from_}, {qlast}, qidx, {qto})")
|
|
|
|
valid = self.beginMoveRows(qidx, from_, qlast, qidx, qto)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
last = from_ + n
|
2019-04-18 07:07:20 +10:00
|
|
|
cut = self._data[from_:last]
|
|
|
|
del self._data[from_:last]
|
|
|
|
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:
|
|
|
|
if isinstance(index, str):
|
|
|
|
index = self.indexWhere(self.mainKey, index)
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
self.beginRemoveRows(QModelIndex(), index, index)
|
2019-04-30 13:51:37 +10:00
|
|
|
del self._data[index] # type: ignore
|
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-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()
|