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

Revision 263, 3.5 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 '''
2 CommentItem diagram item
3 '''
4 # vim:sw=4
5
6 import gobject
7 import pango
8 import diacanvas
9
10 import gaphor.UML as UML
11 from gaphor.diagram import initialize_item
12
13 from modelelement import ModelElementItem
14
15 class CommentItem(ModelElementItem, diacanvas.CanvasEditable):
16     __gproperties__ = {
17         'body': (gobject.TYPE_STRING, 'body', '', '', gobject.PARAM_READWRITE)
18     }
19
20     EAR=15
21     OFFSET=5
22     FONT='sans 10'
23
24     popup_menu = (
25         'EditItem',
26         'separator',
27         'EditDelete',
28     )
29
30     def __init__(self, id=None):
31         ModelElementItem.__init__(self, id)
32         self.set(min_width=CommentItem.EAR + 2 * CommentItem.OFFSET,
33                  height=50, width=100)
34         self._border = diacanvas.shape.Path()
35         self._border.set_line_width(2.0)
36         self._body = diacanvas.shape.Text()
37         self._body.set_font_description(pango.FontDescription(CommentItem.FONT))
38         #self._body.set_text_width(self.width - (CommentItem.OFFSET * 2))
39         self._body.set_markup(False)
40         self._body.set_pos((CommentItem.OFFSET, CommentItem.OFFSET))
41
42     def postload(self):
43         self._body.set_text(self.subject.body or '')
44
45     def edit(self):
46         self.start_editing(self._body)
47
48     def do_set_property(self, pspec, value):
49         if pspec.name == 'body':
50             self.preserve_property('body')
51             self.subject.body = value
52         else:
53             ModelElementItem.do_set_property(self, pspec, value)
54
55     def do_get_property(self, pspec):
56         if pspec.name == 'body':
57             return self.subject.body
58         else:
59             return ModelElementItem.do_get_property(self, pspec)
60
61     def on_subject_notify(self, pspec):
62         """See DiagramItem.on_subject_notify()."""
63         ModelElementItem.on_subject_notify(self, pspec, ('body',))
64
65         if self.subject:
66             self.on_subject_notify__body(self.subject, None)
67
68     def on_subject_notify__body(self, subject, pspec):
69         #print 'on_subject_notify__body: %s' % self.subject.body
70         self._body.set_text(self.subject.body or '')
71         self.request_update()
72
73     # DiaCanvasItem callbacks:
74
75     def on_update(self, affine):
76         # Outline the text, first tell the text how width it may become:
77         self._body.set_text_width(self.width - CommentItem.EAR - CommentItem.OFFSET)
78         w, h = self._body.to_pango_layout(True).get_pixel_size()
79         self.set(min_height=h + CommentItem.OFFSET * 2)
80         #self._body.set_property('height', self.height - CommentItem.OFFSET * 2)
81         #self.update_child(self._body, affine)
82         ModelElementItem.on_update(self, affine)
83
84         # Width and height, adjusted for line width...
85         w = self.width
86         h = self.height
87         ear = CommentItem.EAR
88         self._border.line(((w - ear, 0), (w- ear, ear), (w, ear), (w - ear, 0),
89                             (0, 0), (0, h), (w, h), (w, ear)))
90         self.expand_bounds(1)
91
92     def on_event (self, event):
93         if event.type == diacanvas.EVENT_2BUTTON_PRESS:
94             self.edit()
95             return True
96         else:
97             return ModelElementItem.on_event(self, event)
98
99     def on_shape_iter(self):
100         return iter([self._border, self._body])
101
102     # Editable
103
104     def on_editable_start_editing(self, shape):
105         self.preserve_property('body')
106
107     def on_editable_editing_done(self, shape, new_text):
108         if new_text != self.subject.body:
109             self.subject.body = new_text
110         #self._body.set_text(new_text)
111         self.request_update()
112
113 initialize_item(CommentItem, UML.Comment)
Note: See TracBrowser for help on using the browser.