root/gaphor/tags/gaphor-0.12.0/gaphor/adapters/editors.py

Revision 1379, 5.7 kB (checked in by wrobe..@pld-linux.org, 2 years ago)

- object node editor to be replaced with property page and

DiagramItemTextEditor?

Line 
1 """
2 Adapters
3 """
4
5 from zope import interface, component
6
7 from gaphas.item import NW, SE
8 from gaphas import geometry
9 from gaphas import constraint
10 from gaphor import UML
11 from gaphor.core import inject
12 from gaphor.UML.umllex import render_attribute
13 from gaphor.diagram.interfaces import IEditor
14 from gaphor.diagram import items
15 from gaphor.misc.rattr import rgetattr, rsetattr
16
17
18 class CommentItemEditor(object):
19     """
20     Text edit support for Comment item.
21     """
22     interface.implements(IEditor)
23     component.adapts(items.CommentItem)
24
25     def __init__(self, item):
26         self._item = item
27
28     def is_editable(self, x, y):
29         return True
30
31     def get_text(self):
32         return self._item.subject.body
33
34     def get_bounds(self):
35         return None
36
37     def update_text(self, text):
38         self._item.subject.body = text
39
40     def key_pressed(self, pos, key):
41         pass
42
43 component.provideAdapter(CommentItemEditor)
44
45
46 class NamedItemEditor(object):
47     """
48     Text edit support for Named items.
49     """
50     interface.implements(IEditor)
51     component.adapts(items.NamedItem)
52
53     def __init__(self, item):
54         self._item = item
55
56     def is_editable(self, x, y):
57         return True
58
59     def get_text(self):
60         return self._item.subject.name
61
62     def get_bounds(self):
63         return None
64
65     def update_text(self, text):
66         self._item.subject.name = text
67         self._item.request_update()
68
69     def key_pressed(self, pos, key):
70         pass
71
72 component.provideAdapter(NamedItemEditor)
73
74
75 class DiagramItemTextEditor(object):
76     """
77     Text edit support for diagram items containing text elements.
78     """
79     interface.implements(IEditor)
80     component.adapts(items.DiagramItem)
81
82     def __init__(self, item):
83         self._item = item
84         self._text_element = None
85
86     def is_editable(self, x, y):
87         if not self._item.subject:
88             return False
89
90         for txt in self._item.texts():
91             if (x, y) in txt.bounds:
92                 self._text_element = txt
93                 break
94         return self._text_element is not None
95
96     def get_text(self):
97         if self._text_element:
98             return rgetattr(self._item.subject, self._text_element.attr)
99
100     def get_bounds(self):
101         return None
102
103     def update_text(self, text):
104         if self._text_element:
105             self._text_element.text = text
106             rsetattr(self._item.subject, self._text_element.attr, text)
107
108     def key_pressed(self, pos, key):
109         pass
110
111 component.provideAdapter(DiagramItemTextEditor)
112
113
114 class ClassifierItemEditor(object):
115     """
116     Text editor support for Classifiers. Also features contained in Classifiers'
117     compartments are edited through this interface.
118     """
119     interface.implements(IEditor)
120     component.adapts(items.ClassifierItem)
121
122     def __init__(self, item):
123         self._item = item
124         self._edit = None
125
126     def is_editable(self, x, y):
127         """
128         Find out what's located at point (x, y), is it in the
129         name part or is it text in some compartment
130         """
131         self._edit = self._item.item_at(x, y)
132         return bool(self._edit and self._edit.subject)
133
134     def get_text(self):
135         if hasattr(self._edit.subject, 'render'):
136             return self._edit.subject.render()
137         return self._edit.subject.name
138
139     def get_bounds(self):
140         return None
141
142     def update_text(self, text):
143         if hasattr(self._edit.subject, 'parse'):
144             return self._edit.subject.parse(text)
145         else:
146             self._item.subject.name = text
147
148     def key_pressed(self, pos, key):
149         pass
150
151 component.provideAdapter(ClassifierItemEditor)
152  
153
154 class AssociationItemEditor(object):
155     interface.implements(IEditor)
156     component.adapts(items.AssociationItem)
157
158     def __init__(self, item):
159         self._item = item
160         self._edit = None
161
162     def is_editable(self, x, y):
163         """Find out what's located at point (x, y), is it in the
164         name part or is it text in some compartment
165         """
166         item = self._item
167         if not item.subject:
168             return False
169         if item.head_end.point(x, y) <= 0:
170             self._edit = item.head_end
171         elif item.tail_end.point(x, y) <= 0:
172             self._edit = item.tail_end
173         else:
174             self._edit = item
175         return True
176
177     def get_text(self):
178         if self._edit is self._item:
179             return self._edit.subject.name
180         return render_attribute(self._edit.subject)
181
182     def get_bounds(self):
183         return None
184
185     def update_text(self, text):
186         if hasattr(self._edit.subject, 'parse'):
187             return self._edit.subject.parse(text)
188         else:
189             self._item.subject.name = text
190
191     def key_pressed(self, pos, key):
192         pass
193
194 component.provideAdapter(AssociationItemEditor)
195    
196
197
198 class ForkNodeItemEditor(object):
199     """Text edit support for fork node join specification.
200     """
201     interface.implements(IEditor)
202     component.adapts(items.ForkNodeItem)
203
204     element_factory = inject('element_factory')
205
206     def __init__(self, item):
207         self._item = item
208
209     def is_editable(self, x, y):
210         return True
211
212     def get_text(self):
213         """
214         Get join specification text.
215         """
216         if self._item.subject.joinSpec:
217             return self._item.subject.joinSpec.value
218         else:
219             return ''
220
221     def get_bounds(self):
222         return None
223
224     def update_text(self, text):
225         """
226         Set join specification text.
227         """
228         spec = self._item.subject.joinSpec
229         if not spec:
230             spec = self._item.subject.joinSpec = self.element_factory.create(UML.LiteralSpecification)
231             spec.value = text
232
233     def key_pressed(self, pos, key):
234         pass
235
236 component.provideAdapter(ForkNodeItemEditor)
237
238 # vim:sw=4:et:ai
Note: See TracBrowser for help on using the browser.