E-book viewer: Make all keyboard shortcuts configurable

Fixes #1232019 [[Enhancement] Allow shortcut for Font Increase/Decrease in Viewer](https://bugs.launchpad.net/calibre/+bug/1232019)
This commit is contained in:
Kovid Goyal 2013-10-05 15:37:24 +05:30
parent f55b13aeb0
commit 441dca72de
4 changed files with 77 additions and 45 deletions

View file

@ -498,12 +498,10 @@ def initialize_view(self, debug_javascript=False):
d.OpenImageInNewWindow, d.OpenLink, d.Reload, d.InspectElement]))
self.search_online_action = QAction(QIcon(I('search.png')), '', self)
self.search_online_action.setShortcut(Qt.CTRL+Qt.Key_E)
self.search_online_action.triggered.connect(self.search_online)
self.addAction(self.search_online_action)
self.dictionary_action = QAction(QIcon(I('dictionary.png')),
_('&Lookup in dictionary'), self)
self.dictionary_action.setShortcut(Qt.CTRL+Qt.Key_L)
self.dictionary_action.triggered.connect(self.lookup)
self.addAction(self.dictionary_action)
self.image_popup = ImagePopup(self)
@ -514,7 +512,6 @@ def initialize_view(self, debug_javascript=False):
self.view_table_action.triggered.connect(self.popup_table)
self.search_action = QAction(QIcon(I('dictionary.png')),
_('&Search for next occurrence'), self)
self.search_action.setShortcut(Qt.CTRL+Qt.Key_S)
self.search_action.triggered.connect(self.search_next)
self.addAction(self.search_action)
@ -652,9 +649,9 @@ def contextMenuEvent(self, ev):
text = self._selectedText()
if text and img.isNull():
self.search_online_action.setText(text)
menu.addAction(self.search_online_action)
menu.addAction(self.dictionary_action)
menu.addAction(self.search_action)
for x, sc in (('search_online', 'Search online'), ('dictionary', 'Lookup word'), ('search', 'Next occurrence')):
ac = getattr(self, '%s_action' % x)
menu.addAction(ac.icon(), '%s [%s]' % (unicode(ac.text()), ','.join(self.shortcuts.get_shortcuts(sc))), ac.trigger)
if not text and img.isNull():
menu.addSeparator()

View file

