from gaphas.item import Element from gaphas.constraint import LineConstraint, LessThanConstraint, EqualsConstraint, Constraint, _update, BalanceConstraint from gaphas.item import Line, SW, NE, NW, SE, Element, Handle from gaphas.util import * from port import * class BlockItem(Element): """ This is an ASCEND 'block' in the canvas-based modeller. The block will have sets of input and output ports to which connector lines can be 'glued'. The block will also have a corresponding ASCEND MODEL type, and a name which will be used in ASCEND to refer to this block. Each of the ports will be special visual elements, but note that these are not 'handles', because they can not be used to resize/modify the element. """ def __init__(self, width=64, height=64): self.ports = [] super(BlockItem, self).__init__(width, height) def draw(self, context): #print 'Box.draw', self c = context.cairo phalfsize = 3 for p in self.ports: c.rectangle(p.x - phalfsize, p.y - phalfsize, 2*phalfsize, 2*phalfsize) if p.connected_to is None: c.set_source_rgba(0.8,0.8,1, 0.8) else: c.set_source_rgba(1,0,0,1) c.fill_preserve() c.set_source_rgb(0.8,0.8,0) c.stroke() def glue(self,item, handle, ix, iy): gluerange = 10 mindist = -1; minport = None for p in self.ports: dist = math.sqrt((ix-p.x)**2 + (iy-p.y)**2) if dist < gluerange: if not minport or dist