root/gaphor/tags/gaphor-0.12.5/gaphor/diagram/feature.py

Revision 2090, 5.8 kB (checked in by arj..@yirdis.nl, 1 year ago)

Move need_sync logic from features to classes. Feature was expecting obsolete "parent" attribute in the update context.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim:sw=4:et
2 '''
3 Feature diagram item. Feature is a super class of both Attribute, Operations and
4 Methods.
5 '''
6
7 from gaphas.item import Item
8 from diagramitem import DiagramItem
9 from gaphor.diagram import DiagramItemMeta
10 from gaphas.util import text_extents, text_set_font, text_align
11 import font
12
13 class FeatureItem(DiagramItem):
14     """
15     FeatureItems are model elements who recide inside a ClassifierItem, such
16     as methods and attributes. Those items can have comments attached, but only
17     on the left and right side.
18     Note that features can also be used inside objects.
19     """
20
21     def __init__(self, id=None):
22         DiagramItem.__init__(self, id)
23         self.width = 0
24         self.height = 0
25         self.text = ''
26         # Fool unlink code:
27         self.canvas = None
28         self.need_sync = False
29
30
31     # Ensure we call the right connect functions:
32     connect = DiagramItem.connect
33     disconnect = DiagramItem.disconnect
34     notify = DiagramItem.notify
35
36     def save(self, save_func):
37 #        for prop in ('affine',):
38 #            self.save_property(save_func, prop)
39         DiagramItem.save(self, save_func)
40        
41     def postload(self):
42         if self.subject:
43             self._expression.set_text(self.subject.render())
44
45     def get_size(self, update=False):
46         """
47         Return the size of the feature. If update == True the item is
48         directly updated.
49         """
50         return self.width, self.height
51
52     def get_text(self):
53         return ''
54
55     def update_size(self, text, context):
56         cr = context.cairo
57         self.width, self.height = text_extents(cr, text)
58
59     def on_subject_notify(self, pspec, notifiers=()):
60         DiagramItem.on_subject_notify(self, pspec, notifiers)
61         #log.debug('setting text %s' % self.subject.render() or '')
62         self.text = self.subject and self.subject.render() or ''
63
64     def point(self, x, y):
65         """
66         """
67         return distance_rectangle_point((0, 0, self.width, self.height), (x, y))
68
69
70 class AttributeItem(FeatureItem):
71
72     def __init__(self, id=None):
73         FeatureItem.__init__(self, id)
74         self.need_sync = False
75
76     def on_subject_notify(self, pspec, notifiers=()):
77         FeatureItem.on_subject_notify(self, pspec, ('name',
78                                                     'isDerived',
79                                                     'visibility',
80                                                     'lowerValue.value',
81                                                     'upperValue.value',
82                                                     'defaultValue.value',
83                                                     'typeValue.value',
84                                                     'taggedValue',
85                                                     'association')
86                                                     + notifiers)
87         #self._expression.set_text(self.subject.render() or '')
88         #self.request_update()
89
90     def on_subject_notify__name(self, subject, pspec):
91         #log.debug('setting text %s' % self.subject.render() or '')
92         #self._expression.set_text(self.subject.render() or '')
93         self.request_update()
94
95     on_subject_notify__isDerived = on_subject_notify__name
96     on_subject_notify__visibility = on_subject_notify__name
97     on_subject_notify__lowerValue_value = on_subject_notify__name
98     on_subject_notify__upperValue_value = on_subject_notify__name
99     on_subject_notify__defaultValue_value = on_subject_notify__name
100     on_subject_notify__typeValue_value = on_subject_notify__name
101     on_subject_notify__taggedValue = on_subject_notify__name
102
103     def on_subject_notify__association(self, subject, pspec):
104         """
105         Make sure we update the attribute compartment (in case
106         the class_ property was defined before it is connected to
107         an association.
108         """
109         #if self.parent:
110         #    self.parent.sync_attributes()
111         self.need_sync = True
112         self.request_update()
113
114     def pre_update(self, context):
115 #        if self.need_sync and context.parent:
116 #            context.parent.sync_attributes()
117         self.need_sync = False
118         self.update_size(self.subject.render(), context)
119         #super(AttributeItem, self).pre_update(context)
120
121     def draw(self, context):
122         cr = context.cairo
123         text_set_font(cr, font.FONT)
124         text_align(cr, 0, 0, self.subject.render() or '', align_x=1, align_y=1)
125         #cr.show_text(self.subject.render() or '')
126
127
128
129 # TODO: handle Parameter's
130
131 class OperationItem(FeatureItem):
132
133     def __init__(self, id=None):
134         FeatureItem.__init__(self, id)
135         self.need_sync = False
136
137     def on_subject_notify(self, pspec, notifiers=()):
138         FeatureItem.on_subject_notify(self, pspec,
139                         ('name', 'visibility', 'isAbstract')
140                         + notifiers)
141
142         # TODO: Handle subject.returnResult[*] and subject.formalParameter[*]
143
144     def postload(self):
145         FeatureItem.postload(self)
146         self.need_sync = False
147
148     def on_subject_notify__name(self, subject, pspec):
149         #log.debug('setting text %s' % self.subject.render() or '')
150         self.request_update()
151
152     on_subject_notify__isAbstract = on_subject_notify__name
153     on_subject_notify__visibility = on_subject_notify__name
154     on_subject_notify__taggedValue = on_subject_notify__name
155
156     def pre_update(self, context):
157 #        if self.need_sync and context.parent:
158 #            context.parent.sync_operations()
159         self.need_sync = False
160         self.update_size(self.subject.render(), context)
161         #super(OperationItem, self).pre_update(context)
162
163     def draw(self, context):
164         cr = context.cairo
165         text_set_font(cr, font.FONT)
166         text_align(cr, 0, 0, self.subject.render() or '', align_x=1, align_y=1)
167         #cr.show_text(self.subject.render() or '')
168
169
170 # vim:sw=4:et
Note: See TracBrowser for help on using the browser.