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

Revision 252, 3.2 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 """ClassifierItem diagram item
2 """
3 # vim:sw=4:et
4
5 import gobject
6 import pango
7 import diacanvas
8 from gaphor.diagram import initialize_item
9 from modelelement import ModelElementItem
10
11 class ClassifierItem(ModelElementItem, diacanvas.CanvasEditable):
12     __gproperties__ = {
13         'name': (gobject.TYPE_STRING, 'name', '', '', gobject.PARAM_READWRITE)
14     }
15
16     FONT='sans bold 10'
17
18     popup_menu = (
19         'ItemRename',
20         'separator',
21         'EditDelete',
22     )
23
24     def __init__(self, id=None):
25         ModelElementItem.__init__(self, id)
26
27         self._name = diacanvas.shape.Text()
28         self._name.set_font_description(pango.FontDescription(ClassifierItem.FONT))
29         self._name.set_alignment(pango.ALIGN_CENTER)
30         #self._name.set_wrap_mode(diacanvas.shape.WRAP_NONE)
31         self._name.set_markup(False)
32
33     def postload(self):
34         ModelElementItem.postload(self)
35         # Set values in postload, since the load function doesn't send
36         # notifications.
37         self._name.set_text(self.subject.name or '')
38
39     def rename(self):
40         self.start_editing(self._name)
41
42     def do_set_property(self, pspec, value):
43         if pspec.name == 'name':
44             self.preserve_property('name')
45             self.subject.name = value
46         else:
47             ModelElementItem.do_set_property(self, pspec, value)
48
49     def do_get_property(self, pspec):
50         if pspec.name == 'name':
51             return self.subject.name
52         else:
53             return ModelElementItem.do_get_property(self, pspec)
54
55     def get_name_size(self):
56         """Return the width and height of the name shape.
57         """
58         return self._name.to_pango_layout(True).get_pixel_size()
59
60     def update_name(self, x, y, width, height):
61         self._name.set_pos((x, y))
62         self._name.set_max_width(width)
63         self._name.set_max_height(height)
64
65     def on_subject_notify(self, pspec, notifiers=()):
66         """See DiagramItem.on_subject_notify().
67         """
68         #log.info('ClassifierItem.on_subject_notify: %s' % str(notifiers))
69         ModelElementItem.on_subject_notify(self, pspec, ('name',) + notifiers)
70         self._name.set_text(self.subject and self.subject.name or '')
71
72     def on_subject_notify__name(self, subject, pspec):
73         assert self.subject is subject
74         #print 'on_subject_notify__name: %s' % self.subject.name
75         self._name.set_text(self.subject.name)
76         self.request_update()
77
78     # CanvasItem callbacks:
79
80     def on_update(self, affine):
81         ModelElementItem.on_update(self, affine)
82
83     def on_event (self, event):
84         if event.type == diacanvas.EVENT_2BUTTON_PRESS:
85             self.rename()
86             return True
87         else:
88             return ModelElementItem.on_event(self, event)
89
90     def on_shape_iter(self):
91         return iter([self._name])
92
93     # Editable
94
95     def on_editable_start_editing(self, shape):
96         self.preserve_property('name')
97
98     def on_editable_editing_done(self, shape, new_text):
99         self.preserve_property('name')
100         if new_text != self.subject.name:
101             self.subject.name = new_text
102         self.request_update()
103
104 initialize_item(ClassifierItem)
105 #gobject.type_register(ClassifierItem)
106 #diacanvas.set_editable(ClassifierItem)
107
Note: See TracBrowser for help on using the browser.