def wheelEvent(self, wheelEvent): """ Executed when the mouse wheel is moved on the scene. :type wheelEvent: QWheelEvent """ if wheelEvent.modifiers() & Qt.ControlModifier: wheelPos = wheelEvent.pos() wheelAngle = wheelEvent.angleDelta() zoom = self.zoom zoom += +Zoom.Step if wheelAngle.y() > 0 else -Zoom.Step zoom = clamp(zoom, Zoom.Min, Zoom.Max) if zoom != self.zoom: self.setTransformationAnchor(QGraphicsView.NoAnchor) self.setResizeAnchor(QGraphicsView.NoAnchor) p1 = self.mapToScene(wheelPos) self.scaleView(zoom) self.sgnScaled.emit(zoom) p2 = self.mapToScene(wheelPos) move = p2 - p1 self.translate(move.x(), move.y()) else: super().wheelEvent(wheelEvent)
def keyPressEvent(self, keyEvent): """ Executed when a combination of key is pressed. :type keyEvent: QKeyEvent """ scene = self.scene() key = keyEvent.key() modifiers = keyEvent.modifiers() if scene.mode is DiagramMode.Idle and \ modifiers & Qt.ControlModifier and \ key in {Qt.Key_Minus, Qt.Key_Plus, Qt.Key_0}: if key == Qt.Key_Minus: zoom = self.zoom - Zoom.Step elif key == Qt.Key_Plus: zoom = self.zoom + Zoom.Step else: zoom = Zoom.Default zoom = clamp(zoom, Zoom.Min, Zoom.Max) if zoom != self.zoom: self.setTransformationAnchor(QGraphicsView.NoAnchor) self.setResizeAnchor(QGraphicsView.NoAnchor) self.scaleView(zoom) self.sgnScaled.emit(zoom) else: super().keyPressEvent(keyEvent)
def setLevel(self, level): """ Set the zoom level according to the given value. :type level: float """ if self.isEnabled(): level = clamp(level, Zoom.Min, Zoom.Max) if level != self.level: self.level = level self.refresh() self.sgnChanged.emit(self.level)
def positionChanged(self): """ Move the node properly if the position has been changed. """ R = self.scene.sceneRect() x = clamp(self.xField.value(), R.left(), R.right()) y = clamp(self.yField.value(), R.top(), R.bottom()) pos1 = self.node.pos() pos2 = QPointF(x, y) if pos1 != pos2: commandData = { 'redo': { 'nodes': { self.node: { 'anchors': { k: v + pos2 - pos1 for k, v in self.node.anchors.items() }, 'pos': pos2, } }, 'edges': {}, }, 'undo': { 'nodes': { self.node: { 'anchors': {k: v for k, v in self.node.anchors.items()}, 'pos': pos1, } }, 'edges': {} } } self.scene.undostack.push( CommandNodeMove(scene=self.scene, data=commandData))
def mouseMoveEvent(self, mouseEvent): """ Executed when then mouse is moved on the view. :type mouseEvent: QGraphicsSceneMouseEvent """ scene = self.scene() mousePos = mouseEvent.pos() mouseButtons = mouseEvent.buttons() viewport = self.viewport() if mouseButtons & Qt.RightButton: if (mouseEvent.pos() - self.mousePressPos ).manhattanLength() >= QApplication.startDragDistance(): ######################################################################################################## # # # SCENE DRAG # # # ######################################################################################################## if scene.mode is not DiagramMode.SceneDrag: scene.setMode(DiagramMode.SceneDrag) viewport.setCursor(Qt.ClosedHandCursor) mousePos /= self.zoom mousePressPos = self.mousePressPos / self.zoom self.centerOn(self.mousePressCenterPos - mousePos + mousePressPos) else: super().mouseMoveEvent(mouseEvent) if mouseButtons & Qt.LeftButton: self.stopMove() if scene.mode is DiagramMode.RubberBandDrag: #################################################################################################### # # # RUBBERBAND DRAG # # # #################################################################################################### area = QRectF(self.mapFromScene(self.rubberBandOrigin), mousePos).normalized() path = QPainterPath() path.addRect(area) scene.setSelectionArea(self.mapToScene(path)) self.rubberBand.setGeometry(area.toRect()) if scene.mode in { DiagramMode.BreakPointMove, DiagramMode.InsertEdge, DiagramMode.MoveNode, DiagramMode.ResizeNode, DiagramMode.RubberBandDrag }: #################################################################################################### # # # VIEW SCROLLING # # # #################################################################################################### R = viewport.rect() if not R.contains(mousePos): move = QPointF(0, 0) if mousePos.x() < R.left(): move.setX(mousePos.x() - R.left()) elif mousePos.x() > R.right(): move.setX(mousePos.x() - R.right()) if mousePos.y() < R.top(): move.setY(mousePos.y() - R.top()) elif mousePos.y() > R.bottom(): move.setY(mousePos.y() - R.bottom()) if move: move.setX( clamp(move.x(), -MainView.MoveBound, +MainView.MoveBound)) move.setY( clamp(move.y(), -MainView.MoveBound, +MainView.MoveBound)) self.startMove(move, MainView.MoveRate)
def mouseMoveEvent(self, mouseEvent): """ Executed when then mouse is moved on the view. :type mouseEvent: QGraphicsSceneMouseEvent """ scene = self.scene() mousePos = mouseEvent.pos() mouseButtons = mouseEvent.buttons() viewport = self.viewport() if mouseButtons & Qt.RightButton: if (mouseEvent.pos() - self.mousePressPos).manhattanLength() >= QApplication.startDragDistance(): ######################################################################################################## # # # SCENE DRAG # # # ######################################################################################################## if scene.mode is not DiagramMode.SceneDrag: scene.setMode(DiagramMode.SceneDrag) viewport.setCursor(Qt.ClosedHandCursor) mousePos /= self.zoom mousePressPos = self.mousePressPos / self.zoom self.centerOn(self.mousePressCenterPos - mousePos + mousePressPos) else: super().mouseMoveEvent(mouseEvent) if mouseButtons & Qt.LeftButton: self.stopMove() if scene.mode is DiagramMode.RubberBandDrag: #################################################################################################### # # # RUBBERBAND DRAG # # # #################################################################################################### area = QRectF(self.mapFromScene(self.rubberBandOrigin), mousePos).normalized() path = QPainterPath() path.addRect(area) scene.setSelectionArea(self.mapToScene(path)) self.rubberBand.setGeometry(area.toRect()) if scene.mode in { DiagramMode.BreakPointMove, DiagramMode.InsertEdge, DiagramMode.MoveNode, DiagramMode.ResizeNode, DiagramMode.RubberBandDrag }: #################################################################################################### # # # VIEW SCROLLING # # # #################################################################################################### R = viewport.rect() if not R.contains(mousePos): move = QPointF(0, 0) if mousePos.x() < R.left(): move.setX(mousePos.x() - R.left()) elif mousePos.x() > R.right(): move.setX(mousePos.x() - R.right()) if mousePos.y() < R.top(): move.setY(mousePos.y() - R.top()) elif mousePos.y() > R.bottom(): move.setY(mousePos.y() - R.bottom()) if move: move.setX(clamp(move.x(), -MainView.MoveBound, +MainView.MoveBound)) move.setY(clamp(move.y(), -MainView.MoveBound, +MainView.MoveBound)) self.startMove(move, MainView.MoveRate)