'Reimplement Qt method'

def keyPressEvent(self, event):

    self.key_pressed.emit(event)
    key = event.key()
    ctrl = (event.modifiers() & Qt.ControlModifier)
    shift = (event.modifiers() & Qt.ShiftModifier)
    text = to_text_string(event.text())
    if text:
        self.__clear_occurrences()
    if QToolTip.isVisible():
        self.hide_tooltip_if_necessary(key)
    checks = [('SelectAll', 'Select    All'), ('Copy', 'Copy'), ('Cut', 'Cut'), ('Paste', 'Paste')]
    for (qname, name) in checks:
        seq = getattr(QKeySequence, qname)
        sc = get_shortcut('editor', name)
        default = QKeySequence(seq).toString()
        if ((event == seq) and (sc != default)):
            for shortcut in self.shortcuts:
                (qsc, name, keystr) = shortcut.data
                if (keystr == default):
                    qsc.activated.emit()
                    event.ignore()
                    return
            event.ignore()
            return
    if (key in (Qt.Key_Enter, Qt.Key_Return)):
        if ((not shift) and (not ctrl)):
            if (self.add_colons_enabled and self.is_python_like() and self.autoinsert_colons()):
                self.textCursor().beginEditBlock()
                self.insert_text((':' + self.get_line_separator()))
                self.fix_indent()
                self.textCursor().endEditBlock()
            elif (self.is_completion_widget_visible() and self.codecompletion_enter):
                self.select_completion_list()
            else:
                cmt_or_str_cursor = self.in_comment_or_string()
                cursor = self.textCursor()
                cursor.setPosition(cursor.block().position(), QTextCursor.KeepAnchor)
                cmt_or_str_line_begin = self.in_comment_or_string(cursor=cursor)
                cmt_or_str = (cmt_or_str_cursor and cmt_or_str_line_begin)
                self.textCursor().beginEditBlock()
                TextEditBaseWidget.keyPressEvent(self, event)
                self.fix_indent(comment_or_string=cmt_or_str)
                self.textCursor().endEditBlock()
        elif shift:
            self.run_cell_and_advance.emit()
        elif ctrl:
            self.run_cell.emit()
    elif (shift and (key == Qt.Key_Delete)):
        self.delete_line()
    elif ((key == Qt.Key_Insert) and (not shift) and (not ctrl)):
        self.setOverwriteMode((not self.overwriteMode()))
    elif ((key == Qt.Key_Backspace) and (not shift) and (not ctrl)):
        leading_text = self.get_text('sol', 'cursor')
        leading_length = len(leading_text)
        trailing_spaces = (leading_length - len(leading_text.rstrip()))
        if (self.has_selected_text() or (not self.intelligent_backspace)):
            TextEditBaseWidget.keyPressEvent(self, event)
        else:
            trailing_text = self.get_text('cursor', 'eol')
            if ((not leading_text.strip()) and (leading_length > len(self.indent_chars))):
                if ((leading_length % len(self.indent_chars)) == 0):
                    self.unindent()
                else:
                    TextEditBaseWidget.keyPressEvent(self, event)
            elif (trailing_spaces and (not trailing_text.strip())):
                self.remove_suffix(leading_text[(- trailing_spaces):])
            elif (leading_text and trailing_text and ((leading_text[(-1)] + trailing_text[0]) in ('()', '[]', '{}', "''", '""'))):
                cursor = self.textCursor()
                cursor.movePosition(QTextCursor.PreviousCharacter)
                cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, 2)
                cursor.removeSelectedText()
            else:
                TextEditBaseWidget.keyPressEvent(self, event)
                if self.is_completion_widget_visible():
                    self.completion_text = self.completion_text[:(-1)]
    elif (key == Qt.Key_Period):
        self.insert_text(text)
        if (self.is_python_like() and (not self.in_comment_or_string()) and self.codecompletion_auto):
            last_obj = getobj(self.get_text('sol', 'cursor'))
            if (last_obj and (not last_obj.isdigit())):
                self.do_completion(automatic=True)
    elif (key == Qt.Key_Home):
        self.stdkey_home(shift, ctrl)
    elif (key == Qt.Key_End):
        self.stdkey_end(shift, ctrl)
    elif ((text == '(') and (not self.has_selected_text())):
        self.hide_completion_widget()
        self.handle_parentheses(text)
    elif ((text in ('[', '{')) and (not self.has_selected_text()) and self.close_parentheses_enabled):
        s_trailing_text = self.get_text('cursor', 'eol').strip()
        if ((len(s_trailing_text) == 0) or (s_trailing_text[0] in (',', ')', ']', '}'))):
            self.insert_text({'{': '{}', '[': '[]'}[text])
            cursor = self.textCursor()
            cursor.movePosition(QTextCursor.PreviousCharacter)
            self.setTextCursor(cursor)
        else:
            TextEditBaseWidget.keyPressEvent(self, event)
    elif ((key in (Qt.Key_QuoteDbl, Qt.Key_Apostrophe)) and self.close_quotes_enabled):
        self.autoinsert_quotes(key)
    elif ((key in (Qt.Key_ParenRight, Qt.Key_BraceRight, Qt.Key_BracketRight)) and (not self.has_selected_text()) and self.close_parentheses_enabled and (not self.textCursor().atBlockEnd())):
        cursor = self.textCursor()
        cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
        text = to_text_string(cursor.selectedText())
        if (text == {Qt.Key_ParenRight: ')', Qt.Key_BraceRight: '}', Qt.Key_BracketRight: ']'}[key]):
            cursor.clearSelection()
            self.setTextCursor(cursor)
        else:
            TextEditBaseWidget.keyPressEvent(self, event)
    elif ((key == Qt.Key_Colon) and (not self.has_selected_text()) and self.auto_unindent_enabled):
        leading_text = self.get_text('sol', 'cursor')
        if (leading_text.lstrip() in ('else', 'finally')):
            ind = (lambda txt: (len(txt) - len(txt.lstrip())))
            prevtxt = to_text_string(self.textCursor().block().previous().text())
            if (ind(leading_text) == ind(prevtxt)):
                self.unindent(force=True)
        TextEditBaseWidget.keyPressEvent(self, event)
    elif ((key == Qt.Key_Space) and (not shift) and (not ctrl) and (not self.has_selected_text()) and self.auto_unindent_enabled):
        leading_text = self.get_text('sol', 'cursor')
        if (leading_text.lstrip() in ('elif', 'except')):
            ind = (lambda txt: (len(txt) - len(txt.lstrip())))
            prevtxt = to_text_string(self.textCursor().block().previous().text())
            if (ind(leading_text) == ind(prevtxt)):
                self.unindent(force=True)
        TextEditBaseWidget.keyPressEvent(self, event)
    elif (key == Qt.Key_Tab):
        if ((not self.has_selected_text()) and (not self.tab_mode)):
            self.intelligent_tab()
        else:
            self.indent_or_replace()
    elif (key == Qt.Key_Backtab):
        if ((not self.has_selected_text()) and (not self.tab_mode)):
            self.intelligent_backtab()
        else:
            self.unindent()
    else:
        TextEditBaseWidget.keyPressEvent(self, event)
        if (self.is_completion_widget_visible() and text):
            self.completion_text += text
