def connectionInsertNode(self, conn, pos): newnode = self.createNode(pos) p = conn.port[1] conn.attach(1, newnode) conn.update_path() newconn = Connection(None, self.scene) newconn.attach(0, newnode) newconn.attach(1, p) newconn.update_path() return newconn
def pasteAct(self): if self.scene.selection != []: block_tr = dict() port_tr = dict() conn_list = [] dx, dy = 100, 100 for item in self.scene.selection: newitem = None if isinstance(item, Block): b = item.clone(QtCore.QPointF(dx, dy)) b.setSelected(True) block_tr[item.label.text()] = b elif isConnection(item): conn_list.append(item) elif isPort(item, 'pin') or isPort(item, 'node'): newitem = Port(None, self.scene) port_tr[item] = newitem elif isComment(item): newitem = Comment('') self.scene.addItem(newitem) # else: # print ('skipping paste of ', item.toData()) item.setSelected(False) if newitem: data = item.toData() data['x'] += dx data['y'] += dy newitem.fromData(data) newitem.setSelected(True) for c in conn_list: data = c.toData() conn = Connection(None, scene=self.scene) conn.pos[0] = QtCore.QPointF(data['x0'] + dx, data['y0'] + dy) conn.pos[1] = QtCore.QPointF(data['x1'] + dx, data['y1'] + dy) for ix, p in [(0, 'p0'), (1, 'p1')]: if p in data: # translate blocknames (blkname, pinname) = data[p] # update to new blockname if blkname in block_tr: b = block_tr[blkname] conn.attach(ix, b.ports(retDict=True)[pinname]) elif c.port[ix]: port = c.port[ix] if port in port_tr: conn.port[ix] = port_tr[port] conn.update_path()
def recreateBlock(self, item, scene=None): if scene is None: scene = self.scene pp = dict() for port in item.ports(): portname = port.label.text() pp[portname] = [] for c in port.connections: if c.port[0] == port and c.port[1] != port: pp[portname].append((0, c.port[1])) elif c.port[0] != port and c.port[1] == port: pp[portname].append((1, c.port[0])) data = item.toData() # store blk item.remove() # also removes connections # recreate with new parameters par = item.parameters prop = item.properties b = getBlock(data['libname'], data['blockname'], scene=scene, param=par, properties=prop, name=data['blockname']) b.fromData(data) b.setLabel() # restore connections to block for port in b.ports(): portname = port.label.text() if portname in pp: for (ix, p) in pp[portname]: if ix == 0: c = Connection(None, scene, port) c.attach(1, p) else: c = Connection(None, scene, p) c.attach(1, port) c.update_path()
def dataToDiagram(self, blocks, connections, nodes, comments, center=True, undo=False): errors = [] for data in blocks: prop = data['properties'] if 'properties' in data else dict() if 'parameters' in data: #getBlock(libname, blockname, parent=None, scene=None, param=dict(), name=None, flip=False) b = getBlock(data['libname'], data['blockname'], scene=self.scene, param=data['parameters'], properties=prop, errors=errors) else: b = getBlock(data['libname'], data['blockname'], scene=self.scene, properties=prop, errors=errors) if b: b.fromData(data) for item in nodes: pos = QtCore.QPointF(item['x'], item['y']) if self.scene.find_itemAt(pos): print('discarding overlapping node at x={}, y={}'.format( item['x'], item['y'])) else: p = Port(None, self.scene) p.fromData(item) for data in connections: pos = [QtCore.QPointF(data['x0'], data['y0'])] pos.append(QtCore.QPointF(data['x1'], data['y1'])) if pos[0] == pos[1]: print('discarding zero length segment x={}, y={}'.format( data['x0'], data['y0'])) else: conn = Connection(None, self.scene) for ix in [0, 1]: port = self.scene.find_itemAt(pos[ix], exclude=(Block, Connection, textItem)) if isPort(port): conn.attach(ix, port) else: conn.pos[ix] = pos[ix] print('no port at ', pos[ix]) conn.update_path() if 'label' in data: conn.label = textItem(data['label'], anchor=3, parent=conn) conn.label.setPos(conn.pos2.x(), conn.pos2.y()) if 'signalType' in data: conn.signalType = textItem(data['signalType'], anchor=3, parent=conn) conn.signalType.setPos(conn.pos2.x(), conn.pos2.y()) for data in comments: comment = Comment('') comment.fromData(data) self.scene.addItem(comment) if center: self.scene.mainw.view.centerOn(self.scene.getCenter()[0], self.scene.getCenter()[1]) if errors: error('\n'.join(errors))