moment/src/backend/models/model_item.py

130 lines
4.2 KiB
Python
Raw Normal View History

# Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
2019-12-19 22:46:16 +11:00
# SPDX-License-Identifier: LGPL-3.0-or-later
2020-05-03 03:03:44 +10:00
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Dict, Optional
2020-04-08 01:58:26 +10:00
from ..pyotherside_events import ModelItemSet
from ..utils import serialize_value_for_qml
if TYPE_CHECKING:
from .model import Model
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
2020-10-17 19:22:48 +11:00
@dataclass(eq=False)
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
class ModelItem:
2019-12-19 06:00:34 +11:00
"""Base class for items stored inside a `Model`.
This class must be subclassed and not used directly.
2020-10-17 19:22:48 +11:00
All subclasses must use the `@dataclass(eq=False)` decorator.
2019-12-19 06:00:34 +11:00
Subclasses are also expected to implement `__lt__()`,
to provide support for comparisons with the `<`, `>`, `<=`, `=>` operators
2020-03-13 05:41:00 +11:00
and thus allow a `Model` to keep its data sorted.
2020-10-17 19:22:48 +11:00
Make sure to respect SortedList requirements when implementing `__lt__()`:
http://www.grantjenks.com/docs/sortedcontainers/introduction.html#caveats
2019-12-19 06:00:34 +11:00
"""
2020-05-03 03:03:44 +10:00
id: Any = field()
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
def __new__(cls, *_args, **_kwargs) -> "ModelItem":
cls.parent_model: Optional[Model] = None
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
return super().__new__(cls)
def __setattr__(self, name: str, value) -> None:
2020-10-17 19:22:48 +11:00
self.set_fields(**{name: value})
2020-07-02 02:00:50 +10:00
2020-10-17 19:22:48 +11:00
def __delattr__(self, name: str) -> None:
raise NotImplementedError()
2020-04-08 01:58:26 +10:00
2020-10-17 19:22:48 +11:00
@property
def serialized(self) -> Dict[str, Any]:
"""Return this item as a dict ready to be passed to QML."""
2020-10-17 19:22:48 +11:00
return {
name: self.serialized_field(name)
for name in self.__dataclass_fields__ # type: ignore
if not name.startswith("_")
2020-10-17 19:22:48 +11:00
}
2020-04-08 01:58:26 +10:00
2020-10-17 19:22:48 +11:00
def serialized_field(self, field: str) -> Any:
"""Return a field's value in a form suitable for passing to QML."""
Big performance refactoring & various improvements Instead of passing all sorts of events for the JS to handle and manually add to different data models, we now handle everything we can in Python. For any change, the python models send a sync event with their contents (no more than 4 times per second) to JS, and the QSyncable library's JsonListModel takes care of converting it to a QML ListModel and sending the appropriate signals. The SortFilterProxyModel library is not used anymore, the only case where we need to filter/sort something now is when the user interacts with the "Filter rooms" or "Filter members" fields. These cases are handled by a simple JS function. We now keep separated room and timeline models for different accounts, the previous approach of sharing all the data we could between accounts created a lot of complications (local echoes, decrypted messages replacing others, etc). The users's own account profile changes are now hidden in the timeline. On startup, if all events for a room were only own profile changes, more events will be loaded. Any kind of image format supported by Qt is now handled by the pyotherside image provider, instead of just PNG/JPG. SVGs which previously caused errors are supported as well. The typing members bar paddings/margins are fixed. The behavior of the avatar/"upload a profile picture" overlay is fixed. Config files read from disk are now cached (TODO: make them reloadable again). Pylint is not used anymore because of all its annoying false warnings and lack of understanding for dataclasses, it is replaced by flake8 with a custom config and various plugins. Debug mode is now considered on if the program was compiled with the right option, instead of taking an argument from CLI. When on, C++ will set a flag in the Window QML component. The loading screen is now unloaded after the UI is ready, where previously it just stayed in the background invisible and wasted CPU. The overall refactoring and improvements make us now able to handle rooms with thousand of members and no lazy-loading, where previously everything would freeze and simply scrolling up to load past events in any room would block the UI for a few seconds.
2019-08-11 22:01:22 +10:00
2020-10-17 19:22:48 +11:00
value = getattr(self, field)
return serialize_value_for_qml(value, json_list_dicts=True)
2020-10-17 19:22:48 +11:00
def set_fields(self, _force: bool = False, **fields: Any) -> None:
"""Set one or more field's value and call `ModelItem.notify_change`.
2020-10-17 19:22:48 +11:00
For efficiency, to change multiple fields, this method should be
used rather than setting them one after another with `=` or `setattr`.
"""
2020-02-12 23:10:59 +11:00
2020-10-17 19:22:48 +11:00
parent = self.parent_model
2020-02-12 23:10:59 +11:00
2020-10-17 19:22:48 +11:00
# If we're currently being created or haven't been put in a model yet:
if not parent:
for name, value in fields.items():
super().__setattr__(name, value)
return
2019-12-19 06:00:34 +11:00
2020-10-17 19:22:48 +11:00
with parent.write_lock:
qml_changes = {}
changes = {
name: value for name, value in fields.items()
if _force or getattr(self, name) != value
}
2020-07-02 02:00:50 +10:00
2020-10-17 19:22:48 +11:00
if not changes:
return
2020-07-02 02:00:50 +10:00
2020-10-17 19:22:48 +11:00
# To avoid corrupting the SortedList, we have to take out the item,
# apply the field changes, *then* add it back in.
2020-07-02 02:00:50 +10:00
2020-10-17 19:22:48 +11:00
index_then = parent._sorted_data.index(self)
del parent._sorted_data[index_then]
2020-07-02 02:00:50 +10:00
2020-10-17 19:22:48 +11:00
for name, value in changes.items():
2020-07-02 02:00:50 +10:00
super().__setattr__(name, value)
is_field = name in self.__dataclass_fields__ # type: ignore
2020-07-02 02:00:50 +10:00
if is_field and not name.startswith("_"):
2020-10-17 19:22:48 +11:00
qml_changes[name] = self.serialized_field(name)
2020-10-17 19:22:48 +11:00
parent._sorted_data.add(self)
index_now = parent._sorted_data.index(self)
index_change = index_then != index_now
2020-10-17 19:22:48 +11:00
# Now, inform QML about changed dataclass fields if any.
if not parent.sync_id or (not qml_changes and not index_change):
2020-10-17 19:22:48 +11:00
return
ModelItemSet(parent.sync_id, index_then, index_now, qml_changes)
# Inform any proxy connected to the parent model of the field changes
2020-10-17 19:22:48 +11:00
for sync_id, proxy in parent.proxies.items():
if sync_id != parent.sync_id:
proxy.source_item_set(parent, self.id, self, qml_changes)
def notify_change(self, *fields: str) -> None:
"""Notify the parent model that fields of this item have changed.
2020-10-17 19:22:48 +11:00
The model cannot automatically detect changes inside
object fields, such as list or dicts having their data modified.
In these cases, this method should be called.
"""
2020-10-17 19:22:48 +11:00
kwargs = {name: getattr(self, name) for name in fields}
kwargs["_force"] = True
self.set_fields(**kwargs)