ModelItem can only have one parent model again
This commit is contained in:
parent
2a772e5f54
commit
ac0907fad7
|
@ -79,7 +79,7 @@ class Model(MutableMapping):
|
||||||
# Set parent model on new item
|
# Set parent model on new item
|
||||||
|
|
||||||
if self.sync_id:
|
if self.sync_id:
|
||||||
new.parent_models[self.sync_id] = self
|
new.parent_model = self
|
||||||
|
|
||||||
# Insert into sorted data
|
# Insert into sorted data
|
||||||
|
|
||||||
|
@ -109,7 +109,7 @@ class Model(MutableMapping):
|
||||||
item = self._data[key]
|
item = self._data[key]
|
||||||
|
|
||||||
if self.sync_id:
|
if self.sync_id:
|
||||||
del item.parent_models[self.sync_id]
|
item.parent_model = None
|
||||||
|
|
||||||
del self._data[key]
|
del self._data[key]
|
||||||
|
|
||||||
|
|
|
@ -1,11 +1,10 @@
|
||||||
# SPDX-License-Identifier: LGPL-3.0-or-later
|
# SPDX-License-Identifier: LGPL-3.0-or-later
|
||||||
|
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass, field
|
||||||
from typing import TYPE_CHECKING, Any, Dict
|
from typing import TYPE_CHECKING, Any, Dict, Optional
|
||||||
|
|
||||||
from ..pyotherside_events import ModelItemSet
|
from ..pyotherside_events import ModelItemSet
|
||||||
from ..utils import serialize_value_for_qml
|
from ..utils import serialize_value_for_qml
|
||||||
from . import SyncId
|
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
from .model import Model
|
from .model import Model
|
||||||
|
@ -27,14 +26,14 @@ class ModelItem:
|
||||||
|
|
||||||
|
|
||||||
def __new__(cls, *_args, **_kwargs) -> "ModelItem":
|
def __new__(cls, *_args, **_kwargs) -> "ModelItem":
|
||||||
cls.parent_models: Dict[SyncId, Model] = {}
|
cls.parent_model: Optional[Model] = None
|
||||||
return super().__new__(cls)
|
return super().__new__(cls)
|
||||||
|
|
||||||
|
|
||||||
def __setattr__(self, name: str, value) -> None:
|
def __setattr__(self, name: str, value) -> None:
|
||||||
"""If this item is in a `Model`, alert it of attribute changes."""
|
"""If this item is in a `Model`, alert it of attribute changes."""
|
||||||
|
|
||||||
if name == "parent_model" or not self.parent_models:
|
if name == "parent_model" or not self.parent_model:
|
||||||
super().__setattr__(name, value)
|
super().__setattr__(name, value)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -43,7 +42,11 @@ class ModelItem:
|
||||||
|
|
||||||
super().__setattr__(name, value)
|
super().__setattr__(name, value)
|
||||||
|
|
||||||
for sync_id, model in self.parent_models.items():
|
model = self.parent_model
|
||||||
|
|
||||||
|
if not model.sync_id:
|
||||||
|
return
|
||||||
|
|
||||||
with model._write_lock:
|
with model._write_lock:
|
||||||
index_then = model._sorted_data.index(self)
|
index_then = model._sorted_data.index(self)
|
||||||
model._sorted_data.sort()
|
model._sorted_data.sort()
|
||||||
|
@ -51,7 +54,7 @@ class ModelItem:
|
||||||
|
|
||||||
fields = {name: self.serialize_field(name)}
|
fields = {name: self.serialize_field(name)}
|
||||||
|
|
||||||
ModelItemSet(sync_id, index_then, index_now, fields)
|
ModelItemSet(model.sync_id, index_then, index_now, fields)
|
||||||
|
|
||||||
|
|
||||||
def __delattr__(self, name: str) -> None:
|
def __delattr__(self, name: str) -> None:
|
||||||
|
@ -72,6 +75,6 @@ class ModelItem:
|
||||||
return {
|
return {
|
||||||
name: self.serialize_field(name) for name in dir(self)
|
name: self.serialize_field(name) for name in dir(self)
|
||||||
if not (
|
if not (
|
||||||
name.startswith("_") or name in ("parent_models", "serialized")
|
name.startswith("_") or name in ("parent_model", "serialized")
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user