def keyPressEvent(self, event): """ Performs indentation if tab key presed, else emits the keyPressed signal :param event: QKeyEvent """ # assert isinstance(event, QKeyEvent) event.stop = False # replace tabs by space if event.key() == Qt.Key_Tab: cursor = self.textCursor() assert isinstance(cursor, QTextCursor) if not cursor.hasSelection(): # insert tab at cursor pos cursor.insertText(" " * self.editor().TAB_SIZE) else: # indent whole selection self.indent(self.editor().TAB_SIZE) event.stop = True self.keyPressed.emit(event) if not event.stop: QPlainTextEdit.keyPressEvent(self, event) self.postKeyPressed.emit(event)
def keyPressEvent(self, event): self.dirty = True customKey = False #AutoTab if (event.key() == Qt.Key_Enter or event.key() == 16777220): customKey = True numTab = 0 #new line newBlock = self.textCursor().block() currLine = newBlock.text() tabRE = QRegExp("^[\t]*") tabRE.indexIn(currLine) numTab = tabRE.matchedLength() if (currLine != "" and currLine.strip()[-1] == "{"): numTab += 1 QPlainTextEdit.keyPressEvent(self, event) if (numTab > 0): tCursor = self.textCursor() for _ in range(0, numTab): tCursor.insertText("\t") #automatic close brace if currLine != "" and currLine.strip()[-1] == "{": tCursor.insertText("\n") for _ in range(0, numTab - 1): tCursor.insertText("\t") tCursor.insertText("}") tCursor.movePosition(QTextCursor.PreviousBlock) tCursor.movePosition(QTextCursor.EndOfLine) self.setTextCursor(tCursor) if event.key() == Qt.Key_Tab and self.textCursor().hasSelection(): customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) cur.insertText("\t") currBlock = currBlock.next() if event.key() == Qt.Key_Backtab and self.textCursor().hasSelection(): customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) if currBlock.text().left(1) == "\t": cur.deleteChar() currBlock = currBlock.next() # Allow commenting and uncommenting of blocks of code if event.key() == Qt.Key_Slash and event.modifiers( ) == Qt.ControlModifier: customKey = True selStart = self.textCursor().selectionStart() selEnd = self.textCursor().selectionEnd() cur = self.textCursor() endBlock = self.document().findBlock(selEnd) currBlock = self.document().findBlock(selStart) while currBlock.position() <= endBlock.position(): cur.setPosition(currBlock.position()) if currBlock.text()[0] == "#": cur.deleteChar() # Make sure we remove extra spaces while currBlock.text()[0] == " ": cur.deleteChar() else: cur.insertText("# ") currBlock = currBlock.next() # Open the text finder if event.key() == Qt.Key_F and event.modifiers() == Qt.ControlModifier: customKey = True print("Opening finder...") if not customKey: QPlainTextEdit.keyPressEvent(self, event)
def keyPressEvent(self, event): if self.preKeyPress.get(event.key(), lambda x: False)(event): return QPlainTextEdit.keyPressEvent(self, event)
def keyPressEvent(self, event): if self.preKeyPress.get(event.key(), lambda x: False)(event): return QPlainTextEdit.keyPressEvent(self, event)