| 1 |
""" |
|---|
| 2 |
Object node item. |
|---|
| 3 |
""" |
|---|
| 4 |
|
|---|
| 5 |
import itertools |
|---|
| 6 |
|
|---|
| 7 |
from gaphas.state import observed, reversible_property |
|---|
| 8 |
from gaphor import UML |
|---|
| 9 |
from gaphor.core import inject |
|---|
| 10 |
|
|---|
| 11 |
from gaphor.diagram.nameditem import NamedItem |
|---|
| 12 |
from gaphor.diagram.style import ALIGN_CENTER, ALIGN_BOTTOM |
|---|
| 13 |
from gaphas.util import text_extents, text_multiline |
|---|
| 14 |
|
|---|
| 15 |
DEFAULT_UPPER_BOUND = '*' |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class ObjectNodeItem(NamedItem): |
|---|
| 19 |
""" |
|---|
| 20 |
Representation of object node. Object node is ordered and has upper bound |
|---|
| 21 |
specification. |
|---|
| 22 |
|
|---|
| 23 |
Ordering information can be hidden by user. |
|---|
| 24 |
""" |
|---|
| 25 |
|
|---|
| 26 |
element_factory = inject('element_factory') |
|---|
| 27 |
|
|---|
| 28 |
__uml__ = UML.ObjectNode |
|---|
| 29 |
|
|---|
| 30 |
STYLE_BOTTOM = { |
|---|
| 31 |
'text-align': (ALIGN_CENTER, ALIGN_BOTTOM), |
|---|
| 32 |
'text-outside': True, |
|---|
| 33 |
'text-align-group': 'bottom', |
|---|
| 34 |
} |
|---|
| 35 |
|
|---|
| 36 |
def __init__(self, id = None): |
|---|
| 37 |
NamedItem.__init__(self, id) |
|---|
| 38 |
|
|---|
| 39 |
self._show_ordering = False |
|---|
| 40 |
|
|---|
| 41 |
self._upper_bound = self.add_text('upperBound.value', |
|---|
| 42 |
pattern='{ upperBound = %s }', |
|---|
| 43 |
style=self.STYLE_BOTTOM, |
|---|
| 44 |
visible=self.is_upper_bound_visible) |
|---|
| 45 |
|
|---|
| 46 |
self._ordering = self.add_text('ordering', |
|---|
| 47 |
pattern = '{ ordering = %s }', |
|---|
| 48 |
style = self.STYLE_BOTTOM, |
|---|
| 49 |
visible=self._get_show_ordering) |
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 |
def is_upper_bound_visible(self): |
|---|
| 53 |
""" |
|---|
| 54 |
Do not show upper bound, when it's set to default value. |
|---|
| 55 |
""" |
|---|
| 56 |
subject = self.subject |
|---|
| 57 |
return subject and subject.upperBound \ |
|---|
| 58 |
and subject.upperBound.value != DEFAULT_UPPER_BOUND |
|---|
| 59 |
|
|---|
| 60 |
|
|---|
| 61 |
@observed |
|---|
| 62 |
def _set_ordering(self, ordering): |
|---|
| 63 |
""" |
|---|
| 64 |
Set ordering of object node. |
|---|
| 65 |
""" |
|---|
| 66 |
self.subject.ordering = ordering |
|---|
| 67 |
self.request_update() |
|---|
| 68 |
|
|---|
| 69 |
ordering = reversible_property(lambda s: s.subject.ordering, _set_ordering) |
|---|
| 70 |
|
|---|
| 71 |
@observed |
|---|
| 72 |
def _set_show_ordering(self, value): |
|---|
| 73 |
|
|---|
| 74 |
self._show_ordering = value |
|---|
| 75 |
self.request_update() |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
def _get_show_ordering(self): |
|---|
| 79 |
return self._show_ordering |
|---|
| 80 |
|
|---|
| 81 |
show_ordering = reversible_property(_get_show_ordering, _set_show_ordering) |
|---|
| 82 |
|
|---|
| 83 |
def save(self, save_func): |
|---|
| 84 |
save_func('show-ordering', self._show_ordering) |
|---|
| 85 |
super(ObjectNodeItem, self).save(save_func) |
|---|
| 86 |
|
|---|
| 87 |
def load(self, name, value): |
|---|
| 88 |
if name == 'show-ordering': |
|---|
| 89 |
self._show_ordering = eval(value) |
|---|
| 90 |
else: |
|---|
| 91 |
super(ObjectNodeItem, self).load(name, value) |
|---|
| 92 |
|
|---|
| 93 |
def postload(self): |
|---|
| 94 |
if self.subject and self.subject.upperBound: |
|---|
| 95 |
self._upper_bound.text = self.subject.upperBound.value |
|---|
| 96 |
if self.subject and self._show_ordering: |
|---|
| 97 |
self.set_ordering(self.subject.ordering) |
|---|
| 98 |
super(ObjectNodeItem, self).postload() |
|---|
| 99 |
|
|---|
| 100 |
def on_subject_notify(self, pspec, notifiers = ()): |
|---|
| 101 |
""" |
|---|
| 102 |
Detect subject changes. If subject is set then set upper bound text |
|---|
| 103 |
element subject. |
|---|
| 104 |
""" |
|---|
| 105 |
NamedItem.on_subject_notify(self, pspec, |
|---|
| 106 |
('upperBound', 'upperBound.value', 'ordering') + notifiers) |
|---|
| 107 |
|
|---|
| 108 |
subject = self.subject |
|---|
| 109 |
if subject and not (subject.upperBound and subject.upperBound.value): |
|---|
| 110 |
self.set_upper_bound(DEFAULT_UPPER_BOUND) |
|---|
| 111 |
|
|---|
| 112 |
self.request_update() |
|---|
| 113 |
|
|---|
| 114 |
|
|---|
| 115 |
def draw(self, context): |
|---|
| 116 |
cr = context.cairo |
|---|
| 117 |
cr.rectangle(0, 0, self.width, self.height) |
|---|
| 118 |
cr.stroke() |
|---|
| 119 |
|
|---|
| 120 |
super(ObjectNodeItem, self).draw(context) |
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 |
def set_upper_bound(self, value): |
|---|
| 124 |
""" |
|---|
| 125 |
Set upper bound value of object node. |
|---|
| 126 |
""" |
|---|
| 127 |
subject = self.subject |
|---|
| 128 |
if subject: |
|---|
| 129 |
if not subject.upperBound: |
|---|
| 130 |
subject.upperBound = self.element_factory.create(UML.LiteralSpecification) |
|---|
| 131 |
|
|---|
| 132 |
if not value: |
|---|
| 133 |
value = DEFAULT_UPPER_BOUND |
|---|
| 134 |
|
|---|
| 135 |
subject.upperBound.value = value |
|---|
| 136 |
self._upper_bound.text = value |
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
def set_ordering(self, value): |
|---|
| 140 |
""" |
|---|
| 141 |
Set object node ordering value. |
|---|
| 142 |
""" |
|---|
| 143 |
subject = self.subject |
|---|
| 144 |
subject.ordering = value |
|---|
| 145 |
self._ordering.text = value |
|---|
| 146 |
|
|---|
| 147 |
|
|---|
| 148 |
def on_subject_notify__upperBound(self, subject, pspec=None): |
|---|
| 149 |
self.request_update() |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
def on_subject_notify__upperBound_value(self, subject, pspec=None): |
|---|
| 153 |
self.request_update() |
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
def on_subject_notify__ordering(self, subject, pspec=None): |
|---|
| 157 |
self.request_update() |
|---|
| 158 |
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 |
|
|---|