root/gaphor/tags/gaphor-0.12.0/gaphor/diagram/nameditem.py

Revision 1998, 3.4 kB (checked in by wrobe..@pld-linux.org, 1 year ago)

- allow align text elements on a string
- align messages on a ':' character on communication diagram

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 """
2 Base classes related to items, which represent UML classes deriving
3 from NamedElement.
4 """
5
6 from gaphor.diagram.elementitem import ElementItem
7 from gaphor.diagram.style import get_min_size, ALIGN_CENTER, ALIGN_TOP
8 import gaphor.diagram.font as font
9
10 class NamedItem(ElementItem):
11     __namedelement__ = True
12
13     __style__ = {
14         'min-size'    : (100, 50),
15         'name-align'  : (ALIGN_CENTER, ALIGN_TOP),
16         'name-padding': (5, 10, 5, 10),
17         'name-outside': False,
18         'name-align-str': None,
19     }
20
21     def __init__(self, id=None):
22         """
23         Create named item.
24         """
25         ElementItem.__init__(self, id)
26
27         # create (from ...) text to distinguish diagram items from
28         # different namespace
29         self._from = self.add_text('from',
30                 pattern='(from %s)',
31                 style={'text-align-group': 'stereotype'},
32                 visible=self.is_namespace_info_visible,
33                 font=font.FONT_SMALL)
34
35         # size of stereotype, namespace and name text
36         self._header_size = 0, 0
37
38
39     def is_namespace_info_visible(self):
40         """
41         Display name space info when it is different, then diagram's or
42         parent's namespace.
43         """
44         subject = self.subject
45         canvas = self.canvas
46
47         if not subject or not canvas:
48             return False
49
50         namespace = subject.namespace
51         parent = canvas.get_parent(self)
52
53         # if there is a parent (i.e. interaction)
54         if parent and parent.subject \
55                 and parent.subject.namespace is not namespace:
56             return False
57
58         return self._from.text and namespace is not canvas.diagram.namespace
59
60
61     def on_subject_notify(self, pspec, notifiers=()):
62         #log.debug('Class.on_subject_notify(%s, %s)' % (pspec, notifiers))
63         ElementItem.on_subject_notify(self, pspec,
64                 ('namespace', 'namespace.name') + notifiers)
65         if self.subject:
66             self.on_subject_notify__namespace(self.subject)
67                                    
68
69     def on_subject_notify__namespace(self, subject, pspec=None):
70         """
71         Add a line '(from ...)' to the class item if subject's namespace
72         is not the same as the namespace of this diagram.
73         """
74         subject = self.subject
75         if subject and subject.namespace:
76             self._from.text = subject.namespace.name
77         else:
78             self._from.text = ''
79         self.request_update()
80
81
82     def on_subject_notify__namespace_name(self, subject, pspec=None):
83         """
84         Change the '(from ...)' line if the namespace's name changes.
85         """
86         self.on_subject_notify__namespace(subject, pspec)
87
88
89     def pre_update(self, context):
90         """
91         Calculate minimal size and header size.
92         """
93         super(NamedItem, self).pre_update(context)
94
95         style = self._name.style
96
97         # we can determine minimal size and header size only
98         # when name is aligned inside an item
99         if not style.text_outside:
100             # at this stage stereotype text group should be already updated
101             assert 'stereotype' in self._text_groups_sizes
102
103             nw, nh = self._text_groups_sizes['stereotype']
104             self._header_size = get_min_size(nw, nh, self.style.name_padding)
105
106             self.min_width = max(self.style.min_size[0], self._header_size[0])
107             self.min_height = max(self.style.min_size[1], self._header_size[1])
108
109
110 # vim:sw=4:et:ai
Note: See TracBrowser for help on using the browser.