Ejemplo n.º 1
0
    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
Ejemplo n.º 2
0
    def eventFilter(self, source, event: QtCore.QEvent):
        if source is self._scene:
            if event.type() == QtCore.QEvent.GraphicsSceneMouseRelease and\
               event.button() == QtCore.Qt.LeftButton and\
               event.scenePos().x() >= 0 and event.scenePos().x() < self._scene.width() and\
               event.scenePos().y() >= 0 and event.scenePos().y() < self._scene.height():

                pos = event.scenePos()
                box_clicked_x = self.windowToSquareX(pos.x())
                box_clicked_y = self.windowToSquareYInv(pos.y())
                new_origin = int(box_clicked_x +
                                 box_clicked_y * self.hor_pixels)

                if not self.shift_down:
                    self.curr_origins.clear()

                self.curr_origins.add(new_origin)
                print(self.curr_origins)
                self.originsUpdated.emit(self.curr_origins)

            elif event.type() == QtCore.QEvent.GraphicsSceneMouseMove:
                self.onMouseHover(event.scenePos())

            elif event.type() == QtCore.QEvent.KeyPress:
                if event.key() == QtCore.Qt.Key_Shift:
                    self.shift_down = True

            elif event.type() == QtCore.QEvent.KeyRelease:
                if event.key() == QtCore.Qt.Key_Shift:
                    self.shift_down = False
        return widgets.QWidget.eventFilter(self, source, event)
Ejemplo n.º 3
0
 def _checkLineEditEvent(self, evt: QEvent) -> bool:
     if evt.type() == QEvent.MouseButtonPress:
         return True
     if evt.type() == QEvent.KeyPress:
         if evt.key() == Qt.Key_Return or evt.key() == Qt.Key_Space or evt.key() == Qt.Key_Enter:
             return True
     return False
Ejemplo n.º 4
0
 def eventFilter(self, source: QObject, event: QEvent) -> bool:
     """Override the eventFilter method of QObject to intercept keystrokes."""
     if event.type() == QEvent.KeyPress:
         if app_config['Sound']['sounds'] == 'on':
             if event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
                 self.sound.stop()
                 self.enter_sound.play()
             if event.key() == Qt.Key_Backspace:
                 self.sound.stop()
                 self.backspace_sound.play()
             else:
                 self.sound.play()
         if event.key(
         ) == Qt.Key_Space:  # prank user with instant disappearing dialog
             if random.random() > 0.8:
                 dlg = QMessageBox(self)
                 dlg.setWindowTitle("Are you sure?")
                 dlg.setText("_" * 100)
                 dlg.show()
                 dlg.close()
         if random.random() > 0.97:
             troubleshooter = Troubleshooter(
                 self.ctx)  # pester the user with a troubleshooter
             troubleshooter.exec()
     return False  # imitate overridden method
Ejemplo n.º 5
0
    def event(self, event: QEvent) -> bool:
        if event.type() != QEvent.Type.KeyPress:
            return super().event(event)

        assert isinstance(event, QKeyEvent)

        if not self.text():
            return super().event(event)

        if event.key() not in {Qt.Key.Key_Tab, Qt.Key.Key_Backtab}:
            return super().event(event)

        if self._compl is None:
            compl = self._make_autocomplete()
            if not compl.suggestions:
                return True
            self._compl = compl
            self._compl.index = 0 if event.key() == Qt.Key.Key_Tab else -1
        else:
            self._compl.index += 1 if event.key() == Qt.Key.Key_Tab else 1

        self._compl.index %= len(self._compl.suggestions)
        self.setText(self._compl.prefix[:self._compl.start_pos] +
                     self._compl.suggestions[self._compl.index])
        return True
Ejemplo n.º 6
0
 def keyPressEvent(self, evt: QEvent) -> None:
     """
     Prevent accidentally closing dialog when editing complex widgets
     by ignoring Return and Escape
     """
     if evt.key() == Qt.Key_Enter or evt.key() == Qt.Key_Return:
         return evt.accept()
     super().keyPressEvent(evt)
Ejemplo n.º 7
0
    def eventFilter(self, obj, event: QtCore.QEvent) -> bool:  # pylint: disable=unused-argument
        if event.type() == QtCore.QEvent.KeyPress:
            if event.key() == QtCore.Qt.Key_Control:
                self._dialog._ctrl_pressed = True
                return True

        elif event.type() == QtCore.QEvent.KeyRelease:
            if event.key() == QtCore.Qt.Key_Control:
                self._dialog._ctrl_pressed = False
                return True

        return False
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    def eventFilter(self, object: QtCore.QObject,
                    event: QtCore.QEvent) -> bool:
        """Event Filter."""

        if isinstance(event, QtGui.QKeyEvent):
            if event.key() in (QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter):
                self.open()
                return True

            elif event.key() == QtCore.Qt.Key_Escape:
                self.close()
                return True

        return super().eventFilter(object, event)
