code cleanup

This commit is contained in:
Luke Campagnola 2017-09-15 09:00:59 -07:00
parent fedecc5808
commit 698f37bd10

View File

@ -166,6 +166,8 @@ class Flowchart(Node):
n[oldName].rename(newName) n[oldName].rename(newName)
def createNode(self, nodeType, name=None, pos=None): def createNode(self, nodeType, name=None, pos=None):
"""Create a new Node and add it to this flowchart.
"""
if name is None: if name is None:
n = 0 n = 0
while True: while True:
@ -179,6 +181,10 @@ class Flowchart(Node):
return node return node
def addNode(self, node, name, pos=None): def addNode(self, node, name, pos=None):
"""Add an existing Node to this flowchart.
See also: createNode()
"""
if pos is None: if pos is None:
pos = [0, 0] pos = [0, 0]
if type(pos) in [QtCore.QPoint, QtCore.QPointF]: if type(pos) in [QtCore.QPoint, QtCore.QPointF]:
@ -197,6 +203,8 @@ class Flowchart(Node):
self.sigChartChanged.emit(self, 'add', node) self.sigChartChanged.emit(self, 'add', node)
def removeNode(self, node): def removeNode(self, node):
"""Remove a Node from this flowchart.
"""
node.close() node.close()
def nodeClosed(self, node): def nodeClosed(self, node):
@ -234,7 +242,6 @@ class Flowchart(Node):
term2 = self.internalTerminal(term2) term2 = self.internalTerminal(term2)
term1.connectTo(term2) term1.connectTo(term2)
def process(self, **args): def process(self, **args):
""" """
Process data through the flowchart, returning the output. Process data through the flowchart, returning the output.
@ -326,7 +333,6 @@ class Flowchart(Node):
#print "DEPS:", deps #print "DEPS:", deps
## determine correct node-processing order ## determine correct node-processing order
#deps[self] = []
order = fn.toposort(deps) order = fn.toposort(deps)
#print "ORDER1:", order #print "ORDER1:", order
@ -350,7 +356,6 @@ class Flowchart(Node):
if lastNode is None or ind > lastInd: if lastNode is None or ind > lastInd:
lastNode = n lastNode = n
lastInd = ind lastInd = ind
#tdeps[t] = lastNode
if lastInd is not None: if lastInd is not None:
dels.append((lastInd+1, t)) dels.append((lastInd+1, t))
dels.sort(key=lambda a: a[0], reverse=True) dels.sort(key=lambda a: a[0], reverse=True)
@ -406,26 +411,24 @@ class Flowchart(Node):
else: else:
self.sigStateChanged.emit() self.sigStateChanged.emit()
def chartGraphicsItem(self): def chartGraphicsItem(self):
"""Return the graphicsItem which displays the internals of this flowchart. """Return the graphicsItem that displays the internal nodes and
(graphicsItem() still returns the external-view item)""" connections of this flowchart.
#return self._chartGraphicsItem
Note that the similar method `graphicsItem()` is inherited from Node
and returns the *external* graphical representation of this flowchart."""
return self.viewBox return self.viewBox
def widget(self): def widget(self):
"""Return the control widget for this flowchart.
This widget provides GUI access to the parameters for each node and a
graphical representation of the flowchart.
"""
if self._widget is None: if self._widget is None:
self._widget = FlowchartCtrlWidget(self) self._widget = FlowchartCtrlWidget(self)
self.scene = self._widget.scene() self.scene = self._widget.scene()
self.viewBox = self._widget.viewBox() self.viewBox = self._widget.viewBox()
#self._scene = QtGui.QGraphicsScene()
#self._widget.setScene(self._scene)
#self.scene.addItem(self.chartGraphicsItem())
#ci = self.chartGraphicsItem()
#self.viewBox.addItem(ci)
#self.viewBox.autoRange()
return self._widget return self._widget
def listConnections(self): def listConnections(self):
@ -438,10 +441,11 @@ class Flowchart(Node):
return conn return conn
def saveState(self): def saveState(self):
"""Return a serializable data structure representing the current state of this flowchart.
"""
state = Node.saveState(self) state = Node.saveState(self)
state['nodes'] = [] state['nodes'] = []
state['connects'] = [] state['connects'] = []
#state['terminals'] = self.saveTerminals()
for name, node in self._nodes.items(): for name, node in self._nodes.items():
cls = type(node) cls = type(node)
@ -461,6 +465,8 @@ class Flowchart(Node):
return state return state
def restoreState(self, state, clear=False): def restoreState(self, state, clear=False):
"""Restore the state of this flowchart from a previous call to `saveState()`.
"""
self.blockSignals(True) self.blockSignals(True)
try: try:
if clear: if clear:
@ -470,7 +476,6 @@ class Flowchart(Node):
nodes.sort(key=lambda a: a['pos'][0]) nodes.sort(key=lambda a: a['pos'][0])
for n in nodes: for n in nodes:
if n['name'] in self._nodes: if n['name'] in self._nodes:
#self._nodes[n['name']].graphicsItem().moveBy(*n['pos'])
self._nodes[n['name']].restoreState(n['state']) self._nodes[n['name']].restoreState(n['state'])
continue continue
try: try:
@ -478,7 +483,6 @@ class Flowchart(Node):
node.restoreState(n['state']) node.restoreState(n['state'])
except: except:
printExc("Error creating node %s: (continuing anyway)" % n['name']) printExc("Error creating node %s: (continuing anyway)" % n['name'])
#node.graphicsItem().moveBy(*n['pos'])
self.inputNode.restoreState(state.get('inputNode', {})) self.inputNode.restoreState(state.get('inputNode', {}))
self.outputNode.restoreState(state.get('outputNode', {})) self.outputNode.restoreState(state.get('outputNode', {}))
@ -492,55 +496,52 @@ class Flowchart(Node):
print(self._nodes[n2].terminals) print(self._nodes[n2].terminals)
printExc("Error connecting terminals %s.%s - %s.%s:" % (n1, t1, n2, t2)) printExc("Error connecting terminals %s.%s - %s.%s:" % (n1, t1, n2, t2))
finally: finally:
self.blockSignals(False) self.blockSignals(False)
self.sigChartLoaded.emit() self.sigChartLoaded.emit()
self.outputChanged() self.outputChanged()
self.sigStateChanged.emit() self.sigStateChanged.emit()
#self.sigOutputChanged.emit()
def loadFile(self, fileName=None, startDir=None): def loadFile(self, fileName=None, startDir=None):
"""Load a flowchart (*.fc) file.
"""
if fileName is None: if fileName is None:
if startDir is None: if startDir is None:
startDir = self.filePath startDir = self.filePath
if startDir is None: if startDir is None:
startDir = '.' startDir = '.'
self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Flowchart (*.fc)") self.fileDialog = FileDialog(None, "Load Flowchart..", startDir, "Flowchart (*.fc)")
#self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
#self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
self.fileDialog.show() self.fileDialog.show()
self.fileDialog.fileSelected.connect(self.loadFile) self.fileDialog.fileSelected.connect(self.loadFile)
return return
## NOTE: was previously using a real widget for the file dialog's parent, but this caused weird mouse event bugs.. ## NOTE: was previously using a real widget for the file dialog's parent, but this caused weird mouse event bugs..
#fileName = QtGui.QFileDialog.getOpenFileName(None, "Load Flowchart..", startDir, "Flowchart (*.fc)")
fileName = unicode(fileName) fileName = unicode(fileName)
state = configfile.readConfigFile(fileName) state = configfile.readConfigFile(fileName)
self.restoreState(state, clear=True) self.restoreState(state, clear=True)
self.viewBox.autoRange() self.viewBox.autoRange()
#self.emit(QtCore.SIGNAL('fileLoaded'), fileName)
self.sigFileLoaded.emit(fileName) self.sigFileLoaded.emit(fileName)
def saveFile(self, fileName=None, startDir=None, suggestedFileName='flowchart.fc'): def saveFile(self, fileName=None, startDir=None, suggestedFileName='flowchart.fc'):
"""Save this flowchart to a .fc file
"""
if fileName is None: if fileName is None:
if startDir is None: if startDir is None:
startDir = self.filePath startDir = self.filePath
if startDir is None: if startDir is None:
startDir = '.' startDir = '.'
self.fileDialog = FileDialog(None, "Save Flowchart..", startDir, "Flowchart (*.fc)") self.fileDialog = FileDialog(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
#self.fileDialog.setFileMode(QtGui.QFileDialog.AnyFile)
self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave) self.fileDialog.setAcceptMode(QtGui.QFileDialog.AcceptSave)
#self.fileDialog.setDirectory(startDir)
self.fileDialog.show() self.fileDialog.show()
self.fileDialog.fileSelected.connect(self.saveFile) self.fileDialog.fileSelected.connect(self.saveFile)
return return
#fileName = QtGui.QFileDialog.getSaveFileName(None, "Save Flowchart..", startDir, "Flowchart (*.fc)")
fileName = unicode(fileName) fileName = unicode(fileName)
configfile.writeConfigFile(self.saveState(), fileName) configfile.writeConfigFile(self.saveState(), fileName)
self.sigFileSaved.emit(fileName) self.sigFileSaved.emit(fileName)
def clear(self): def clear(self):
"""Remove all nodes from this flowchart except the original input/output nodes.
"""
for n in list(self._nodes.values()): for n in list(self._nodes.values()):
if n is self.inputNode or n is self.outputNode: if n is self.inputNode or n is self.outputNode:
continue continue
@ -553,18 +554,15 @@ class Flowchart(Node):
self.inputNode.clearTerminals() self.inputNode.clearTerminals()
self.outputNode.clearTerminals() self.outputNode.clearTerminals()
#class FlowchartGraphicsItem(QtGui.QGraphicsItem):
class FlowchartGraphicsItem(GraphicsObject): class FlowchartGraphicsItem(GraphicsObject):
def __init__(self, chart): def __init__(self, chart):
#print "FlowchartGraphicsItem.__init__"
#QtGui.QGraphicsItem.__init__(self)
GraphicsObject.__init__(self) GraphicsObject.__init__(self)
self.chart = chart ## chart is an instance of Flowchart() self.chart = chart ## chart is an instance of Flowchart()
self.updateTerminals() self.updateTerminals()
def updateTerminals(self): def updateTerminals(self):
#print "FlowchartGraphicsItem.updateTerminals"
self.terminals = {} self.terminals = {}
bounds = self.boundingRect() bounds = self.boundingRect()
inp = self.chart.inputs() inp = self.chart.inputs()
@ -760,6 +758,7 @@ class FlowchartCtrlWidget(QtGui.QWidget):
item = self.items[node] item = self.items[node]
self.ui.ctrlList.setCurrentItem(item) self.ui.ctrlList.setCurrentItem(item)
class FlowchartWidget(dockarea.DockArea): class FlowchartWidget(dockarea.DockArea):
"""Includes the actual graphical flowchart and debugging interface""" """Includes the actual graphical flowchart and debugging interface"""
def __init__(self, chart, ctrl): def __init__(self, chart, ctrl):