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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1979 - (hide annotations) (download) (as text)
Sat Jan 10 00:21:37 2009 UTC (13 years, 7 months ago) by jpye
File MIME type: text/x-python
File size: 2638 byte(s)
Basic MODEL export functionality now in place for canvas.
Still needs lots of cleaning up though.

1 jpye 1942 from gaphas import Canvas
2 jpye 1968 from gaphas.item import Line
3 jpye 1942
4     class BlockCanvas(Canvas):
5 jpye 1961 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 jpye 1970 if hasattr(item,'ports'):
14     for p in item._ports:
15     if hasattr(p,"point"):
16     request_resolve(p.point.x)
17     request_resolve(p.point.y)
18 jpye 1942
19 jpye 1961 super(BlockCanvas,self).update_constraints(items)
20 jpye 1942
21 jpye 1961 def _normalize(self, items):
22     """
23     Correct offset of ports due to movement of left-side handles.
24     """
25     dirty_matrix_items = set()
26     for item in items:
27 jpye 1970 if not hasattr(item, 'ports'):
28     continue
29     handles = item.handles()
30     ports = item._ports
31     if not handles or not ports:
32     continue
33     x, y = map(float, handles[0].pos)
34     # Dirty marking is done by the superclass' method
35     if x:
36     for p in ports:
37     if hasattr(p,"point"):
38     p.point.x._value -= x
39     if y:
40     for p in ports:
41     if hasattr(p,"point"):
42     p.point.y._value -= y
43 jpye 1961 dirty_matrix_items.update(super(BlockCanvas, self)._normalize(items))
44     return dirty_matrix_items
45 jpye 1942
46 jpye 1961 def reattach_ascend(self, library, notesdb):
47 jpye 1967 """
48     After unpickling a canvas, this method gives a way of reattaching
49     the model to an ASCEND library, by connecting the types named in the
50     pickle with the typenames present in the Library.
51     """
52     # FIXME need to set ASCEND to *load* the missing types if they're not
53     # already in the Library.
54 jpye 1959 items = []
55 jpye 1961 for item in self.get_all_items():
56     if not hasattr(item, 'blockinstance'):
57 jpye 1959 continue
58 jpye 1961 bi = item.blockinstance
59     if bi.blocktype.type is None:
60     bi.blocktype.reattach_ascend(library, notesdb)
61 jpye 1954
62 jpye 1967 def __str__(self):
63     """
64     Create ASCEND code corresponding to canvas model, return the
65     code as a string.
66     """
67     s = "\n(* automatically generated model from blocklist.py *)\n";
68     s += "MODEL canvasmodel;\n"
69     for item in self.get_all_items():
70     if not hasattr(item, 'blockinstance'):
71     continue
72 jpye 1968 s += str(item.blockinstance)
73 jpye 1967
74 jpye 1973 for line in self.get_all_items():
75     if not isinstance(line, Line):
76 jpye 1967 continue
77 jpye 1979 s += str(line.lineinstance)
78 jpye 1967 s += "END canvasmodel;\n";
79     return s
80    
81 jpye 1965 def __getstate__(self):
82 jpye 1967 """
83     Placeholder for any special pickling stuff that we want to do with
84     our canvas.
85     """
86 jpye 1965 return super(BlockCanvas,self).__getstate__()
87 jpye 1959
88 jpye 1965 def __setstate__(self, state):
89 jpye 1967 """
90     Placeholder for any special pickling stuff that we want to do with
91     our canvas.
92     """
93 jpye 1965 super(BlockCanvas,self).__setstate__(state)
94    

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