@ -6,6 +6,7 @@
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en'
from calibre.constants import isosx
SHORTCUTS = {
'Next Page' : (['PgDown', 'Space'],
@ -50,4 +51,37 @@
'Forward': (['Alt+Right'],
_('Forward')),
'Quit': (['Ctrl+Q', 'Ctrl+W', 'Alt+F4'],
_('Quit')),
'Focus Search': (['/', 'Ctrl+F'],
_('Start search')),
'Show metadata': (['Ctrl+I'],
_('Show metadata')),
'Font larger': (['Ctrl+='],
_('Font size larger')),
'Font smaller': (['Ctrl+-'],
_('Font size smaller')),
'Fullscreen': ((['Ctrl+Meta+F'] if isosx else ['Ctrl+Shift+F', 'F11']),
_('Fullscreen')),
'Find next': (['F3'],
_('Find next')),
'Find previous': (['Shift+F3'],
_('Find previous')),
'Search online': (['Ctrl+E'],
_('Search online for word')),
'Lookup word': (['Ctrl+L'],
_('Lookup word in dictionary')),
'Next occurrence': (['Ctrl+S'],
_('Go to next occurrence of selected word')),
}

View file

@ -8,7 +8,7 @@
from PyQt4.Qt import (QApplication, Qt, QIcon, QTimer, QByteArray, QSize,
QTime, QDoubleSpinBox, QLabel, QTextBrowser, QPropertyAnimation,
QPainter, QBrush, QColor, pyqtSignal, QUrl, QRegExpValidator, QRegExp,
QLineEdit, QToolButton, QMenu, QInputDialog, QAction, QKeySequence,
QLineEdit, QToolButton, QMenu, QInputDialog, QAction,
QModelIndex)
from calibre.gui2.viewer.main_ui import Ui_EbookViewer
@ -234,18 +234,9 @@ def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None,
self.view_resized_timer.timeout.connect(self.viewport_resize_finished)
self.view_resized_timer.setSingleShot(True)
self.resize_in_progress = False
qs = [Qt.CTRL+Qt.Key_Q,Qt.CTRL+Qt.Key_W]
self.action_quit.setShortcuts(qs)
self.action_quit.triggered.connect(self.quit)
self.action_focus_search = QAction(self)
self.addAction(self.action_focus_search)
self.action_focus_search.setShortcuts([Qt.Key_Slash,
QKeySequence(QKeySequence.Find)])
self.action_focus_search.triggered.connect(lambda x:
self.search.setFocus(Qt.OtherFocusReason))
self.action_copy.setDisabled(True)
self.action_metadata.setCheckable(True)
self.action_metadata.setShortcut(Qt.CTRL+Qt.Key_I)
self.action_table_of_contents.setCheckable(True)
self.toc.setMinimumWidth(80)
self.action_reference_mode.setCheckable(True)
@ -255,18 +246,14 @@ def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None,
self.action_copy.triggered[bool].connect(self.copy)
self.action_font_size_larger.triggered.connect(self.font_size_larger)
self.action_font_size_smaller.triggered.connect(self.font_size_smaller)
self.action_font_size_larger.setShortcut(Qt.CTRL+Qt.Key_Equal)
self.action_font_size_smaller.setShortcut(Qt.CTRL+Qt.Key_Minus)
self.action_open_ebook.triggered[bool].connect(self.open_ebook)
self.action_next_page.triggered.connect(self.view.next_page)
self.action_previous_page.triggered.connect(self.view.previous_page)
self.action_find_next.triggered.connect(self.find_next)
self.action_find_previous.triggered.connect(self.find_previous)
self.action_full_screen.triggered[bool].connect(self.toggle_fullscreen)
self.action_full_screen.setShortcuts([Qt.Key_F11, Qt.CTRL+Qt.SHIFT+Qt.Key_F])
self.action_full_screen.setToolTip(_('Toggle full screen (%s)') %
_(' or ').join([unicode(x.toString(x.NativeText)) for x in
self.action_full_screen.shortcuts()]))
self.action_full_screen.setToolTip(_('Toggle full screen [%s]') %
_(' or ').join([x for x in self.view.shortcuts.get_shortcuts('Fullscreen')]))
self.action_back.triggered[bool].connect(self.back)
self.action_forward.triggered[bool].connect(self.forward)
self.action_preferences.triggered.connect(self.do_config)
@ -348,11 +335,6 @@ def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None,
self.pos_label.setFocusPolicy(Qt.NoFocus)
self.clock_timer = QTimer(self)
self.clock_timer.timeout.connect(self.update_clock)
self.esc_full_screen_action = a = QAction(self)
self.addAction(a)
a.setShortcut(Qt.Key_Escape)
a.setEnabled(False)
a.triggered.connect(self.action_full_screen.trigger)
self.print_menu = QMenu()
self.print_menu.addAction(QIcon(I('print-preview.png')), _('Print Preview'))
@ -360,9 +342,6 @@ def __init__(self, pathtoebook=None, debug_javascript=False, open_at=None,
self.tool_bar.widgetForAction(self.action_print).setPopupMode(QToolButton.MenuButtonPopup)
self.action_print.triggered.connect(self.print_book)
self.print_menu.actions()[0].triggered.connect(self.print_preview)
ca = self.view.copy_action
ca.setShortcut(QKeySequence.Copy)
self.addAction(ca)
self.open_history_menu = QMenu()
self.clear_recent_history_action = QAction(
_('Clear list of recently opened books'), self)
@ -518,7 +497,7 @@ def print_preview(self):
p = Printing(self.iterator, self)
p.start_preview()
def toggle_fullscreen(self, x):
def toggle_fullscreen(self):
if self.isFullScreen():
self.showNormal()
else:
@ -544,7 +523,6 @@ def showFullScreen(self):
def show_full_screen_label(self):
f = self.full_screen_label
self.esc_full_screen_action.setEnabled(True)
height = 200
width = int(0.7*self.view.width())
f.resize(width, height)
@ -609,7 +587,6 @@ def showNormal(self):
self.clock_timer.stop()
self.vertical_scrollbar.setVisible(True)
self.window_mode_changed = 'normal'
self.esc_full_screen_action.setEnabled(False)
self.settings_changed()
self.full_screen_label.setVisible(False)
if hasattr(self, '_original_frame_margins'):
@ -732,11 +709,11 @@ def font_size_smaller(self):
def magnification_changed(self, val):
tt = '%(action)s [%(sc)s]\n'+_('Current magnification: %(mag).1f')
sc = unicode(self.action_font_size_larger.shortcut().toString())
sc = _(' or ').join(self.view.shortcuts.get_shortcuts('Font larger'))
self.action_font_size_larger.setToolTip(
tt %dict(action=unicode(self.action_font_size_larger.text()),
mag=val, sc=sc))
sc = unicode(self.action_font_size_smaller.shortcut().toString())
sc = _(' or ').join(self.view.shortcuts.get_shortcuts('Font smaller'))
self.action_font_size_smaller.setToolTip(
tt %dict(action=unicode(self.action_font_size_smaller.text()),
mag=val, sc=sc))
@ -1121,10 +1098,40 @@ def previous_document(self):
self.load_path(self.iterator.spine[self.current_index-1], pos=1.0)
def keyPressEvent(self, event):
MainWindow.keyPressEvent(self, event)
if not event.isAccepted():
if not self.view.handle_key_press(event):
event.ignore()
if event.key() == Qt.Key_Escape:
if self.metadata.isVisible():
self.metadata.setVisible(False)
event.accept()
return
if self.isFullScreen():
self.toggle_fullscreen()
event.accept()
return
try:
key = self.view.shortcuts.get_match(event)
except AttributeError:
return MainWindow.keyPressEvent(self, event)
action = {
'Quit':self.action_quit,
'Show metadata':self.action_metadata,
'Copy':self.view.copy_action,
'Font larger': self.action_font_size_larger,
'Font smaller': self.action_font_size_smaller,
'Fullscreen': self.action_full_screen,
'Find next': self.action_find_next,
'Find previous': self.action_find_previous,
'Search online': self.view.search_online_action,
'Lookup word': self.view.dictionary_action,
'Next occurrence': self.view.search_action,
}.get(key, None)
if action is not None:
event.accept()
action.trigger()
return
if key == 'Focus Search':
self.search.setFocus(Qt.OtherFocusReason)
if not self.view.handle_key_press(event):
event.ignore()
def __enter__(self):
return self

View file

@ -248,9 +248,6 @@
<property name="toolTip">
<string>Find next occurrence</string>
</property>
<property name="shortcut">
<string>F3</string>
</property>
</action>
<action name="action_copy">
<property name="icon">
@ -317,9 +314,6 @@
<property name="toolTip">
<string>Find previous occurrence</string>
</property>
<property name="shortcut">
<string>Shift+F3</string>
</property>
</action>
<action name="action_toggle_paged_mode">
<property name="checkable">