root/gaphor/tags/gaphor-0.12.5/gaphor/UML/diagram.py

Revision 1682, 2.7 kB (checked in by wrobe..@pld-linux.org, 1 year ago)

- code cleanup

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 # vim: sw=4
2 """
3 This module contains a model element Diagram which is the abstract
4 representation of a UML diagram. Diagrams can be visualized and edited.
5 """
6
7 __author__ = 'Arjan Molenaar'
8 __version__ = '$revision$'
9 __date__ = '$date$'
10
11 import gobject
12 import gaphas
13 from gaphor.misc import uniqueid
14 from uml2 import Namespace, PackageableElement
15
16 class DiagramCanvas(gaphas.Canvas):
17     """
18     Some additions to gaphas.Canvas class.
19     Esp. load and save functionallity.
20     """
21
22     def __init__(self, diagram):
23         super(DiagramCanvas, self).__init__()
24         self._diagram = diagram
25         self._block_updates = False
26
27     diagram = property(lambda s: s._diagram)
28
29     def _set_block_updates(self, block):
30         self._block_updates = block
31         if not block:
32             self.update_now()
33
34     block_updates = property(lambda s: s._block_updates, _set_block_updates)
35
36     def update_now(self):
37         if self._block_updates:
38             #log.debug('Update blocked for canvas %s' % self)
39             return
40         super(DiagramCanvas, self).update_now()
41
42     def save(self, save_func):
43         #for prop in DiagramCanvas._savable_canvas_properties:
44         #    save_func(prop, self.get_property(prop))
45         #save_func('root_affine', self.root.get_property('affine'))
46         # Save child items:
47         for item in self.get_root_items():
48             save_func(None, item)
49
50     def postload(self):
51         pass #self.block_updates = False
52
53     def select(self, expression=lambda e: True):
54         """
55         Return a list of all canvas items that match expression.
56         """
57         return filter(expression, self.get_all_items())
58
59
60 class Diagram(Namespace, PackageableElement):
61     """
62     Diagrams may contain model elements and can be owned by a Package.
63     """
64
65     def __init__(self, id=None, factory=None):
66         super(Diagram, self).__init__(id, factory)
67         self.canvas = DiagramCanvas(self)
68
69     def save(self, save_func):
70         super(Diagram, self).save(save_func)
71         save_func('canvas', self.canvas)
72
73     def postload(self):
74         super(Diagram, self).postload()
75         self.canvas.postload()
76
77     def create(self, type, parent=None, subject=None):
78         """
79         Create a new canvas item on the canvas. It is created with
80         a unique ID and it is attached to the diagram's root item.
81         """
82         assert issubclass(type, gaphas.Item)
83         obj = type(uniqueid.generate_id())
84         if subject:
85             obj.subject = subject
86         self.canvas.add(obj, parent)
87         return obj
88
89     def unlink(self):
90         # Make sure all canvas items are unlinked
91         for item in self.canvas.get_all_items():
92             try:
93                 item.unlink()
94             except:
95                 pass
96
97         super(Diagram, self).unlink()
Note: See TracBrowser for help on using the browser.