Ejemplo n.º 10
0
    def keyPressEvent(self, e: QtCore.QEvent):
        if e.key() == 16777223:  # delete
            if len(self.text()):
                self.last_text = self.text()

            self.setText("")

        elif e.key() == 90 and e.modifiers(
        ) == QtCore.Qt.ControlModifier:  # ctrl+z
            if self.last_text:
                self.setText(self.last_text)
                self.last_text = None

        else:
            e.ignore()
Ejemplo n.º 11
0
    def eventFilter(self, obj: QObject, ev: QEvent):
        if obj != self.popup:
            return False

        if ev.type() == QEvent.MouseButtonPress:
            self.popup.hide()
            self.editor.setFocus()
            return True

        if ev.type() == QEvent.KeyPress:
            consumed = False
            ev: QKeyEvent
            key = ev.key()

            if (Qt.Key_Enter == key) or (Qt.Key_Return == key):
                self.doneCompletion()
                consumed = True
            elif Qt.Key_Return == key:
                self.editor.setFocus()
                self.popup.hide()
                consumed = True
            elif (key == Qt.Key_Undo) or (key == Qt.Key_Down) or (
                    key == Qt.Key_Home) or (key == Qt.Key_End) or (
                        key == Qt.Key_PageUp) or (key == Qt.Key_PageDown):
                pass
            else:
                self.editor.setFocus()
                self.editor.event(ev)
                self.popup.hide()
            return consumed
        return False
Ejemplo n.º 12
0
 def keyPressEvent(self, event: QEvent):
     super().keyPressEvent(event)
     if event.key() in [Qt.Key_Space, Qt.Key_Escape]:
         event.accept()
         self.accept.emit()
     else:
         event.ignore()
Ejemplo n.º 13
0
 def eventFilter(self, widget: QObject, event: QEvent):
     """
     Event filter for tab down on third column.
     """
     if event.type() == QEvent.KeyRelease and event.key() == Qt.Key_Backtab:
         row = self.currentRow()
         if row > 0:
             self.setCurrentCell(row - 1, 2)
         return True
     if event.type() == QEvent.KeyRelease and event.key() == Qt.Key_Tab:
         row = self.currentRow()
         self.setCurrentCell(row, 2)
         return True
     if event.type() == QEvent.Wheel:
         pass
     return QObject.eventFilter(self, widget, event)
Ejemplo n.º 14
0
    def keyPressEvent(self, event: QEvent):
        key = event.key()
        logger.info("keyPressEvent: %s", key)

        if key in [Qt.Key_Q]:
            event.accept()
            self.quit.emit()
        else:
            event.ignore()
Ejemplo n.º 15
0
 def eventFilter(self, o: QObject, e: QEvent) -> bool:
     if e.type() == QEvent.KeyPress:
         k = e.key()
         if self._editable and (Qt.Key_0 <= k <= Qt.Key_9
                                or Qt.Key_Up <= k <= Qt.Key_Down or k
                                == Qt.Key_Backspace or k == Qt.Key_Delete):
             return True
         else:
             super().eventFilter(o, e)
     else:
         super().eventFilter(o, e)
Ejemplo n.º 16
0
    def editorEvent(
        self,
        event: QEvent,
        model: QAbstractItemModel,
        option: QStyleOptionViewItem,
        index: QModelIndex,
    ) -> bool:
        if not int(index.flags() & Qt.ItemIsEditable) > 0:
            return False

        if (
            event.type() == QEvent.MouseButtonRelease
            and event.button() == Qt.LeftButton
        ):
            self.setModelData(None, model, index)
            return True
        elif event.type() == QEvent.KeyPress:
            if event.key() != Qt.Key_Space and event.key() != Qt.Key_Select:
                return False
            self.setModelData(None, model, index)
            return True
        return False
Ejemplo n.º 17
0
 def eventFilter(self, source: QObject, event: QEvent) -> bool:
     if event.type() == QEvent.KeyPress and source is self.searchBar:
         if event.key() == Qt.Key_Enter - 1:
             self.search_in_table()
             return True
     if event.type(
     ) == QEvent.ContextMenu and source is self.table and self.table.currentIndex(
     ).row() >= 0:
         index = self.table.currentIndex().row()
         self.table.selectRow(index)
         if not self.context_table_menu.exec_(event.globalPos()):
             self.table.setCurrentIndex(QModelIndex())
         return True
     return super().eventFilter(source, event)
