# HG changeset patch # User windel # Date 1353245101 -3600 # Node ID 317a73256bf00adb985553d7fe8c993d580dac26 # Parent f3849c8c15fa768aa659b211d5746bdf657c16b6 Added code generator functions diff -r f3849c8c15fa -r 317a73256bf0 python/apps/diagrameditor.py --- a/python/apps/diagrameditor.py Sun Nov 18 13:41:51 2012 +0100 +++ b/python/apps/diagrameditor.py Sun Nov 18 14:25:01 2012 +0100 @@ -34,6 +34,9 @@ while newname in names: newname, i = name + str(i), i + 1 return newname +def indent(lines): + return [' ' + line for line in lines] + class Connection(QGraphicsPathItem): """ Implementation of a connection between blocks """ def __init__(self, fromPort=None, toPort=None): @@ -278,6 +281,14 @@ for outp in d['outputs']: self.addOutput(OutputPort(outp['name'], self)) self.subModel.Dict = d['submodel'] Dict = property(getDict, setDict) + def gencode(self): + c = ['def {0}():'.format(self.name)] + if self.code: + c += indent(self.code.split('\n')) + else: + c += indent(['pass']) + print(c) + return c def addInput(self, i): self.inputs.append(i) self.updateSize() @@ -354,6 +365,28 @@ if hasattr(self.diagram, 'containingBlock'): self.diagram = self.diagram.containingBlock.scene() self.zoomAll() + def showCode(self): + if self.model: + c = self.model.gencode() + c = '\n'.join(c) + d = QDialog() + l = QFormLayout(d) + codeview = QTextEdit() + codeview.setPlainText(c) + l.addRow('code', codeview) + runButton = QPushButton('Run') + outputview = QTextEdit() + l.addRow('Output', outputview) + l.addWidget(runButton) + def print2(txt): + txt2 = outputview.toPlainText() + outputview.setPlainText(txt2 + '\n' + txt) + def runIt(): + outputview.clear() + globs = {'print': print2} + exec(codeview.toPlainText(), globs) + runButton.clicked.connect(runIt) + d.exec_() def zoomAll(self): """ zoom to fit all items """ rect = self.diagram.itemsBoundingRect() @@ -396,6 +429,13 @@ self.rootDiagram.Dict = d self.modelReset.emit() Dict = property(lambda s: s.rootDiagram.Dict, setDict) + def gencode(self): + c = ['def topLevel():'] + c += indent(self.rootDiagram.gencode()) + c.append('print("Running model")') + c.append('topLevel()') + c.append('print("Done")') + return c def index(self, row, column, parent=None): if parent.isValid(): parent = parent.internalPointer().subModel @@ -451,6 +491,13 @@ def getDict(self): return {'blocks': [b.Dict for b in self.blocks], 'connections': [c.Dict for c in self.connections]} Dict = property(getDict, setDict) + def gencode(self): + c = [] + for b in self.blocks: + c += b.gencode() + for b in self.blocks: + c.append('{0}()'.format(b.name)) + return c def findPort(self, blockname, portname): block = self.findBlock(blockname) if block: @@ -548,6 +595,7 @@ act('Full screen', QKeySequence("F11"), self.toggleFullScreen) act('Fit in view', QKeySequence("F8"), self.editor.zoomAll) act('Go up', QKeySequence(Qt.Key_Up), self.editor.goUp) + act('Model code', QKeySequence("F7"), self.editor.showCode) def addDock(name, widget): dock = QDockWidget(name, self) dock.setObjectName(name)