Fix room subtitle reloading

Replace the "reloadThis" ListModel hack by an actual signal,
works when the subtitle is displayed in more than one place
(e.g. two accounts in the same room connected).
This commit is contained in:
miruka 2019-04-14 14:32:51 -04:00
parent 5a180120b1
commit 3ec51b9253
2 changed files with 20 additions and 16 deletions

View File

@ -5,13 +5,16 @@ from typing import (
from namedlist import namedlist from namedlist import namedlist
from PyQt5.QtCore import ( from PyQt5.QtCore import (
QAbstractListModel, QModelIndex, QObject, Qt, pyqtProperty, pyqtSlot QAbstractListModel, QModelIndex, QObject, Qt, pyqtProperty, pyqtSignal,
pyqtSlot
) )
NewValue = Union[Mapping[str, Any], Sequence] NewValue = Union[Mapping[str, Any], Sequence]
class ListModel(QAbstractListModel): class ListModel(QAbstractListModel):
changed = pyqtSignal()
def __init__(self, def __init__(self,
initial_data: Optional[List[NewValue]] = None, initial_data: Optional[List[NewValue]] = None,
parent: Optional[QObject] = None) -> None: parent: Optional[QObject] = None) -> None:
@ -20,8 +23,6 @@ class ListModel(QAbstractListModel):
self._roles: Tuple[str, ...] = () self._roles: Tuple[str, ...] = ()
self._list: list = [] self._list: list = []
self._update_count: int = 0
if initial_data: if initial_data:
self.extend(initial_data) self.extend(initial_data)
@ -115,7 +116,7 @@ class ListModel(QAbstractListModel):
self.beginInsertRows(QModelIndex(), index, index) self.beginInsertRows(QModelIndex(), index, index)
self._list.insert(index, value) self._list.insert(index, value)
self.endInsertRows() self.endInsertRows()
self._update_count += 1 self.changed.emit()
@pyqtSlot(list) @pyqtSlot(list)
@ -135,7 +136,7 @@ class ListModel(QAbstractListModel):
value = self._convert_new_value(value) value = self._convert_new_value(value)
self._list[index] = value self._list[index] = value
self.dataChanged.emit(qidx, qidx, self.roleNames()) self.dataChanged.emit(qidx, qidx, self.roleNames())
self._update_count += 1 self.changed.emit()
@pyqtSlot(int, str, "QVariant") @pyqtSlot(int, str, "QVariant")
@ -143,7 +144,7 @@ class ListModel(QAbstractListModel):
self._list[index][self._roles.index(prop)] = value self._list[index][self._roles.index(prop)] = value
qidx = QAbstractListModel.index(self, index, 0) qidx = QAbstractListModel.index(self, index, 0)
self.dataChanged.emit(qidx, qidx, self.roleNames()) self.dataChanged.emit(qidx, qidx, self.roleNames())
self._update_count += 1 self.changed.emit()
# pylint: disable=invalid-name # pylint: disable=invalid-name
@ -173,7 +174,7 @@ class ListModel(QAbstractListModel):
self._list[to:to] = cut self._list[to:to] = cut
self.endMoveRows() self.endMoveRows()
self._update_count += 1 self.changed.emit()
@pyqtSlot(int) @pyqtSlot(int)
@ -181,7 +182,7 @@ class ListModel(QAbstractListModel):
self.beginRemoveRows(QModelIndex(), index, index) self.beginRemoveRows(QModelIndex(), index, index)
del self._list[index] del self._list[index]
self.endRemoveRows() self.endRemoveRows()
self._update_count += 1 self.changed.emit()
@pyqtSlot() @pyqtSlot()
@ -190,9 +191,4 @@ class ListModel(QAbstractListModel):
self.beginRemoveRows(QModelIndex(), 0, self.rowCount()) self.beginRemoveRows(QModelIndex(), 0, self.rowCount())
self._list.clear() self._list.clear()
self.endRemoveRows() self.endRemoveRows()
self._update_count += 1 self.changed.emit()
@pyqtProperty(int, constant=True)
def reloadThis(self):
return self._update_count

View File

@ -39,10 +39,18 @@ MouseArea {
rightPadding: leftPadding rightPadding: leftPadding
} }
Base.HLabel { Base.HLabel {
function get_text() {
return SidePaneJS.get_last_room_event_text(room_id)
}
Connections {
target: Backend.models.roomEvents.get(room_id)
onChanged: subtitleLabel.text = subtitleLabel.get_text()
}
id: subtitleLabel id: subtitleLabel
visible: text !== "" visible: text !== ""
text: Backend.models.roomEvents.get(room_id).reloadThis, text: get_text()
SidePaneJS.get_last_room_event_text(room_id)
textFormat: Text.StyledText textFormat: Text.StyledText
font.pixelSize: smallSize font.pixelSize: smallSize