Show joined rooms, delete left rooms
To make the models update correctly in QML: - ListModel and _QtModel merged - Return a ListModelMap QObject from properties instead of a DefaultDict → QVariantMap
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import logging
|
||||
from collections.abc import MutableSequence
|
||||
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
|
||||
from typing import (
|
||||
Any, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple, Union
|
||||
)
|
||||
|
||||
from namedlist import namedlist
|
||||
from PyQt5.QtCore import (
|
||||
@@ -10,13 +11,39 @@ from PyQt5.QtCore import (
|
||||
NewValue = Union[Mapping[str, Any], Sequence]
|
||||
|
||||
|
||||
class _QtListModel(QAbstractListModel):
|
||||
def __init__(self) -> None:
|
||||
class ListModel(QAbstractListModel):
|
||||
def __init__(self, initial_data: Optional[List[NewValue]] = None) -> None:
|
||||
super().__init__()
|
||||
self._ref_namedlist = None
|
||||
self._roles: Tuple[str, ...] = ()
|
||||
self._list: list = []
|
||||
|
||||
self._update_count: int = 0
|
||||
|
||||
if initial_data:
|
||||
self.extend(initial_data)
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "%s[%s]" % (type(self).__name__,
|
||||
", ".join((repr(i) for i in self)))
|
||||
|
||||
def __getitem__(self, index):
|
||||
return self._list[index]
|
||||
|
||||
|
||||
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()
|
||||
|
||||
|
||||
def roleNames(self) -> Dict[int, bytes]:
|
||||
return {Qt.UserRole + i: bytes(f, "utf-8")
|
||||
for i, f in enumerate(self._roles, 1)}
|
||||
@@ -60,6 +87,11 @@ class _QtListModel(QAbstractListModel):
|
||||
raise TypeError("Value must be a mapping or sequence.")
|
||||
|
||||
|
||||
@pyqtProperty(int, constant=True)
|
||||
def count(self) -> int: # pylint: disable=arguments-differ
|
||||
return self.rowCount()
|
||||
|
||||
|
||||
@pyqtSlot(int, result="QVariantMap")
|
||||
def get(self, index: int) -> Dict[str, Any]:
|
||||
return self._list[index]._asdict()
|
||||
@@ -81,11 +113,7 @@ class _QtListModel(QAbstractListModel):
|
||||
self.beginInsertRows(QModelIndex(), index, index)
|
||||
self._list.insert(index, value)
|
||||
self.endInsertRows()
|
||||
|
||||
|
||||
@pyqtProperty(int)
|
||||
def count(self) -> int:
|
||||
return self.rowCount()
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
@pyqtSlot(list)
|
||||
@@ -93,19 +121,27 @@ class _QtListModel(QAbstractListModel):
|
||||
self.insert(self.rowCount(), value)
|
||||
|
||||
|
||||
@pyqtSlot(list)
|
||||
def extend(self, values: Iterable[NewValue]) -> None:
|
||||
for val in values:
|
||||
self.append(val)
|
||||
|
||||
|
||||
@pyqtSlot(int, list)
|
||||
def set(self, index: int, value: NewValue) -> None:
|
||||
qidx = self.index(index, 0)
|
||||
qidx = QAbstractListModel.index(index, 0)
|
||||
value = self._convert_new_value(value)
|
||||
self._list[index] = value
|
||||
self.dataChanged.emit(qidx, qidx, self.roleNames())
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
@pyqtSlot(int, str, "QVariant")
|
||||
def setProperty(self, index: int, prop: str, value: Any) -> None:
|
||||
self._list[index][self._roles.index(prop)] = value
|
||||
qidx = self.index(index, 0)
|
||||
qidx = QAbstractListModel.index(index, 0)
|
||||
self.dataChanged.emit(qidx, qidx, self.roleNames())
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@@ -135,13 +171,15 @@ class _QtListModel(QAbstractListModel):
|
||||
self._list[to:to] = cut
|
||||
|
||||
self.endMoveRows()
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
@pyqtSlot(int)
|
||||
def remove(self, index: int) -> None:
|
||||
def remove(self, index: int) -> None: # pylint: disable=arguments-differ
|
||||
self.beginRemoveRows(QModelIndex(), index, index)
|
||||
del self._list[index]
|
||||
self.endRemoveRows()
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
@pyqtSlot()
|
||||
@@ -150,56 +188,9 @@ class _QtListModel(QAbstractListModel):
|
||||
self.beginRemoveRows(QModelIndex(), 0, self.rowCount())
|
||||
self._list.clear()
|
||||
self.endRemoveRows()
|
||||
self._update_count += 1
|
||||
|
||||
|
||||
class ListModel(MutableSequence):
|
||||
def __init__(self, initial_data: Optional[List[NewValue]] = None) -> None:
|
||||
super().__init__()
|
||||
self.qt_model = _QtListModel()
|
||||
|
||||
if initial_data:
|
||||
self.extend(initial_data)
|
||||
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return "%s[%s]" % (type(self).__name__,
|
||||
", ".join((repr(i) for i in self)))
|
||||
|
||||
def __getitem__(self, index):
|
||||
# pylint: disable=protected-access
|
||||
return self.qt_model._list[index]
|
||||
|
||||
|
||||
def __setitem__(self, index, value) -> None:
|
||||
self.qt_model.set(index, value)
|
||||
|
||||
|
||||
def __delitem__(self, index) -> None:
|
||||
self.qt_model.remove(index)
|
||||
|
||||
|
||||
def __len__(self) -> int:
|
||||
return self.qt_model.rowCount()
|
||||
|
||||
|
||||
def insert(self, index: int, value: NewValue) -> None:
|
||||
self.qt_model.insert(index, value)
|
||||
|
||||
|
||||
def indexWhere(self, prop: str, is_value: Any) -> int:
|
||||
return self.qt_model.indexWhere(prop, is_value)
|
||||
|
||||
|
||||
def setProperty(self, index: int, prop: str, value: Any) -> None:
|
||||
"Set role of the item at *index* to *value*."
|
||||
self.qt_model.setProperty(index, prop, value)
|
||||
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
def move(self, from_: int, to: int, n: int = 1) -> None:
|
||||
"Move *n* items *from_* index *to* another."
|
||||
self.qt_model.move(from_, to, n)
|
||||
|
||||
|
||||
def clear(self) -> None:
|
||||
self.qt_model.clear()
|
||||
@pyqtProperty(int, constant=True)
|
||||
def reloadThis(self):
|
||||
return self._update_count
|
||||
|
Reference in New Issue
Block a user