Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
78:85bfa15c01f1 | 79:44f075fe71eb |
---|---|
130 self.block.code = self.codeEdit.toPlainText() | 130 self.block.code = self.codeEdit.toPlainText() |
131 self.close() | 131 self.close() |
132 | 132 |
133 class PortItem(QGraphicsPathItem): | 133 class PortItem(QGraphicsPathItem): |
134 """ Represents a port to a subsystem """ | 134 """ Represents a port to a subsystem """ |
135 def __init__(self, name, block, direction): | 135 def __init__(self, name, block): |
136 super(PortItem, self).__init__(block) | 136 super(PortItem, self).__init__(block) |
137 self.textItem = QGraphicsTextItem(self) | |
137 self.connection = None | 138 self.connection = None |
138 d = 10.0 | |
139 if direction == 'input': | |
140 self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])) | |
141 else: | |
142 self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])) | |
143 self.direction = direction | |
144 self.block = block | 139 self.block = block |
145 self.setCursor(QCursor(Qt.CrossCursor)) | 140 self.setCursor(QCursor(Qt.CrossCursor)) |
146 self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap)) | 141 self.setPen(QPen(Qt.blue, 2, cap=Qt.RoundCap)) |
147 self.textItem = QGraphicsTextItem(self) | |
148 self.name = name | 142 self.name = name |
149 self.posCallbacks = [] | 143 self.posCallbacks = [] |
150 self.setFlag(self.ItemSendsScenePositionChanges, True) | 144 self.setFlag(self.ItemSendsScenePositionChanges, True) |
151 def getName(self): return self.textItem.toPlainText() | 145 def getName(self): return self.textItem.toPlainText() |
152 def setName(self, name): | 146 def setName(self, name): |
153 self.textItem.setPlainText(name) | 147 self.textItem.setPlainText(name) |
154 rect = self.textItem.boundingRect() | 148 rect = self.textItem.boundingRect() |
155 lw, lh = rect.width(), rect.height() | 149 lw, lh = rect.width(), rect.height() |
156 lx = 3 if self.direction == 'input' else -3 - lw | 150 lx = 3 if type(self) is InputPort else -3 - lw |
157 self.textItem.setPos(lx, -lh / 2) | 151 self.textItem.setPos(lx, -lh / 2) |
158 name = property(getName, setName) | 152 name = property(getName, setName) |
159 def getDict(self): | 153 def getDict(self): return {'name': self.name} |
160 return {'name': self.name} | |
161 Dict = property(getDict) | 154 Dict = property(getDict) |
162 def itemChange(self, change, value): | 155 def itemChange(self, change, value): |
163 if change == self.ItemScenePositionHasChanged: | 156 if change == self.ItemScenePositionHasChanged: |
164 for cb in self.posCallbacks: | 157 for cb in self.posCallbacks: cb(value) |
165 cb(value) | |
166 return value | 158 return value |
167 return super(PortItem, self).itemChange(change, value) | 159 return super(PortItem, self).itemChange(change, value) |
160 | |
161 class OutputPort(PortItem): | |
162 def __init__(self, name, block): | |
163 super(OutputPort, self).__init__(name, block) | |
164 d = 10.0 | |
165 self.setPath(buildPath([QPointF(0.0, -d), QPointF(d, 0), QPointF(0.0, d)])) | |
168 def mousePressEvent(self, event): | 166 def mousePressEvent(self, event): |
169 if self.direction == 'output': | 167 self.scene().startConnection(self) |
170 self.scene().startConnection(self) | 168 |
171 | 169 class InputPort(PortItem): |
172 class OutputPort(PortItem): | 170 def __init__(self, name, block): |
173 # TODO: create a subclass OR make a member porttype | 171 super(InputPort, self).__init__(name, block) |
174 pass | 172 d = 10.0 |
173 self.setPath(buildPath([QPointF(-d, -d), QPointF(0, 0), QPointF(-d, d)])) | |
175 | 174 |
176 class HandleItem(QGraphicsEllipseItem): | 175 class HandleItem(QGraphicsEllipseItem): |
177 """ A handle that can be moved by the mouse """ | 176 """ A handle that can be moved by the mouse """ |
178 def __init__(self, parent=None): | 177 def __init__(self, parent=None): |
179 dx = 13.0 | 178 dx = 13.0 |
238 for view in scene.views(): | 237 for view in scene.views(): |
239 view.dScene = self.subModel | 238 view.dScene = self.subModel |
240 view.zoomAll() | 239 view.zoomAll() |
241 def newInputPort(self): | 240 def newInputPort(self): |
242 names = [i.name for i in self.inputs + self.outputs] | 241 names = [i.name for i in self.inputs + self.outputs] |
243 self.addInput(PortItem(uniqify('in', names), self, 'input')) | 242 self.addInput(InputPort(uniqify('in', names), self)) |
244 def newOutputPort(self): | 243 def newOutputPort(self): |
245 names = [i.name for i in self.inputs + self.outputs] | 244 names = [i.name for i in self.inputs + self.outputs] |
246 self.addOutput(PortItem(uniqify('out', names), self, 'output')) | 245 self.addOutput(OutputPort(uniqify('out', names), self)) |
247 def setName(self, name): self.label.setPlainText(name) | 246 def setName(self, name): self.label.setPlainText(name) |
248 def getName(self): return self.label.toPlainText() | 247 def getName(self): return self.label.toPlainText() |
249 name = property(getName, setName) | 248 name = property(getName, setName) |
250 def getDict(self): | 249 def getDict(self): |
251 d = {'x': self.scenePos().x(), 'y': self.scenePos().y()} | 250 d = {'x': self.scenePos().x(), 'y': self.scenePos().y()} |
258 def setDict(self, d): | 257 def setDict(self, d): |
259 self.name = d['name'] | 258 self.name = d['name'] |
260 self.code = d['code'] | 259 self.code = d['code'] |
261 self.setPos(d['x'], d['y']) | 260 self.setPos(d['x'], d['y']) |
262 self.sizer.setPos(d['width'], d['height']) | 261 self.sizer.setPos(d['width'], d['height']) |
263 for inp in d['inputs']: | 262 for inp in d['inputs']: self.addInput(InputPort(inp['name'], self)) |
264 self.addInput(PortItem(inp['name'], self, 'input')) | 263 for outp in d['outputs']: self.addOutput(OutputPort(outp['name'], self)) |
265 for outp in d['outputs']: | |
266 self.addOutput(PortItem(outp['name'], self, 'output')) | |
267 Dict = property(getDict, setDict) | 264 Dict = property(getDict, setDict) |
268 def addInput(self, i): | 265 def addInput(self, i): |
269 self.inputs.append(i) | 266 self.inputs.append(i) |
270 self.updateSize() | 267 self.updateSize() |
271 def addOutput(self, o): | 268 def addOutput(self, o): |
366 return mimedata | 363 return mimedata |
367 | 364 |
368 class ModelHierarchyModel(QStandardItemModel): | 365 class ModelHierarchyModel(QStandardItemModel): |
369 def __init__(self): | 366 def __init__(self): |
370 super(ModelHierarchyModel, self).__init__() | 367 super(ModelHierarchyModel, self).__init__() |
371 self.items = [] | 368 self.rootDiagram = DiagramScene() |
372 def flags(self, idx): | 369 def flags(self, idx): |
373 if idx.isValid(): | 370 if idx.isValid(): |
374 return Qt.ItemIsSelectable | Qt.ItemIsEnabled | 371 return Qt.ItemIsSelectable | Qt.ItemIsEnabled |
375 def data(self): | 372 def data(self): |
376 pass | 373 pass |
397 fromPort = self.findPort(con['fromBlock'], con['fromPort']) | 394 fromPort = self.findPort(con['fromBlock'], con['fromPort']) |
398 toPort = self.findPort(con['toBlock'], con['toPort']) | 395 toPort = self.findPort(con['toBlock'], con['toPort']) |
399 c = Connection(fromPort, toPort) | 396 c = Connection(fromPort, toPort) |
400 self.addItem(c) | 397 self.addItem(c) |
401 def getDict(self): | 398 def getDict(self): |
402 d = {} | 399 return {'blocks': [b.Dict for b in self.blocks], 'connections': [c.Dict for c in self.connections]} |
403 d['blocks'] = [b.Dict for b in self.blocks] | |
404 d['connections'] = [c.Dict for c in self.connections] | |
405 return d | |
406 Dict = property(getDict, setDict) | 400 Dict = property(getDict, setDict) |
407 def findPort(self, blockname, portname): | 401 def findPort(self, blockname, portname): |
408 block = self.findBlock(blockname) | 402 block = self.findBlock(blockname) |
409 if block: | 403 if block: |
410 for port in block.inputs + block.outputs: | 404 for port in block.inputs + block.outputs: |
411 if port.name == portname: | 405 if port.name == portname: return port |
412 return port | |
413 def findBlock(self, blockname): | 406 def findBlock(self, blockname): |
414 blocks = [item for item in self.items() if type(item) is BlockItem] | 407 for block in self.blocks: |
415 for block in blocks: | 408 if block.name == blockname: return block |
416 if block.name == blockname: | |
417 return block | |
418 def addNewBlock(self, pos, name): | 409 def addNewBlock(self, pos, name): |
419 blocknames = [item.name for item in self.items() if type(item) is BlockItem] | 410 blocknames = [item.name for item in self.blocks] |
420 b1 = BlockItem(uniqify(name, blocknames)) | 411 b1 = BlockItem(uniqify(name, blocknames)) |
421 b1.setPos(pos) | 412 b1.setPos(pos) |
422 self.addItem(b1) | 413 self.addItem(b1) |
423 def mouseMoveEvent(self, event): | 414 def mouseMoveEvent(self, event): |
424 if self.startedConnection: | 415 if self.startedConnection: |
426 self.startedConnection.setEndPos(pos) | 417 self.startedConnection.setEndPos(pos) |
427 super(DiagramScene, self).mouseMoveEvent(event) | 418 super(DiagramScene, self).mouseMoveEvent(event) |
428 def mouseReleaseEvent(self, event): | 419 def mouseReleaseEvent(self, event): |
429 if self.startedConnection: | 420 if self.startedConnection: |
430 for item in self.items(event.scenePos()): | 421 for item in self.items(event.scenePos()): |
431 if type(item) is PortItem: | 422 if type(item) is InputPort and item.connection == None: |
432 if item.direction == 'input' and item.connection == None: | 423 self.startedConnection.setToPort(item) |
433 self.startedConnection.setToPort(item) | 424 self.startedConnection = None |
434 self.startedConnection = None | 425 return |
435 return | |
436 self.startedConnection.myDelete() | 426 self.startedConnection.myDelete() |
437 self.startedConnection = None | 427 self.startedConnection = None |
438 super(DiagramScene, self).mouseReleaseEvent(event) | 428 super(DiagramScene, self).mouseReleaseEvent(event) |
439 def startConnection(self, port): | 429 def startConnection(self, port): |
440 self.startedConnection = Connection(port, None) | 430 self.startedConnection = Connection(port, None) |
515 app = QApplication(sys.argv) | 505 app = QApplication(sys.argv) |
516 main = Main() | 506 main = Main() |
517 main.show() | 507 main.show() |
518 main.resize(700, 500) | 508 main.resize(700, 500) |
519 main.editor.model = loadModel('diagram4.usd') | 509 main.editor.model = loadModel('diagram4.usd') |
520 main.editor.zoomAll() | 510 #main.editor.model = DiagramScene() |
521 app.exec_() | 511 app.exec_() |
522 | 512 |