def keyPressEvent(self, event): modifiers = QApplication.keyboardModifiers() if (modifiers != Qt.KeyboardModifier.ShiftModifier and event.key() in (Qt.Key.Key_Return, Qt.Key.Key_Enter)): self.returnPressed.emit() return super().keyPressEvent(event)
def event(self, event): super().event(event) modifiers = QApplication.keyboardModifiers() if not VERSION_QT5: ctrl_is_active = modifiers & Qt.KeyboardModifier.ControlModifier else: ctrl_is_active = modifiers & Qt.ControlModifier if event.type == Event.MousePressEvent and MouseEvent.LeftButton in event.buttons and self._controller.getToolsEnabled(): if ctrl_is_active: self._controller.setActiveTool("TranslateTool") return if self._skip_press: # The selection was previously cleared, do not add/remove an support mesh but # use this click for selection and reactivating this tool only. self._skip_press = False return if self._selection_pass is None: # The selection renderpass is used to identify objects in the current view self._selection_pass = CuraApplication.getInstance().getRenderer().getRenderPass("selection") picked_node = self._controller.getScene().findObject(self._selection_pass.getIdAtPosition(event.x, event.y)) if not picked_node: # There is no slicable object at the picked location return node_stack = picked_node.callDecoration("getStack") if node_stack: if node_stack.getProperty("support_mesh", "value"): self._removeSupportMesh(picked_node) return elif node_stack.getProperty("anti_overhang_mesh", "value") or node_stack.getProperty("infill_mesh", "value") or node_stack.getProperty("support_mesh", "value"): # Only "normal" meshes can have support_mesh added to them return # Create a pass for picking a world-space location from the mouse location active_camera = self._controller.getScene().getActiveCamera() picking_pass = PickingPass(active_camera.getViewportWidth(), active_camera.getViewportHeight()) picking_pass.render() picked_position = picking_pass.getPickedPosition(event.x, event.y) # Add the support_mesh cube at the picked location self._createSupportMesh(picked_node, picked_position)
def mouseMoveEvent(self, event): data = self.mapToScene(event.position().toPoint()) rules = [self.measuring_length, self.measuring_angle, self.measuring_area] modifiers = QApplication.keyboardModifiers() if modifiers == QtCore.Qt.KeyboardModifier.ShiftModifier and self.oldPos: QApplication.setOverrideCursor(QtCore.Qt.CursorShape.OpenHandCursor) self.newPos = data delta = self.newPos - self.oldPos self.translate(delta.x(), delta.y()) elif (any(rules) or self.measuring_widths): QApplication.setOverrideCursor(QtCore.Qt.CursorShape.CrossCursor) #change cursor else: QApplication.setOverrideCursor(QtCore.Qt.CursorShape.ArrowCursor) #change cursor #dragging line if self._thispos and any(rules): if self.measuring_length: self.parent().statusbar.showMessage( 'Click to place next point... double click to finish') if self.measuring_area: self.parent().statusbar.showMessage( 'Click to place next point... close polygon to finish') if self.measuring_angle: self.parent().statusbar.showMessage( 'Click point to define vector') end = QtCore.QPointF(data)#self.mapToScene(event.pos())) start = self._thispos if self.measuring_angle and self._lastpos: start = self._thispos if self.scene.testline: #remove old line self.scene.removeItem(self.scene.testline) self.scene.testline = False if self.measuring_area and self.line_count > 2: intersect, xi, yi, k = self.A.checkIntersect(data.x(),data.y()) if self.scene.area_ellipseItem: #remove existing intersect self.scene.removeItem(self.scene.area_ellipseItem) self.scene.area_ellipseItem = False if self.scene.polyItem: self.scene.removeItem(self.scene.polyItem) self.scene.polyItem = False if intersect: #indicate intersect point p = QtCore.QPointF(xi, yi) self.scene.area_ellipseItem = QGraphicsEllipseItem(0, 0, 10, 10) self.scene.area_ellipseItem.setPos(p.x() - 10 / 2, p.y() - 10 / 2) self.scene.area_ellipseItem.setBrush( QtGui.QBrush(QtCore.QtColor('blue'))) #, style=QtCore.Qt.BrushStyle.SolidPattern)) self.scene.area_ellipseItem.setFlag( QGraphicsItem.GraphicsItemFlag.ItemIgnoresTransformations, False) #size stays small, but doesnt translate if set to false self.scene.addItem(self.scene.area_ellipseItem) #shade polygon region points = [ QtCore.QPointF(x,y) for x,y in zip( self.A.x[k:], self.A.y[k:] ) ] points.append(QtCore.QPointF(xi,yi)) self.scene.polyItem = QGraphicsPolygonItem(QtGui.QPolygonF(points)) self.scene.polyItem.setBrush( QtGui.QBrush(QtGui.QColor(255,255,255,127)) ) self.scene.addItem(self.scene.polyItem) self.scene.testline = QGraphicsLineItem(QtCore.QLineF(start, end)) self.scene.addItem(self.scene.testline)
def __eq__(self, other: QKeyEvent): assert isinstance( other, QKeyEvent ), f"invalid type {type(other)}, must be type <'QKeyEvent'>" return self._key == other.key() and QApplication.keyboardModifiers( ) == self.modifier
def event(self, event: Event) -> bool: result = super().event(event) if not self._tool_enabled: return result # overridden from ToolHandle.event(), because we also want to show the handle when there is no selection # disabling the tool oon Event.ToolDeactivateEvent is properly handled in ToolHandle.evemt() if event.type == Event.ToolActivateEvent: if self._handle: self._handle.setParent( self.getController().getScene().getRoot()) self._handle.setEnabled(True) self._selection_tool = self._controller._selection_tool self._controller.setSelectionTool(None) self._application.callLater( lambda: self._forceToolEnabled(passive=True)) if event.type == Event.ToolDeactivateEvent: self._controller.setSelectionTool(self._selection_tool or "SelectionTool") self._selection_tool = None self._application.callLater( lambda: self._forceToolEnabled(passive=True)) if (event.type == Event.MouseReleaseEvent and MouseEvent.LeftButton in cast(MouseEvent, event).buttons): self._dragging = False if (event.type == Event.MousePressEvent and MouseEvent.LeftButton in cast(MouseEvent, event).buttons): mouse_event = cast(MouseEvent, event) if QApplication.keyboardModifiers() & KeyboardShiftModifier: if self._active_point == 0: self._active_point = 1 else: self._active_point = 0 else: distances = [] # type: List[float] camera = self._controller.getScene().getActiveCamera() for point in self._points: if camera.isPerspective(): projected_point = camera.project( Vector(point.x(), point.y(), point.z())) else: # Camera.project() does not work for orthographic views in Cura 4.9 and before, so we calculate our own projection projection = camera.getProjectionMatrix() view = camera.getWorldTransformation() view.invert() position = Vector(point.x(), point.y(), point.z()) position = position.preMultiply(view) position = position.preMultiply(projection) projected_point = (position.x, position.y) dx = projected_point[0] - ( (camera.getWindowSize()[0] * (mouse_event.x + 1) / camera.getViewportWidth()) - 1) dy = projected_point[1] + mouse_event.y distances.append(dx * dx + dy * dy) self._active_point = 0 if distances[1] < distances[0]: self._active_point = 1 self._dragging = True result = self._handle_mouse_event(event, result) if event.type == Event.MouseMoveEvent: if self._dragging: result = self._handle_mouse_event(event, result) if self._selection_tool: self._selection_tool.event(event) return result