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 namedlist import namedlist
|
|
|
|
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
|
|
|
)
|
|
|
|
|
|
|
|
NewValue = Union[Mapping[str, Any], Sequence]
|
2019-04-15 06:12:07 +10:00
|
|
|
ReturnItem = Dict[str, Any]
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
class ListModel(QAbstractListModel):
|
2019-04-15 04:32:51 +10:00
|
|
|
changed = pyqtSignal()
|
|
|
|
|
2019-04-13 23:41:02 +10:00
|
|
|
def __init__(self,
|
2019-04-18 07:07:20 +10:00
|
|
|
initial_data: Optional[List[NewValue]] = None,
|
|
|
|
container: Callable[..., MutableSequence] = list,
|
|
|
|
parent: Optional[QObject] = None) -> None:
|
2019-04-13 23:41:02 +10:00
|
|
|
super().__init__(parent)
|
2019-04-18 07:07:20 +10:00
|
|
|
self._ref_namedlist = None
|
|
|
|
self._roles: Tuple[str, ...] = ()
|
|
|
|
self._data: MutableSequence = 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-18 07:07:20 +10:00
|
|
|
return self._data[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:
|
|
|
|
return self.rowCount()
|
|
|
|
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
def roleNames(self) -> Dict[int, bytes]:
|
|
|
|
return {Qt.UserRole + i: bytes(f, "utf-8")
|
|
|
|
for i, f in enumerate(self._roles, 1)}
|
|
|
|
|
|
|
|
|
|
|
|
def data(self, index: QModelIndex, role: int = Qt.DisplayRole) -> Any:
|
|
|
|
if role <= Qt.UserRole:
|
|
|
|
return None
|
|
|
|
|
2019-04-18 07:07:20 +10:00
|
|
|
return self._data[index.row()][role - Qt.UserRole - 1]
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
def rowCount(self, _: QModelIndex = QModelIndex()) -> int:
|
2019-04-18 07:07:20 +10:00
|
|
|
return len(self._data)
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
def _convert_new_value(self, value: NewValue) -> Any:
|
|
|
|
if isinstance(value, Mapping):
|
|
|
|
if not self._ref_namedlist:
|
|
|
|
self._ref_namedlist = namedlist("ListItem", value.keys())
|
|
|
|
self._roles = tuple(value.keys())
|
|
|
|
|
|
|
|
return self._ref_namedlist(**value) # type: ignore
|
|
|
|
|
|
|
|
if isinstance(value, Sequence):
|
|
|
|
if not self._ref_namedlist:
|
|
|
|
try:
|
|
|
|
self._ref_namedlist = namedlist(
|
|
|
|
value.__class__.__name__, value._fields # type: ignore
|
|
|
|
)
|
|
|
|
self._roles = tuple(value._fields) # type: ignore
|
|
|
|
except AttributeError:
|
|
|
|
raise TypeError(
|
|
|
|
"Need a mapping/dict, namedtuple or namedlist as "
|
|
|
|
"first value to set allowed keys/fields."
|
|
|
|
)
|
|
|
|
|
|
|
|
return self._ref_namedlist(*value) # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
raise TypeError("Value must be a mapping or sequence.")
|
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
@pyqtProperty(int, constant=True)
|
|
|
|
def count(self) -> int: # pylint: disable=arguments-differ
|
|
|
|
return self.rowCount()
|
|
|
|
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
@pyqtSlot(int, result="QVariantMap")
|
2019-04-15 06:12:07 +10:00
|
|
|
def get(self, index: int) -> ReturnItem:
|
2019-04-18 07:07:20 +10:00
|
|
|
return self._data[index]._asdict()
|
2019-03-22 14:28:14 +11: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
|
|
|
|
|
|
|
|
raise ValueError(f"No {type(self._ref_namedlist)} in list with "
|
|
|
|
f"property {prop!r} set to {is_value!r}.")
|
|
|
|
|
|
|
|
|
2019-04-15 06:12:07 +10:00
|
|
|
@pyqtSlot(str, "QVariant", result="QVariantMap")
|
|
|
|
def getWhere(self, prop: str, is_value: Any) -> ReturnItem:
|
|
|
|
return self.get(self.indexWhere(prop, is_value))
|
|
|
|
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
@pyqtSlot(int, list)
|
|
|
|
def insert(self, index: int, value: NewValue) -> None:
|
|
|
|
value = self._convert_new_value(value)
|
|
|
|
self.beginInsertRows(QModelIndex(), index, index)
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data.insert(index, value)
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endInsertRows()
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
|
|
|
@pyqtSlot(list)
|
|
|
|
def append(self, value: NewValue) -> None:
|
|
|
|
self.insert(self.rowCount(), value)
|
|
|
|
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
@pyqtSlot(list)
|
|
|
|
def extend(self, values: Iterable[NewValue]) -> None:
|
|
|
|
for val in values:
|
|
|
|
self.append(val)
|
|
|
|
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
@pyqtSlot(int, list)
|
|
|
|
def set(self, index: int, value: NewValue) -> None:
|
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-18 07:07:20 +10:00
|
|
|
self._data[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")
|
|
|
|
def setProperty(self, index: int, prop: str, value: Any) -> None:
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data[index][self._roles.index(prop)] = value
|
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
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
@pyqtSlot(int, int)
|
|
|
|
@pyqtSlot(int, int, int)
|
|
|
|
def move(self, from_: int, to: int, n: int = 1) -> None:
|
|
|
|
qlast = from_ + n - 1
|
|
|
|
|
|
|
|
if (n <= 0) or (from_ == to) or (qlast == to) or \
|
|
|
|
not (self.rowCount() > qlast >= 0) or \
|
|
|
|
not self.rowCount() >= to >= 0:
|
|
|
|
logging.warning("No need for move or out of range")
|
|
|
|
return
|
|
|
|
|
|
|
|
qidx = QModelIndex()
|
|
|
|
qto = min(self.rowCount(), to + n if to > from_ else to)
|
|
|
|
# print(f"self.beginMoveRows(qidx, {from_}, {qlast}, qidx, {qto})")
|
|
|
|
valid = self.beginMoveRows(qidx, from_, qlast, qidx, qto)
|
|
|
|
|
|
|
|
if not valid:
|
|
|
|
logging.warning("Invalid move operation")
|
|
|
|
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-13 03:18:46 +10:00
|
|
|
def remove(self, index: int) -> None: # pylint: disable=arguments-differ
|
2019-03-22 14:28:14 +11:00
|
|
|
self.beginRemoveRows(QModelIndex(), index, index)
|
2019-04-18 07:07:20 +10:00
|
|
|
del self._data[index]
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endRemoveRows()
|
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)
|
|
|
|
self.beginRemoveRows(QModelIndex(), 0, self.rowCount())
|
2019-04-18 07:07:20 +10:00
|
|
|
self._data.clear()
|
2019-03-22 14:28:14 +11:00
|
|
|
self.endRemoveRows()
|
2019-04-15 04:32:51 +10:00
|
|
|
self.changed.emit()
|