root/gaphor/trunk/gaphor/diagram/classes/feature.py

Revision 2255, 3.7 kB (checked in by arj..@yirdis.nl, 9 months ago)

moved classes items to seperate package. More unit tests for interface item.

  • 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 gaphor import UML
8 from gaphas.item import Item
9 from gaphor.diagram import DiagramItemMeta
10 from gaphor.diagram.diagramitem import DiagramItem
11 from gaphas.util import text_extents, text_set_font, text_align
12 from gaphor.diagram import font
13
14 class FeatureItem(DiagramItem):
15     """
16     FeatureItems are model elements who recide inside a ClassifierItem, such
17     as methods and attributes. Those items can have comments attached, but only
18     on the left and right side.
19     Note that features can also be used inside objects.
20     """
21
22     def __init__(self, id=None):
23         DiagramItem.__init__(self, id)
24         self.width = 0
25         self.height = 0
26         self.text = ''
27         # Fool unlink code (attribute is not a gaphas.Item):
28         self.canvas = None
29
30
31     def save(self, save_func):
32         DiagramItem.save(self, save_func)
33        
34
35     def postload(self):
36         if self.subject:
37             self._expression.set_text(self.subject.render())
38
39
40     def get_size(self, update=False):
41         """
42         Return the size of the feature. If update == True the item is
43         directly updated.
44         """
45         return self.width, self.height
46
47
48     def get_text(self):
49         return ''
50
51
52     def update_size(self, text, context):
53         if text:
54             cr = context.cairo
55             self.width, self.height = text_extents(cr, text)
56         else:
57             self.width, self.height = 0, 0
58
59
60     def point(self, x, y):
61         """
62         """
63         return distance_rectangle_point((0, 0, self.width, self.height), (x, y))
64
65
66 class AttributeItem(FeatureItem):
67
68     def __init__(self, id=None):
69         FeatureItem.__init__(self, id)
70
71         self.add_watch(UML.Property.name)
72         self.add_watch(UML.Property.isDerived)
73         self.add_watch(UML.Property.visibility)
74         self.add_watch(UML.Property.lowerValue)
75         self.add_watch(UML.Property.upperValue)
76         self.add_watch(UML.Property.defaultValue)
77         self.add_watch(UML.Property.typeValue)
78         self.add_watch(UML.Property.taggedValue)
79         self.add_watch(UML.LiteralSpecification.value, self.on_feature_value)
80
81     def on_feature_value(self, event):
82         element = event.element
83         subject = self.subject
84         if subject and element in (subject.lowerValue, subject.upperValue, subject.defaultValue, subject.typeValue, subject.taggedValue):
85             self.request_update()
86
87
88     def pre_update(self, context):
89         self.update_size(self.subject.render(), context)
90         #super(AttributeItem, self).pre_update(context)
91
92     def draw(self, context):
93         cr = context.cairo
94         text_set_font(cr, font.FONT)
95         text_align(cr, 0, 0, self.subject.render() or '', align_x=1, align_y=1)
96
97
98 # TODO: handle Parameter's
99
100 class OperationItem(FeatureItem):
101
102     def __init__(self, id=None):
103         FeatureItem.__init__(self, id)
104        
105         self.add_watch(UML.Operation.name)
106         self.add_watch(UML.Operation.visibility)
107         self.add_watch(UML.Operation.isAbstract)
108         self.add_watch(UML.Operation.taggedValue)
109         # Parameters
110         # TODO: Handle subject.returnResult[*] and subject.formalParameter[*]
111         self.add_watch(UML.Operation.isAbstract)
112
113
114     def postload(self):
115         FeatureItem.postload(self)
116
117     def pre_update(self, context):
118         self.update_size(self.subject.render(), context)
119         #super(OperationItem, 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 # vim:sw=4:et
Note: See TracBrowser for help on using the browser.