-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
presentation.py
90 lines (67 loc) · 2.09 KB
/
presentation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
"""
Base code for presentation elements
"""
from __future__ import annotations
from typing import (
TYPE_CHECKING,
Any,
Callable,
Generic,
Iterable,
Optional,
TypeVar,
Union,
)
from gaphor.UML.element import Element
from gaphor.UML.properties import association, relation_one
if TYPE_CHECKING:
from gaphas.canvas import Canvas # noqa
from gaphas.matrix import Matrix # noqa
S = TypeVar("S", bound=Element)
class Presentation(Element, Generic[S]):
"""
This presentation is used to link the behaviors of `gaphor.UML.Element` and `gaphas.Item`.
"""
def __init__(self, id=None, model=None):
super().__init__(id, model)
def update(event):
self.request_update()
self._watcher = self.watcher(default_handler=update)
self.watch("subject")
subject: relation_one[S] = association(
"subject", Element, upper=1, opposite="presentation"
)
request_update: Callable[[Presentation], None]
canvas: Optional[Canvas]
matrix: Matrix
def watch(self, path, handler=None):
"""
Watch a certain path of elements starting with the DiagramItem.
The handler is optional and will default to a simple
self.request_update().
Watches should be set in the constructor, so they can be registered
and unregistered in one shot.
This interface is fluent(returns self).
"""
self._watcher.watch(path, handler)
return self
def subscribe_all(self):
"""
Subscribe all watched paths, as defined through `watch()`.
"""
self._watcher.subscribe_all()
def unsubscribe_all(self):
"""
Subscribe all watched paths, as defined through `watch()`.
"""
self._watcher.unsubscribe_all()
def unlink(self):
"""
Remove the item from the canvas and set subject to None.
"""
if self.canvas:
self.canvas.remove(self)
super().unlink()
Element.presentation = association(
"presentation", Presentation, composite=True, opposite="subject"
)