Edit Book: On windows do not trigger shortcuts when using the right Alt (AltGr) key. This allows it to be used for entering special characters instead. Fixes #1627487 [bug in Polish mode of Calibre (error in keyboard shortcut)](https://bugs.launchpad.net/calibre/+bug/1627487)

This commit is contained in:
Kovid Goyal 2016-09-26 13:04:36 +05:30
parent 237e4a9455
commit a08356207b
2 changed files with 40 additions and 18 deletions

View File

@ -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()

View File

@ -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__':