class TreeItemController(QObject): """ the Controller for the TreeView """ def __init__(self, distributedObjects, name, view, model, addDockWidget): """ Constructor <br> Create a TreeView, a TreeVWFactory and a VariableList <br> Listens to the following Signals: SignalProxy::AddTree(QString), SignalProxy::insertDockWidgets() and SignalProxy::cleanupModels() @param distributedObjects distributedobjects.DistributedObjects, the DistributedObjects-Instance """ QObject.__init__(self) self.distributedObjects = distributedObjects self.name = name self.vwFactory = TreeVWFactory() self.model = model(self, self.distributedObjects) self.view = view self.view.controller = self self.view.setModel(self.model) self.variableList = VariableList(self.vwFactory, self.distributedObjects) self.distributedObjects.signalProxy.cleanupModels.connect(self.clear) if addDockWidget: self.distributedObjects.mainwindow.insertDockWidget(self.view, name, Qt.BottomDockWidgetArea, True) def clear(self): """ clears the TreeView and the VariableList <br> this function is connected to the signal SignalProxy::cleanupModels() """ # clear lists self.variableList.clear() self.model.clear() def add(self, vw): vw.setParent(self.model.root) # add children self.model.root.addChild(vw) self.model.addVar(vw)
class DataGraphController(QObject): """ the Controller for the DataGraph """ def __init__(self, distributedObjects): QObject.__init__(self) self.distributedObjects = distributedObjects self.signalProxy = distributedObjects.signalProxy self._view = DataGraphView(None) self.variableList = VariableList(DataGraphVariableFactory, self.distributedObjects) # register with session manager to save Graph self.signalProxy.registerWithSessionManager.emit(self, "Graph") # connect signals self.signalProxy.cleanupModels.connect(self.clearDataGraph) self.distributedObjects.mainwindow.insertDockWidget(self._view, "Graph", Qt.LeftDockWidgetArea, True, QIcon(":/icons/images/datagraph.png")) def addWatch(self, watch, xPos=0, yPos=0): """ adds `watch' to the DataGraph @param watch str, variable to add @param xPos int, x coordinate of the desired position @param yPos int, y coordinate of the desired position """ try: var = self.variableList.addVarByName(watch) var.setData(self.distributedObjects) var.createView() var.getView().render() var.setXPos(xPos) var.setYPos(yPos) self._view.addItem(var.getView()) return var except VariableNotFoundException: return None def addDereferencedPointer(self, source, name): target = self.addWatch(str(name)) self.addPointer(source.getView(), target.getView()) def removeVar(self, var): self.variableList.removeVar(var) self._view.removeItem(var.getView()) def addPointer(self, fromView, toView): """ fromView and toView are QGraphicsWebViews @param fromView datagraph.htmlvariableview.HtmlVariableView, starting point of the Pointer @param toView datagraph.htmlvariableview.HtmlVariableView, end point of the Pointer """ self._view.addItem(Pointer(None, fromView, toView, self.distributedObjects)) def removePointer(self, pointer): """ removes the given pointer from the DataGraphView and the PointerList @param pointer datagraph.pointer.Pointer, pointer to remove """ self._view.removeItem(pointer) def clearDataGraph(self): """ clears the DataGraphView and the VariableList <br> this function is connected to the signal SignalProxy::cleanupModels() """ self.variableList.clear() self._view.clear()
class DataGraphController(QObject): """ the Controller for the DataGraph """ def __init__(self, distributedObjects): """ Constructor <br> Creates a DataGraphView, a DataGraphVWFactory and a VariableList <br> Listens to the following Signals: SignalProxy::insertDockWidgets() and SignalProxy::cleanupModels() @param distributedObjects distributedobjects.DistributedObjects, the DistributedObjects-Instance """ QObject.__init__(self) # controllers ## @var distributedObjects # distributedobjects.DistributedObjects, the DistributedObjects-Instance self.distributedObjects = distributedObjects ## @var signalProxy # signalproxy.SignalProxy, the SignalProxy-Instance from the DistributedObjects self.signalProxy = distributedObjects.signalProxy ## @var debugController # debugcontroller.DebugController, the DebugController-Instance from the DistributedObjects self.debugController = distributedObjects.debugController ## @var variablePool # variables.variablepool.VariablePool, the variablePool-Instance from the DistributedObjects self.variablePool = distributedObjects.variablePool # views ## @var data_graph_view # datagraph.datagraphview.DataGraphView, private, self-created DataGraphView <br> # GUI-Element that shows the DataGraphView self.data_graph_view = DataGraphView(None, self) # models ## @var vwFactory # datagraph.datagraphvwfactory.DataGraphVWFactory, private, self-created DataGraphVWFactory self.vwFactory = DataGraphVWFactory(self.distributedObjects) ## @var variableList # variables.variablelist.VariableList, private, self-created VariableList self.variableList = VariableList(self.vwFactory, self.distributedObjects) self.pointerList = [] #register with session manager to save Graph self.signalProxy.emitRegisterWithSessionManager(self, "Graph") # connect signals #QObject.connect(self.variableList, SIGNAL('reset()'), self.repaintDataGraph) QObject.connect(self.signalProxy, SIGNAL('insertDockWidgets()'), self.insertDockWidgets) QObject.connect(self.signalProxy, SIGNAL('cleanupModels()'), self.clearDataGraph) def insertDockWidgets(self): """ adds the Datagraph-DockWidget to the GUI <br> this function is connected to the signal SignalProxy::insertDockWidgets() """ self.dataGraphDock = QDockWidget("Graph") self.dataGraphDock.setObjectName("DataGraphView") self.dataGraphDock.setWidget(self.data_graph_view) self.signalProxy.addDockWidget(Qt.LeftDockWidgetArea, self.dataGraphDock, True) def addWatch(self, watch, xPos=0, yPos=0): """ adds the Variable watch to the VariableList and its wrapper to the DataGraph @param watch variables.variable.Variable, the Variable to watch to add @param xPos Integer, the X-Coordinate of the Position where to add the Variable @param yPos Integer, the Y-Coordinate of the Position where to add the Variable """ varWrapper = self.variableList.addVarByName(watch) self.addVar(varWrapper, xPos, yPos, False) return varWrapper def addVar(self, varWrapper, xPos=0, yPos=0, addVarToList=True): """ adds the given VariableWrapper varWrapper to the DataGraph and - if addVarToList is true - also to the VariableList @param varWrapper variables.variablewrapper.VariableWrapper, the VariableWrapper to add @param xPos Integer, the X-Coordinate of the Position where to add the VariableWrapper @param yPos Integer, the Y-Coordinate of the Position where to add the VariableWrapper @param addVarToList Boolean, tells if varWrapper should be added to the VariableList too """ varWrapper.createView() try: varWrapper.getView().render() except: from mako import exceptions logging.error("Caught exception while rendering template: %s", exceptions.text_error_template().render()) varWrapper.setXPos(xPos) varWrapper.setYPos(yPos) self.data_graph_view.addItem(varWrapper.getView()) if addVarToList: self.variableList.addVar(varWrapper) QObject.connect(varWrapper, SIGNAL('replace(PyQt_PyObject, PyQt_PyObject)'), self.replaceVariable) def replaceVariable(self, pendingVar, newVar): """ replaces existing variable in list with new one @param pendingVar variables.variable.Variable, the pending Variable to replace with newVar @param newVar variables.variable.Variable, the new Variable pendingVar is replaced with """ self.removeVar(pendingVar) newVW = newVar.makeWrapper(self.vwFactory) self.addVar(newVW, pendingVar.getXPos(), pendingVar.getYPos()) #for pointer in pendingVar.getView().getIncomingPointers(): # self.addPointer(pointer.getFromView(), newVW.getView()) #for pointer in pendingVar.getView().getOutgoingPointers(): # self.addPointer(newVW.getView(), pointer.getToView()) def removeVar(self, varWrapper): """ removes the given varWrapper from the DataGraphView and the PointerList @param varWrapper variables.variablewrapper.VariableWrapper, the VariableWrapper to remove """ self.variableList.removeVar(varWrapper) self.data_graph_view.removeItem(varWrapper.getView()) def addPointer(self, fromView, toView): """ fromView and toView are QGraphicsWebViews @param fromView datagraph.htmlvariableview.HtmlVariableView, starting point of the Pointer @param toView datagraph.htmlvariableview.HtmlVariableView, end point of the Pointer """ pointer = Pointer(None, fromView, toView, self.distributedObjects) self.data_graph_view.addItem(pointer) self.pointerList.append(pointer) def removePointer(self, pointer): """ removes the given pointer from the DataGraphView and the PointerList @param pointer datagraph.pointer.Pointer, pointer to remove """ self.data_graph_view.removeItem(pointer) self.pointerList.remove(pointer) def clearDataGraph(self): """ clears the DataGraphView and the VariableList <br> this function is connected to the signal SignalProxy::cleanupModels() """ self.variableList.clear() self.data_graph_view.clear() def saveSession(self, xmlHandler): """ Insert session info to xml file @param xmlHandler sessionmanager.XmlHandler, handler to write to the session-xml-file """ dgWatches = xmlHandler.createNode("GraphWatches") for vw in self.variableList: xmlHandler.createNode("Watch", dgWatches, {'expression': vw.getExp(), 'xPos': vw.getXPos(), 'yPos': vw.getYPos()}) #dgPointers = xmlHandler.createNode("Pointers") #for pointer in self.pointerList: # xmlHandler.createNode("Pointer", dgPointers, { 'expFrom': pointer.fromView.var.getExp(), 'expTo': pointer.toView.var.getExp() }) def loadSession(self, xmlHandler): """ load session info to xml file @param xmlHandler sessionmanager.XmlHandler, handler to read from the session-xml-file """ dgParent = xmlHandler.getNode("GraphWatches") if dgParent != None: childnodes = dgParent.childNodes() for i in range(childnodes.size()): attr = xmlHandler.getAttributes(childnodes.at(i)) self.addWatch(attr["expression"], int(attr["xPos"]), int(attr["yPos"]))
class VariableModel(QAbstractItemModel): def __init__(self, do, parent=None): QAbstractItemModel.__init__(self, parent) self._vars = VariableList(TreeVariableFactory, do) do.signalProxy.cleanupModels.connect(self.clear) do.signalProxy.aboutToUpdateVariables.connect(self.__unmarkAll) def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() parentItem = parent.internalPointer() if parent.isValid() else None if parentItem: childItem = parentItem._child(row) else: childItem = self._vars[row] return self.createIndex(row, column, childItem) def parent(self, index): if not index.isValid(): return QModelIndex() childItem = index.internalPointer() parentItem = childItem._parent if not parentItem: return QModelIndex() return self.createIndex(parentItem._row, 0, parentItem) def rowCount(self, parent): if parent.column() > 0: return 0 if not parent.isValid(): return len(self._vars) else: return parent.internalPointer()._childCount() def columnCount(self, _): return 3 def data(self, index, role): if not index.isValid(): return None item = index.internalPointer() ret = None if role == Qt.DisplayRole: if index.column() == 0: ret = item.exp elif index.column() == 1: ret = item.type elif index.column() == 2: ret = item.value elif role == Qt.EditRole: if index.column() == 2: ret = item.value elif role == Qt.DecorationRole: if index.column() == 0: if item.access in ['private', 'protected']: iconprefix = item.access + "_" else: iconprefix = "" icon = None if not item.inScope: return QPixmap(":/icons/images/outofscope.png") elif not isinstance(item, StdVariable): icon = QPixmap(":/icons/images/" + iconprefix + "struct.png") else: # leave item icon = QPixmap(":/icons/images/" + iconprefix + "var.png") # overlay for arguments if icon and item.arg: ol = QPixmap(":/icons/images/overlay_arg.png") p = QPainter(icon) p.drawPixmap(ol.rect(), ol) elif icon and item.exp == "Return value": ol = QPixmap(":/icons/images/overlay_ret.png") p = QPainter(icon) p.drawPixmap(ol.rect(), ol) return icon elif role == Qt.BackgroundRole: if not item.inScope: ret = QBrush(Qt.gray) elif index.column() == 2: if item.marked and item.inScope: ret = QBrush(Qt.yellow) return ret def headerData(self, section, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return ["Expression", "Type", "Value"][section] return None def addVar(self, name): self.beginInsertRows(QModelIndex(), len(self._vars), len(self._vars)) var = self._vars.addVarByName(name) var.setNodeData(len(self._vars) - 1, None, self) self.endInsertRows() return var def hasChildren(self, parent): if not parent.isValid(): return True return parent.internalPointer()._hasChildren() def emitDataChanged(self, tl, br): self.dataChanged.emit(tl, br) def mimeTypes(self): return [variable.MIME_TYPE] def mimeData(self, indexes): if len(indexes) == 1: item = indexes[0].internalPointer() d = QMimeData() d.setData(variable.MIME_TYPE, item.uniqueName) return d else: return None def flags(self, index): if not index.isValid(): return Qt.ItemIsDropEnabled item = index.internalPointer() if not item.inScope: return Qt.ItemIsEnabled | Qt.ItemIsSelectable ret = Qt.ItemIsEnabled | Qt.ItemIsSelectable if index.column() == 2: ret |= Qt.ItemIsEditable elif index.column() == 0: ret |= Qt.ItemIsDragEnabled return ret def removeVar(self, var): pos = self._vars._vars.index(var) self.removeRows(pos, 1, QModelIndex()) def removeRows(self, position, rows, parent): """ removes the selected row in the model @param position int, starting position of selection @param rows int, number of rows to delete beginning at starting position @param parent TreeItem, parent item containing items to delete """ parentItem = parent.internalPointer() if parentItem: logging.error("Cannot remove a child variable.") return False self.beginRemoveRows(parent, position, position + rows - 1) for i in range(position, position + rows): self._vars.removeIdx(i) # fix the _row members of the remaining elements for offset, var in enumerate(self._vars[position:]): var._row = position + offset self.endRemoveRows() return True def clear(self): self.beginResetModel() self._vars.clear() self.endResetModel() def setData(self, index, value, role): if index.isValid() and role == Qt.EditRole: index.internalPointer().assignValue(value) return True return False def __unmarkAll(self): for i in self._vars._vars: i.unmarkAll()
class VariableModel(QAbstractItemModel): def __init__(self, do, parent=None): QAbstractItemModel.__init__(self, parent) self._vars = VariableList(TreeVariableFactory, do) do.signalProxy.cleanupModels.connect(self.clear) do.signalProxy.aboutToUpdateVariables.connect(self.__unmarkAll) def index(self, row, column, parent): if not self.hasIndex(row, column, parent): return QModelIndex() parentItem = parent.internalPointer() if parent.isValid() else None if parentItem: childItem = parentItem._child(row) else: childItem = self._vars[row] return self.createIndex(row, column, childItem) def parent(self, index): if not index.isValid(): return QModelIndex() childItem = index.internalPointer() parentItem = childItem._parent if not parentItem: return QModelIndex() return self.createIndex(parentItem._row, 0, parentItem) def rowCount(self, parent): if parent.column() > 0: return 0 if not parent.isValid(): return len(self._vars) else: return parent.internalPointer()._childCount() def columnCount(self, _): return 3 def data(self, index, role): if not index.isValid(): return None item = index.internalPointer() ret = None if role == Qt.DisplayRole: if index.column() == 0: ret = item.exp elif index.column() == 1: ret = item.type elif index.column() == 2: ret = item.value elif role == Qt.EditRole: if index.column() == 2: ret = item.value elif role == Qt.DecorationRole: if index.column() == 0: if item.access in ['private', 'protected']: iconprefix = item.access + "_" else: iconprefix = "" icon = None if not item.inScope: return QPixmap(":/icons/images/outofscope.png") elif not isinstance(item, StdVariable): icon = QPixmap(":/icons/images/" + iconprefix + "struct.png") else: # leave item icon = QPixmap(":/icons/images/" + iconprefix + "var.png") # overlay for arguments if icon and item.arg: ol = QPixmap(":/icons/images/overlay_arg.png") p = QPainter(icon) p.drawPixmap(ol.rect(), ol) elif icon and item.exp == "Return value": ol = QPixmap(":/icons/images/overlay_ret.png") p = QPainter(icon) p.drawPixmap(ol.rect(), ol) return icon elif role == Qt.BackgroundRole: if not item.inScope: ret = QBrush(Qt.gray) elif index.column() == 2: if item.marked and item.inScope: ret = QBrush(Qt.yellow) return ret def headerData(self, section, orientation, role): if orientation == Qt.Horizontal and role == Qt.DisplayRole: return ["Expression", "Type", "Value"][section] return None def addVar(self, name): self.beginInsertRows(QModelIndex(), len(self._vars), len(self._vars)) var = self._vars.addVarByName(name) var.setNodeData(len(self._vars) - 1, None, self) self.endInsertRows() return var def hasChildren(self, parent): if not parent.isValid(): return True return parent.internalPointer()._hasChildren() def emitDataChanged(self, tl, br): self.dataChanged.emit(tl, br) def mimeTypes(self): return QStringList([variable.MIME_TYPE]) def mimeData(self, indexes): if len(indexes) == 1: item = indexes[0].internalPointer() d = QMimeData() d.setData(variable.MIME_TYPE, item.uniqueName) return d else: return None def flags(self, index): if not index.isValid(): return Qt.ItemIsDropEnabled item = index.internalPointer() if not item.inScope: return Qt.ItemIsEnabled | Qt.ItemIsSelectable ret = Qt.ItemIsEnabled | Qt.ItemIsSelectable if index.column() == 2: ret |= Qt.ItemIsEditable elif index.column() == 0: ret |= Qt.ItemIsDragEnabled return ret def removeVar(self, var): pos = self._vars._vars.index(var) self.removeRows(pos, 1, QModelIndex()) def removeRows(self, position, rows, parent): """ removes the selected row in the model @param position int, starting position of selection @param rows int, number of rows to delete beginning at starting position @param parent TreeItem, parent item containing items to delete """ parentItem = parent.internalPointer() if parentItem: logging.error("Cannot remove a child variable.") return False self.beginRemoveRows(parent, position, position + rows - 1) for i in xrange(position, position + rows): self._vars.removeIdx(i) # fix the _row members of the remaining elements for offset, var in enumerate(self._vars[position:]): var._row = position + offset self.endRemoveRows() return True def clear(self): self.beginResetModel() self._vars.clear() self.endResetModel() def setData(self, index, value, role): if index.isValid() and role == Qt.EditRole: index.internalPointer().assignValue(value.toString()) return True return False def __unmarkAll(self): for i in self._vars._vars: i.unmarkAll()