root/gaphor/tags/gaphor-0.3.0/gaphor/diagram/commentline.py

Revision 220, 5.1 kB (checked in by arjanmol, 5 years ago)

*** empty log message ***

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim:sw=4:et
2 '''
3 CommentLine -- A line that connects a comment to another model element.
4
5 TODO: Why do we lose the __id property when we do a get_property after a model
6 has been loaded. It works okay when creating new items.
7 '''
8
9 import gobject
10 import diacanvas
11 import gaphor.UML as UML
12 from gaphor.diagram import initialize_item
13
14 from diagramitem import DiagramItem
15 from diagramline import DiagramLine
16
17 class CommentLineItem(DiagramLine, DiagramItem):
18     __gproperties__ = DiagramItem.__gproperties__
19
20     __gsignals__ = DiagramItem.__gsignals__
21
22     def __init__(self, id=None):
23         #diacanvas.CanvasLine.__init__(self)
24         DiagramLine.__init__(self)
25         DiagramItem.__init__(self, id)
26         self.set_property('dash', (7.0, 5.0))
27         self.__notify_id = None
28
29     #id = property(lambda self: self._id, doc='Id')
30
31     def save (self, save_func):
32         DiagramItem.save(self, save_func)
33         DiagramLine.save(self, save_func)
34    
35     def load (self, name, value):
36         if name == 'subject':
37             DiagramItem.load(self, name, value)
38         else:
39             DiagramLine.load(self, name, value)
40
41     def postload(self):
42         DiagramItem.postload(self)
43         DiagramLine.postload(self)
44
45     # Ensure we call the right connect functions:
46     connect = DiagramItem.connect
47     disconnect = DiagramItem.disconnect
48     notify = DiagramItem.notify
49
50     def on_notify_comment_parent(self, comment, pspec):
51         if not comment.parent and self.parent:
52             self.parent.remove(self)
53            
54     def on_glue(self, handle, wx, wy):
55         "No connections are allowed on a CommentLine."
56         return None
57
58     def on_connect_handle(self, handle):
59         "No connections are allows to the CommentLine."
60         return 0
61
62     def on_disconnect_handle(self, handle):
63         "No connections are allows to the CommentLine."
64         return 0
65
66     # Gaphor Connection Protocol
67
68     def allow_connect_handle(self, handle, connecting_to):
69         """See DiagramLine.allow_connect_handle().
70         """
71         h = self.handles
72         c1 = h[0].connected_to
73         c2 = h[-1].connected_to
74         # OK if both sides are not connected yet.
75         if not c1 and not c2:
76             return 1
77        
78         if handle is h[0]:
79             c1 = connecting_to
80         elif handle is h[-1]:
81             c2 = connecting_to
82         else:
83             raise AttributeError, 'handle should be the first or the last handle of the CommentLine'
84
85         # We should not connect if both ends will become a Comment
86         if isinstance(c1.subject, UML.Comment) and \
87                 isinstance(c2.subject, UML.Comment):
88             return False
89         # Also do not connect if both ends are non-Comments
90         if not isinstance(c1.subject, UML.Comment) and \
91                 not isinstance(c2.subject, UML.Comment):
92             return False
93
94         # Allow connection
95         return True
96
97     def confirm_connect_handle (self, handle):
98         """See DiagramLine.confirm_connect_handle().
99         """
100         #print 'confirm_connect_handle', handle
101         c1 = self.handles[0].connected_to
102         c2 = self.handles[-1].connected_to
103         if c1 and c2:
104             s1 = c1.subject
105             s2 = c2.subject
106             if isinstance (s1, UML.Comment):
107                 s1.annotatedElement = s2
108                 if not self.__notify_id:
109                     self.__notify_id = c1.connect('notify::parent',
110                                                   self.on_notify_comment_parent)
111             elif isinstance (s2, UML.Comment):
112                 s2.annotatedElement = s1
113                 if not self.__notify_id:
114                     self.__notify_id = c2.connect('notify::parent',
115                                                   self.on_notify_comment_parent)
116             else:
117                 raise TypeError, 'One end of the CommentLine should connect to a Comment'
118
119     def allow_disconnect_handle (self, handle):
120         """See DiagramLine.allow_disconnect_handle().
121         """
122         return True
123
124     def confirm_disconnect_handle (self, handle, was_connected_to):
125         """See DiagramLine.confirm_disconnect_handle().
126         """
127         #print 'confirm_disconnect_handle', handle
128         c1 = None
129         c2 = None
130         if handle is self.handles[0]:
131             c1 = was_connected_to
132             c2 = self.handles[-1].connected_to
133         elif handle is self.handles[-1]:
134             c1 = self.handles[0].connected_to
135             c2 = was_connected_to
136        
137         if c1 and c2:
138             s1 = c1.subject
139             s2 = c2.subject
140             if isinstance (s1, UML.Comment):
141                 c1.disconnect(self.__notify_id)
142                 self.__notify_id = None
143                 del s1.annotatedElement[s2]
144             elif isinstance (s2, UML.Comment):
145                 c2.disconnect(self.__notify_id)
146                 self.__notify_id = None
147                 del s2.annotatedElement[s1]
148             #else:
149                 #raise TypeError, 'One end of the CommentLine should connect to a Comment. How could this connect anyway?'
150
151 initialize_item(CommentLineItem)
152 #gobject.type_register(CommentLineItem)
153 #diacanvas.set_callbacks(CommentLineItem)
154
Note: See TracBrowser for help on using the browser.