def sceneEventFilter(self, event: QtCore.QEvent) -> bool: # call the inherited method if MaskTool.sceneEventFilter(self, event): return True if self.parent.maskTypeChooser.active_draw_type is None: return True # Mouse wheel to change the cursor size if event.type() == QtCore.QEvent.GraphicsSceneWheel: try: # PyQt 5 angle = event.angleDelta().y() except AttributeError: # PyQt 4 angle = event.delta() # wheel with CTRL means changing the cursor size if event.modifiers() == QtCore.Qt.ControlModifier: if angle > 0: self.parent.changeCursorSize(+1) else: self.parent.changeCursorSize(-1) event.accept() return True # Mouse press starts drawing if event.type() == QtCore.QEvent.GraphicsSceneMousePress and event.button() == QtCore.Qt.LeftButton: # if no mask has been created, create one for painting if self.parent.MaskEmpty is True: self.parent.AddEmptyMask() # store the coordinates self.last_x = event.pos().x() self.last_y = event.pos().y() # add a first circle (so that even if the mouse isn't moved something is drawn) self.DrawLine(self.last_x, self.last_x + 0.00001, self.last_y, self.last_y) # set the changed flag for the database self.parent.data_file.setChangesMade() # accept the event return True if event.type() == QtCore.QEvent.GraphicsSceneMouseRelease and event.button() == QtCore.Qt.LeftButton: if self.parent.config.auto_mask_update: with self.parent.window.changeTracker("draw line in mask"): self.parent.save() # Mouse move event to draw the stroke if event.type() == QtCore.QEvent.GraphicsSceneMouseMove: # get the new position self.parent.DrawCursor.setPos(event.pos()) pos_x = event.pos().x() pos_y = event.pos().y() # draw a line and store the position self.DrawLine(pos_x, self.last_x, pos_y, self.last_y) self.last_x = pos_x self.last_y = pos_y # accept the event return True # Mouse hover updates the color_under_cursor and displays the brush cursor if event.type() == QtCore.QEvent.GraphicsSceneHoverMove: # move brush cursor self.parent.DrawCursor.setPos(event.pos()) # don't accept the event, so that others can accept it return False
def sceneEventFilter(self, event: QtCore.QEvent) -> bool: if MaskTool.sceneEventFilter(self, event): return True # Mouse press starts drawing if event.type() == QtCore.QEvent.GraphicsSceneMousePress and event.button() == QtCore.Qt.LeftButton: with self.parent.window.changeTracker("bucket fill in mask"): self.fillColor(event.pos().x(), event.pos().y()) self.parent.save() # accept the event return True # don't accept the event, so that others can accept it return False
def editorEvent( self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: QStyleOptionViewItem, index: QtCore.QModelIndex, ) -> bool: """Called when an event has occured in the editor. This can be used to customize how the delegate handles mouse/key events """ if (event.type() == event.MouseButtonRelease and event.button() == Qt.RightButton): self.show_context_menu(index, model, event.globalPos(), option.widget) # if the user clicks quickly on the visibility checkbox, we *don't* # want it to be interpreted as a double-click. We want the visibilty # to simply be toggled. if event.type() == event.MouseButtonDblClick: self.initStyleOption(option, index) style = option.widget.style() check_rect = style.subElementRect( style.SE_ItemViewItemCheckIndicator, option, option.widget) if check_rect.contains(event.pos()): cur_state = index.data(Qt.CheckStateRole) if model.flags(index) & Qt.ItemIsUserTristate: state = Qt.CheckState((cur_state + 1) % 3) else: state = Qt.Unchecked if cur_state else Qt.Checked return model.setData(index, state, Qt.CheckStateRole) # refer all other events to the QStyledItemDelegate return super().editorEvent(event, model, option, index)
def editorEvent(self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> bool: decorationRect = option.rect if event.type() == QEvent.MouseButtonPress: item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return QStyledItemDelegate.editorEvent(self, event, model, option, index) type = item['type'] value = item['value'] if type == 'device_activity': text_rect = calculate_text_rect( value['device'], font=ResourceLoader().qt_font_text_xs) if calculate_middle_rect(decorationRect, text_rect.width(), text_rect.height(), x=110, y=15).contains(event.pos()): print('点击了设备{device}'.format(device=value['device'])) return QStyledItemDelegate.editorEvent(self, event, model, option, index)
def sceneEventFilter(self, event: QtCore.QEvent) -> bool: if MaskTool.sceneEventFilter(self, event): return True # Mouse press starts drawing if event.type() == QtCore.QEvent.GraphicsSceneMousePress and event.button() == QtCore.Qt.LeftButton: self.PickColor() # accept the event return True # Mouse hover updates the color_under_cursor and displays the brush cursor if event.type() == QtCore.QEvent.GraphicsSceneHoverMove: # get color at this position if self.parent.MaskEmpty is True: self.color_under_cursor = 0 else: color = self.parent.MaskDisplay.GetColor(event.pos().x(), event.pos().y()) if color is not None: self.color_under_cursor = color # don't accept the event, so that others can accept it return False
def editorEvent(self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: 'QStyleOptionViewItem', index: QtCore.QModelIndex) -> bool: decorationRect = option.rect if event.type() == QEvent.MouseButtonPress and calculate_middle_rect( decorationRect, 20, 20).contains(event.pos()): item = index.model().data(index, Qt.UserRole) if isinstance(item, QVariant): return QStyledItemDelegate.editorEvent(self, event, model, option, index) value = item['value'] type = item['type'] if type == 'checkbox': # 数据转换 value = 1 if value == 0 else 0 model.setData(index, value, Qt.DisplayRole) return QStyledItemDelegate.editorEvent(self, event, model, option, index)
def eventFilter(self, widget, event: QEvent): """Handle frameless window events. Args: widget (QObject): Widget. event (QEvent): Event. """ if (hasattr(widget, "window") and widget.window() is self): self.__updateGripRect() if (hasattr(event, "x") and self.titlebar().rect().contains( self.mapFromParent(QCursor.pos())) and self.titlebar().mouseOverTitlebar( event.x(), event.y())): # when cursor is over titlebar if event.type( ) == QMouseEvent.MouseButtonPress and event.buttons( ) == Qt.LeftButton: # if titlebar clicked with left button self.__moving = True margins = self.titlebar().contentsMargins() self.__move_offset = event.pos() + self.titlebar().pos( ) - QPoint(margins.left(), margins.top()) elif event.type() == QMouseEvent.MouseMove and event.buttons( ) == Qt.LeftButton: # if holding left button and moving window if self.__moving: if self.windowState() == Qt.WindowMaximized: self.setWindowState(Qt.WindowNoState) self.titlebar().on_btnRestore_clicked() self.move(event.pos() - self.mapFromParent(event.pos())) self.__moving = False else: self.move(event.globalPos() - self.__move_offset) elif event.type( ) == QMouseEvent.MouseButtonRelease and event.button( ) == Qt.LeftButton: # if left button released self.__moving = False screen = QApplication.instance().desktop( ).availableGeometry() if event.globalY() == 0: # snap to top edge self.titlebar().on_btnMaximize_clicked() elif event.globalX() == 0: # snap to left edge self.move(QPoint(0, 0)) self.resize(QSize(screen.width() / 2, screen.height())) self.setWindowState(Qt.WindowNoState) elif event.globalX() + 1 >= screen.width(): # snap to right edge self.move(QPoint(screen.width() / 2, 0)) self.resize(QSize(screen.width() / 2, screen.height())) self.setWindowState(Qt.WindowNoState) elif self.geometry().y() < 0: # move window if top of window is outside display self.move(QPoint(self.geometry().x(), 0)) elif event.type() == QMouseEvent.MouseButtonDblClick: # maximize/restore on double click if self.windowState() == Qt.WindowMaximized: self.setWindowState(Qt.WindowNoState) elif self.windowState() == Qt.WindowNoState: self.showMaximized() elif event.type() == QEvent.Leave and widget is self.titlebar(): # fixes __moving remain True after clicking titlebar on border and leaving titlebar self.__moving = False elif (self.windowState() not in (Qt.WindowFullScreen, Qt.WindowMaximized) and self.isResizingEnabled()): # when cursor is not over titlebar # and window is not maximized or in full screen mode # button pressed if event.type() == QMouseEvent.MouseButtonPress: if self.__griprect.contains( event.pos() ) and not self.__horizontalResizing and not self.__verticalResizing: # corner grip self.__resizing = True elif self.__hgriprect.contains(event.pos( )) and not self.__resizing and not self.__verticalResizing: # right grip self.__horizontalResizing = True elif self.__vgriprect.contains( event.pos() ) and not self.__resizing and not self.__horizontalResizing: # bottom grip self.__verticalResizing = True if event.type() == QMouseEvent.MouseButtonRelease: # stop resizing if self.__resizing or self.__horizontalResizing or self.__verticalResizing: self.__resizing = False self.__horizontalResizing = False self.__verticalResizing = False self.setCursor(Qt.ArrowCursor) if event.type() == QMouseEvent.MouseMove: # moving cursor while holding left button -> resize if self.__resizing and not self.__horizontalResizing and not self.__verticalResizing: if event.buttons() == Qt.LeftButton: self.setCursor(Qt.SizeFDiagCursor) self.resize(QSize(event.x(), event.y())) elif self.__horizontalResizing and not self.__resizing and not self.__verticalResizing: if event.buttons() == Qt.LeftButton: self.setCursor(Qt.SizeHorCursor) self.resize(QSize(event.x(), self.height())) elif self.__verticalResizing and not self.__resizing and not self.__horizontalResizing: if event.buttons() == Qt.LeftButton: self.setCursor(Qt.SizeVerCursor) self.resize(QSize(self.width(), event.y())) else: if (self.__griprect.contains(event.pos()) and not self.__horizontalResizing and not self.__verticalResizing): self.setCursor(Qt.SizeFDiagCursor) elif (self.__hgriprect.contains(event.pos()) and not self.__resizing and not self.__verticalResizing): self.setCursor(Qt.SizeHorCursor) elif (self.__vgriprect.contains(event.pos()) and not self.__resizing and not self.__horizontalResizing): self.setCursor(Qt.SizeVerCursor) else: self.setCursor(Qt.ArrowCursor) return super().eventFilter(widget, event)