2019-04-21 07:36:21 +10:00
|
|
|
from typing import Any, Callable, Optional, Tuple, Union
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
from PyQt5.QtCore import QObject, pyqtProperty, pyqtSignal
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
class ListItem(QObject):
|
|
|
|
roles: Tuple[str, ...] = ()
|
2019-04-19 16:07:01 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__()
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
for role, value in zip(self.roles, args):
|
|
|
|
setattr(self, role, value)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
for role, value in kwargs.items():
|
|
|
|
setattr(self, role, value)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
def __repr__(self) -> str:
|
|
|
|
return "%s(%s)" % (
|
|
|
|
type(self).__name__,
|
|
|
|
", ".join((f"{r}={getattr(self, r)!r}" for r in self.roles)),
|
|
|
|
)
|
2019-04-12 18:33:09 +10:00
|
|
|
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
@pyqtProperty(str, constant=True)
|
|
|
|
def repr(self) -> str:
|
|
|
|
return repr(self)
|
|
|
|
|
|
|
|
|
|
|
|
def prop(qt_type: Union[str, Callable],
|
|
|
|
name: str,
|
|
|
|
signal: Optional[pyqtSignal] = None,
|
|
|
|
default_value: Any = None) -> pyqtProperty:
|
|
|
|
|
|
|
|
def fget(self, name=name, default_value=default_value):
|
|
|
|
if not hasattr(self, f"_{name}"):
|
|
|
|
setattr(self, f"_{name}", default_value)
|
|
|
|
return getattr(self, f"_{name}")
|
|
|
|
|
|
|
|
def fset(self, value, name=name, signal=signal):
|
|
|
|
setattr(self, f"_{name}", value)
|
|
|
|
if signal:
|
|
|
|
getattr(self, f"{name}Changed").emit(value)
|
|
|
|
|
|
|
|
kws = {"notify": signal} if signal else {"constant": True}
|
|
|
|
|
|
|
|
return pyqtProperty(qt_type, fget=fget, fset=fset, **kws)
|
|
|
|
|
|
|
|
|
|
|
|
class User(ListItem):
|
|
|
|
roles = ("userId", "displayName", "avatarUrl", "statusMessage")
|
|
|
|
|
|
|
|
displayNameChanged = pyqtSignal("QVariant")
|
|
|
|
avatarUrlChanged = pyqtSignal("QVariant")
|
|
|
|
statusMessageChanged = pyqtSignal(str)
|
|
|
|
|
|
|
|
userId = prop(str, "userId")
|
|
|
|
displayName = prop("QVariant", "displayName", displayNameChanged)
|
|
|
|
avatarUrl = prop(str, "avatarUrl", avatarUrlChanged)
|
|
|
|
statusMessage = prop(str, "statusMessage", statusMessageChanged, "")
|
|
|
|
|
|
|
|
|
|
|
|
class Room(ListItem):
|
|
|
|
roles = ("roomId", "displayName", "topic", "typingUsers")
|
|
|
|
|
|
|
|
displayNameChanged = pyqtSignal("QVariant")
|
|
|
|
topicChanged = pyqtSignal(str)
|
|
|
|
typingUsersChanged = pyqtSignal("QVariantList")
|
|
|
|
|
|
|
|
roomId = prop(str, "roomId")
|
|
|
|
displayName = prop(str, "displayName", displayNameChanged)
|
|
|
|
topic = prop(str, "topic", topicChanged, "")
|
|
|
|
typingUsers = prop(list, "typingUsers", typingUsersChanged, [])
|
|
|
|
|
|
|
|
|
|
|
|
class RoomEvent(ListItem):
|
|
|
|
roles = ("type", "dateTime", "dict", "isLocalEcho")
|
|
|
|
|
|
|
|
type = prop(str, "type")
|
|
|
|
dateTime = prop("QVariant", "dateTime")
|
|
|
|
dict = prop("QVariantMap", "dict")
|
|
|
|
isLocalEcho = prop(bool, "isLocalEcho", None, False)
|