Ejemplo n.º 18
0
 def eventFilter(self, obj: QObject, event: QEvent) -> None:
     if self.currentlySelectingNewOrigin:
         et = event.type()
         if et == QEvent.MouseMove:
             np = self.rasterSurface.mapFromGlobal(event.globalPos())
             self.originPoint.x, self.originPoint.y = \
                 np.x(), \
                 self.rasterSurface.height() - np.y()
             self.rasterSurface.repaint()
         elif et == QEvent.MouseButtonRelease:
             self.endSelectNewOriginPoint()
         elif et == QEvent.KeyRelease:
             key = event.key()
             if key == Qt.Key_Escape:
                 self.endSelectNewOriginPoint(False)
     return False
Ejemplo n.º 19
0
 def excel_like_enter_filter(source: QtWidgets.QTableView,
                             event: QtCore.QEvent):
     if event.type() == event.KeyPress:
         if event.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter]:
             if int(source.editTriggers()) > int(
                     QtWidgets.QAbstractItemView.NoEditTriggers):
                 next_row = source.currentIndex().row() + 1
                 if next_row + 1 > source.model().rowCount():
                     next_row -= 1
                 if source.state() == source.EditingState:
                     next_index = source.model().index(
                         next_row,
                         source.currentIndex().column())
                     source.setCurrentIndex(next_index)
                 else:
                     source.edit(source.currentIndex())
Ejemplo n.º 20
0
    def keyPressEvent(self, event: QtCore.QEvent):
        if event.key() == 90 and event.modifiers(
        ) == QtCore.Qt.ControlModifier:
            if self.deleted_domain:
                domain = self.deleted_domain
                self.addDomain(
                    domain[1], domain[0], domain[2]
                )  # addDomain(name, id_)  /  domain = (id_, name, indexAt)

                if hasattr(self.parent.window, "config"):
                    self.parent.window.config.write("peers",
                                                    (domain[2],
                                                     (domain[0], domain[1])))

                self.deleted_domain = None
        else:
            event.ignore()
    def keyPressEvent(self, event: QtCore.QEvent):
        if event.key() == 90 and event.modifiers(
        ) == QtCore.Qt.ControlModifier:
            if self.deleted_peer:
                peer = self.deleted_peer
                self.addPeer(
                    peer[1], peer[0], peer[2]
                )  # addPeer(name, id_)  /  peer = (id_, name, indexAt)

                if hasattr(self.parent.window, "config"):
                    self.parent.window.config.write("peers",
                                                    (peer[2],
                                                     (peer[0], peer[1])))

                self.deleted_peer = None
        else:
            event.ignore()
Ejemplo n.º 22
0
    def eventFilter(self, obj: QObject, event: QEvent) -> None:
        if obj is not self.rasterSurface or self._editingPolygon is None:
            return False

        et = event.type()
        if et == QEvent.MouseMove:
            self._cursorPos = self.rasterSurface.mapFromGlobal(
                event.globalPos()
            )
            if self._editingPolygon is not None:
                if self._highlightedPoint is not None and \
                   self._highlightedPointActive:
                    self._highlightedPoint.x, self._highlightedPoint.y = \
                        self._cursorPos.x(), \
                        self.rasterSurface.height() - self._cursorPos.y()
                    self._editingPolygon.update_cache()
                self.rasterSurface.repaint()
        elif self._editingPolygon is not None:
            if et == QEvent.MouseButtonPress:
                np = self._cursorPos
                if self.rasterSurface.geometry().contains(np):
                    x = np.x()
                    y = np.y()
                    height = self.rasterSurface.height()

                    for p in self._editingPolygon.points:
                        if abs(x - p.x) <= 5 and abs(y - height + p.y) <= 5:
                            self._highlightedPoint = p
                            self._highlightedPointActive = True
                            self.rasterSurface.repaint()
                            break
            elif et == QEvent.MouseButtonRelease:
                # self._highlightedPoint = None
                self._highlightedPointActive = False
                self.rasterSurface.repaint()
            elif et == QEvent.KeyRelease:
                key = event.key()
                if key == Qt.Key_Return or key == Qt.Key_Enter:
                    self.endEditing(True)
                elif key == Qt.Key_Escape:
                    self.endEditing(False)

        return False
Ejemplo n.º 23
0
    def keyPressEvent(self, event: QEvent):
        """keyPressEvent override of the QDialog class to extend keyboard
        interaction with the tables (i.e. delete rows).
        """
        if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Delete:
            # if the delete key was pressed
            widget = QApplication.focusObject()  # get widget that has focus
            widget_name = widget.objectName()
            if widget_name == self.ui.tableViewInput.objectName() \
                    or widget_name == self.ui.tableViewOutput.objectName():
                table_sel_model = widget.selectionModel()
                indexes = [
                    QPersistentModelIndex(index)
                    for index in table_sel_model.selectedIndexes()
                ]

                for index in indexes:
                    # delete selected rows
                    widget.model().removeRow(index.row())

                # clear selection
                widget.selectionModel().clearSelection()
