def __messageFilter(self, message): if message.match(EGraphHandle.kMessageEditBegin): #print 'Debug message: Edit begin...' self.__kDummy.toggleEditMode() return if message.match(EGraphHandle.kMessageEditEnd): #print 'Debug message: Edit end...' if self.__kDummy.isEditMode(): self.__kDummy.toggleEditMode() return if message.match(EGraphHandle.kMessageNodeAdded): self.addItem(ENode(message.getData())) return if message.match(EGraphHandle.kMessageNodeRemoved): return if message.match(EGraphHandle.kMessageConnectionMade): dataOne = self.__getDataFromId(message.getData()[0]) dataTwo = self.__getDataFromId(message.getData()[1]) if dataOne and dataTwo: conn = EEdge(dataOne, dataTwo, message.getData()[2]) self.__connections[conn.Id] = conn self.addItem(conn) headData = conn.Head tailData = conn.Tail print 'Result: Connected %s.%s to %s.%s' % (headData[ENode.kGuiAttributeParentName], headData[ENode.kGuiAttributeLongName], tailData[ENode.kGuiAttributeParentName], tailData[ENode.kGuiAttributeLongName]) return if message.match(EGraphHandle.kMessageConnectionBroke): if message.getData() in self.__connections.keys(): self.removeItem(self.__connections[message.getData()]) self.__connections.pop(message.getData(), None) self.update() #print "Disconnected..." return if message.match(EGraphHandle.kMessageUnknown) or message.match(EGraphHandle.kMessageInternalError): print 'No event <%s>' % message.getData() if self.__kDummy.isEditMode(): self.__kDummy.toggleEditMode() self.update()
def __init__(self, view, controller, parent=None): QGraphicsScene.__init__(self, parent) self.__view = view self.__controller = controller self.__controller.setScene(self) self.__controller.Handle.Message.connect(self.__messageFilter) self.__controller.Message.connect(self.__messageFilter) self.__gridSize = 35 self.__isEditMode = False self.__isGridActive = True self.__isAltModifier = False self.__isControlModifier = False self.__isNodePressed = False self.__kDummy = EDummy() self.__kDummy.setGridSize(self.__gridSize) self.__kDummy.onMouseClick.connect(self.__onDummyMouseClick) self.__kDummy.onMouseGesture.connect(self.__onDummyMouseGesture) self.__kDummy.hide() self.addItem(self.__kDummy) self.__kSelected = ESceneSelection() self.addItem(self.__kSelected) self.__kCutLine = QGraphicsLineItem() self.__kCutLine.hide() self.__dummyHead = EDummyData() self.__dummyTail = EDummyData() self.__dummyHead.hide() self.__dummyTail.hide() self.__kConnectionDummy = EEdge(self.__dummyHead.data(), self.__dummyTail.data(), uuid.uuid1(), True) self.__kConnectionDummy.hide() self.addItem(self.__kConnectionDummy) self.addItem(self.__kCutLine) self.addItem(self.__dummyHead) self.addItem(self.__dummyTail) self.__nodes = {} self.__defaultNodeNames = {} self.__userNodeNames = {} self.__connections = {} self.__pressedAttributes = None self.__kSelected.Item = None
class EScene(QGraphicsScene): onSelectionChanged = pyqtSignal(type) def __init__(self, view, controller, parent=None): QGraphicsScene.__init__(self, parent) self.__view = view self.__controller = controller self.__controller.setScene(self) self.__controller.Handle.Message.connect(self.__messageFilter) self.__controller.Message.connect(self.__messageFilter) self.__gridSize = 35 self.__isEditMode = False self.__isGridActive = True self.__isAltModifier = False self.__isControlModifier = False self.__isNodePressed = False self.__kDummy = EDummy() self.__kDummy.setGridSize(self.__gridSize) self.__kDummy.onMouseClick.connect(self.__onDummyMouseClick) self.__kDummy.onMouseGesture.connect(self.__onDummyMouseGesture) self.__kDummy.hide() self.addItem(self.__kDummy) self.__kSelected = ESceneSelection() self.addItem(self.__kSelected) self.__kCutLine = QGraphicsLineItem() self.__kCutLine.hide() self.__dummyHead = EDummyData() self.__dummyTail = EDummyData() self.__dummyHead.hide() self.__dummyTail.hide() self.__kConnectionDummy = EEdge(self.__dummyHead.data(), self.__dummyTail.data(), uuid.uuid1(), True) self.__kConnectionDummy.hide() self.addItem(self.__kConnectionDummy) self.addItem(self.__kCutLine) self.addItem(self.__dummyHead) self.addItem(self.__dummyTail) self.__nodes = {} self.__defaultNodeNames = {} self.__userNodeNames = {} self.__connections = {} self.__pressedAttributes = None self.__kSelected.Item = None def __isNode(self, EObject): return isinstance(EObject, ENode) def __isEdge(self, EObject): return isinstance(EObject, EEdge) def __onDummyMouseClick(self): if self.__isNode(self.sender()): pressedId, pressedPlug = self.sender().mapFromPoint(self.__kDummy.scenePos()) if not self.__nodes.has_key(pressedId): self.__pressedAttributes = pressedId self.sender().setFlag(QGraphicsItem.ItemIsMovable, False) self.__dummyHead.setPos(pressedPlug) if self.sender().Handle.IsContainer: self.__dummyTail.Polygon = self.sender().Polygon self.__kConnectionDummy.Head = self.__dummyHead.data() self.__kConnectionDummy.Tail = self.__dummyTail.data() if self.__controller.Handle.getAttributeFromId(pressedId).Type.matches(EAttribute.kTypeInput): self.__kConnectionDummy.Tail = self.__dummyHead.data() self.__kConnectionDummy.Head = self.__dummyTail.data() self.sender().mute(pressedId) self.__kConnectionDummy.update() self.__kConnectionDummy.show() self.__kSelected.Item = self.sender() dataBlock = type("EData", (EObject,), {'nodeName': self.__kSelected.Item.Name, 'nodeProperties': self.__kSelected.Item.Handle.lsProperties()}) self.onSelectionChanged.emit(dataBlock) def __onDummyMouseGesture(self, line): if self.__isNode(self.itemAt(line.p1())) and self.__isNode(self.itemAt(line.p2())): return self.__kCutLine.setLine(line) data = [item for item in self.__kCutLine.collidingItems() if isinstance(item, EEdge) or isinstance(item, ENode)] if len(data): self.__isEditMode = True for item in data: if isinstance(item, EEdge): if self.__kCutLine.collidesWithPath(item.shape()): self.__controller.Handle.delConnection(item.Id) elif isinstance(item, ENode): self.__controller.deleteNode(item.Id) else: self.__isEditMode = False def __getDataFromId(self, theId): handle = self.__controller.Handle.getAttributeHandleId(theId) if handle: return self.__nodes[handle].mapFromId(theId) return None def __messageFilter(self, message): if message.matches(EController.kMessageNodeAdded): handle, name = message.getData() if not name: name = None if handle.IsContainer: newNode = EPoint(handle) else: newNode = ENode(handle) newNode.setPos(self.__kDummy.scenePos()) self.addItem(newNode, name) self.__kSelected.Item = newNode if message.matches(EController.kMessageNodeRemoved): self.removeItem(self.__nodes[message.getData()]) self.__nodes.pop(message.getData(), None) return if message.matches(EController.kMessageConnectionMade): if not len(message.getData()): return dataOne = self.__getDataFromId(message.getData()[0]) dataTwo = self.__getDataFromId(message.getData()[1]) if dataOne and dataTwo: if any([isinstance(dataOne[ENode.kGuiAttributeParent], EPoint), isinstance(dataTwo[ENode.kGuiAttributeParent], EPoint)]): conn = EEdge(dataOne, dataTwo, message.getData()[2], True) dataOne[ENode.kGuiAttributeParent].togglePlug(message.getData()[0]) dataTwo[ENode.kGuiAttributeParent].togglePlug(message.getData()[1]) else: conn = EEdge(dataOne, dataTwo, message.getData()[2]) self.__connections[conn.Id] = conn self.addItem(conn) return if message.matches(EGraphHandle.kMessageConnectionBroke): if message.getData() in self.__connections.keys(): connHead = self.__connections[message.getData()].Head#[ENode.kGuiAttributeId] connTail = self.__connections[message.getData()].Tail#[ENode.kGuiAttributeId] self.removeItem(self.__connections[message.getData()]) self.__connections.pop(message.getData(), None) self.update() return @property def IsEditMode(self): return self.__isEditMode def getNodes(self): return self.__nodes def getConnections(self): return self.__connections def addItem(self, graphicsItem, graphicsItemName=None): if self.__isNode(graphicsItem): graphicsItem.setZValue(1.0) graphicsItem.onPress.connect(self.__onDummyMouseClick) if graphicsItemName is None: graphicsItemName = graphicsItem.Name if self.__defaultNodeNames.has_key(graphicsItemName): self.__defaultNodeNames[graphicsItemName] += 1 else: self.__defaultNodeNames[graphicsItemName] = 1 graphicsItem.Name = "%s_%s" % (graphicsItemName, self.__defaultNodeNames[graphicsItemName]) else: if self.__userNodeNames.has_key(graphicsItemName): print graphicsItemName.split('_') graphicsItem.Name = graphicsItemName self.__userNodeNames[graphicsItemName] = 1 self.__nodes[graphicsItem.Id] = graphicsItem QGraphicsScene.addItem(self, graphicsItem) def drawBackground(self, painter, rect): self.update() if self.__isGridActive: painter.setPen(Qt.NoPen) painter.fillRect(rect, EDraw.EColor.DefaultSceneFillColor) left = int(rect.left()) - (int(rect.left()) % self.__gridSize) top = int(rect.top()) - (int(rect.top()) % self.__gridSize) lines = [] right = int(rect.right()) bottom = int(rect.bottom()) for x in range(left, right, self.__gridSize): lines.append(QLineF(x, rect.top(), x, rect.bottom())) for y in range(top, bottom, self.__gridSize): lines.append(QLineF(rect.left(), y, rect.right(), y)) painter.setPen(QPen(EDraw.EColor.DefaultSceneGridColor, 1, Qt.SolidLine)) painter.drawLines(lines) return painter.fillRect(rect, EDraw.EColor.DefaultSceneFillColor) def contextMenuEvent(self, mouseEvent): QGraphicsScene.contextMenuEvent(self, mouseEvent) def mouseMoveEvent(self, mouseEvent): QGraphicsScene.mouseMoveEvent(self, mouseEvent) self.__kDummy.setPos(mouseEvent.scenePos()) if self.__kDummy.IsSnapMode: if self.__kSelected.Item: #self.__kSelected.Item.setPos(self.__kDummy.Position) #self.__kSelected.Item.onMove.emit() pass self.__dummyTail.setPos(mouseEvent.scenePos()) self.__kConnectionDummy.update() self.update() def mousePressEvent(self, mouseEvent): for node in self.__nodes.itervalues(): node.setZValue(1.0) if self.__isControlModifier: return if self.__isAltModifier: self.__kDummy.ToggleEditMode() return if not self.itemAt(mouseEvent.scenePos()): self.__kSelected.Item = None self.onSelectionChanged.emit(type("EData", (EObject,), {'nodeName': '', 'nodeProperties': []})) if mouseEvent.button() == Qt.RightButton: if self.__isNode(self.itemAt(mouseEvent.scenePos())): self.__isNodePressed = True self.__kDummy.ToggleEditMode() return QGraphicsScene.mousePressEvent(self, mouseEvent) def mouseReleaseEvent(self, mouseEvent): if self.__isAltModifier: self.__kDummy.ToggleEditMode() self.update() return if mouseEvent.button() == Qt.RightButton: if self.__isNodePressed: self.__isNodePressed = False self.__kDummy.ToggleEditMode() return if self.__kSelected.Item: self.__kSelected.Item.setFlag(QGraphicsItem.ItemIsMovable, True) self.__kConnectionDummy.hide() self.__kSelected.Item.mute(None) if isinstance(self.itemAt(mouseEvent.scenePos()), ENode): hitId, hitPoint = self.itemAt(mouseEvent.scenePos()).mapFromPoint(mouseEvent.scenePos()) self.__controller.connectAttr(self.__pressedAttributes, hitId) self.update() QGraphicsScene.mouseReleaseEvent(self, mouseEvent) def keyPressEvent(self, keyEvent): QGraphicsScene.keyPressEvent(self, keyEvent) if keyEvent.key() == Qt.Key_Control: self.__view.setDragMode(QGraphicsView.ScrollHandDrag) self.__isControlModifier = True if keyEvent.key() == Qt.Key_Alt: self.__isAltModifier = True if keyEvent.key() == 88: self.__kDummy.IsSnapMode = True self.__kDummy.show() if keyEvent.key() == Qt.Key_Tab: print 'Tab, Tab..' def keyReleaseEvent(self, keyEvent): QGraphicsScene.keyReleaseEvent(self, keyEvent) if keyEvent.key() == Qt.Key_Control: self.__view.setDragMode(QGraphicsView.NoDrag) self.__isControlModifier = False if keyEvent.key() == Qt.Key_Alt: self.__isAltModifier = False if keyEvent.key() == 88: self.__kDummy.IsSnapMode = False self.__kDummy.hide() if keyEvent.key() == Qt.Key_Tab: print 'Tab, Tab..'