Mercurial > lcfOS
diff python/apps/diagrameditor.py @ 79:44f075fe71eb
Changed to use inputport and outputport
author | windel |
---|---|
date | Tue, 13 Nov 2012 08:16:59 +0100 |
parents | 85bfa15c01f1 |
children | d1925eb3bbd5 |
line wrap: on
line diff
--- a/python/apps/diagrameditor.py Mon Nov 12 20:25:41 2012 +0100 +++ b/python/apps/diagrameditor.py Tue Nov 13 08:16:59 2012 +0100 @@ -132,19 +132,13 @@ class PortItem(QGraphicsPathItem): """ Represents a port to a subsystem """ - def __init__(self, name, block, direction): + def __init__(self, name, block): super(PortItem, self).__init__(block) + self.textItem = QGraphicsTextItem(self) self.connection = None - d = 10.0 - if direction == 'input': - self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])) - else: - self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])) - self.direction = direction self.block = block self.setCursor(QCursor(Qt.CrossCursor)) self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap)) - self.textItem = QGraphicsTextItem(self) self.name = name self.posCallbacks = [] self.setFlag(self.ItemSendsScenePositionChanges, True) @@ -153,25 +147,30 @@ self.textItem.setPlainText(name) rect = self.textItem.boundingRect() lw, lh = rect.width(), rect.height() - lx = 3 if self.direction == 'input' else -3 - lw + lx = 3 if type(self) is InputPort else -3 - lw self.textItem.setPos(lx, -lh / 2) name = property(getName, setName) - def getDict(self): - return {'name': self.name} + def getDict(self): return {'name': self.name} Dict = property(getDict) def itemChange(self, change, value): if change == self.ItemScenePositionHasChanged: - for cb in self.posCallbacks: - cb(value) + for cb in self.posCallbacks: cb(value) return value return super(PortItem, self).itemChange(change, value) - def mousePressEvent(self, event): - if self.direction == 'output': - self.scene().startConnection(self) class OutputPort(PortItem): - # TODO: create a subclass OR make a member porttype - pass + def __init__(self, name, block): + super(OutputPort, self).__init__(name, block) + d = 10.0 + self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])) + def mousePressEvent(self, event): + self.scene().startConnection(self) + +class InputPort(PortItem): + def __init__(self, name, block): + super(InputPort, self).__init__(name, block) + d = 10.0 + self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])) class HandleItem(QGraphicsEllipseItem): """ A handle that can be moved by the mouse """ @@ -240,10 +239,10 @@ view.zoomAll() def newInputPort(self): names = [i.name for i in self.inputs + self.outputs] - self.addInput(PortItem(uniqify('in', names), self, 'input')) + self.addInput(InputPort(uniqify('in', names), self)) def newOutputPort(self): names = [i.name for i in self.inputs + self.outputs] - self.addOutput(PortItem(uniqify('out', names), self, 'output')) + self.addOutput(OutputPort(uniqify('out', names), self)) def setName(self, name): self.label.setPlainText(name) def getName(self): return self.label.toPlainText() name = property(getName, setName) @@ -260,10 +259,8 @@ self.code = d['code'] self.setPos(d['x'], d['y']) self.sizer.setPos(d['width'], d['height']) - for inp in d['inputs']: - self.addInput(PortItem(inp['name'], self, 'input')) - for outp in d['outputs']: - self.addOutput(PortItem(outp['name'], self, 'output')) + for inp in d['inputs']: self.addInput(InputPort(inp['name'], self)) + for outp in d['outputs']: self.addOutput(OutputPort(outp['name'], self)) Dict = property(getDict, setDict) def addInput(self, i): self.inputs.append(i) @@ -368,7 +365,7 @@ class ModelHierarchyModel(QStandardItemModel): def __init__(self): super(ModelHierarchyModel, self).__init__() - self.items = [] + self.rootDiagram = DiagramScene() def flags(self, idx): if idx.isValid(): return Qt.ItemIsSelectable | Qt.ItemIsEnabled @@ -399,24 +396,18 @@ c = Connection(fromPort, toPort) self.addItem(c) def getDict(self): - d = {} - d['blocks'] = [b.Dict for b in self.blocks] - d['connections'] = [c.Dict for c in self.connections] - return d + return {'blocks': [b.Dict for b in self.blocks], 'connections': [c.Dict for c in self.connections]} Dict = property(getDict, setDict) def findPort(self, blockname, portname): block = self.findBlock(blockname) if block: for port in block.inputs + block.outputs: - if port.name == portname: - return port + if port.name == portname: return port def findBlock(self, blockname): - blocks = [item for item in self.items() if type(item) is BlockItem] - for block in blocks: - if block.name == blockname: - return block + for block in self.blocks: + if block.name == blockname: return block def addNewBlock(self, pos, name): - blocknames = [item.name for item in self.items() if type(item) is BlockItem] + blocknames = [item.name for item in self.blocks] b1 = BlockItem(uniqify(name, blocknames)) b1.setPos(pos) self.addItem(b1) @@ -428,11 +419,10 @@ def mouseReleaseEvent(self, event): if self.startedConnection: for item in self.items(event.scenePos()): - if type(item) is PortItem: - if item.direction == 'input' and item.connection == None: - self.startedConnection.setToPort(item) - self.startedConnection = None - return + if type(item) is InputPort and item.connection == None: + self.startedConnection.setToPort(item) + self.startedConnection = None + return self.startedConnection.myDelete() self.startedConnection = None super(DiagramScene, self).mouseReleaseEvent(event) @@ -517,6 +507,6 @@ main.show() main.resize(700, 500) main.editor.model = loadModel('diagram4.usd') - main.editor.zoomAll() + #main.editor.model = DiagramScene() app.exec_()