Ejemplo n.º 24
0
    def eventFilter(self, source: QWidget, event: QEvent):
        """Quotes editor content for <Ctrl>+<Enter> and <Ctrl>+<Return>

        Overrides SpellTextEdit default shortcut. Counts as undoable action.

        :param source: Source widget of event
        :param event: Event to be filtered

        """

        if event.type() == QEvent.ShortcutOverride \
           and event.modifiers() == Qt.ControlModifier \
           and source == self \
           and event.key() in (Qt.Key_Return, Qt.Key_Enter):

            code = quote(source.toPlainText())
            index = self.main_window.grid.currentIndex()
            description = "Quote code for cell {}".format(index)
            cmd = commands.SetCellCode(code, self.main_window.grid.model,
                                       index, description)
            self.main_window.undo_stack.push(cmd)

        return QWidget.eventFilter(self, source, event)
    def eventFilter(self, obj: QObject, event: QEvent) -> None:
        if obj is not self.rasterSurface:
            return False

        et = event.type()
        if et == QEvent.MouseMove:
            self._cursorPos = self.rasterSurface.mapFromGlobal(
                event.globalPos())
            if self._editingPolygon is not None:
                self.rasterSurface.repaint()
        elif self._editingPolygon is not None:
            if et == QEvent.MouseButtonPress:
                pass
            elif et == QEvent.MouseButtonRelease:
                np = self._cursorPos
                if self.rasterSurface.geometry().contains(np):
                    x = np.x()
                    y = np.y()

                    if self._editingPolygon.length <= 0:
                        self._firstPolygonPoint = np
                    elif abs(x - self._firstPolygonPoint.x()) <= 5 and \
                            abs(y - self._firstPolygonPoint.y()) < 5:
                        self.endEditing()  # the last point == first point
                        return False

                    self._editingPolygon.add_point(
                        x,
                        self.rasterSurface.height() - y)
            elif et == QEvent.KeyRelease:
                key = event.key()
                if key == Qt.Key_Return or key == Qt.Key_Enter:
                    self.endEditing(self._editingPolygon.length >= 3)
                elif key == Qt.Key_Escape:
                    self.endEditing(False)

        return False
Ejemplo n.º 26
0
    def keyPressEvent(self, event: QEvent):
        '''
        Process a key press. Any key press (not release) while the
        focus is in the board comes here. The key code is event.key()
        and the names of keys are defined in the Qt namespace, see
        http://doc.qt.io/qt-5/qt.html#Key-enum

        If we can handle the event we do; else pass it to our parent.

        Note that in left, right and rotate keys, we could look at the return
        from tryMove() and if it is False, we could maybe implement the "wall
        kick" feature.
        '''

        if self.isStarted and self.curPiece is not NO_T_mo:
            key = event.key()
            if key in self.validKeys:
                event.accept()  # Tell Qt, we got this one
                if key == Qt.Key_P:
                    self.togglePause()
                elif key == Qt.Key_Left:
                    self.tryMove(self.curPiece, self.curX - 1, self.curY)
                elif key == Qt.Key_Right:
                    self.tryMove(self.curPiece, self.curX + 1, self.curY)
                elif key == Qt.Key_Down:
                    self.tryMove(self.curPiece.rotateRight(), self.curX,
                                 self.curY)
                elif key == Qt.Key_Up:
                    self.tryMove(self.curPiece.rotateLeft(), self.curX,
                                 self.curY)
                elif key == Qt.Key_Space:
                    self.dropDown()
                elif key == Qt.Key_D:
                    self.oneLineDown()
        if not event.isAccepted():
            '''either we are paused or not one of our keys'''
            super().keyPressEvent(event)
Ejemplo n.º 27
0
    def keyPressEvent(self, event: QEvent) -> None:
        """ Track key press """

        if event.key() == Qt.Key_Escape:
            self._close()
Ejemplo n.º 28
0
 def keyPressEvent(self, event: QtCore.QEvent):
     if event.key() == 90 and event.modifiers(
     ) == QtCore.Qt.ControlModifier:
         self.domain_list.keyPressEvent(event)
     else:
         super().keyPressEvent(event)
Ejemplo n.º 29
0
 def keyPressEvent(self, event: QtCore.QEvent) -> None:
     if event.key() == QtCore.Qt.Key_Return:
         event.accept()
         self.enterPressed.emit(tuple(s.text() for s in self.selectedItems()))
     else:
         super().keyPressEvent(event)
Ejemplo n.º 30
0
 def event(self, event: QEvent):
     if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
         # print("tab pressed")
         return True
     return QGraphicsTextItem.event(self, event)