2019-03-22 14:28:14 +11:00
|
|
|
import logging
|
2019-05-11 10:12:58 +10:00
|
|
|
import textwrap
|
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,
|
2019-05-10 06:08:34 +10:00
|
|
|
Sequence, Set, 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-05-12 05:52:56 +10:00
|
|
|
initial_data: Optional[List[NewItem]] = None,
|
|
|
|
container: Callable[..., MutableSequence] = list,
|
|
|
|
default_factory: Optional[Callable[[str], ListItem]] = None,
|
|
|
|
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-05-12 05:52:56 +10:00
|
|
|
self.default_factory = default_factory
|
|
|
|
|
2019-04-13 03:18:46 +10:00
|
|
|
if initial_data:
|
|
|
|
self.extend(initial_data)
|
|
|
|
|
|
|
|
|
|
|
|
def __repr__(self) -> str:
|
2019-05-11 10:12:58 +10:00
|
|
|
if not self._data:
|
2019-05-12 05:52:56 +10:00
|
|
|
return "\033[35m%s\033[0m()" % type(self).__name__
|
2019-05-11 10:12:58 +10:00
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
return "\033[35m%s\033[0m(\n%s\n)" % (
|
2019-05-11 10:12:58 +10:00
|
|
|
type(self).__name__,
|
|
|
|
textwrap.indent(
|
|
|
|
",\n".join((repr(item) for item in self._data)),
|
|
|
|
prefix = " " * 4,
|
|
|
|
)
|
|
|
|
)
|
2019-04-18 07:07:20 +10:00
|
|
|
|
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:
|
2019-05-12 05:52:56 +10:00
|
|
|
self.indexWhere(index)
|
2019-05-03 06:10:41 +10:00
|
|
|
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-12 05:52:56 +10:00
|
|
|
def __bool__(self) -> bool:
|
|
|
|
return bool(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-05-12 05:52:56 +10:00
|
|
|
@pyqtSlot("QVariant", result=int)
|
|
|
|
def indexWhere(self,
|
|
|
|
main_key_is_value: Any,
|
|
|
|
_can_use_default_factory: bool = True) -> int:
|
|
|
|
|
2019-04-18 07:07:20 +10:00
|
|
|
for i, item in enumerate(self._data):
|
2019-05-12 05:52:56 +10:00
|
|
|
if getattr(item, self.mainKey) == main_key_is_value:
|
2019-04-12 18:33:09 +10:00
|
|
|
return i
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
if _can_use_default_factory and self.default_factory:
|
|
|
|
return self.append(self.default_factory(main_key_is_value))
|
|
|
|
|
|
|
|
raise ValueError(
|
|
|
|
f"No item in model data with "
|
|
|
|
f"property {self.mainKey} is set to {main_key_is_value!r}."
|
|
|
|
)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
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:
|
2019-05-12 05:52:56 +10:00
|
|
|
i_index: int = \
|
|
|
|
self.indexWhere(index, _can_use_default_factory=False) \
|
|
|
|
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):
|
2019-05-12 05:52:56 +10:00
|
|
|
if self.default_factory and isinstance(index, str):
|
|
|
|
item = self.default_factory(index)
|
|
|
|
self.append(item)
|
|
|
|
return item
|
2019-05-03 04:20:21 +10:00
|
|
|
raise
|
2019-05-12 05:52:56 +10:00
|
|
|
|
2019-05-03 04:20:21 +10:00
|
|
|
return default
|
2019-04-15 06:12:07 +10:00
|
|
|
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
@pyqtSlot(int, "QVariantMap", result=int)
|
|
|
|
def insert(self, index: int, value: NewItem) -> int:
|
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-05-12 05:52:56 +10:00
|
|
|
return index
|
2019-03-22 14:28:14 +11:00
|
|
|
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
@pyqtSlot("QVariantMap", result=int)
|
|
|
|
def append(self, value: NewItem) -> int:
|
|
|
|
return 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-05-15 09:06:59 +10:00
|
|
|
def updateItem(self,
|
|
|
|
index: Index,
|
|
|
|
value: NewItem,
|
|
|
|
ignore_roles: Sequence[str] = ()) -> int:
|
2019-04-21 07:36:21 +10:00
|
|
|
value = self._convert_new_value(value)
|
|
|
|
|
2019-05-12 05:52:56 +10:00
|
|
|
i_index: int = self.indexWhere(index, _can_use_default_factory=False) \
|
2019-05-03 04:20:21 +10:00
|
|
|
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
|
|
|
|
2019-05-10 06:08:34 +10:00
|
|
|
updated_roles: Set[int] = set()
|
|
|
|
|
|
|
|
for role_name, role_num in self.roleNumbers().items():
|
|
|
|
if role_name not in ignore_roles:
|
|
|
|
old_value = getattr(to_update, role_name)
|
|
|
|
new_value = getattr(value, role_name)
|
|
|
|
|
|
|
|
if old_value != new_value:
|
|
|
|
try:
|
|
|
|
setattr(to_update, role_name, new_value)
|
|
|
|
except AttributeError: # constant/not settable
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
updated_roles.add(role_num)
|
|
|
|
|
|
|
|
if updated_roles:
|
|
|
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
|
|
|
self.dataChanged.emit(qidx, qidx, updated_roles)
|
|
|
|
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-15 09:06:59 +10:00
|
|
|
index = self.updateItem(
|
|
|
|
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-12 05:52:56 +10:00
|
|
|
i_index: int = self.indexWhere(index) \
|
2019-05-03 04:20:21 +10:00
|
|
|
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-12 05:52:56 +10:00
|
|
|
i_index: int = self.indexWhere(index) \
|
2019-05-03 04:20:21 +10:00
|
|
|
if isinstance(index, str) else index
|
2019-04-30 13:51:37 +10:00
|
|
|
|
2019-05-10 06:08:34 +10:00
|
|
|
if getattr(self[i_index], prop) != value:
|
|
|
|
setattr(self[i_index], prop, value)
|
|
|
|
qidx = QAbstractListModel.index(self, i_index, 0)
|
|
|
|
self.dataChanged.emit(qidx, qidx, (self.roleNumbers()[prop],))
|
|
|
|
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-12 05:52:56 +10:00
|
|
|
i_from: int = self.indexWhere(from_) \
|
2019-05-03 06:10:41 +10:00
|
|
|
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-12 05:52:56 +10:00
|
|
|
i_index: int = self.indexWhere(index) \
|
2019-05-03 04:20:21 +10:00
|
|
|
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:
|
2019-05-12 05:52:56 +10:00
|
|
|
i_index: int = self.indexWhere(index) \
|
2019-05-03 04:20:21 +10:00
|
|
|
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:
|
2019-05-10 06:11:15 +10:00
|
|
|
if not self._data:
|
|
|
|
return
|
|
|
|
|
2019-03-22 14:28:14 +11:00
|
|
|
# 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()
|