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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1954 - (show annotations) (download) (as text)
Tue Nov 11 10:01:18 2008 UTC (13 years, 10 months ago) by jpye
File MIME type: text/x-python
File size: 3222 byte(s)
Some changes to facilitate pickling.
1 from gaphas.item import Element
2 from gaphas.constraint import LineConstraint, LessThanConstraint, EqualsConstraint, Constraint, _update, BalanceConstraint
3 from gaphas.item import Line, SW, NE, NW, SE, Element, Handle
4 from gaphas.util import *
5
6 from port import *
7
8 class BlockItem(Element):
9 """
10 This is an ASCEND 'block' in the canvas-based modeller. The block will have
11 sets of input and output ports to which connector lines can be 'glued'.
12 The block will also have a corresponding ASCEND MODEL type, and a name
13 which will be used in ASCEND to refer to this block. Each of the ports will
14 be special visual elements, but note that these are not 'handles', because
15 they can not be used to resize/modify the element.
16 """
17
18 def __init__(self, width=64, height=64):
19
20 self.ports = []
21 super(BlockItem, self).__init__(width, height)
22
23 def draw(self, context):
24 #print 'Box.draw', self
25 c = context.cairo
26
27 phalfsize = 3
28 for p in self.ports:
29 c.rectangle(p.x - phalfsize, p.y - phalfsize, 2*phalfsize, 2*phalfsize)
30 if p.connected_to is None:
31 c.set_source_rgba(0.8,0.8,1, 0.8)
32 else:
33 c.set_source_rgba(1,0,0,1)
34 c.fill_preserve()
35 c.set_source_rgb(0.8,0.8,0)
36 c.stroke()
37
38 def glue(self,item, handle, ix, iy):
39 gluerange = 10
40 mindist = -1;
41 minport = None
42 for p in self.ports:
43 dist = math.sqrt((ix-p.x)**2 + (iy-p.y)**2)
44 if dist < gluerange:
45 if not minport or dist<mindist:
46 mindist = dist
47 minport = p
48 return mindist, minport
49
50 def pre_update(self,context):
51 #print "PRE-UPDATE BLOCK"
52 pass
53
54 class DefaultBlockItem(BlockItem):
55 """
56 This is a 'default block' with a certain number of input and output ports
57 shown depending on the values sent to __init__. It is drawn as a simple
58 box with the input ports on the left and the output ports on the right.
59 """
60
61 def __init__(self, blockinstance):
62
63 self.blockinstance = blockinstance
64 inputs = len(blockinstance.blocktype.inputs)
65 outputs = len(blockinstance.blocktype.outputs)
66 super(DefaultBlockItem, self).__init__(64, 64)
67
68 eq = EqualsConstraint
69 bal = BalanceConstraint
70 handles = self._handles
71 h_nw = handles[NW]
72 h_ne = handles[NE]
73 h_sw = handles[SW]
74 h_se = handles[SE]
75
76 for i in range(inputs):
77 p = Port(self)
78 self.ports.append(p)
79 self._constraints.append(eq(p.x, h_nw.x))
80 self._constraints.append(bal(band=(h_nw.y, h_sw.y),v=p.y, balance=(0.5 + i)/inputs))
81
82 for i in range(outputs):
83 p = Port(self)
84 self.ports.append(p)
85 self._constraints.append(eq(p.x, h_ne.x))
86 self._constraints.append(bal(band=(h_ne.y,h_se.y),v=p.y, balance=(0.5 + i)/outputs))
87
88 def draw(self, context):
89 # draw the box itself
90 c = context.cairo
91 nw = self._handles[NW]
92 c.rectangle(nw.x, nw.y, self.width, self.height)
93 if context.hovered:
94 c.set_source_rgba(.8,.8,1, .8)
95 else:
96 c.set_source_rgba(1,1,1, .8)
97 c.fill_preserve()
98 c.set_source_rgb(0,0,0.8)
99 c.stroke()
100
101 text_center(c,self.width/2,self.height/2,self.blockinstance.name)
102
103 # now the draw the ports using the base class
104 super(DefaultBlockItem, self).draw(context)
105

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