def __init__(self, width=10, height=10): super(Element, self).__init__() self._handles = [h(strength=VERY_STRONG) for h in [Handle] * 4] handles = self._handles h_nw = handles[NW] h_ne = handles[NE] h_sw = handles[SW] h_se = handles[SE] # Share variables h_sw.pos.set_x(h_nw.pos.x) h_se.pos.set_x(h_ne.pos.x) h_ne.pos.set_y(h_nw.pos.y) h_se.pos.set_y(h_sw.pos.y) # edge of element define default element ports self._ports = [ LinePort(h_nw.pos, h_ne.pos), LinePort(h_ne.pos, h_se.pos), LinePort(h_se.pos, h_sw.pos), LinePort(h_sw.pos, h_nw.pos), ] # initialize min_x variables self.min_width, self.min_height = 10, 10 # create minimal size constraints self.constraint(left_of=(h_nw.pos, h_se.pos), delta=self._min_width) self.constraint(above=(h_nw.pos, h_se.pos), delta=self._min_height) self.width = width self.height = height
def __init__(self, id=None, model=None): super().__init__(id, model) self.bar_width = 12 ht, hb = Handle(), Handle() ht.connectable = True # TODO: need better interface for this! self._handles.append(ht) self._handles.append(hb) self.constraint(vertical=(ht.pos, hb.pos)) r = self.bar_width / 2 nw = Position((-r, 0), strength=WEAK) ne = Position((r, 0), strength=WEAK) se = Position((r, 0), strength=WEAK) sw = Position((-r, 0), strength=WEAK) self.constraint(horizontal=(sw, hb.pos)) self.constraint(horizontal=(se, hb.pos)) self._ports.append(LinePort(nw, sw)) self._ports.append(LinePort(ne, se)) self.shape = Box(style={"fill": "white"}, draw=draw_border)
def __init__(self, width=10, height=10): super(PortoBox, self).__init__(width, height) # disable default ports for p in self._ports: p.connectable = False nw = self._handles[NW] sw = self._handles[SW] ne = self._handles[NE] se = self._handles[SE] # handle for movable port self._hm = Handle(strength=WEAK) self._hm.pos = width, height / 2.0 self._handles.append(self._hm) # movable port self._ports.append(PointPort(self._hm.pos)) # keep movable port at right edge self.constraint(vertical=(self._hm.pos, ne.pos), delta=10) self.constraint(above=(ne.pos, self._hm.pos)) self.constraint(above=(self._hm.pos, se.pos)) # static point port self._sport = PointPort(Position((width / 2.0, height))) l = sw.pos, se.pos self.constraint(line=(self._sport.point, l)) self._ports.append(self._sport) # line port self._lport = LinePort(nw.pos, se.pos) self._ports.append(self._lport)
def do_split(segment, count): handles = item.handles() p0 = handles[segment].pos p1 = handles[segment + 1].pos dx, dy = p1.x - p0.x, p1.y - p0.y new_h = Handle((p0.x + dx / count, p0.y + dy / count), strength=WEAK) item.insert_handle(segment + 1, new_h) p0 = LinePort(p0, new_h.pos) p1 = LinePort(new_h.pos, p1) item.remove_port(item.ports()[segment]) item.insert_port(segment, p0) item.insert_port(segment + 1, p1) if count > 2: do_split(segment + 1, count - 1)
def __init__(self, id=None, model=None): super().__init__(id, model) h1 = Handle(connectable=True) self._handles.append(h1) d = self.dimensions() top_left = Position((d.x, d.y)) top_right = Position((d.x1, d.y)) bottom_right = Position((d.x1, d.y1)) bottom_left = Position((d.x, d.y1)) self._ports.append(LinePort(top_left, top_right)) self._ports.append(LinePort(top_right, bottom_right)) self._ports.append(LinePort(bottom_right, bottom_left)) self._ports.append(LinePort(bottom_left, top_left)) self._last_connected_side = None self.watch("subject[NamedElement].name")
def __init__(self): super(FatLine, self).__init__() self._handles.extend((Handle(), Handle())) h1, h2 = self._handles self._ports.append(LinePort(h1.pos, h2.pos)) self.constraint(vertical=(h1.pos, h2.pos)) self.constraint(above=(h1.pos, h2.pos), delta=20)
def __init__(self, connections: Connections, width: float = 10, height: float = 10, **kwargs: object) -> None: super().__init__(**kwargs) self._handles = [h(strength=VERY_STRONG) for h in [Handle] * 4] handles = self._handles h_nw = handles[NW] h_ne = handles[NE] h_sw = handles[SW] h_se = handles[SE] # edge of element define default element ports self._ports = [ LinePort(h_nw.pos, h_ne.pos), LinePort(h_ne.pos, h_se.pos), LinePort(h_se.pos, h_sw.pos), LinePort(h_sw.pos, h_nw.pos), ] # initialize min_x variables self.min_width, self.min_height = 10, 10 add = connections.add_constraint add(self, constraint(horizontal=(h_nw.pos, h_ne.pos))) add(self, constraint(horizontal=(h_sw.pos, h_se.pos))) add(self, constraint(vertical=(h_nw.pos, h_sw.pos))) add(self, constraint(vertical=(h_ne.pos, h_se.pos))) # create minimal size constraints add(self, constraint(left_of=(h_nw.pos, h_se.pos), delta=self.min_width)) add(self, constraint(above=(h_nw.pos, h_se.pos), delta=self.min_height)) self.width = width self.height = height # Trigger solver to honour width/height by SE handle pos self._handles[SE].pos.x.dirty() self._handles[SE].pos.y.dirty()
def _update_ports(self) -> None: """Update line ports. This destroys all previously created ports and should only be used when initializing the line. """ assert len(self._handles) >= 2, "Not enough segments" self._ports = [] handles = self._handles for h1, h2 in zip(handles[:-1], handles[1:]): self._ports.append(LinePort(h1.pos, h2.pos))
def __init__(self, diagram, id=None): super().__init__(diagram, id=id) self._connections = diagram.connections self.bar_width = 12 ht, hb = Handle(), Handle() ht.connectable = True self._handles = [ht, hb] self.watch_handle(ht) self.watch_handle(hb) self._connections.add_constraint(self, constraint(vertical=(ht.pos, hb.pos))) r = self.bar_width / 2 nw = Position(-r, 0, strength=WEAK) ne = Position(r, 0, strength=WEAK) se = Position(r, 0, strength=WEAK) sw = Position(-r, 0, strength=WEAK) for c in ( constraint(horizontal=(nw, ht.pos)), constraint(horizontal=(ne, ht.pos)), constraint(horizontal=(sw, hb.pos)), constraint(horizontal=(se, hb.pos)), constraint(vertical=(nw, ht.pos), delta=-r), constraint(vertical=(ne, ht.pos), delta=r), constraint(vertical=(sw, hb.pos), delta=-r), constraint(vertical=(se, hb.pos), delta=r), ): self._connections.add_constraint(self, c) self._ports = [LinePort(nw, sw), LinePort(ne, se)] self.shape = Box( style={"background-color": (1.0, 1.0, 1.0, 1.0)}, draw=draw_border )
def __init__(self, diagram, id=None): super().__init__(diagram, id) self._connections = diagram.connections h1 = Handle(connectable=True) self._handles = [h1] self.watch_handle(h1) d = self.dimensions() top_left = Position(d.x, d.y) top_right = Position(d.x1, d.y) bottom_right = Position(d.x1, d.y1) bottom_left = Position(d.x, d.y1) self._ports = [ LinePort(top_left, top_right), LinePort(top_right, bottom_right), LinePort(bottom_right, bottom_left), LinePort(bottom_left, top_left), ] self._last_connected_side = None self.watch("subject[NamedElement].name") self.update_shapes()
def __init__(self, connections, id=None, model=None): super().__init__(id=id, model=model) self._matrix = Matrix() self._matrix_i2c = Matrix() self._connections = connections h1 = Handle(connectable=True) self._handles = [h1] d = self.dimensions() top_left = Position(d.x, d.y) top_right = Position(d.x1, d.y) bottom_right = Position(d.x1, d.y1) bottom_left = Position(d.x, d.y1) self._ports = [ LinePort(top_left, top_right), LinePort(top_right, bottom_right), LinePort(bottom_right, bottom_left), LinePort(bottom_left, top_left), ] self._last_connected_side = None self.watch("subject[NamedElement].name") self.update_shapes()
def merge_segment(self, segment, count=2): """Merge two (or more) item segments. Tuple of two lists is returned, list of deleted handles and list of deleted ports. :Parameters: segment Segment number to start merging from (starting from zero). count Amount of segments to be merged (minimum 2). """ item = self.item if len(item.ports()) < 2: raise ValueError("Cannot merge line with one segment") if item.orthogonal and len(item.ports()) < 1 + count: raise ValueError("Cannot merge orthogonal line to one segment") if segment < 0 or segment >= len(item.ports()): raise ValueError("Incorrect segment") if count < 2 or segment + count > len(item.ports()): raise ValueError("Incorrect count of segments") # remove handle and ports which share position with handle deleted_handles = item.handles()[segment + 1 : segment + count] deleted_ports = item.ports()[segment : segment + count] for h in deleted_handles: item.remove_handle(h) for p in deleted_ports: item.remove_port(p) # create new port, which replaces old ports destroyed due to # deleted handle p1 = item.handles()[segment].pos p2 = item.handles()[segment + 1].pos port = LinePort(p1, p2) item.insert_port(segment, port) # force orthogonal constraints to be recreated item.update_orthogonal_constraints(item.orthogonal) self.recreate_constraints() self.model.request_update(item) return deleted_handles, deleted_ports
def _create_port(self, p1, p2): return LinePort(p1, p2)