def frameNodes(self, nodes): if len(nodes) == 0: return def computeWindowFrame(): windowRect = self.rect() windowRect.setLeft(windowRect.left() + 16) windowRect.setRight(windowRect.right() - 16) windowRect.setTop(windowRect.top() + 16) windowRect.setBottom(windowRect.bottom() - 16) return windowRect nodesRect = None for node in nodes: nodeRectF = node.transform().mapRect(node.rect()) nodeRect = QtCore.QRect(nodeRectF.x(), nodeRectF.y(), nodeRectF.width(), nodeRectF.height()) if nodesRect is None: nodesRect = nodeRect else: nodesRect = nodesRect.united(nodeRect) windowRect = computeWindowFrame() scaleX = float(windowRect.width()) / float(nodesRect.width()) scaleY = float(windowRect.height()) / float(nodesRect.height()) if scaleY > scaleX: scale = scaleX else: scale = scaleY if scale < 1.0: self.setTransform(QtGui.QTransform.fromScale(scale, scale)) else: self.setTransform(QtGui.QTransform()) sceneRect = self.sceneRect() pan = sceneRect.center() - nodesRect.center() sceneRect.translate(-pan.x(), -pan.y()) self.setSceneRect(sceneRect) # Update the main panel when reframing. self.update()
def mousePressEvent(self, event): if event.button() is QtCore.Qt.MouseButton.LeftButton and self.itemAt( event.pos()) is None: self.beginNodeSelection.emit() self._manipulationMode = MANIP_MODE_SELECT self._mouseDownSelection = copy.copy(self.getSelectedNodes()) self._selectionRect = SelectionRect(graph=self, mouseDownPos=self.mapToScene( event.pos())) elif event.button() is QtCore.Qt.MouseButton.MiddleButton: self.setCursor(QtCore.Qt.OpenHandCursor) self._manipulationMode = MANIP_MODE_PAN self._lastPanPoint = self.mapToScene(event.pos()) elif event.button() is QtCore.Qt.MouseButton.RightButton: self.setCursor(QtCore.Qt.SizeHorCursor) self._manipulationMode = MANIP_MODE_ZOOM self._lastZoomPoint = self.mapToScene(event.pos()) self._lastTransform = QtGui.QTransform(self.transform()) else: super(GraphView, self).mousePressEvent(event)
def mousePressEvent(self, event): print "KGraphView: mousePressEvent" modifiers = QtWidgets.QApplication.keyboardModifiers() if event.button() == QtCore.Qt.MouseButton.RightButton: print "KGraphView: mousePressEvent.RightButton" zoom_with_alt_rmb = self.window().preferences.getPreferenceValue( 'zoom_with_alt_rmb') if zoom_with_alt_rmb and modifiers == QtCore.Qt.AltModifier: self._manipulationMode = MANIP_MODE_ZOOM self.setCursor(QtCore.Qt.SizeHorCursor) self._lastMousePos = event.pos() self._lastTransform = QtGui.QTransform(self.transform()) self._lastSceneRect = self.sceneRect() self._lastSceneCenter = self._lastSceneRect.center() self._lastScenePos = self.mapToScene(event.pos()) self._lastOffsetFromSceneCenter = self._lastScenePos - self._lastSceneCenter return def graphItemAt(item): if isinstance(item, KNode): return item if isinstance(item, Connection): return item elif item is not None: return graphItemAt(item.parentItem()) return None graphicItem = graphItemAt(self.itemAt(event.pos())) pos = self.mapToScene(event.pos()) if graphicItem is None: contextMenu = QtWidgets.QMenu(self.getGraphViewWidget()) contextMenu.setObjectName('rightClickContextMenu') contextMenu.setMinimumWidth(150) if self.getClipboardData() is not None: def pasteSettings(): self.pasteSettings(pos) def pasteSettingsMirrored(): self.pasteSettings(pos, mirrored=True) contextMenu.addAction("Paste").triggered.connect( pasteSettings) contextMenu.addAction("Paste Mirrored").triggered.connect( pasteSettingsMirrored) contextMenu.addSeparator() graphViewWidget = self.getGraphViewWidget() contextMenu.addAction("Add Backdrop").triggered.connect( graphViewWidget.addBackdrop) contextMenu.popup(event.globalPos()) if isinstance(graphicItem, KNode): self.selectNode(graphicItem, clearSelection=True, emitSignal=True) contextMenu = QtWidgets.QMenu(self.getGraphViewWidget()) contextMenu.setObjectName('rightClickContextMenu') contextMenu.setMinimumWidth(150) def copySettings(): self.copySettings(pos) contextMenu.addAction("Copy").triggered.connect(copySettings) if self.getClipboardData() is not None: def pasteSettings(): # Paste the settings, not modifying the location, because that will be used to determine symmetry. pasteData = self.getClipboardData()['components'][0] pasteData.pop('graphPos', None) graphicItem.getComponent().pasteData(pasteData, setLocation=False) contextMenu.addSeparator() contextMenu.addAction("Paste Data").triggered.connect( pasteSettings) contextMenu.popup(event.globalPos()) elif isinstance(graphicItem, Connection): outPort = graphicItem.getSrcPortCircle().getPort() inPort = graphicItem.getDstPortCircle().getPort() if outPort.getDataType() != inPort.getDataType(): if outPort.getDataType().startswith(inPort.getDataType( )) and outPort.getDataType().endswith('[]'): globalPos = event.globalPos() contextMenu = QtWidgets.QMenu( self.getGraphViewWidget()) contextMenu.setObjectName('rightClickContextMenu') contextMenu.setMinimumWidth(150) def editIndex(): componentInput = graphicItem.getDstPortCircle( ).getPort().getComponentInput() EditIndexWidget(componentInput, pos=globalPos, parent=self.getGraphViewWidget()) contextMenu.addAction("EditIndex").triggered.connect( editIndex) contextMenu.popup(globalPos) elif event.button( ) is QtCore.Qt.MouseButton.LeftButton and self.itemAt( event.pos()) is None: print "KGraphView: mousePressEvent.LeftButton" self.beginNodeSelection.emit() self._manipulationMode = MANIP_MODE_SELECT self._mouseDownSelection = copy.copy(self.getSelectedNodes()) self._selectionRect = SelectionRect(graph=self, mouseDownPos=self.mapToScene( event.pos())) elif event.button() is QtCore.Qt.MouseButton.MiddleButton: print "KGraphView: mousePressEvent.MiddleButton" pan_with_alt = self.window().preferences.getPreferenceValue( 'pan_with_alt') if pan_with_alt is True and modifiers != QtCore.Qt.AltModifier: return self.setCursor(QtCore.Qt.OpenHandCursor) self._manipulationMode = MANIP_MODE_PAN self._lastPanPoint = self.mapToScene(event.pos()) else: super(GraphView, self).mousePressEvent(event)