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 |
if hasattr(p,"point"): |
16 |
request_resolve(p.point.x) |
17 |
request_resolve(p.point.y) |
18 |
|
19 |
super(BlockCanvas,self).update_constraints(items) |
20 |
|
21 |
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 |
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 |
dirty_matrix_items.update(super(BlockCanvas, self)._normalize(items)) |
44 |
return dirty_matrix_items |
45 |
|
46 |
def reattach_ascend(self, library, notesdb): |
47 |
""" |
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 |
items = [] |
55 |
for item in self.get_all_items(): |
56 |
if not hasattr(item, 'blockinstance'): |
57 |
continue |
58 |
bi = item.blockinstance |
59 |
if bi.blocktype.type is None: |
60 |
bi.blocktype.reattach_ascend(library, notesdb) |
61 |
|
62 |
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 |
s += str(item.blockinstance) |
73 |
|
74 |
for line in self.get_all_items(): |
75 |
if not isinstance(line, Line): |
76 |
continue |
77 |
s += str(line.lineinstance) |
78 |
s += "END canvasmodel;\n"; |
79 |
return s |
80 |
|
81 |
def __getstate__(self): |
82 |
""" |
83 |
Placeholder for any special pickling stuff that we want to do with |
84 |
our canvas. |
85 |
""" |
86 |
return super(BlockCanvas,self).__getstate__() |
87 |
|
88 |
def __setstate__(self, state): |
89 |
""" |
90 |
Placeholder for any special pickling stuff that we want to do with |
91 |
our canvas. |
92 |
""" |
93 |
super(BlockCanvas,self).__setstate__(state) |
94 |
|