diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index eb26b02243..ba28bbf013 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -590,24 +590,20 @@ class TextEdit(PlainTextEdit): num += 1 # }}} - def event(self, ev): - if ev.type() == ev.ToolTip: - self.show_tooltip(ev) + def override_shortcut(self, ev): + # Let the global cut/copy/paste/undo/redo shortcuts work, this avoids the nbsp + # problem as well, since they use the overridden copy() method + # instead of the one from Qt, and allows proper customization + # of the shortcuts + if ev in (QKeySequence.Copy, QKeySequence.Cut, QKeySequence.Paste, QKeySequence.Undo, QKeySequence.Redo): + ev.ignore() return True - if ev.type() == ev.ShortcutOverride: - # Let the global cut/copy/paste/undo/redo shortcuts work, this avoids the nbsp - # problem as well, since they use the overridden copy() method - # instead of the one from Qt, and allows proper customization - # of the shortcuts - if ev in (QKeySequence.Copy, QKeySequence.Cut, QKeySequence.Paste, QKeySequence.Undo, QKeySequence.Redo): - ev.ignore() - return True - # This is used to convert typed hex codes into unicode - # characters - if ev.key() == Qt.Key_X and ev.modifiers() == Qt.AltModifier: - ev.accept() - return True - return QPlainTextEdit.event(self, ev) + # This is used to convert typed hex codes into unicode + # characters + if ev.key() == Qt.Key_X and ev.modifiers() == Qt.AltModifier: + ev.accept() + return True + return PlainTextEdit.override_shortcut(self, ev) def text_for_range(self, block, r): c = self.textCursor() diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index ff86d0b3e1..6df74312c7 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -19,6 +19,7 @@ from PyQt5.Qt import ( QPixmap, QRect, QPlainTextEdit, QMimeData) from calibre import prepare_string_for_xml, human_readable +from calibre.constants import iswindows from calibre.ebooks.oeb.polish.utils import lead_text, guess_type from calibre.gui2 import error_dialog, choose_files, choose_save_file, info_dialog, choose_images from calibre.gui2.tweak_book import tprefs, current_container @@ -1118,7 +1119,7 @@ class AddCover(Dialog): class PlainTextEdit(QPlainTextEdit): # {{{ ''' A class that overrides some methods from QPlainTextEdit to fix handling - of the nbsp unicode character. ''' + of the nbsp unicode character and AltGr input method on windows. ''' def __init__(self, parent=None): QPlainTextEdit.__init__(self, parent) @@ -1148,6 +1149,31 @@ class PlainTextEdit(QPlainTextEdit): # {{{ ans = QMimeData() ans.setText(self.selected_text) return ans + + def show_tooltip(self, ev): + pass + + def override_shortcut(self, ev): + if iswindows and self.windows_ignore_altgr_shortcut(ev): + ev.accept() + return True + + def windows_ignore_altgr_shortcut(self, ev): + import win32api, win32con + s = win32api.GetAsyncKeyState(win32con.VK_RMENU) & 0xffff # VK_RMENU == R_ALT + return s & 0x8000 + + def event(self, ev): + et = ev.type() + if et == ev.ToolTip: + self.show_tooltip(ev) + return True + if et == ev.ShortcutOverride: + ret = self.override_shortcut(ev) + if ret: + return True + return QPlainTextEdit.event(self, ev) + # }}} if __name__ == '__main__':