# HG changeset patch # User windel # Date 1333555728 -7200 # Node ID 38ff8e178fe4ab5c6ad146f501b8be648ede40df # Parent 2b07ab2c0dc4c6adc1718d438f7b8a4b98ce7829 Fixed saving of connections diff -r 2b07ab2c0dc4 -r 38ff8e178fe4 applications/lab/diagrameditor.py --- a/applications/lab/diagrameditor.py Wed Apr 04 07:30:21 2012 +0200 +++ b/applications/lab/diagrameditor.py Wed Apr 04 18:08:48 2012 +0200 @@ -24,7 +24,6 @@ pen.setColor(Qt.blue) pen.setCapStyle(Qt.RoundCap) self.setPen(pen) - editor.diagramScene.addItem(self) self.setFromPort(fromPort) self.setToPort(toPort) def setFromPort(self, fromPort): @@ -50,7 +49,7 @@ """ if self.pos1 is None or self.pos2 is None: return - + # TODO: do not get the scene here? ds = editor.diagramScene # TODO: create pieces of lines. @@ -109,8 +108,6 @@ path.lineTo(p4) hits = stripHits(ds.items(path)) - - #print('Items:', hits) self.setPath(path) def delete(self): @@ -124,23 +121,42 @@ self.button = QPushButton('Ok', self) l = QGridLayout(self) l.addWidget(QLabel('Name:', self), 0, 0) - self.nameEdit = QLineEdit(self.block.label.toPlainText()) + self.nameEdit = QLineEdit(self.block.getName()) l.addWidget(self.nameEdit, 0, 1) l.addWidget(self.button, 1, 0) self.button.clicked.connect(self.OK) def OK(self): - self.block.label.setPlainText(self.nameEdit.text()) + self.block.setName(self.nameEdit.text()) + self.close() +# TODO: merge dialogs? + +class AddPortDialog(QDialog): + def __init__(self, block, parent = None): + super(AddPortDialog, self).__init__(parent) + self.block = block + l = QGridLayout(self) + l.addWidget(QLabel('Name:', self), 0, 0) + self.nameEdit = QLineEdit('bla') + l.addWidget(self.nameEdit, 0, 1) + self.button = QPushButton('Ok', self) + l.addWidget(self.button, 1, 0) + self.button.clicked.connect(self.OK) + + def OK(self): + name = self.nameEdit.text() + self.block.addInput(PortItem(name, self.block)) self.close() class PortItem(QGraphicsEllipseItem): """ Represents a port to a subsystem """ - def __init__(self, name, parent=None): - QGraphicsEllipseItem.__init__(self, QRectF(-6,-6,12.0,12.0), parent) + def __init__(self, name, block): + QGraphicsEllipseItem.__init__(self, QRectF(-6,-6,12.0,12.0), block) + self.block = block self.setCursor(QCursor(Qt.CrossCursor)) - # Properties: self.setBrush(QBrush(Qt.red)) - # Name: self.name = name + self.textItem = QGraphicsTextItem(name, self) + self.textItem.setPos(10, 0) # TODO self.posCallbacks = [] self.setFlag(self.ItemSendsScenePositionChanges, True) def itemChange(self, change, value): @@ -189,34 +205,33 @@ """ def __init__(self, name='Untitled', parent=None): super(BlockItem, self).__init__(parent) - w = 60.0 - h = 40.0 # Properties of the rectangle: self.setPen(QPen(Qt.blue, 2)) self.setBrush(QBrush(Qt.lightGray)) self.setFlags(self.ItemIsSelectable | self.ItemIsMovable) self.setCursor(QCursor(Qt.PointingHandCursor)) - # Label: self.label = QGraphicsTextItem(name, self) + self.name = name # Create corner for resize: self.sizer = HandleItem(self) - self.sizer.setPos(w, h) self.sizer.posChangeCallbacks.append(self.changeSize) # Connect the callback #self.sizer.setVisible(False) self.sizer.setFlag(self.sizer.ItemIsSelectable, True) # Inputs and outputs of the block: self.inputs = [] - #self.inputs.append( PortItem('a', self) ) - #self.inputs.append( PortItem('b', self) ) - #self.inputs.append( PortItem('c', self) ) self.outputs = [] - #self.outputs.append( PortItem('y', self) ) # Update size: - self.changeSize(w, h) + self.changeSize(60, 40) def editParameters(self): pd = ParameterDialog(self, self.window()) pd.exec_() + def addPort(self): + pd = AddPortDialog(self, self.window()) + pd.exec_() + def setName(self, name): + self.name = name + self.label.setPlainText(name) def addInput(self, i): self.inputs.append(i) self.updateSize() @@ -230,6 +245,8 @@ da.triggered.connect(self.delete) pa = menu.addAction('Parameters') pa.triggered.connect(self.editParameters) + pa = menu.addAction('Add port') + pa.triggered.connect(self.addPort) menu.exec_(event.screenPos()) def delete(self): @@ -242,16 +259,16 @@ if len(self.inputs) == 1: self.inputs[0].setPos(-4, h / 2) elif len(self.inputs) > 1: - y = 5 - dy = (h - 10) / (len(self.inputs) - 1) + y = 8 + dy = (h - 16) / (len(self.inputs) - 1) for inp in self.inputs: inp.setPos(-4, y) y += dy if len(self.outputs) == 1: self.outputs[0].setPos(w+4, h / 2) elif len(self.outputs) > 1: - y = 5 - dy = (h - 10) / (len(self.outputs) + 0) + y = 8 + dy = (h - 16) / (len(self.outputs) + 0) for outp in self.outputs: outp.setPos(w+4, y) y += dy @@ -360,7 +377,6 @@ testShortcut.activated.connect(self.test) zoomShortcut = QShortcut(QKeySequence("F8"), self) zoomShortcut.activated.connect(self.zoomAll) - self.loadDiagram('diagram2.usd') def test(self): self.diagramView.rotate(11) @@ -380,7 +396,7 @@ x, y = block.scenePos().x(), block.scenePos().y() rect = block.rect() w, h = rect.width(), rect.height() - blockElement.setAttribute("name", block.label.toPlainText()) + blockElement.setAttribute("name", block.name) blockElement.setAttribute("x", str(int(x))) blockElement.setAttribute("y", str(int(y))) blockElement.setAttribute("width", str(int(w))) @@ -395,12 +411,11 @@ blockElement.appendChild(portElement) modelElement.appendChild(blockElement) for connection in connections: - print(connection) connectionElement = doc.createElement("connection") fromPort = connection.fromPort.name toPort = connection.toPort.name - fromBlock = "" - toBlock = "" + fromBlock = connection.fromPort.block.name + toBlock = connection.toPort.block.name connectionElement.setAttribute("fromBlock", fromBlock) connectionElement.setAttribute("fromPort", fromPort) connectionElement.setAttribute("toBlock", toBlock) @@ -442,13 +457,30 @@ block.addOutput(outp) connectionElements = sysElement.getElementsByTagName('connection') for connectionElement in connectionElements: - print(connectionElement) + fromBlock = connectionElement.getAttribute('fromBlock') + fromPort = connectionElement.getAttribute('fromPort') + toBlock = connectionElement.getAttribute('toBlock') + toPort = connectionElement.getAttribute('toPort') + fromPort = self.findPort(fromBlock, fromPort) + toPort = self.findPort(toBlock, toPort) + connection = Connection(fromPort, toPort) + self.diagramScene.addItem(connection) + self.zoomAll() + def findPort(self, blockname, portname): + items = self.diagramScene.items() + blocks = [item for item in items if type(item) is BlockItem] + for block in [b for b in blocks if b.name == blockname]: + for port in block.inputs + block.outputs: + if port.name == portname: + return port + def toggleFullScreen(self): self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) def startConnection(self, port): self.startedConnection = Connection(port, None) + self.diagramScene.addItem(self.startedConnection) def zoomAll(self): """ zoom to fit all items """ rect = self.diagramScene.itemsBoundingRect() @@ -478,7 +510,9 @@ app = QApplication(sys.argv) global editor editor = DiagramEditor() + editor.loadDiagram('diagram2.usd') editor.show() editor.resize(700, 500) + editor.zoomAll() app.exec_()