2019-04-22 05:20:20 +10:00
|
|
|
from typing import Any, Callable, Optional, Sequence, 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-22 05:20:20 +10:00
|
|
|
def __init__(self, *args, no_update: Sequence[str] = (), **kwargs):
|
2019-04-21 07:36:21 +10:00
|
|
|
super().__init__()
|
2019-04-22 05:20:20 +10:00
|
|
|
self.no_update = no_update
|
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:
|
2019-04-22 05:20:20 +10:00
|
|
|
return "%s(no_update=%s, %s)" % (
|
2019-04-21 07:36:21 +10:00
|
|
|
type(self).__name__,
|
2019-04-22 05:20:20 +10:00
|
|
|
self.no_update,
|
2019-04-21 07:36:21 +10:00
|
|
|
", ".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)
|
2019-04-23 01:37:41 +10:00
|
|
|
def _repr(self) -> str:
|
2019-04-21 07:36:21 +10:00
|
|
|
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):
|
2019-04-22 01:15:03 +10:00
|
|
|
roles = ("roomId", "category", "displayName", "topic", "typingUsers",
|
2019-04-22 05:20:20 +10:00
|
|
|
"inviter", "leftEvent")
|
2019-04-21 07:36:21 +10:00
|
|
|
|
2019-04-21 20:56:59 +10:00
|
|
|
categoryChanged = pyqtSignal(str)
|
2019-04-21 07:36:21 +10:00
|
|
|
displayNameChanged = pyqtSignal("QVariant")
|
|
|
|
topicChanged = pyqtSignal(str)
|
|
|
|
typingUsersChanged = pyqtSignal("QVariantList")
|
2019-04-22 05:34:08 +10:00
|
|
|
inviterChanged = pyqtSignal("QVariant")
|
|
|
|
leftEventChanged = pyqtSignal("QVariant")
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
roomId = prop(str, "roomId")
|
2019-04-22 05:34:08 +10:00
|
|
|
category = prop(str, "category", categoryChanged)
|
2019-04-21 07:36:21 +10:00
|
|
|
displayName = prop(str, "displayName", displayNameChanged)
|
|
|
|
topic = prop(str, "topic", topicChanged, "")
|
|
|
|
typingUsers = prop(list, "typingUsers", typingUsersChanged, [])
|
2019-04-22 05:34:08 +10:00
|
|
|
inviter = prop("QVariant", "inviter", inviterChanged)
|
|
|
|
leftEvent = prop("QVariant", "leftEvent", leftEventChanged)
|
2019-04-21 07:36:21 +10:00
|
|
|
|
|
|
|
|
|
|
|
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)
|