Mercurial > lcfOS
changeset 74:f506f6b74697
Cleanup
author | windel |
---|---|
date | Sun, 11 Nov 2012 10:49:46 +0100 |
parents | 191d70a0ed52 |
children | ba11d99b1d4a |
files | python/apps/diagrameditor.py |
diffstat | 1 files changed, 30 insertions(+), 62 deletions(-) [+] |
line wrap: on
line diff
--- a/python/apps/diagrameditor.py Sat Nov 10 11:53:55 2012 +0100 +++ b/python/apps/diagrameditor.py Sun Nov 11 10:49:46 2012 +0100 @@ -19,12 +19,19 @@ path.lineTo(pt) return path +def equalSpace(n, l): + offset = 15 + if n == 1: + return [l / 2] + elif n > 1: + return [offset + (l - offset*2)/(n - 1)*i for i in range(n)] + return [] + class Connection(QGraphicsPathItem): """ Implementation of a connection between blocks """ def __init__(self, fromPort, toPort): super(Connection, self).__init__() - self.pos1, self.pos2 = None, None - self.fromPort, self.toPort = None, None + self.pos2 = self.fromPort = self.toPort = None self.setFlag(self.ItemIsSelectable, True) self.setFlag(self.ItemClipsToShape, True) pen = QPen(Qt.blue, 2) @@ -39,7 +46,7 @@ self.setToPort(toPort) def mouseDoubleClickEvent(self, event): pos = event.scenePos() - pts = [self.pos1] + [v.pos() for v in self.vias] + [self.pos2] + pts = [self.getPos1()] + [v.pos() for v in self.vias] + [self.pos2] idx = 0 tidx = 0 for p1, p2 in zip(pts[0:-1], pts[1:]): @@ -70,7 +77,6 @@ self.setFromPort(None) self.setToPort(None) scene.removeItem(self) - def setFromPort(self, fromPort): if self.fromPort: self.fromPort.posCallbacks.remove(self.setBeginPos) @@ -78,7 +84,7 @@ self.fromPort = fromPort if self.fromPort: self.fromPort.connection = self - self.setBeginPos(fromPort.scenePos()) + self.updateLineStukken() self.fromPort.posCallbacks.append(self.setBeginPos) def setToPort(self, toPort): if self.toPort: @@ -89,8 +95,10 @@ self.setEndPos(toPort.scenePos()) self.toPort.connection = self self.toPort.posCallbacks.append(self.setEndPos) + def getPos1(self): + if self.fromPort: + return self.fromPort.scenePos() def setBeginPos(self, pos1): - self.pos1 = pos1 self.updateLineStukken() def setEndPos(self, endpos): self.pos2 = endpos @@ -107,9 +115,10 @@ This algorithm determines the optimal routing of all signals. TODO: implement nice automatic line router """ - if self.pos1 is None or self.pos2 is None: + pos1 = self.getPos1() + if pos1 is None or self.pos2 is None: return - pts = [self.pos1] + [v.pos() for v in self.vias] + [self.pos2] + pts = [pos1] + [v.pos() for v in self.vias] + [self.pos2] self.arrowhead.setPos(self.pos2) if pts[-1].x() < pts[-2].x(): self.arrowhead.setRotation(-90) @@ -164,7 +173,6 @@ pen = QPen(Qt.blue, 2) pen.setCapStyle(Qt.RoundCap) self.setPen(pen) - self.name = name self.textItem = QGraphicsTextItem(name, self) self.setName(name) self.posCallbacks = [] @@ -209,9 +217,6 @@ """ Move function without moving the other selected elements """ p = self.mapToParent(event.pos()) self.setPos(p) - def mySetPos(self, p): - # TODO: use this instead of itemChange? - self.setPos(p) def itemChange(self, change, value): if change == self.ItemPositionChange: for cb in self.posChangeCallbacks: @@ -264,12 +269,11 @@ self.buttonItemAddOutput = QGraphicsProxyWidget(self) self.buttonItemAddOutput.setWidget(button) self.buttonItemAddOutput.setVisible(False) - # Inputs and outputs of the block: self.inputs = [] self.outputs = [] # Update size: - self.sizer.mySetPos(QPointF(60, 40)) # This is a better resize function + self.sizer.setPos(QPointF(60, 40)) # This is a better resize function def editParameters(self): pd = ParameterDialog(self, self.window()) pd.exec_() @@ -290,7 +294,6 @@ def addOutput(self, o): self.outputs.append(o) self.updateSize() - def contextMenuEvent(self, event): menu = QMenu() pa = menu.addAction('Parameters') @@ -302,54 +305,27 @@ child.setVisible(value) return super(BlockItem, self).itemChange(change, value) def myDelete(self): - # TODO: move the below to blockitem: scene = self.scene() if scene: for p in self.inputs + self.outputs: if p.connection: p.connection.myDelete() scene.removeItem(self) - def updateSize(self): rect = self.rect() h, w = rect.height(), rect.width() self.buttonItemAddInput.setPos(0, h + 4) self.buttonItemAddOutput.setPos(w+10, h+4) - for inp in self.inputs: - inp.setX(0.0) - for inp in self.outputs: - inp.setX(w) - if len(self.inputs) == 1: - self.inputs[0].setPos(0.0, h / 2) - elif len(self.inputs) > 1: - y = 15 - dy = (h - 30) / (len(self.inputs) - 1) - for inp in self.inputs: - inp.setPos(0.0, y) - y += dy - if len(self.outputs) == 1: - self.outputs[0].setPos(w, h / 2) - elif len(self.outputs) > 1: - y = 15 - dy = (h - 30) / (len(self.outputs) - 1) - for outp in self.outputs: - outp.setPos(w, y) - y += dy - + for inp, y in zip(self.inputs, equalSpace(len(self.inputs), h)): + inp.setPos(0.0, y) + for outp, y in zip(self.outputs, equalSpace(len(self.outputs), h)): + outp.setPos(w, y) def changeSize(self, p): - """ Resize block function """ - w, h = p.x(), p.y() - # Limit the block size: - h = 20 if h < 20 else h - w = 40 if w < 40 else w + h = 20 if p.y() < 20 else p.y() + w = 40 if p.x() < 40 else p.x() self.setRect(0.0, 0.0, w, h) - # center label: rect = self.label.boundingRect() - lw, lh = rect.width(), rect.height() - lx = (w - lw) / 2 - ly = (h - lh) / 2 - self.label.setPos(lx, ly) - # Update port positions: + self.label.setPos((w - rect.width()) / 2, (h - rect.height()) / 2) self.updateSize() return QPointF(w, h) @@ -377,7 +353,6 @@ def test(self): self.rotate(30) self.zoomAll() - def wheelEvent(self, event): pos = event.pos() posbefore = self.mapToScene(pos) @@ -415,12 +390,10 @@ def __init__(self, parent=None): super(DiagramScene, self).__init__(parent) self.startedConnection = None - def saveDiagram(self, filename): items = self.items() blocks = [item for item in items if type(item) is BlockItem] connections = [item for item in items if type(item) is Connection] - doc = md.Document() modelElement = doc.createElement('system') doc.appendChild(modelElement) @@ -540,12 +513,12 @@ super(DiagramScene, self).mouseMoveEvent(event) def mouseReleaseEvent(self, event): if self.startedConnection: - items = self.items(event.scenePos()) - for item in items: + for item in self.items(event.scenePos()): if type(item) is PortItem: - self.startedConnection.setToPort(item) - self.startedConnection = None - return + if item.direction == 'input' and item.connection == None: + self.startedConnection.setToPort(item) + self.startedConnection = None + return self.startedConnection.myDelete() self.startedConnection = None super(DiagramScene, self).mouseReleaseEvent(event) @@ -555,7 +528,6 @@ self.startedConnection.setEndPos(pos) self.addItem(self.startedConnection) def deleteItems(self): - # Try to delete all currently selected items: for item in list(self.selectedItems()): item.myDelete() @@ -600,14 +572,11 @@ act('Load', QKeySequence.Open, self.editor.load) act('Full screen', QKeySequence("F11"), self.toggleFullScreen) act('Fit in view', QKeySequence("F8"), self.editor.zoomAll) - self.library = LibraryWidget() libraryDock = QDockWidget('Library', self) libraryDock.setWidget(self.library) self.addDockWidget(Qt.LeftDockWidgetArea, libraryDock) - self.editor.loadDiagram('diagram2.usd') - def toggleFullScreen(self): self.setWindowState(self.windowState() ^ Qt.WindowFullScreen) self.editor.zoomAll() @@ -616,7 +585,6 @@ if sys.version_info.major != 3: print('Please use python 3.x') sys.exit(1) - app = QApplication(sys.argv) main = Main() main.show()