From 52d538e99582e8765da9f186abff9be7f88880ac Mon Sep 17 00:00:00 2001 From: miruka Date: Thu, 2 May 2019 14:31:47 -0400 Subject: [PATCH] Merge Backend and QMLModels MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Backend.models.accounts → Backend.accounts Backend.models.roomEvents → Backend.roomEvents --- harmonyqml/backend/backend.py | 24 ++++++++++------- harmonyqml/backend/model/__init__.py | 1 - harmonyqml/backend/model/qml_models.py | 26 ------------------- harmonyqml/backend/signal_manager.py | 23 ++++++++-------- harmonyqml/components/Chat/Chat.qml | 5 ++-- .../Chat/RoomEventList/RoomEventList.qml | 2 +- .../components/SidePane/AccountList.qml | 2 +- .../SidePane/RoomCategoriesList.qml | 2 +- .../components/SidePane/RoomDelegate.qml | 2 +- harmonyqml/components/SidePane/RoomList.qml | 2 +- harmonyqml/components/SidePane/utils.js | 2 +- 11 files changed, 35 insertions(+), 56 deletions(-) delete mode 100644 harmonyqml/backend/model/qml_models.py diff --git a/harmonyqml/backend/backend.py b/harmonyqml/backend/backend.py index 00c03853..5a0c106b 100644 --- a/harmonyqml/backend/backend.py +++ b/harmonyqml/backend/backend.py @@ -2,13 +2,12 @@ # This file is part of harmonyqml, licensed under GPLv3. from concurrent.futures import ThreadPoolExecutor -from typing import Dict, Sequence, Set +from typing import Deque, Dict, Sequence, Set from PyQt5.QtCore import QObject, pyqtProperty, pyqtSlot from .html_filter import HtmlFilter -from .model.items import RoomEvent -from .model.qml_models import QMLModels +from .model import ListModel, ListModelMap from .pyqt_future import futurize @@ -22,10 +21,13 @@ class Backend(QObject): self.past_tokens: Dict[str, str] = {} self.fully_loaded_rooms: Set[str] = set() + self._html_filter: HtmlFilter = HtmlFilter(self) + from .client_manager import ClientManager self._client_manager: ClientManager = ClientManager(self) - self._models: QMLModels = QMLModels(self) - self._html_filter: HtmlFilter = HtmlFilter(self) + + self._accounts: ListModel = ListModel(parent=parent) + self._room_events: ListModelMap = ListModelMap(Deque, parent) from .signal_manager import SignalManager self._signal_manager: SignalManager = SignalManager(self) @@ -33,17 +35,21 @@ class Backend(QObject): self.clientManager.configLoad() + @pyqtProperty("QVariant", constant=True) + def htmlFilter(self): + return self._html_filter + @pyqtProperty("QVariant", constant=True) def clientManager(self): return self._client_manager @pyqtProperty("QVariant", constant=True) - def models(self): - return self._models + def accounts(self): + return self._accounts @pyqtProperty("QVariant", constant=True) - def htmlFilter(self): - return self._html_filter + def roomEvents(self): + return self._room_events @pyqtSlot(str, result="QVariant") diff --git a/harmonyqml/backend/model/__init__.py b/harmonyqml/backend/model/__init__.py index 3fd8e1a9..e41d21f0 100644 --- a/harmonyqml/backend/model/__init__.py +++ b/harmonyqml/backend/model/__init__.py @@ -4,4 +4,3 @@ from . import items from .list_model import ListModel from .list_model_map import ListModelMap -from .qml_models import QMLModels diff --git a/harmonyqml/backend/model/qml_models.py b/harmonyqml/backend/model/qml_models.py deleted file mode 100644 index c89b8d03..00000000 --- a/harmonyqml/backend/model/qml_models.py +++ /dev/null @@ -1,26 +0,0 @@ -# Copyright 2019 miruka -# This file is part of harmonyqml, licensed under GPLv3. - -from typing import Deque - -from PyQt5.QtCore import QObject, pyqtProperty - -from .list_model import ListModel -from .list_model_map import ListModelMap - - -class QMLModels(QObject): - def __init__(self, parent: QObject) -> None: - super().__init__(parent) - self._accounts: ListModel = ListModel(parent=parent) - self._room_events: ListModelMap = ListModelMap(Deque, parent) - - - @pyqtProperty(ListModel, constant=True) - def accounts(self): - return self._accounts - - - @pyqtProperty("QVariant", constant=True) - def roomEvents(self): - return self._room_events diff --git a/harmonyqml/backend/signal_manager.py b/harmonyqml/backend/signal_manager.py index 826bc3d4..115f0e12 100644 --- a/harmonyqml/backend/signal_manager.py +++ b/harmonyqml/backend/signal_manager.py @@ -11,8 +11,7 @@ from nio.rooms import MatrixRoom from .backend import Backend from .client import Client -from .model.items import Account, Room, RoomCategory, RoomEvent -from .model.list_model import ListModel +from .model.items import Account, ListModel, Room, RoomCategory, RoomEvent Inviter = Optional[Dict[str, str]] LeftEvent = Optional[Dict[str, str]] @@ -36,7 +35,7 @@ class SignalManager(QObject): def onClientAdded(self, client: Client) -> None: self.connectClient(client) - self.backend.models.accounts.append(Account( + self.backend.accounts.append(Account( userId = client.userId, roomCategories = ListModel([ RoomCategory("Invites", ListModel()), @@ -48,7 +47,7 @@ class SignalManager(QObject): def onClientDeleted(self, user_id: str) -> None: - del self.backend.models.accounts[user_id] + del self.backend.accounts[user_id] def connectClient(self, client: Client) -> None: @@ -79,7 +78,7 @@ class SignalManager(QObject): inviter: Inviter = None) -> None: nio_room = client.nio.invited_rooms[room_id] - categories = self.backend.models.accounts[client.userId].roomCategories + categories = self.backend.accounts[client.userId].roomCategories categories["Rooms"].rooms.pop(room_id, None) categories["Left"].rooms.pop(room_id, None) @@ -94,7 +93,7 @@ class SignalManager(QObject): def onRoomJoined(self, client: Client, room_id: str) -> None: nio_room = client.nio.rooms[room_id] - categories = self.backend.models.accounts[client.userId].roomCategories + categories = self.backend.accounts[client.userId].roomCategories categories["Invites"].rooms.pop(room_id, None) categories["Left"].rooms.pop(room_id, None) @@ -110,7 +109,7 @@ class SignalManager(QObject): client: Client, room_id: str, left_event: LeftEvent = None) -> None: - categories = self.backend.models.accounts[client.userId].roomCategories + categories = self.backend.accounts[client.userId].roomCategories previous = categories["Rooms"].rooms.pop(room_id, None) previous = previous or categories["Invites"].rooms.pop(room_id, None) @@ -155,7 +154,7 @@ class SignalManager(QObject): self.last_room_events.appendleft(edict["event_id"]) - model = self.backend.models.roomEvents[room_id] + model = self.backend.roomEvents[room_id] date_time = QDateTime\ .fromMSecsSinceEpoch(edict["server_timestamp"]) new_event = RoomEvent(type=etype, dateTime=date_time, dict=edict) @@ -218,7 +217,7 @@ class SignalManager(QObject): client: Client, room_id: str, users: List[str]) -> None: - categories = self.backend.models.accounts[client.userId].roomCategories + categories = self.backend.accounts[client.userId].roomCategories for categ in categories: try: categ.rooms[room_id].typingUsers = users @@ -233,7 +232,7 @@ class SignalManager(QObject): content: Dict[str, str]) -> None: with self._lock: - model = self.backend.models.roomEvents[room_id] + model = self.backend.roomEvents[room_id] nio_event = nio.events.RoomMessage.parse_event({ "event_id": "", "sender": client.userId, @@ -252,9 +251,9 @@ class SignalManager(QObject): def onRoomAboutToBeForgotten(self, client: Client, room_id: str) -> None: - categories = self.backend.models.accounts[client.userId].roomCategories + categories = self.backend.accounts[client.userId].roomCategories for categ in categories: categ.rooms.pop(room_id, None) - self.backend.models.roomEvents[room_id].clear() + self.backend.roomEvents[room_id].clear() diff --git a/harmonyqml/components/Chat/Chat.qml b/harmonyqml/components/Chat/Chat.qml index 9fc3b2fe..1033ff42 100644 --- a/harmonyqml/components/Chat/Chat.qml +++ b/harmonyqml/components/Chat/Chat.qml @@ -10,8 +10,9 @@ HColumnLayout { property string roomId: "" readonly property var roomInfo: - Backend.models.accounts.get(userId) - .roomCategories.get(category).rooms.get(roomId) + Backend.accounts.get(userId) + .roomCategories.get(category) + .rooms.get(roomId) property bool canLoadPastEvents: true diff --git a/harmonyqml/components/Chat/RoomEventList/RoomEventList.qml b/harmonyqml/components/Chat/RoomEventList/RoomEventList.qml index 12778c2d..7bdd3ce4 100644 --- a/harmonyqml/components/Chat/RoomEventList/RoomEventList.qml +++ b/harmonyqml/components/Chat/RoomEventList/RoomEventList.qml @@ -10,7 +10,7 @@ HGlassRectangle { ListView { id: roomEventListView delegate: RoomEventDelegate {} - model: Backend.models.roomEvents.get(chatPage.roomId) + model: Backend.roomEvents.get(chatPage.roomId) anchors.fill: parent anchors.leftMargin: space diff --git a/harmonyqml/components/SidePane/AccountList.qml b/harmonyqml/components/SidePane/AccountList.qml index ff45dafa..1a74def1 100644 --- a/harmonyqml/components/SidePane/AccountList.qml +++ b/harmonyqml/components/SidePane/AccountList.qml @@ -9,6 +9,6 @@ ListView { topMargin: spacing bottomMargin: topMargin - model: Backend.models.accounts + model: Backend.accounts delegate: AccountDelegate {} } diff --git a/harmonyqml/components/SidePane/RoomCategoriesList.qml b/harmonyqml/components/SidePane/RoomCategoriesList.qml index d4c85413..a584045a 100644 --- a/harmonyqml/components/SidePane/RoomCategoriesList.qml +++ b/harmonyqml/components/SidePane/RoomCategoriesList.qml @@ -6,6 +6,6 @@ ListView { property string userId: "" id: roomCategoriesList - model: Backend.models.accounts.get(userId).roomCategories + model: Backend.accounts.get(userId).roomCategories delegate: RoomCategoryDelegate {} } diff --git a/harmonyqml/components/SidePane/RoomDelegate.qml b/harmonyqml/components/SidePane/RoomDelegate.qml index 3e4351d7..41f665a5 100644 --- a/harmonyqml/components/SidePane/RoomDelegate.qml +++ b/harmonyqml/components/SidePane/RoomDelegate.qml @@ -43,7 +43,7 @@ MouseArea { } Connections { - target: Backend.models.roomEvents.get(roomId) + target: Backend.roomEvents.get(roomId) onChanged: subtitleLabel.text = subtitleLabel.getText() } diff --git a/harmonyqml/components/SidePane/RoomList.qml b/harmonyqml/components/SidePane/RoomList.qml index 9f3f9bf8..2e939bd8 100644 --- a/harmonyqml/components/SidePane/RoomList.qml +++ b/harmonyqml/components/SidePane/RoomList.qml @@ -8,6 +8,6 @@ ListView { id: roomList spacing: accountList.spacing - model: Backend.models.accounts.get(userId).roomCategories.get(category).rooms + model: Backend.accounts.get(userId).roomCategories.get(category).rooms delegate: RoomDelegate {} } diff --git a/harmonyqml/components/SidePane/utils.js b/harmonyqml/components/SidePane/utils.js index 21b572b2..6b78f6b8 100644 --- a/harmonyqml/components/SidePane/utils.js +++ b/harmonyqml/components/SidePane/utils.js @@ -2,7 +2,7 @@ function getLastRoomEventText(roomId, accountId) { - var eventsModel = Backend.models.roomEvents.get(roomId) + var eventsModel = Backend.roomEvents.get(roomId) if (eventsModel.count < 1) { return "" } var ev = eventsModel.get(0)