Show last room message as roomDelegate subtitle

This commit is contained in:
miruka 2019-03-27 18:38:48 -04:00
parent 463dca7c55
commit c35f7f35af
5 changed files with 42 additions and 9 deletions

View File

@ -20,7 +20,7 @@ class User(NamedTuple):
class Room(NamedTuple): class Room(NamedTuple):
room_id: str room_id: str
display_name: str display_name: str
subtitle: str = "" description: str = ""
unread_messages: int = 0 unread_messages: int = 0
presence: Presence = Presence.none presence: Presence = Presence.none
activity: Activity = Activity.none activity: Activity = Activity.none

View File

@ -4,19 +4,28 @@ from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple, Union
from namedlist import namedlist from namedlist import namedlist
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QAbstractListModel, QModelIndex, Qt, pyqtProperty, pyqtSlot QAbstractListModel, QModelIndex, Qt, pyqtProperty, pyqtSlot, pyqtSignal
) )
NewValue = Union[Mapping[str, Any], Sequence] NewValue = Union[Mapping[str, Any], Sequence]
class _QtListModel(QAbstractListModel): class _QtListModel(QAbstractListModel):
updated = pyqtSignal()
def __init__(self) -> None: def __init__(self) -> None:
super().__init__() super().__init__()
self._ref_namedlist = None self._ref_namedlist = None
self._roles: Tuple[str, ...] = () self._roles: Tuple[str, ...] = ()
self._list: list = [] 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]: def roleNames(self) -> Dict[int, bytes]:
return {Qt.UserRole + i: bytes(f, "utf-8") return {Qt.UserRole + i: bytes(f, "utf-8")
for i, f in enumerate(self._roles, 1)} for i, f in enumerate(self._roles, 1)}
@ -61,7 +70,7 @@ class _QtListModel(QAbstractListModel):
@pyqtSlot(int, result="QVariantMap") @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() return self._list[index]._asdict()
@ -73,7 +82,7 @@ class _QtListModel(QAbstractListModel):
self.endInsertRows() self.endInsertRows()
@pyqtProperty(int) @pyqtProperty(int, constant=True)
def count(self) -> int: def count(self) -> int:
return self.rowCount() return self.rowCount()
@ -142,6 +151,13 @@ class _QtListModel(QAbstractListModel):
self.endRemoveRows() 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): class ListModel(MutableSequence):
def __init__(self, initial_data: Optional[List[NewValue]] = None) -> None: def __init__(self, initial_data: Optional[List[NewValue]] = None) -> None:
super().__init__() super().__init__()

View File

@ -32,8 +32,8 @@ Rectangle {
} }
Base.HLabel { Base.HLabel {
id: "roomSubtitle" id: "roomDescription"
text: chatPage.room.subtitle text: chatPage.room.description
font.pixelSize: smallSize font.pixelSize: smallSize
elide: Text.ElideRight elide: Text.ElideRight
maximumLineCount: 1 maximumLineCount: 1

View File

@ -75,12 +75,13 @@ ColumnLayout {
0 0
Layout.maximumHeight: Layout.minimumHeight Layout.maximumHeight: Layout.minimumHeight
Layout.minimumWidth: parent.width - Layout.leftMargin * 2 Layout.minimumWidth:
parent.width - Layout.leftMargin - Layout.rightMargin
Layout.maximumWidth: Layout.minimumWidth Layout.maximumWidth: Layout.minimumWidth
Layout.margins: accountList.spacing Layout.margins: accountList.spacing
Layout.leftMargin: Layout.leftMargin:
sidePane.width < 36 + Layout.margins ? 0 : Layout.margins sidePane.width < 36 + Layout.margins ? 0 : Layout.margins
Layout.rightMargin: Layout.leftMargin Layout.rightMargin: 0
} }
} }

View File

@ -37,9 +37,25 @@ MouseArea {
rightPadding: leftPadding rightPadding: leftPadding
} }
Base.HLabel { 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 id: subtitleLabel
visible: text !== "" visible: text !== ""
text: subtitle text: Backend.messagesModel[room_id].reloadThis, get_text()
textFormat: Text.StyledText
font.pixelSize: smallSize font.pixelSize: smallSize
elide: Text.ElideRight elide: Text.ElideRight
maximumLineCount: 1 maximumLineCount: 1