Add A go to context menu to the ebook viewer. Fixes #1230 (Feature Suggestions:)

This commit is contained in:
Kovid Goyal 2010-04-27 07:42:01 -06:00
parent cb3609645e
commit abb608a226
2 changed files with 54 additions and 21 deletions

View File

@ -127,6 +127,8 @@ class BookInfoDisplay(QWidget):
keys.sort(cmp=lambda x, y: cmp(self.WEIGHTS[x], self.WEIGHTS[y]))
for key in keys:
txt = data[key]
if not txt or not txt.strip() or txt == 'None':
continue
if isinstance(key, str):
key = key.decode(preferred_encoding, 'replace')
if isinstance(txt, str):

View File

@ -7,10 +7,12 @@ __docformat__ = 'restructuredtext en'
'''
import os, math, re, glob, sys
from base64 import b64encode
from functools import partial
from PyQt4.Qt import QSize, QSizePolicy, QUrl, SIGNAL, Qt, QTimer, \
QPainter, QPalette, QBrush, QFontDatabase, QDialog, \
QColor, QPoint, QImage, QRegion, QVariant, QIcon, \
QFont, pyqtSignature, QAction, QByteArray
QFont, pyqtSignature, QAction, QByteArray, QMenu
from PyQt4.QtWebKit import QWebPage, QWebView, QWebSettings
from calibre.utils.config import Config, StringConfig
@ -449,6 +451,50 @@ class DocumentView(QWebView):
_('&Lookup in dictionary'), self)
self.dictionary_action.setShortcut(Qt.CTRL+Qt.Key_L)
self.dictionary_action.triggered.connect(self.lookup)
self.goto_location_action = QAction(_('Go to...'), self)
self.goto_location_menu = m = QMenu(self)
self.goto_location_actions = a = {
'Next Page': self.next_page,
'Previous Page': self.previous_page,
'Section Top' : partial(self.scroll_to, 0),
'Document Top': self.goto_document_start,
'Section Bottom':partial(self.scroll_to, 1),
'Document Bottom': self.goto_document_end,
'Next Section': self.goto_next_section,
'Previous Section': self.goto_previous_section,
}
for name, key in [(_('Next Section'), 'Next Section'),
(_('Previous Section'), 'Previous Section'),
(None, None),
(_('Document Start'), 'Document Top'),
(_('Document End'), 'Document Bottom'),
(None, None),
(_('Section Start'), 'Section Top'),
(_('Section End'), 'Section Bottom'),
(None, None),
(_('Next Page'), 'Next Page'),
(_('Previous Page'), 'Previous Page')]:
if key is None:
m.addSeparator()
else:
m.addAction(name, a[key], self.shortcuts.get_sequences(key)[0])
self.goto_location_action.setMenu(self.goto_location_menu)
def goto_next_section(self, *args):
if self.manager is not None:
self.manager.goto_next_section()
def goto_previous_section(self, *args):
if self.manager is not None:
self.manager.goto_previous_section()
def goto_document_start(self, *args):
if self.manager is not None:
self.manager.goto_start()
def goto_document_end(self, *args):
if self.manager is not None:
self.manager.goto_end()
@property
def copy_action(self):
@ -488,6 +534,8 @@ class DocumentView(QWebView):
text = unicode(self.selectedText())
if text:
menu.insertAction(list(menu.actions())[0], self.dictionary_action)
menu.addSeparator()
menu.addAction(self.goto_location_action)
menu.exec_(ev.globalPos())
def lookup(self, *args):
@ -763,20 +811,9 @@ class DocumentView(QWebView):
def keyPressEvent(self, event):
key = self.shortcuts.get_match(event)
if key == 'Next Page':
self.next_page()
elif key == 'Previous Page':
self.previous_page()
elif key == 'Section Top':
self.scroll_to(0)
elif key == 'Document Top':
if self.manager is not None:
self.manager.goto_start()
elif key == 'Section Bottom':
self.scroll_to(1)
elif key == 'Document Bottom':
if self.manager is not None:
self.manager.goto_end()
func = self.goto_location_actions.get(key, None)
if func is not None:
func()
elif key == 'Down':
self.scroll_by(y=15)
elif key == 'Up':
@ -785,12 +822,6 @@ class DocumentView(QWebView):
self.scroll_by(x=-15)
elif key == 'Right':
self.scroll_by(x=15)
elif key == 'Next Section':
if self.manager is not None:
self.manager.goto_next_section()
elif key == 'Previous Section':
if self.manager is not None:
self.manager.goto_previous_section()
else:
return QWebView.keyPressEvent(self, event)