def eventFilter(self, obj: QObject, event: QEvent): if event.type() == QEvent.MouseButtonPress and obj == self.view( ).viewport(): # Prevent pop-up to close if user click in TreeView index = self.view().indexAt(event.pos()) if not self.view().visualRect(index).contains(event.pos()): self._skip_next_hide = True return False
def eventFilter(self, watched: QObject, event: QEvent) -> bool: pos = tuple(map(int, self._posModel.cursorPos[:2])) if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Space: # Bring to front and select the box at the bottom of the stack. items = watched.scene().items(QPointF(*pos)) rects = [item for item in items if isinstance(item, QGraphicsResizableRect)] if len(rects) > 1: rects[-1].setZValue(rects[0].zValue() + 1) rects[0].setSelected(False) rects[-1].setSelected(True) elif event.type() == QEvent.KeyPress and event.key() == Qt.Key_Control: QApplication.setOverrideCursor(Qt.OpenHandCursor) elif event.type() == QEvent.KeyRelease and event.key() == Qt.Key_Control: QApplication.restoreOverrideCursor() elif event.type() == QEvent.MouseButtonPress and event.button() == Qt.LeftButton: # Start new box rectangle. self._origin = QPoint(event.pos()) self._originPos = pos self._rubberBand.setGeometry(self._origin.x(), self._origin.y(), 0, 0) items = watched.scene().items(QPointF(*pos)) handles = [item for item in items if isinstance(item, ResizeHandle)] modifiers = QApplication.keyboardModifiers() if modifiers != Qt.ControlModifier and modifiers != Qt.ShiftModifier and not handles: self._rubberBand.show() elif event.type() == QEvent.MouseMove: if self._rubberBand.isVisible(): # Resize new box rectangle. self._rubberBand.setGeometry( min(self._origin.x(), event.pos().x()), min(self._origin.y(), event.pos().y()), abs(self._origin.x() - event.pos().x()), abs(self._origin.y() - event.pos().y()), ) elif event.type() == QEvent.MouseButtonRelease and event.button() == Qt.LeftButton: if self._rubberBand.isVisible(): # Finish new box rectangle. self._rubberBand.hide() self.boxDrawn.emit(roi2rect(self._originPos, pos)) self._origin = None self._originPos = None return self._base.eventFilter(watched, event)
def eventFilter(self, object: QtCore.QObject, event: QtCore.QEvent): # If mouse is on an edge, start the drag resize process if event.type() == QtCore.QEvent.MouseButtonPress: x = event.pos().x() if self.over_column_edge(x) is not None: self.column_being_resized = self.over_column_edge(x) self.resize_start_position = x self.initial_column_width = self.columnWidth( self.column_being_resized) return True else: self.column_being_resized = None # End the drag process if event.type() == QtCore.QEvent.MouseButtonRelease: self.column_being_resized = None # Auto size the column that was double clicked if event.type() == QtCore.QEvent.MouseButtonDblClick: x = event.pos().x() if self.over_column_edge(x) is not None: column_index = self.over_column_edge(x) self.parent.auto_size_column(column_index) return True # Handle active drag resizing if event.type() == QtCore.QEvent.MouseMove: x = event.pos().x() # If this is None, there is no drag resize happening if self.column_being_resized is not None: width = self.initial_column_width + ( x - self.resize_start_position) if width > 10: self.setColumnWidth(self.column_being_resized, width) if self.orientation == Qt.Horizontal: self.parent.dataView.setColumnWidth( self.column_being_resized, width) self.updateGeometry() self.parent.dataView.updateGeometry() return True # Set the cursor shape if self.over_column_edge(x) is not None: self.viewport().setCursor(QtGui.QCursor(Qt.SplitHCursor)) else: self.viewport().setCursor(QtGui.QCursor(Qt.ArrowCursor)) return False
def makeMove(event: QEvent): currentPlayerIndex = GamePlayer.getCurrentPlayerIndex() if event.type() == QEvent.MouseButtonRelease: pos = event.pos() i = pos.x() // 100 j = pos.y() // 100 if i < 0 or i > 2 or j < 0 or j > 2: return None playerSymbol = playerSymbols[currentPlayerIndex] if board[i][j] == '_': board[i][j] = playerSymbol if hasWon(playerSymbol): GamePlayer.showMessageLaterForAll( 'Game Over', GamePlayer.getPlayerNames()[currentPlayerIndex] + ' has won the game.') return -1 draw = True for i in range(3): for j in range(3): if board[i][j] == '_': draw = False break if draw: GamePlayer.showMessageForAll('Game Over', 'The game is a draw.') return -1 else: return (GamePlayer.getCurrentPlayerIndex() + 1) % GamePlayer.getPlayerCount()
def editorEvent( self, event: QtCore.QEvent, model: QtCore.QAbstractItemModel, option: QStyleOptionViewItem, index: QtCore.QModelIndex, ) -> bool: if not (index.flags() & Qt.ItemIsEditable) != 0: return False if event.type() == QEvent.MouseButtonRelease or event.type( ) == QEvent.MouseButtonDblClick: if event.button() != Qt.LeftButton or not self.get_checkbox_rect( option).contains(event.pos()): return False if event.type() == QEvent.MouseButtonDblClick: return True elif event.type() == QEvent.KeyPress: if event.key() != Qt.Key_Space and event.key() != Qt.Key_Select: return False else: return False self.setModelData(None, model, index) return True
def _rightClicked(self, row: int, e: QtCore.QEvent): ''' opens a context menu ''' domain = self.getDomainByIndex(row) self.setSelection(self.rectForIndex(self.model.indexFromItem(domain)), QtCore.QItemSelectionModel.ClearAndSelect) domain.popMenu.exec_(self.mapToGlobal(e.pos()))
def eventFilter(self, triggering_object: QObject, event: QEvent) -> bool: """Event filter, looking for window resize events so we can remember the new size""" if isinstance(event, QResizeEvent): window_size = event.size() self._preferences.set_main_window_size(window_size) elif isinstance(event, QMoveEvent): new_position = event.pos() self._preferences.set_main_window_position(new_position) return False # Didn't handle event
def eventFilter(self, source, event: QEvent): if self.data is not None: if event.type() == QEvent.MouseButtonRelease: pos = event.pos() data = self.get_data_at(pos.x(), pos.y()) if data is not None: if event.button() == Qt.LeftButton: self.mouseLeftClicked.emit(data) elif event.button() == Qt.RightButton: self.mouseRightClicked.emit(data) elif event.type() == QEvent.MouseMove: pos = event.pos() data = self.get_data_at(pos.x(), pos.y()) if data is not None: self.mouseOver.emit(data) self.viewport().setCursor(Qt.PointingHandCursor) else: self.viewport().setCursor(Qt.ArrowCursor) return QWidget.eventFilter(self, source, event)
def mousePressEvent(self, event: QEvent): x = event.pos().x() y = event.pos().y() for entity in self.engine.entities: if entity.contains_point(x, y): new_mass = entity.eject(int(entity.amount / 2), entity.density, random.randint(-2, 2), random.randint(-2, 2)) if new_mass != None: self.engine.add_mass(new_mass) break
def makeMove(event: QEvent): # DEFINITON DER FUNKTION MAKE A MOVE currentPlayerIndex = GamePlayer.getCurrentPlayerIndex() if event.type( ) == QEvent.MouseButtonRelease: # BERECHNET FELD AN DER MOUSE LOSGELASSEN WURDE pos = event.pos() x = math.floor(pos.x() / 100) y = math.floor(pos.y() / 100) print("==================") print("x", x) print("y", y) if y != 0 or ( x < 0 or x > cols - 1 ): # CHECK OB AUSSERHALB DES FELDES GEDRÜCKT WURDE WENN NICHT ZU return None playerSymbol = playerSymbols[currentPlayerIndex] # If the place at the top if not empty, then it means that the # whole column is occupy if board[x][1] == '_': # ZUGEWIESEN for yToCheck in range(rows - 1, 0, -1): # if board[y + 1][x] == '_': if board[x][yToCheck] == '_': board[x][yToCheck] = playerSymbol break # board[y][x] = playerSymbol # CHANGE FOR VIERGEWINNT WENN FELD DRUNTER FREI IST; DANN J ERHOEHEN # UND NÄCHSTEN CHECKEN if hasWon(playerSymbol): GamePlayer.showMessageLaterForAll( 'Game Over', GamePlayer.getPlayerNames()[currentPlayerIndex] + ' has won the game.') return -1 draw = True for y in range(rows): for x in range(cols): if board[y][x] == '_': draw = False break if draw: GamePlayer.showMessageForAll('Game Over', 'The game is a draw.') return -1 else: return (GamePlayer.getCurrentPlayerIndex() + 1) % GamePlayer.getPlayerCount()
def eventFilter(self, triggering_object: QObject, event: QEvent) -> bool: """ Event filter, looking for window resize events so we can remember the new size :param triggering_object: Object trigger the event we will be inspecting :param event: The event we are inspecting for move or resize :return: Always "false" indicating event still needs to be handled (we didn't) """ if isinstance(event, QResizeEvent): window_size = event.size() self._preferences.set_main_window_size(window_size) elif isinstance(event, QMoveEvent): new_position = event.pos() self._preferences.set_main_window_position(new_position) return False # Didn't handle event
def sceneEventFilter(self, watched: QGraphicsItem, event: QEvent) -> bool: """ Override of QGraphicsItem::sceneEventFilter Responds to events initiated by anchors (graphics children). Adjusts the ROI and anchors in response to a mouse drag on the anchors. @param watched: anchor (graphics child) @param event: graphics scene event @return: should always be False to allow the event to propagate further. """ # check event is a mouse drag of an anchor if isinstance(watched, Anchor) and isinstance( event, QGraphicsSceneMouseEvent): self.adjust_roi(watched, event.pos()) # continue processing return False
def eventFilter(self, widget, event: QtCore.QEvent): if widget is self.viewport(): index = self._last_index if event.type() == QtCore.QEvent.MouseMove: index = self.indexAt(event.pos()) elif event.type() == QtCore.QEvent.Leave: index = QtCore.QModelIndex() if index != self._last_index: row = index.row() column = index.column() item = self.item(row, column) if item is not None: self.rowEntered.emit(row) self._last_index = QtCore.QPersistentModelIndex(index) return super(DistrictTableWidget, self).eventFilter(widget, event)
def eventFilter(self, obj: QtCore.QObject, event: QtCore.QEvent) -> bool: if obj == self.lineEdit(): if event.type() == QtCore.QEvent.MouseButtonRelease \ and event.button() == QtCore.Qt.LeftButton: if self.closeOnLineEditClick: self.hidePopup() else: self.showPopup() return True return False elif obj == self.view().viewport(): if event.type() == QtCore.QEvent.MouseButtonRelease: index = self.view().indexAt(event.pos()) item = self.model().item(index.row()) if item.checkState() == QtCore.Qt.Checked: item.setCheckState(QtCore.Qt.Unchecked) else: item.setCheckState(QtCore.Qt.Checked) return True return False
def eventFilter(self, object: QObject, event: QEvent) -> bool: try: # start dragging process if event.type() == QEvent.MouseButtonPress: # store mouse position and header being resized mousePosition = event.pos() self._header_being_resized = self.overHeaderEdge(mousePosition) if self._header_being_resized is not None: self.print('>>Resize triggered') self.print('Header selected : ', self._header_being_resized) # store initial header size if self.isHorizontal(): self._resize_start_position = mousePosition.x() self._header_initial_size = self.columnWidth( self._header_being_resized) else: self._resize_start_position = mousePosition.y() self._header_initial_size = self.rowHeight( self._header_being_resized) return True # end dragging process elif event.type() == QEvent.MouseButtonRelease: self._header_being_resized = None return True # Handle active drag resizing elif event.type() == QEvent.MouseMove: if self._header_being_resized is not None: self.print('>>Resizing') mousePosition = event.pos() if self.isHorizontal(): new_size = self._header_initial_size + mousePosition.x( ) - self._resize_start_position self.setColumnWidth(self._header_being_resized, new_size) self.dataView.setColumnWidth( self._header_being_resized, new_size) else: new_size = self._header_initial_size + mousePosition.y( ) - self._resize_start_position self.setRowHeight(self._header_being_resized, new_size) self.dataView.setRowHeight(self._header_being_resized, new_size) return True else: # Change cursor upon hover if self.overHeaderEdge(event.pos()) is not None: if self.isHorizontal(): # QApplication.setOverrideCursor(Qt.SplitHCursor) self.setCursor(Qt.SplitHCursor) else: # QApplication.setOverrideCursor(Qt.SplitVCursor) self.setCursor(Qt.SplitVCursor) else: # QApplication.restoreOverrideCursor() self.setCursor(Qt.ArrowCursor) except Exception as e: dumpException(e) return False
def eventFilter(self, a0: QtCore.QObject, a1: QtCore.QEvent) -> bool: if a0 == self or a0 == self.label or a0 == self.label_2: if a1.type() == QtCore.QEvent.HoverEnter: self.setStyleSheet("QFrame{background-color:rgb(%s)}" % self.bjs) self.w.show() else: if a1.type() == QtCore.QEvent.HoverLeave: self.setStyleSheet("QFrame{}") self.w.hide() else: if a1.type() == QtCore.QEvent.HoverMove: if a1.pos().x() < 15: if a1.pos().y() < 15: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor)) self.mc = MC.BD else: if self.height() - a1.pos().y() < 15: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor)) self.mc = MC.BD else: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor)) self.mc = MC.H else: if self.width() - a1.pos().x() < 15: if a1.pos().y() < 15: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeBDiagCursor)) self.mc = MC.BD else: if self.height() - a1.pos().y() < 15: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeFDiagCursor)) self.mc = MC.BD else: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeHorCursor)) self.mc = MC.H else: if self.height() - a1.pos().y() < 15 or a1.pos().y() < 15: self.setCursor(QtGui.QCursor(QtCore.Qt.SizeVerCursor)) self.mc = MC.V else: self.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor)) # QtCore.Qt.OpenHandCursor if self.ml: c = self.cursor().pos() if self.cursor().shape() == QtCore.Qt.ArrowCursor: self.move(self.p + c - self.mp) else: if self.mc == MC.BD: self.setFixedSize_my( self.size() + QtCore.QSize(c.x() - self.mp.x(), c.y() - self.mp.y())) else: if self.mc == MC.H: self.setFixedSize_my(self.size() + QtCore.QSize(c.x() - self.mp.x(), 0)) else: self.setFixedSize_my(self.size() + QtCore.QSize(0, c.y() - self.mp.y())) self.p = self.pos() self.mp = c else: if a1.type() == QtCore.QEvent.MouseButtonRelease: self.ml = False return QtWidgets.QWidget.eventFilter(self, a0, a1)
def enterEvent(self, evt: QtCore.QEvent) -> None: evt = down_cast(QtGui.QEnterEvent, evt) self.setHoverMeasureEditor(self.measureEditorAt(evt.pos()), evt)
def eventFilter(self, target: QtCore.QObject, evt: QtCore.QEvent) -> bool: if evt.type() == QtCore.QEvent.MouseMove: assert isinstance(evt, QtGui.QMouseEvent), evt assert isinstance(target, (QtWidgets.QWidget, QtGui.QWindow)), target self.globalMousePosChanged.emit(target.mapToGlobal(evt.pos())) return super().eventFilter(target, evt)
def eventFilter(self, object: QtCore.QObject, event: QtCore.QEvent): # If mouse is on an edge, start the drag resize process if event.type() == QtCore.QEvent.MouseButtonPress: if self.orientation == Qt.Horizontal: mouse_position = event.pos().x() else: mouse_position = event.pos().y() if self.over_header_edge(mouse_position) is not None: self.header_being_resized = self.over_header_edge(mouse_position) self.resize_start_position = mouse_position if self.orientation == Qt.Horizontal: self.initial_header_size = self.columnWidth( self.header_being_resized ) elif self.orientation == Qt.Vertical: self.initial_header_size = self.rowHeight(self.header_being_resized) return True else: self.header_being_resized = None # End the drag process if event.type() == QtCore.QEvent.MouseButtonRelease: self.header_being_resized = None # Auto size the column that was double clicked if event.type() == QtCore.QEvent.MouseButtonDblClick: if self.orientation == Qt.Horizontal: mouse_position = event.pos().x() else: mouse_position = event.pos().y() # Find which column or row edge the mouse was over and auto size it if self.over_header_edge(mouse_position) is not None: header_index = self.over_header_edge(mouse_position) if self.orientation == Qt.Horizontal: self.parent().auto_size_column(header_index) elif self.orientation == Qt.Vertical: self.parent().auto_size_row(header_index) return True # Handle active drag resizing if event.type() == QtCore.QEvent.MouseMove: if self.orientation == Qt.Horizontal: mouse_position = event.pos().x() elif self.orientation == Qt.Vertical: mouse_position = event.pos().y() # If this is None, there is no drag resize happening if self.header_being_resized is not None: size = self.initial_header_size + ( mouse_position - self.resize_start_position ) if size > 10: if self.orientation == Qt.Horizontal: self.setColumnWidth(self.header_being_resized, size) self.parent().dataView.setColumnWidth(self.header_being_resized, size) if self.orientation == Qt.Vertical: self.setRowHeight(self.header_being_resized, size) self.parent().dataView.setRowHeight(self.header_being_resized, size) self.updateGeometry() self.parent().dataView.updateGeometry() return True # Set the cursor shape if self.over_header_edge(mouse_position) is not None: if self.orientation == Qt.Horizontal: self.viewport().setCursor(QtGui.QCursor(Qt.SplitHCursor)) elif self.orientation == Qt.Vertical: self.viewport().setCursor(QtGui.QCursor(Qt.SplitVCursor)) else: self.viewport().setCursor(QtGui.QCursor(Qt.ArrowCursor)) return False