/[ascend]/trunk/pygtk/canvas/blockcanvas.py
ViewVC logotype

Contents of /trunk/pygtk/canvas/blockcanvas.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1968 - (show annotations) (download) (as text)
Mon Dec 1 08:13:10 2008 UTC (13 years, 7 months ago) by jpye
File MIME type: text/x-python
File size: 2724 byte(s)
Adding lines to export, but not done yet.
1 from gaphas import Canvas
2 from gaphas.item import Line
3
4 class BlockCanvas(Canvas):
5 def update_constraints(self, items):
6 """
7 Update constraints. Also variables may be marked as dirty before the
8 constraint solver kicks in.
9 """
10 # request solving of external constraints associated with dirty items
11 request_resolve = self._solver.request_resolve
12 for item in items:
13 if hasattr(item,'ports'):
14 for p in item.ports:
15 request_resolve(p.x)
16 request_resolve(p.y)
17
18 super(BlockCanvas,self).update_constraints(items)
19
20 def _normalize(self, items):
21 """
22 Correct offset of ports due to movement of left-side handles.
23 """
24 dirty_matrix_items = set()
25 for item in items:
26 if not hasattr(item, 'ports'):
27 continue
28 handles = item.handles()
29 ports = item.ports
30 if not handles or not ports:
31 continue
32 x, y = map(float, handles[0].pos)
33 # Dirty marking is done by the superclass' method
34 if x:
35 for p in ports:
36 p.x._value -= x
37 if y:
38 for p in ports:
39 p.y._value -= y
40 dirty_matrix_items.update(super(BlockCanvas, self)._normalize(items))
41 return dirty_matrix_items
42
43 def reattach_ascend(self, library, notesdb):
44 """
45 After unpickling a canvas, this method gives a way of reattaching
46 the model to an ASCEND library, by connecting the types named in the
47 pickle with the typenames present in the Library.
48 """
49 # FIXME need to set ASCEND to *load* the missing types if they're not
50 # already in the Library.
51 items = []
52 for item in self.get_all_items():
53 if not hasattr(item, 'blockinstance'):
54 continue
55 bi = item.blockinstance
56 if bi.blocktype.type is None:
57 bi.blocktype.reattach_ascend(library, notesdb)
58
59 def __str__(self):
60 """
61 Create ASCEND code corresponding to canvas model, return the
62 code as a string.
63 """
64 s = "\n(* automatically generated model from blocklist.py *)\n";
65 s += "MODEL canvasmodel;\n"
66 for item in self.get_all_items():
67 if not hasattr(item, 'blockinstance'):
68 continue
69 s += str(item.blockinstance)
70
71 for item in self.get_all_items():
72 if not isinstance(item, Line):
73 continue
74 s += "\t%s, %s ARE_THE_SAME;\n" % (item._handles[0].connected_to.blockinstance.name, item._handles[1].connected_to.blockinstance.name)
75 s += "END canvasmodel;\n";
76 return s
77
78 def __getstate__(self):
79 """
80 Placeholder for any special pickling stuff that we want to do with
81 our canvas.
82 """
83 return super(BlockCanvas,self).__getstate__()
84
85 def __setstate__(self, state):
86 """
87 Placeholder for any special pickling stuff that we want to do with
88 our canvas.
89 """
90 super(BlockCanvas,self).__setstate__(state)
91

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22