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

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

added 'name-align-str' style property.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 """
2 Activity control nodes.
3 """
4
5 import math
6
7 from gaphas.util import path_ellipse
8 from gaphas.state import observed, reversible_property
9 from gaphas.item import Handle, Item
10 from gaphas.constraint import EqualsConstraint, LessThanConstraint
11 from gaphas.geometry import distance_line_point
12
13 from gaphor import UML
14 from gaphor.core import inject
15 from gaphor.diagram.diagramitem import DiagramItem
16 from gaphor.diagram.nameditem import NamedItem
17 from gaphor.diagram.style import ALIGN_LEFT, ALIGN_CENTER, ALIGN_TOP, \
18         ALIGN_RIGHT, ALIGN_BOTTOM
19 from gaphor.diagram.style import get_text_point
20
21
22 DEFAULT_JOIN_SPEC = 'and'
23
24
25 class ActivityNodeItem(NamedItem):
26     """Basic class for simple activity nodes.
27     Simple activity node is not resizable.
28     """
29     __style__   = {
30         'name-outside': True,
31         'name-padding': (2, 2, 2, 2),
32     }
33
34     def __init__(self, id=None):
35         NamedItem.__init__(self, id)
36         # Do not allow resizing of the node
37         for h in self._handles:
38             h.movable = False
39
40        
41 class InitialNodeItem(ActivityNodeItem):
42     """
43     Representation of initial node. Initial node has name which is put near
44     top-left side of node.
45     """
46     __uml__     = UML.InitialNode
47     __style__   = {
48         'min-size':   (20, 20),
49         'name-align': (ALIGN_LEFT, ALIGN_TOP),
50     }
51    
52     RADIUS = 10
53
54     def draw(self, context):
55         cr = context.cairo
56         r = self.RADIUS
57         d = r * 2
58         path_ellipse(cr, r, r, d, d)
59         cr.set_line_width(0.01)
60         cr.fill()
61        
62         super(InitialNodeItem, self).draw(context)
63
64
65 class ActivityFinalNodeItem(ActivityNodeItem):
66     """Representation of activity final node. Activity final node has name
67     which is put near right-bottom side of node.
68     """
69
70     __uml__ = UML.ActivityFinalNode
71     __style__   = {
72         'min-size':   (30, 30),
73         'name-align': (ALIGN_RIGHT, ALIGN_BOTTOM),
74     }
75
76     RADIUS_1 = 10
77     RADIUS_2 = 15
78
79     def draw(self, context):
80         cr = context.cairo
81         r = self.RADIUS_2 + 1
82         d = self.RADIUS_1 * 2
83         path_ellipse(cr, r, r, d, d)
84         cr.set_line_width(0.01)
85         cr.fill()
86
87         d = r * 2
88         path_ellipse(cr, r, r, d, d)
89         cr.set_line_width(0.01)
90         cr.set_line_width(2)
91         cr.stroke()
92
93         super(ActivityFinalNodeItem, self).draw(context)
94
95
96 class FlowFinalNodeItem(ActivityNodeItem):
97     """
98     Representation of flow final node. Flow final node has name which is
99     put near right-bottom side of node.
100     """
101
102     __uml__ = UML.FlowFinalNode
103     __style__   = {
104         'min-size':   (20, 20),
105         'name-align': (ALIGN_RIGHT, ALIGN_BOTTOM),
106     }
107
108     RADIUS = 10
109
110     def draw(self, context):
111         cr = context.cairo
112         r = self.RADIUS
113         d = r * 2
114         path_ellipse(cr, r, r, d, d)
115         cr.stroke()
116
117         dr = (1 - math.sin(math.pi / 4)) * r
118         cr.move_to(dr, dr)
119         cr.line_to(d - dr, d - dr)
120         cr.move_to(dr, d - dr)
121         cr.line_to(d - dr, dr)
122         cr.stroke()
123        
124         super(FlowFinalNodeItem, self).draw(context)
125        
126
127
128 class DecisionNodeItem(ActivityNodeItem):
129     """
130     Representation of decision or merge node.
131     """
132     __uml__   = UML.DecisionNode
133     __style__   = {
134         'min-size':   (20, 30),
135         'name-align': (ALIGN_LEFT, ALIGN_TOP),
136     }
137
138     RADIUS = 15
139
140     def __init__(self, id=None):
141         ActivityNodeItem.__init__(self, id)
142         self._combined = None
143         #self.set_prop_persistent('combined')
144
145     def save(self, save_func):
146         if self._combined:
147             save_func('combined', self._combined, reference=True)
148         super(DecisionNodeItem, self).save(save_func)
149
150     def load(self, name, value):
151         if name == 'combined':
152             self._combined = value
153         else:
154             super(DecisionNodeItem, self).load(name, value)
155
156     @observed
157     def _set_combined(self, value):
158         #self.preserve_property('combined')
159         self._combined = value
160
161     combined = reversible_property(lambda s: s._combined, _set_combined)
162        
163     def draw(self, context):
164         """
165         Draw diamond shape, which represents decision and merge nodes.
166         """
167         cr = context.cairo
168         r = self.RADIUS
169         r2 = r * 2/3
170
171         cr.move_to(r2, 0)
172         cr.line_to(r2 * 2, r)
173         cr.line_to(r2, r * 2)
174         cr.line_to(0, r)
175         cr.close_path()
176         cr.stroke()
177
178         super(DecisionNodeItem, self).draw(context)
179
180
181
182 class ForkNodeItem(Item, DiagramItem):
183     """
184     Representation of fork and join node.
185     """
186     __namedelement__ = True
187
188     element_factory = inject('element_factory')
189
190     __uml__   = UML.ForkNode
191
192     __style__ = {
193         'min-size':   (6, 45),
194         'name-align': (ALIGN_CENTER, ALIGN_BOTTOM),
195         'name-padding': (2, 2, 2, 2),
196         'name-outside': True,
197         'name-align-str': None,
198     }
199
200     STYLE_TOP = {
201         'text-align': (ALIGN_CENTER, ALIGN_TOP),
202         'text-outside': True,
203     }
204
205     def __init__(self, id=None):
206         Item.__init__(self)
207         DiagramItem.__init__(self, id)
208        
209         self._handles.extend((Handle(), Handle()))
210
211         self._combined = None
212         self._constraints = []
213
214         self._join_spec = self.add_text('joinSpec.value',
215             pattern='{ joinSpec = %s }',
216             style=self.STYLE_TOP,
217             visible=self.is_join_spec_visible)
218
219
220     def save(self, save_func):
221         save_func('matrix', tuple(self.matrix))
222         save_func('height', float(self._handles[1].y))
223         if self._combined:
224             save_func('combined', self._combined, reference=True)
225         DiagramItem.save(self, save_func)
226
227     def load(self, name, value):
228         if name == 'matrix':
229             self.matrix = eval(value)
230         elif name == 'height':
231             self._handles[1].y = eval(value)
232         elif name == 'combined':
233             self._combined = value
234         else:
235             #DiagramItem.load(self, name, value)
236             super(ForkNodeItem, self).load(name, value)
237
238     def postload(self):
239         subject = self.subject
240         if subject and isinstance(subject, UML.JoinNode) and subject.joinSpec:
241             self._join_spec.text = self.subject.joinSpec.value
242         super(ForkNodeItem, self).postload()
243
244     @observed
245     def _set_combined(self, value):
246         #self.preserve_property('combined')
247         self._combined = value
248
249     combined = reversible_property(lambda s: s._combined, _set_combined)
250
251     def setup_canvas(self):
252         Item.setup_canvas(self)
253
254         h1, h2 = self._handles
255         cadd = self.canvas.solver.add_constraint
256         c1 = EqualsConstraint(a=h1.x, b=h2.x)
257         c2 = LessThanConstraint(smaller=h1.y, bigger=h2.y, delta=30)
258         self._constraints.extend((cadd(c1), cadd(c2)))
259
260
261     def teardown_canvas(self):
262         super(ForkNodeItem, self).teardown_canvas()
263         for c in self._constraints:
264             self.canvas.solver.remove_constraint(c)
265
266
267     def is_join_spec_visible(self):
268         """
269         Check if join specification should be displayed.
270         """
271         return isinstance(self.subject, UML.JoinNode) \
272             and self.subject.joinSpec is not None \
273             and self.subject.joinSpec.value != DEFAULT_JOIN_SPEC
274
275
276     def text_align(self, extents, align, padding, outside):
277         h1, h2 = self._handles
278         w, _ = self.style.min_size
279         h = h2.y - h1.y
280         x, y = get_text_point(extents, w, h, align, padding, outside)
281
282         return x, y
283
284
285     def pre_update(self, context):
286         self.update_stereotype()
287         Item.pre_update(self, context)
288         DiagramItem.pre_update(self, context)
289
290
291     def post_update(self, context):
292         Item.post_update(self, context)
293         DiagramItem.post_update(self, context)
294
295
296     def draw(self, context):
297         """
298         Draw vertical line - symbol of fork and join nodes. Join
299         specification is also drawn above the item.
300         """
301         Item.draw(self, context)
302         DiagramItem.draw(self, context)
303
304         cr = context.cairo
305
306         cr.set_line_width(6)
307         h1, h2 = self._handles
308         cr.move_to(h1.x, h1.y)
309         cr.line_to(h2.x, h2.y)
310
311         cr.stroke()
312
313
314     def point(self, x, y):
315         h1, h2 = self._handles
316         d, p = distance_line_point(h1.pos, h2.pos, (x, y))
317         # Substract line_width / 2
318         return d - 3
319
320
321     def on_subject_notify(self, pspec, notifiers = ()):
322         """
323         Detect changes of subject.
324
325         If subject is join node, then set subject of join specification
326         text element.
327         """
328         subject = self.subject
329         if is_join_node(subject):
330             join_spec_notifiers = ('joinSpec', 'joinSpec.value')
331         else:
332             join_spec_notifiers = ()
333
334         DiagramItem.on_subject_notify(self, pspec, join_spec_notifiers + notifiers)
335
336         if is_join_node(subject) and not (subject.joinSpec and subject.joinSpec.value):
337             self.set_join_spec(DEFAULT_JOIN_SPEC)
338         self.request_update()
339
340
341     def set_join_spec(self, value):
342         """
343         Set join specification.
344         """
345         subject = self.subject
346         if not is_join_node(subject):
347             return
348
349         if not subject.joinSpec:
350             subject.joinSpec = self.element_factory.create(UML.LiteralSpecification)
351
352         if not value:
353             value = DEFAULT_JOIN_SPEC
354
355         subject.joinSpec.value = value
356         self._join_spec.text = value
357
358
359     def on_subject_notify__joinSpec(self, subject, pspec=None):
360         self.request_update()
361
362
363     def on_subject_notify__joinSpec_value(self, subject, pspec=None):
364         self.request_update()
365
366
367 def is_join_node(subject):
368     """
369     Check if ``subject`` is join node.
370     """
371     return subject and isinstance(subject, UML.JoinNode)
372
373 # vim:sw=4:et
Note: See TracBrowser for help on using the browser.