Show last room message as roomDelegate subtitle
This commit is contained in:
parent
463dca7c55
commit
c35f7f35af
|
@ -20,7 +20,7 @@ class User(NamedTuple):
|
|||
class Room(NamedTuple):
|
||||
room_id: str
|
||||
display_name: str
|
||||
subtitle: str = ""
|
||||
description: str = ""
|
||||
unread_messages: int = 0
|
||||
presence: Presence = Presence.none
|
||||
activity: Activity = Activity.none
|
||||
|
|
|
@ -4,19 +4,28 @@ from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
|
|||
|
||||
from namedlist import namedlist
|
||||
from PyQt5.QtCore import (
|
||||
QAbstractListModel, QModelIndex, Qt, pyqtProperty, pyqtSlot
|
||||
QAbstractListModel, QModelIndex, Qt, pyqtProperty, pyqtSlot, pyqtSignal
|
||||
)
|
||||
|
||||
NewValue = Union[Mapping[str, Any], Sequence]
|
||||
|
||||
|
||||
class _QtListModel(QAbstractListModel):
|
||||
updated = pyqtSignal()
|
||||
|
||||
def __init__(self) -> None:
|
||||
super().__init__()
|
||||
self._ref_namedlist = None
|
||||
self._roles: Tuple[str, ...] = ()
|
||||
self._list: list = []
|
||||
|
||||
self._update_counter: int = 0
|
||||
|
||||
for sig in (self.dataChanged, self.layoutChanged, self.modelReset,
|
||||
self.rowsInserted, self.rowsMoved, self.rowsRemoved):
|
||||
sig.connect(self.updated.emit)
|
||||
|
||||
|
||||
def roleNames(self) -> Dict[int, bytes]:
|
||||
return {Qt.UserRole + i: bytes(f, "utf-8")
|
||||
for i, f in enumerate(self._roles, 1)}
|
||||
|
@ -61,7 +70,7 @@ class _QtListModel(QAbstractListModel):
|
|||
|
||||
|
||||
@pyqtSlot(int, result="QVariantMap")
|
||||
def get(self, index: int) -> Dict[str, Any]:
|
||||
def get(self, index: int) -> Optional[Dict[str, Any]]:
|
||||
return self._list[index]._asdict()
|
||||
|
||||
|
||||
|
@ -73,7 +82,7 @@ class _QtListModel(QAbstractListModel):
|
|||
self.endInsertRows()
|
||||
|
||||
|
||||
@pyqtProperty(int)
|
||||
@pyqtProperty(int, constant=True)
|
||||
def count(self) -> int:
|
||||
return self.rowCount()
|
||||
|
||||
|
@ -142,6 +151,13 @@ class _QtListModel(QAbstractListModel):
|
|||
self.endRemoveRows()
|
||||
|
||||
|
||||
@pyqtProperty(int, notify=updated)
|
||||
def reloadThis(self) -> int:
|
||||
# http://www.mardy.it/blog/2016/11/qml-trick-force-re-evaluation-of.html
|
||||
self._update_counter += 1
|
||||
return self._update_counter
|
||||
|
||||
|
||||
class ListModel(MutableSequence):
|
||||
def __init__(self, initial_data: Optional[List[NewValue]] = None) -> None:
|
||||
super().__init__()
|
||||
|
|
|
@ -32,8 +32,8 @@ Rectangle {
|
|||
}
|
||||
|
||||
Base.HLabel {
|
||||
id: "roomSubtitle"
|
||||
text: chatPage.room.subtitle
|
||||
id: "roomDescription"
|
||||
text: chatPage.room.description
|
||||
font.pixelSize: smallSize
|
||||
elide: Text.ElideRight
|
||||
maximumLineCount: 1
|
||||
|
|
|
@ -75,12 +75,13 @@ ColumnLayout {
|
|||
0
|
||||
Layout.maximumHeight: Layout.minimumHeight
|
||||
|
||||
Layout.minimumWidth: parent.width - Layout.leftMargin * 2
|
||||
Layout.minimumWidth:
|
||||
parent.width - Layout.leftMargin - Layout.rightMargin
|
||||
Layout.maximumWidth: Layout.minimumWidth
|
||||
|
||||
Layout.margins: accountList.spacing
|
||||
Layout.leftMargin:
|
||||
sidePane.width < 36 + Layout.margins ? 0 : Layout.margins
|
||||
Layout.rightMargin: Layout.leftMargin
|
||||
Layout.rightMargin: 0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,9 +37,25 @@ MouseArea {
|
|||
rightPadding: leftPadding
|
||||
}
|
||||
Base.HLabel {
|
||||
function get_text() {
|
||||
var msgs = Backend.messagesModel[room_id]
|
||||
if (msgs.count < 1) { return "" }
|
||||
|
||||
var msg = msgs.get(-1)
|
||||
var color_ = (msg.sender_id === roomList.user.user_id ?
|
||||
"darkblue" : "purple")
|
||||
|
||||
return "<font color=\"" + color_ + "\">" +
|
||||
Backend.getUser(msg.sender_id).display_name +
|
||||
":</font> " +
|
||||
msg.content
|
||||
}
|
||||
|
||||
id: subtitleLabel
|
||||
visible: text !== ""
|
||||
text: subtitle
|
||||
text: Backend.messagesModel[room_id].reloadThis, get_text()
|
||||
textFormat: Text.StyledText
|
||||
|
||||
font.pixelSize: smallSize
|
||||
elide: Text.ElideRight
|
||||
maximumLineCount: 1
|
||||
|
|
Loading…
Reference in New Issue
Block a user